Commit f0679422 authored by Tim Hegemann's avatar Tim Hegemann
Browse files

add task sheet 7

parent 1d20a3aa
......@@ -15,6 +15,7 @@ usually marked with `???`.
| 4: Laws, Monoids, and Foldable | [`fp04`](src/main/scala/fp04) |
| 5: Typeclasses, Functors, and Cats | [`fp05`](src/main/scala/fp05) |
| 6: Monads | [`fp06`](src/main/scala/fp06) |
| 7: Applicatives | [`fp07`](src/main/scala/fp07) |
## Usage tips:
To keep your local solutions to the exercises when pulling from the repository,
......
package fp07
import cats.Applicative
import fp05.{Branch, Leaf, Tree, TreeInstances}
import scala.language.higherKinds
object FP07 {
import cats.implicits._
// Aufgabe 1
def applicativeForProducts[F[_]:Applicative,G[_]: Applicative]: Applicative[Lambda[a => (F[a],G[a])]] = ???
// Aufgabe 2a
def apViaProductAndMap[F[_]: Applicative,A,B](ff: F[A => B])(fa: F[A]): F[B] = ???
// Aufgabe 2b
def productViaApAndMap[F[_]: Applicative,A,B](fa: F[A], fb: F[B]): F[(A,B)] = ???
// Aufgabe 3a
implicit val applicativeForTree: Applicative[Tree] = ???
def main(args: Array[String]): Unit = {
val a: Tree[Int] = Branch(Branch(Leaf(1), Leaf(2)), Branch(Leaf(3), Branch(Leaf(4), Leaf(5))))
val b: Tree[String] = Branch(Leaf("Cats"), Leaf("Dogs"))
println(b.map2(a)(_ + _))
// Proove the Applicative laws for some examples...
val f = (_:Int) * 3.14
val g = (_:Double).toString
def ff[F[_]: Applicative,A,B](f: A => B)(fa: F[_]) = fa.map(_ => f)
def pure[A] = Applicative[Tree].pure[A] _
val compose = pure((f: Double => String) => (_: Int => Double) andThen f)
println(s"Identity law for ap: ${(pure(identity[Int] _) <*> a) == a}")
println(s"Composition law for ap: ${(ff(g)(a) <*> (ff(f)(b) <*> a)) == (compose <*> ff(g)(a) <*> ff(f)(b) <*> a)}")
println(s"Homomorphism law for ap: ${(pure(f) <*> pure(2)) == pure(f(2))}")
println(s"Interchange law for ap: ${(ff(f)(a) <*> pure(2)) == (pure((h: Int => Double) => h(2)) <*> ff(f)(a))}")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment