Skip to main content

Posts

Showing posts from June, 2014

Clojure for Machine Learning book review

A Clojure for Machine Learning, together with Clojure Data Analysis Cookbook, are two compelling books for people interested in data mining and reasoning. It is also worth mentioning that the amount of publications not dedicated to Clojure itself but how to effectively use it in real world problems is growing. Therefore Clojure for Machine Learning is not a suitable book for newcomers to the language. It will probably not be a good starting point for people completely new to machine learning as well. However basic Clojure knowledge and rough understanding of core concepts in machine learning will be enough to enjoy this book.

Book goes through pretty much all standard machine learning topics, including: linear regression, various classification algorithms, clustering, artificial neural networks and support vector machines. Author also briefly covers large scale machine learning on top off Hadoop and Map Reduce. Too bad other more modern BigData solutions were not represented. This bo…

Option.fold() considered unreadable

We had a lengthy discussion recently during code review whether scala.Option.fold() is idiomatic and clever or maybe unreadable and tricky? Let's first describe what the problem is. Option.fold does two things: maps a function f over Option's value (if any) or returns an alternative alt if it's absent. Using simple pattern matching we can implement it as follows:

val option: Option[T] = //... def alt: R = //... def f(in: T): R = //... val x: R = option match { case Some(v) => f(v) case None => alt } If you prefer one-liner, fold is actually a combination of map and getOrElse

val x: R = option map f getOrElse alt Or, if you are a C programmer that still wants to write in C, but using Scala compiler:

val x: R = if (option.isDefined) f(option.get) else alt Interestingly this is similar to how fold() is actually implemented, but that's an implementation detail. OK, all of the above can be replaced with single Option.fold():

val x: R = option.fold(alt)…