Skip to content

Instantly share code, notes, and snippets.

@softberries
Created November 11, 2019 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softberries/da6f72eca908861fb4033654d47e6201 to your computer and use it in GitHub Desktop.
Save softberries/da6f72eca908861fb4033654d47e6201 to your computer and use it in GitHub Desktop.
import cats.instances.list._
import cats.instances.option._
val complexContainer = List(
List(
Some(Map(1 -> false, 2 -> true)),
None
),
List(
Some(Map(10 -> true, 11 -> true))
)
)
type IntMap[V] = Map[Int, V]
//there is no functor for Map. It's needed in order to create complexContainerFunctor
implicit val mvalueFunctor = new Functor[IntMap] {
override def map[A, B](fa: IntMap[A])(f: (A) => B): IntMap[B] = fa.mapValues(f)
}
implicit val complexContainerFunctor = Functor[List].compose[List].compose[Option].compose[IntMap]
//and the complexContainerFunctor in action:
val complexContainerWithStrings = complexContainerFunctor.map(complexContainer)(if (_) "Yes" else "No")
println(complexContainerWithStrings)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment