Skip to content

Instantly share code, notes, and snippets.

@deanwampler
Last active March 8, 2021 14:02
Show Gist options
  • Save deanwampler/ac8c95d827bcd8ac51729b81f158372f to your computer and use it in GitHub Desktop.
Save deanwampler/ac8c95d827bcd8ac51729b81f158372f to your computer and use it in GitHub Desktop.
// Adapted from:
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/main/scala/progscala3/typesystem/typelambdas/Functor.scala
// https://github.com/deanwampler/programming-scala-book-code-examples/blob/master/src/script/scala/progscala3/typesystem/typelambdas/Functor.scala
scala> type MapKV = [K] =>> [V] =>> Map[K,V]
// defined alias type MapKV[K] = [V] =>> Map[K, V]
scala> given [K]: Functor[MapKV[K]] with
| extension [V1] (map: MapKV[K][V1])
| def map2[V2](f: V1 => V2): MapKV[K][V2] = map.view.mapValues(f).toMap
// defined class given_Functor_V1_MapKV
scala> Map("one" -> 1, "two" -> 2, "three" -> 3).map2(_ * 2.2)
| Map.empty[String,Int].map2(_.toString)
val res0: MapKV[String][Double] = Map(one -> 2.2, two -> 4.4, three -> 6.6000000000000005)
val res1: MapKV[String][String] = Map()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment