Functional, message passing, parallel patterns in Erlang

Is there exists any? Isn’t functional languages expressive enough to don’t bother with patterns? I won’t give another rant about that topic. Interested people i encourage to look very fun and insightful presentation Functional Design Patterns by Aino Corry on InfoQ.

There are many very useful concepts, ideas techniques (choose your best matching word 🙂 ) in functional programming. I am not an expert, and I don’t know many functional languages (i have played a little with Scheme and used commercially Erlang for a few months) – so i may change my mind about it. But currently I think that there are general enough, sometimes not obvious enough and reoccurring patterns in functional style of programming.

Functional patterns

Functional programming is all about defectively using higher order functions, ignoring concept of shared state, doing cool things with lists and using recursion (always tail of course). So this determine basic areas of patterns:

The concept of using closures to share state in a few dynamically created functions Ihave  seen in famous Structure and Interpretation of Computer Programs. I suspect that studying this book alone (and lisp) might bring me a few powerful concepts.

The concepts of fancy list manipulation (lists:foldl, lists:map) is probably basic enough to not be called patterns. Similar situation with pattern matching using to bound variables and extracting elements from data structures. List comprehension is the syntactic construct so we can skip it probably .

Message passing patterns

In Erlang community patterns means OTP behaviours:

  • gen_server (remembering some small portion of state by passing it to internally in every invocation, synchronous and asynchronous calls)
  • supervisor (managing e.g. restarting after failure work of other tasks)
  • gen_fsm (Finite State Machine pattern)
  • gen_event (sending events)

There is a few concepts described in Programming Erlang by Joe Armstrong which AFAIK are not directly supported by those behaviours: priority receiving, flushing. But again are probably basic enough to not count as patterns.

Parallel computation patterns

Took me while to find out some tips on how to use Erlang message passing capabilities to do parallel computing. OK I know that I should bang messages to other process mailbox, i could register them or use pids but how actually use them?

Mentioned above book describe this in chapter 20. I found few interesting functions in OTP to support that: rpc:pmap and rpc:parallel_eval. They are very powerful hiding a lot of details about synchronizing results and spreading computation for different nodes/processes. In rpc module there are many interesting functions. And what is most important basic pmap concept can be modified to achieve different building block to create parallel algorithm (see the 20 chapter for details). In here i can’t name patterns yet but i hope this will change.

Other

I don’t know Haskell concepts of monads and lazy evaluation so can’t say anything about them in context of Erlang or functional languages in general. Maybe there are a lot of powerful concepts hiding there 🙂

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s