A chap named ittayd has written a nice gentle tutorial on functional programming in Scala. My only minor complaint is that he calls the applicative functor “sequential application” function apply
. While this is a logical choice, in Scala apply
is used everywhere to implement plain old function application, so it could lead to confusion. I think it’s better to call it <*>
, as in Haskell. Read on.
I found the tutorial interesting because it talks a bit about applicative functors, and how to give them a nice syntax in Scala. The one thing that has bugged me about applicatives is that using them in Scala always felt clunkier than Haskell. Ittayd comes up with a scheme I like, but says “we have one difficulty: we can no longer define apply inside Future”. He uses a standard object-pimping implicit conversion to get around this, and that works fine. However, it is possible to define <*>
inside your thing-with-an-applicative-functor, using one of my favourite Scala features: generalized type constraints. To continue with ittayd’s example using Future
:
trait Future[A] { … def <*>[X, Y](x: Future[X])(implicit ev: A <:< (X => Y)) = new Future[Y] { def get = Future.this.get(x.get) def isDone = Future.this.isDone && x.isDone } }
The compiler will only allow <*>
to be called on a Future
that provides an appropriate function as its value.
Ittayd bootstraps things simply by lifting the function to be applied into Future
, but personally I prefer the Haskell style of using <$>
, aka “map”. In Scala, <$>
is unsuitable for a few reasons, so I picked **:
:
trait Future[A] { … def **:[B](f: A => B): Future[B] = map(f) }
Obviously, this is just the standard map
method with a different name, what’s the point? Well, because the operator ends with a :, it’s right associative; and because it starts with an *, it has higher precedence than <*>
; so you end up with:
(marry _).curried **: joe <*> jane
Or, if you subscribe to the slippery slope theory of gay marriage:
(gayMarry _).curried **: joe <*> bob <*> george <*> fido <*> aDonkey
This is reasonably close to the Haskell idiom:
gayMarry <$> joe <*> bob <*> george <*> fido <*> aDonkey
You may wonder why obsess over the best way of dealing with applicative functors? After all, most of the applicative functors you see have an associated monad, so we can use Scala’s nice for
syntax. Yes this is true for many useful applicative functors, but not all of them.
Just a quick asside on termonology. In casual conversation, you may hear it said that “Option is a monad”. But it’s more accurate to say “Option has a monad”. Because many types only have one useful monad associated with them, we tend to think that the type itself is the monad, but that’s not quite right. This doesn’t just apply to monads, of course, the same goes for monoids, applicative functors, etc. For example, numeric types have two useful monoids: one for addition and one for multiplication.
Now consider a Validation type from this Tony Morris tutorial. You can write a useful monad for this type, which implies an applicative functor as well. However, the tutorial is concerned with a second useful applicative functor for Validation. This second applicative is only relevant for the case where the Validation is using a semigroup as its error type (the tutorial explains what a semigroup is), and it allows using the semigroup to accumulate a list of all the validation failures. The classic example of this is validating data entered by a user on a form with several fields. The “plain” applicative/monad will only record the first validation failure it finds, whereas the semigroup applicative will record all the failures.
Now, finally, to my point (I think). This applicative functor that uses semigroup does not have a corresponding monad. Which means that we can’t use it with Scala’s for
syntax. Hence the desire for a nice syntax for applicative functors. It is most definitely useful.
Obligatory Scalaz reference: those guys have pretty much worked out the best way to do all this stuff in Scala. But I find I learn a lot by experimenting myself.