Sunday, July 8

Tips & Tricks with Clojure Reducers

After noticing a few others encounter the same issues I ran into when first playing with Clojure's reducers, I thought I should collect some of what I discovered into a quick blog post.

First off, reducers are *AWESOME* and fully realize the power of parallel sequences but as they are still alpha have a few rough edges to work around. I'm sure in the near future it will become much more polished but until then be aware of the follow.


Note, clojure.core.reducers will be abbreviated as "r" below;

  • In many cases r/map and r/reduce are faster than their core counterparts, but not in all cases. At this point in time I only use the r/ variants for larger collections (> 1000 elements) and that has worked well.
  • The parallel part of reducers doesn't kick in until you use r/fold instead of reduce. For r/fold to be faster than r/reduce though, it needs to be able to "divide-n-conquer", and hence needs to be able to quickly divide up the collection.
    Summary: r/fold is only faster than r/reduce when operating on a vector.
  • (into [] (r/map inc (vec (range 10)))) works, but uses r/reduce instead of r/fold.
    Instead it's faster to use the following to take advantage of fold;


    so,
    (fold-into-vec (r/map inc (vec (range 2000))))
  • Reducing into map is many times slower than reducing into a single value or vector. Unfortunately merge and merge-with become the bottlenecks here, and this is the biggest rock in my sandal at the moment. :(
  • Don't forget mapv, which was introduced in Clojure 1.4 I believe. It provides a nice middle ground between core/map and core.reducers/map.


The function I'd specifically like to tune further with reducers is the following;

Wednesday, July 4

Datomic with examples in Clojure

So for some quick background, Rich Hickey the inventor of Clojure created yet another NoSQL database called Datomic. I was quite skeptical at it's release with the plethora of other NoSQL solutions available, but was converted by a simple question asked during Clojure Conj West. 

"What is faster, a local spinning disk or an SSD connected via a fat pipe?"

Hearing this I suddenly realized that the significant changes that have occurred with large memory, many locally connected machines, and SSD's really have changed the game and that a re-architecture of databases was called for. 

For more details on that topic, I high recommend starting with Jay Kreps blog post here.

Anyways, Datomic's idea of moving much of the read-only query work to the client, and abstracting away data storage, is very attractive. Loads more details here.

Finally jumping in to Datomic, after I started playing with working sets many times larger than available RAM; I ran into a couple issues that I thought I should mention to hopefully help any other "datomic clojure intro howto" googlers.
  1. Grab the latest greatest version of datamic from here.
  2. Second, unzip it and do;
    $ mvn install:install-file -DgroupId=com.datomic -DartifactId=datomic -Dfile=datomic-${VERSION}.jar -DpomFile=pom.xml

  3. Where ${VERSION} is the numbered version of datomic that you download.
  4. Check out some Clojure examples, and project, I extracted from official Datomic tutorial found here.Examples -> https://github.com/thebusby/datomic-tester/blob/master/src/datomic_tester/core.clj

Hope it helps ;)

Friday, July 9

Back in town for a bit, and lots of photos

Well I'm back in the US for a few weeks, working and having fun. I wont be posting much here for a while, but lots of new pictures on flickr.