Commit 36163b7d authored by Tim Hegemann's avatar Tim Hegemann
Browse files

add task sheet 9: Traversable

parent 227f72d0
......@@ -17,6 +17,7 @@ usually marked with `???`.
| 6: Monads | [`fp06`](src/main/scala/fp06) |
| 7: Applicative Functors | [`fp07`](src/main/scala/fp07) |
| 8: State (and some Applicative) | [`fp08`](src/main/scala/fp08) |
| 9: Traversable Functors | [`fp09`](src/main/scala/fp09) |
## Usage tips:
To keep your local solutions to the exercises when pulling from the repository,
......
package fp09
import cats.data.State
import cats.{Applicative, Eval, Traverse}
import fp05.{Branch, Leaf, Tree}
import scala.language.higherKinds
object FP09 {
import cats.implicits._
// Aufgabe 1: map via traverse
def mapViaTraverse[F[_]:Traverse,A,B](fa: F[A])(f: A => B): F[B] = ???
// Aufgabe 2a: Traverse-Instanz für Tree
implicit val treeTraverse: Traverse[Tree] = ???
// Aufgabe 3: Akkumulieren mit State
def mapAccum[F[_]:Traverse,S,A,B](fa: F[A], s: S)(f: (S,A) => (S,B)): (S,F[B]) =
fa.traverse(a => State(s => f(s, a))).run(s).value
// Aufgabe 3a: reverse
def reverse[F[_]:Traverse,A](fa: F[A]): F[A] = ???
def reverseLaw[F[_]:Traverse,A](x: F[A], y: F[A]): Boolean =
reverse(x).toList ::: reverse(y).toList == reverse(x.toList ::: y.toList)
// Aufgabe 3b: foldLeft
def foldLeftViaMapAccum[F[_]:Traverse,A,B](fa: F[A], z: B)(f: (B, A) => B): B = ???
}
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