123avi (29) [Avatar] Offline
#1
Thank you for this great book , I really find it useful and start implement it's principles in my projects . however I am missing the point regarding the Functor pattern , while I understand the functionality I'm missing what's the big deal?. looking at the example in pg 126-127 we just replaced the mapping :
def calculateInterest(no: String) = accountFor(no).map(interestOn)

with a functor mapping
def calculateInterest(no: String) = fmap(accountFor(no))(interestOn)
how is it making it more reusable ?
Debasish Ghosh (111) [Avatar] Offline
#2
The core value of abstractions like Functor, Monad, Applicative etc. is parametricity, which means that they allow you to write generic functions. This is a huge value which you cannot do using only the Standard Scala library. Here's a very simple example that uses Functor :-

def foo[F[_]: Functor, A, B](f: F[A])(g: A => B) = f map g


This is a very simple example but it demonstrates the power of parametricity. The function foo is completely independent of any specific type. The only constraint it imposes is that F needs to be a Functor. So you can call this function with any F that provides an instance of a Functor. e.g.

scala> val g: Int => Int = _ * 4
g: Int => Int = <function1>

scala> foo(Option(10))(g)
res10: Option[Int] = Some(40)

scala> foo(List(1,2,3))(g)
res11: List[Int] = List(4, 8, 12)


You can have this reusability only because you have the generic Functor abstraction. The book discusses many such parametric functions in domain modeling in chapters 3 and 4. Let me know if you need any more explanation.

Thanks.