Friday, April 5, 2013

7 languages in 7 weeks: Scala day 2

Day two is all about collections. Forcing the typical Java developer (me) to think about iteration in a different way.

Exercise 1 is to take a List of strings and add up the number of characters using the fold left function. There is a very similar example in the book so I think the author is just checking you are paying attention:

Exercise 2 introduces you to Traits by asking you to develop a trait that has a method which censors a string by substituting words in a string. The words to substitute and what to substitute them with should be stored in a Map.


Then create a version that loads the substitutions from a file:

I chose to extend the trait and populate the alternatives field. Spending all my days writing Java what I ended up producing looks very alien to me. It works (I think) something like this:

  • Open and read a file using the library method fromFile
  • Get the lines of the file one by one using getLines
  • Map each like to an Array by splitting the like with the = sign (the file is key value pairs like a Java properties file)
  • Map the array to a tuple then turn it into a list
  • Construct a map from the List of tuples
And done! Looks a lot nicer than it sounds! The full source code is available on my github.


Functional Programming for the Object-Oriented Programmer: Exercise 1 & 2

The first section of this book introduces you to Clojure - mainly how the read evaluate and print processing works.

You're introduced to:

  • Defining functions
  • Built in functions such as nth
  • Naming things such as functions and values
  • Lists
The exercises are to define a function that returns the second element in a list and at least a couple of implementations of a function that returns the third element in the list. 

Here are my implementations of second:


And here are my implementations of third:

I've used the inbuilt functions nth, first, rest and done an implementation of third that uses one of my implementations of second.

Thursday, April 4, 2013

Getting started with Clojure

The LJC book club is moving on to the book Functional Programming for the Object Orientated Programmer.

The easiest way to get going with Clojure on a mac is with home brew:

brew install clojure

This gives you the clj command which launches a REPL where you can do the customary hello world:



Leiningen is a useful tool for creating Clojure projects; it is also available with home brew:

brew install leiningen

Creating a clojure project with leiningen is as easy as:

lein new oobook-lein

There is a clojure plugin and a leiningen plugin for Intellij that'll make your life much easier. Just go to plugins and search Clojure and Leiningen from the Settings -> Plugins -> Brouse Repositories. Once you have done this you can open your leiningen project in Intellij and you get an environment like this:


The integration with IntelliJ is really nice.

To open up a REPL to play with: Tools -> Start Clojure Console
To load the current clojure script into the REPL so you can use anything you've defined: Tools -> Clojure REPL -> Load files to REPL

To test your setup you can use the hello world example:

And then load it into your REPL then execute the following:
And you should get the output like in the IntelliJ screenshot!

Wednesday, April 3, 2013

7 languages in 7 weeks: Scala day 1

I went to a great talk by Sandro Mancuso at Devoxx UK last week. He mentioned the project he is currently working on being written in Java for the main business flow and Scala for the data manipulation (sorry if I have remembered that incorrectly). This got me thinking it was about time I learnt some Scala. I've dipped in and out of the 7 languages in 7 weeks book; fortunately it has a chapter on Scala. So here I go...

 Googling around and reading the first day the following stands out:

  • Type inference with the safety of static typing (we're all sick of typing types aren't we??)
  • Companion objects instead of static methods
  • Higher order functions (why do we need lambas? Can't we just switch to Scala?)
  • Interfaces, no I mean mixins, wait traits: interfaces with implementation? I think these are great when used well.
  • Tuples and ranges.
So on to my home work!

The exercise for day 1 is to write noughts and crosses. I decided to concentrate on setting up a nice environment for writing scala rather than great code. I started with vim, then sublime but decided I fancied using an IDE. I started with Eclipse but moved to IntelliJ as it looks nicer on a retina screen :) In  addition to setting up an IDE I wanted to do TDD from the start, so I got to grips with ScalaTest.

Here are a couple of example tests:


Here is the main Board object. I could pull out the logic to determine a winner into a class like Game but as this was my first Scala code I decided to keep it simple in a single file.


I've omitted the code that calculates the different types thought it's all available on github. I looked at using a case class rather than a class that extends Enumeration but I haven't been taught case classes yet  so I'll leave that until later.

That's it for day one!