Saturday, January 12, 2013

Clojure reducers: when FP matters

I'm experiencing cultural shock after familiarizing myself with the upcoming Clojure 1.5 feature called 'Reducers'. In this post I will try to describe what reducers are all about presuming no reader's functional programming experience.

What's it all about?

It's about performance. It's about giving back such a computer program property so that to make a program faster one just needs to wait 18 months. Now, when CPU clock speeds got stuck, it doesn't work anymore. Sequential programs can't benefit from more CPU cores. If we set aside IO, what our programs do the most? I guess they spend most their time in loops, processing some grouped data, which may be represented conceptually by lists, maps, sets, sequences, and so on. So, the ability to process grouped data faster, perhaps, using more cores, is rather tempting, since it makes possible to spread to more cores automatically. Let's go next.

How does one work with collections in functional style?

Since I presume no reader's FP knowledge, this section will elaborate on that. To make my example I will borrow Rich's Pie Maker analogy he used in his talk. How to accomplish the Pie Maker's task which is: having a bag of apples, get rid of rotten ones, take the stickers off each apple, chop each apple into pieces, and finally put those pieces into pie. In Java-style it would be something like:

Wednesday, November 21, 2012

Java: throw checked exception without signature declaration

How to throw checked exception from Java method without declaring it in method's signature? Works due to type erasure.

Found in Clojure source.

Despite being ugly hack I guess it might be useful in situations when
  • you want to prevent your API pollution with checked exceptions;
  • you don't want to wrap those checked exception into runtime exceptions to re-throw them and re-catch higher in the call hierarchy (e.g. for performance reasons).
That way you can 'throw' checked exceptions (which are mostly a mistake in Java design) as if they were runtime exceptions, saving on one object allocation, and then catch them as checked exceptions without unwrapping.

Thursday, October 4, 2012

How to iterate functionally

Recently, I participated in Senior Java interview where the candidate was asked to find a verbal solution to the following problem: given two arrays, find their intersection and subtraction. One may need to ask some additional questions, like whether to honor duplicates or just consider arrays as sets and whether ordering is important. Candidate was supposed to show his abilities in algorithmic thinking and data types knowledge. The solution ought to be the fastest possible - generally better than O(n^2).

One idea would be to somehow 'import' data into hash sets and then use HashSet's methods to find an answer. Another one is to sort data first and then implement the algorithm iterating both arrays at once. Duplicates honoring would considerably complicate the task. The rest of the article discusses my personal problem solution in Haskell. The goal is to somehow break the potential reader's imperative looping thinking and give an alternative functional view of the problem since looping is one of the main obstacles on the way to functional programming.

Problem definition

What I wanted was to yield a solution based on approach 2 (with pre-sorting) which would honor duplicates in input lists so that intersection of [1,2,3,3,4,4] and [1,3,3,3,4] would be [1,3,3,4] and [1,2,3,3,4,4] subtract [1,3,3,3,4] would be [2,4]. Ordering is not important.

Monday, July 30, 2012

Setting up Emacs for Clojure programming

When starting my first Clojure journey recently, I faced the problem of setting up Emacs for Clojure programming. It can seem cumbersome to do it right if you aren't aware of some pitfalls, and many tutorials and answers at stackoverflow are outdated. So, I leave simple up to date instructions here intended for complete Emacs beginners (you must have at least read the built-in tutorial available on C-h t).

Why Emacs

Personally, I find Emacs the most suited editor for Clojure (far beyond Eclipse, IntelliJ plugins). It supports Lisps very well through being itself written and extended (mostly) in Lisp dialect. Secondly, I loved its minimalistic and fully configurable interface, so now I find myself trying to reduce the visual clutter when using Eclipse or IntelliJ by hiding tool-bars, panels, menus and so on. And finally: you will never overgrow it. Using Emacs is a perpetual process of discovering something new and training your brains by memorizing all those key-bindings. :) And you needn't to use arrows keys anymore (you will really hate them after becoming accustomed with Emacs key-map). In these instructions I omitted most of unrelated to Clojure programming Emacs tricks (but left some useful ones) aiming to end up with simplest but still working configuration to start coding (e.g. by reading some good book like this one). Anyone is welcome to extend his config later. I just want not to mix up general Emacs configuration with Clojure-related one.