wir33658 (4) [Avatar] Offline
#1
Hi, from time to time I use Scala and I love it since it allows you to slowly slide from OOP to more functional programming. Since I am not a math-pro I do not have enough background of category theory. Therefor your explanations on Functors and Monads are quite good.

BUT, for example for the ManagedResource-Example I do not get the advantage at all. To count some words in a file !?!?! This looks like there is so much overhead and I do not see any other advantages - like generification (means using part of the code in some other way).

Can you explain a little bit more why (e.g. exactly for this example) this might be an advantage using Monads and Functors (I got that you can use it to pretend to have some kind of workflow-style, but common, doing it without the whole fuss would make it even more readable - so I guess there is more of the story and I - and maybe others - do not get) ?

Thanks in advance, Rob.
joshua.suereth (60) [Avatar] Offline
#2
Re: Chapter 11 - ManagedResource example
Here's an example of leveraging monadic abstraction to swap the *execution time* meaning of a block of code: http://www.youtube.com/watch?feature=player_embedded&v=UKjcJ13bD3s

The code is here: https://github.com/jsuereth/intro-to-fp

In this instance, we're leveraging the full power of monads to change the *context* in which a computation runs.

For the ManageResource example, the primary benefit of using monadic design is the ability to generate a computation against a resource that you can re-use over and over again.

Note: The ManagedResource does a few things:

(1) It pushes any action you want to perform in the scope of an active resource
(2) It allows you ad "add behavior" that happens before (or after) a computation is done.

This is a bit more powerful than just a simple "try catch" block. It enables to glue together code from various contexts in a fundamentally different way. One thing to note on this design, is you're thinking about very small minimal functions/libraries being combined to create the full computation. When we think of "re-usable" function, we start think a lot lower level when we're using heavy FP. Things like "+" and "*".

Hope that helps!