Monday 18 June 2012

Scala: Functor (scalaz example)

Function programming got a proven handy in structuring the code, from where we have common structures/classes presented in many functional languages. All this magic comes from mathematic and in the most parts aren't so easy to get for regular developers with imperative experience. I've been googling a lot to get essence and still getting a lot of "news". Looks like the best way to review FP types in Scala is using examples from scalaz library. Let try to start from the Functor.

Functor 

Functor is a something you can map over, it has only one method map (called fmap scalaz). Class diagram is very simple:
And Scala view is exactly the same.
InvariantFunctor defines method what accepts Invariant transformation from A to B and from B back to A (without losing something). Covariant implementation of this function in Functor just ignores second param. Lets implement example: keeping in mind that Scala's Option is Functor as well implementation can look like delegation: Second variant is coming from scalaz library. If we look inside Functor.scala from this library we will see a lot of implicit declarations for Functor implementation. They are all used as Type Classes for a specific functor. Lets imagine situation when we want to declare map method that accepts all types: By the way it looks as context bound declaration, lets refactoring it according previous lesson: and of course an example with Option type: Here we got a working Functor, coming as Context Bounded construction. There are heavier examples in scalaz library: Looks like the mess, almost Clojure look&feel;) Don'w worry, we are going to review in the details:
1. List is implicitly converted to MASugar type, that looks like: 2. Method ∘ is calling for a map method that is wrapper around fmap in functor (implicit parameter): , as we can see it is almost equals to our early example. Actually looking into Functor.scala file we can guess that TraversableFunctor will be injected in our case. It is delegates fmap call to map on List. Ha, quite useless construction for List, while List as Functor as well as an Option! As we will find later in Standart Scala library all Monadic classes are Functor classes in the same time! 3. Construction (1 +) is implicitly converted to function1, suppose it's not so clean, here is an example of usage Synthetic Function: Explicit type declaration is required, otherwise compiler will assume that this is not finished expression. And if you want you can review complex Functor example with sclaz library, Map across the Option functor within a map across the List functor:

    (List(Some(7), None, Some(8)) ∘∘ (1 + (_: Int))) // List(Some(8), None, Some(9))

Cheers!

No comments:

Post a Comment