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

@odbc1986: HomeWork 4 - Foldables - Collect, FlatMap, String #36

Open
wants to merge 1 commit into
base: run4
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
29 changes: 29 additions & 0 deletions src/main/scala/fpspeedrun/foldables/ProperString.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package fpspeedrun.foldables

import cats.{Eval, Foldable}

final case class ProperString[A](value: String)

object ProperString {
implicit def foldableProperString: Foldable[ProperString] = new Foldable[ProperString] {
override def foldLeft[A, B](fa: ProperString[A], b: B)(f: (B, A) => B): B =
fa.value match {
case str if str.length > 0 =>
foldLeft(
ProperString(str.substring(1)),
f(b, str.substring(0, 1).asInstanceOf[A])
)((b: B, a: String) => f(b, a.asInstanceOf[A]))
case _ => b
}

override def foldRight[A, B](fa: ProperString[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
fa.value match {
case str if str.length > 0 =>
foldRight(
ProperString(str.substring(0, str.length - 1)),
f(str.substring(str.length - 1).asInstanceOf[A], lb)
)((a: String, b: Eval[B]) => f(a.asInstanceOf[A], b))
case _ => lb
}
}
}
37 changes: 31 additions & 6 deletions src/main/scala/fpspeedrun/foldables/streams.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,51 @@
package fpspeedrun.foldables
import cats.Foldable
import cats.{Eval, Foldable}
import cats.syntax.foldable._

final case class Filter[F[_], A](fa: F[A], p: A => Boolean)

object Filter {
implicit def foldable[F[_]: Foldable]: Foldable[Filter[F, ?]] = ???
implicit def foldable[F[_]: Foldable]: Foldable[Filter[F, ?]] = new Foldable[Filter[F, ?]] {
override def foldLeft[A, B](fa: Filter[F, A], b: B)(f: (B, A) => B): B =
fa.fa.foldLeft(b)((acc, a) => if (fa.p(a)) f(acc, a) else acc)

override def foldRight[A, B](fa: Filter[F, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
fa.fa.foldRight(lb)((a, acc) => if (fa.p(a)) f(a, acc) else acc)
}
}

final case class Map[F[_], A, B](fa: F[A], p: A => B)

object Map {
implicit def foldable[F[_]: Foldable, A]: Foldable[Map[F, A, ?]] = ???
implicit def foldable[F[_]: Foldable, T]: Foldable[Map[F, T, ?]] = new Foldable[Map[F, T, ?]] {
override def foldLeft[A, B](fa: Map[F, T, A], b: B)(f: (B, A) => B): B =
fa.fa.foldLeft(b)((acc, a) => f(acc, fa.p(a)))

override def foldRight[A, B](fa: Map[F, T, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
fa.fa.foldRight(lb)((a, acc) => f(fa.p(a), acc))
}
}

final case class Collect[F[_], A, B](fa: F[A], pf: PartialFunction[A, B])

object Collect {
implicit def foldable[F[_]: Foldable, A]: Foldable[Collect[F, A, ?]] = ???
implicit def foldable[F[_]: Foldable, T]: Foldable[Collect[F, T, ?]] = new Foldable[Collect[F, T, ?]] {
override def foldLeft[A, B](fa: Collect[F, T, A], b: B)(f: (B, A) => B): B =
fa.fa.foldLeft(b)((acc, a) => if (fa.pf.isDefinedAt(a)) f(acc, fa.pf(a)) else acc)

override def foldRight[A, B](fa: Collect[F, T, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
fa.fa.foldRight(lb)((a, acc) => if (fa.pf.isDefinedAt(a)) f(fa.pf(a), acc) else acc)
}
}

final case class FlatMap[F[_], A, G[_], B](fa: F[A], f: A => G[B])

object FlatMap {
implicit def foldable[F[_]: Foldable, A, G[_]: Foldable]: Foldable[FlatMap[F, A, G, ?]] = ???
}
implicit def foldable[F[_]: Foldable, T, G[_]: Foldable]: Foldable[FlatMap[F, T, G, ?]] = new Foldable[FlatMap[F, T, G, ?]] {
override def foldLeft[A, B](fa: FlatMap[F, T, G, A], b: B)(f: (B, A) => B): B =
fa.fa.foldLeft(b)((acc, a) => fa.f(a).foldLeft(acc)(f))

override def foldRight[A, B](fa: FlatMap[F, T, G, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] =
fa.fa.foldRight(lb)((a, acc) => fa.f(a).foldRight(acc)(f))
}
}