Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homework_5 - Карамышев Владислав #205

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions homeworks/homework_5/src/main/scala/Task1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,17 @@ object Task1 extends App {
}

object ShowInstance {
implicit val catShow: Show[Cat] = ???
implicit val catShow: Show[Cat] = {
case VeryLittleCat(name) => s"очень маленький кот $name"
case LittleCat(name) => s"маленький кот $name"
case NormalCat(name) => s"нормальный кот $name"
case BigCat(name) => s"большой кот $name"
case VeryBigCat(name) => s"очень большой кот $name"
}

implicit def boxShow[A: Show]: Show[Box[A]] = ???
implicit def boxShow[A: Show](implicit Show : Show[A]): Show[Box[A]] = {
case BoxWith(value) => s"кот вида ${Show.show(value)} в коробке"
case EmptyBox => s"кот сбежал из коробки"
}

object ShowSyntax {
Expand Down
28 changes: 25 additions & 3 deletions homeworks/homework_5/src/main/scala/Task2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,38 @@ import cats.implicits._
object Task2 extends App {
case class RadiusVector(x: Int, y: Int)
object RadiusVector {
implicit val monoid: Monoid[RadiusVector] = ???
implicit val monoid: Monoid[RadiusVector] = new Monoid[RadiusVector]{
override def empty: RadiusVector = new RadiusVector(0, 0)

override def combine(x: RadiusVector, y: RadiusVector): RadiusVector =
new RadiusVector(x.x + y.x, x.y + y.y)
}
}
case class DegreeAngle(angel: Double)
object DegreeAngle {
implicit val monoid: Monoid[DegreeAngle] = ???
implicit val monoid: Monoid[DegreeAngle] = new Monoid[DegreeAngle]{
override def empty: DegreeAngle = DegreeAngle(0)

override def combine(x: DegreeAngle, y: DegreeAngle): DegreeAngle = {
DegreeAngle((x.angel + y.angel) % 360)
}
}
}

case class SquareMatrix[A : Monoid](values: ((A, A, A), (A, A, A), (A, A, A)))
object SquareMatrix {
implicit def monoid[A: Monoid]: Monoid[SquareMatrix[A]] = ???
implicit def monoid[A: Monoid]: Monoid[SquareMatrix[A]] = new Monoid[SquareMatrix[A]]{
override def empty: SquareMatrix[A] = {
new SquareMatrix[A](values = (
(Monoid[A].empty, Monoid[A].empty, Monoid[A].empty),
(Monoid[A].empty, Monoid[A].empty, Monoid[A].empty),
(Monoid[A].empty, Monoid[A].empty, Monoid[A].empty)
))
}

override def combine(x: SquareMatrix[A], y: SquareMatrix[A]): SquareMatrix[A] =
new SquareMatrix[A](values = x.values |+| y.values)
}
}

val radiusVectors = Vector(RadiusVector(0, 0), RadiusVector(0, 1), RadiusVector(-1, 1))
Expand Down
33 changes: 30 additions & 3 deletions homeworks/homework_5/src/main/scala/Task3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import cats.implicits._

import scala.concurrent.{Await, Future}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.DurationInt
import scala.concurrent.duration.{Duration, DurationInt}

/*
Задание №3
Expand All @@ -25,8 +25,35 @@ object Task3 extends App {
case class Count(word: String, count: Int)
case class WordsCount(count: Seq[Count])
object WordsCount {
implicit val monoid: Monoid[WordsCount] = ???
implicit val monoid: Monoid[WordsCount] = new Monoid[WordsCount] {
override def empty: WordsCount = new WordsCount(count = Seq[Count]())

override def combine(x: WordsCount, y: WordsCount): WordsCount = {
val combinedCounts = (x.count ++ y.count)
.groupBy(_.word)
.map { case (word, counts) =>
Count(word, counts.map(_.count).sum)
}
.toSeq

WordsCount(combinedCounts)
}
}
}

def countWords(lines: Vector[String]): WordsCount = {
val ft = mapReduce[String, WordsCount](lines) { line =>
val wordCounts = line.split(' ').toVector
.groupBy(identity)
.map { case (word, occurrences) =>
Count(word, occurrences.length)
}
.toSeq

WordsCount(wordCounts)
}

Await.result(ft, Duration.Inf)
}

def countWords(lines: Vector[String]): WordsCount = ???
}
29 changes: 26 additions & 3 deletions homeworks/homework_5/src/main/scala/Task4.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ object Task4 extends App {
def pure[A](value: A): F[E, A]
def flatMap[A, B](fa: F[E, A])(f: A => F[E, B]): F[E, B]

def map[A, B](fa: F[E, A])(f: A => B): F[E, B] = ???
def map[A, B](fa: F[E, A])(f: A => B): F[E, B] =
flatMap(fa)(f.andThen(pure))

def raiseError[A](fa: F[E, A])(error: => E): F[E, A]
def handleError[A](fa: F[E, A])(handle: E => A): F[E, A]
Expand All @@ -28,9 +29,31 @@ object Task4 extends App {

def error[E, A](error: E): EIO[E, A] = EIO[E, A](Left(error))

def possibleError[A](f: => A): EIO[Throwable, A] = ???
def possibleError[A](f: => A): EIO[Throwable, A] = {
Try(f) match {
case Success(value) => EIO(value)
case Failure(exception) => EIO.error(exception)
}
}

implicit def monad[E]: MonadError[EIO, E] = new MonadError[EIO, E] {
override def pure[A](value: A): EIO[E, A] = EIO(value)
override def flatMap[A, B](fa: EIO[E, A])(f: A => EIO[E, B]): EIO[E, B] = {
fa.value match {
case Right(a) => f(a)
case Left(e) => EIO(Left(e))
}
}

implicit def monad[E]: MonadError[EIO, E] = ???
override def raiseError[A](fa: EIO[E, A])(error: => E): EIO[E, A] = EIO(Left(error))

override def handleError[A](fa: EIO[E, A])(handle: E => A): EIO[E, A] = {
fa.value match {
case Left(e) => EIO(handle(e))
case Right(a) => EIO(a)
}
}
}
}

object EIOSyntax {
Expand Down
35 changes: 31 additions & 4 deletions homeworks/homework_5/src/main/scala/Task5.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,39 @@ object Task5 extends App {
sealed trait MyEither[+E, +A] {
def isError: Boolean
}
case class MyRight[+A](value: A) extends MyEither[Nothing, A] {
override def isError: Boolean = false
}

case class MyLeft[+E](error: E) extends MyEither[E, Nothing] {
override def isError: Boolean = true
}

object MyEither {
def apply[A](value: A): MyEither[Nothing, A] = ???
def error[E, A](error: E): MyEither[E, A] = ???
def possibleError[A](f: => A): MyEither[Throwable, A] = ???
def apply[A](value: A): MyEither[Nothing, A] = MyRight(value)
def error[E, A](error: E): MyEither[E, A] = MyLeft(error)
def possibleError[A](f: => A): MyEither[Throwable, A] = Try(f) match {
case Success(value) => MyRight(value)
case Failure(exception) => MyLeft(exception)
}

implicit def myEitherMonad[E]: MonadError[MyEither, E] = ???
implicit def myEitherMonad[E]: MonadError[MyEither, E] = new MonadError[MyEither, E]{

override def pure[A](value: A): MyEither[E, A] = MyRight(value)

override def flatMap[A, B](fa: MyEither[E, A])(f: A => MyEither[E, B]): MyEither[E, B] =
fa match {
case MyRight(value) => f(value)
case MyLeft(error) => MyLeft(error)
}

override def raiseError[A](fa: MyEither[E, A])(error: => E): MyEither[E, A] = MyLeft(error)

override def handleError[A](fa: MyEither[E, A])(handle: E => A): MyEither[E, A] = fa match {
case MyRight(value) => MyRight(value)
case MyLeft(exception) => MyRight(handle(exception))
}
}
}

object MyEitherSyntax {
Expand Down