Surjective composition: the first function need not be surjective. (Photo credit: Wikipedia)

Function composition is a nice way to sequence transformations on data. For example in a compiler you take your source, parse, check, optimize and generate code(in a nutshell). It’s a linear series of transformations -> perfect for function composition In Haskell you can use this beautiful syntax

This creates an object and reimplements “compose” but I really like the syntax:

compile = parse --> check --> optimize --> codegen

Functional programming can be imagined as a waterfall of data, flowing from one function into the next, and the --> operator represents this nicely. Let’s go a step further. If the composition is one-off, and this is a waterfall of DATA it could be nice to represent that. Something like

Goin async

What about asynchronous calls? Can I compose those too? I think it’s possible with Lift actors(or with scalaz’s?), but I needed to integrate that into Android’s activities quite recently. Well I did not need to do it, but it was quite a nice solution.

The usual way of doing things async in Android is with the conveniently named AsyncTask. The problem is - you can’t subclass it in scala because of some compiler bug regarding varargs parameters. Silly.

So let’s do a lightweight(in terms of code) substitution. We can “spawn a thread” using scala’s actors. And activity can receive messages through a Handler.

In other words, do “waterfall composition” in background using some input I have now and post the result back into the UI thread to method displayResults. That should be the magic of the -!> operator. Do the left side in bg and post it to the right side async. I need a new trait for that