Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created November 5, 2020 21:16
Show Gist options
  • Save rupeshtr78/83e87dd15676c65589d9d8d4c0d19a82 to your computer and use it in GitHub Desktop.
Save rupeshtr78/83e87dd15676c65589d9d8d4c0d19a82 to your computer and use it in GitHub Desktop.
package MoniodsFunctorMonad
object FunctorBasics {
trait Functor[F[_]] {
def map[T, Y](l: F[T])(f: T => Y): F[Y]
}
val listFunctor = new Functor[List] {
override def map[T, Y](l: List[T])(f: T => Y): List[Y] = l.map(f)
}
val numbers = List(1, 2, 3, 4, 5, 6)
val mapping = Map(
1 -> "one",
2 -> "two",
3 -> "three",
4 -> "four",
5 -> "five",
6 -> "six"
)
listFunctor.map(numbers)(_ * 2) //List(2, 4, 6, 8, 10, 12)
listFunctor.map(numbers)(i => (i, mapping(i))) // List((1,one), (2,two), (3,three) ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment