3 ms·
Tricky Scala bits: The return type is at the end of the function definition, preceded by : . It's optional if there is an implementation provided, as Scala can
by hibikir 12y ago
Tricky Scala bits:
The return type is at the end of the function definition, preceded by : . It's optional if there is an implementation provided, as Scala can do type inference.
The => is a shorthand for defining a function type. String => Int is a function that takes a String, and returns an Int.
[A,B] is saying that the function is a generic function, with some generic types named A and B, that will be resolved at compile time. Not unlike generics in many other languages.
so def map[A,B] (fa: F[A], f: A=>B) : F[B] means that we are defining a function map, that takes a Functor of A fa, and a function from A to B, and returns a functor of type F[B].
Slide 92 writes it differently:
def map[A,B] : (A=>B) => (F[A] => F[B])
Here, map returns a function that takes a function from A to B, and returns another function, that turns a Functor of A into a functor of B.
Now, knowing functors and scala, I don't find those slides that illuminating anyway. I did not think that I'd ever say this about anything, but here it goes. I think Scalaz's implementation is easier to understand.