Early Fable Adventures – Building A Memory Tiles Game

Getting aquianted with fable

Just about a month ago, I had the great pleasure of attending F# eXchange 2017 in London. It was 2 days of wonderful F# indulgence with fantastic talks and workshops. It was also a great experience meeting some of the people who have pretty much been virtual mentors to me on my F# journey like Kit Eason and Scott Wlaschin. I did Kit’s pluralsight F# courses and Scott’s fsharp for fun and profit site is a constant source of learning for me so to get chatting with these guys in person was really cool.

Among all of these great talks and workshops was a talk by Alfonso García-Caro, about his Fable compiler. This is an F# to javascript compiler that hooks in really well with the javascript ecosystem. I’m lucky that in my job I am coding in F# across the full stack currently using websharper for our F# to javascript needs. So Alfonso’s talk about Fable really caught my attention. I couldn’t wait to get a bit of free time to dive in and try it out.

Getting up and running with Fable was very quick and easy thanks to this extremely helpful blog post by Krzysztof Cieslak. I started by simply trying out simple dom event handling. Out of the box, Fable has an API that appears to be a light F# wrapper around the javascript dom API.
This is accessed simply with

open Fable.Import

From here, the dom is at your finger tips through the eyes of F# through the module Fable.Import.Browser.

A little game to get my teeth into Fable

After playing around with some dom operations through Fable, I wanted something a bit more substantial to get my teeth into. Recently I was playing this common memory game with my 3 year old daughter. Its a game where you have a grid of cards with pictures on them where every picture will appear on exactly 2 cards. You lay them out randomly upside down. Then you take turns turning 2 cards over at a time. If you get matching pictures, you keep the cards but if there is no match, you turn the cards upside down again. At the end, the player with the most mathed cards wins.

I thought this would be a great game to implement with Fable so I could get started properly with this wonderful F# to javascript compiler. I simplified the game for my first version and built just a one player game. You get a grid of blacked out tiles which have hidden colors where every color will exist twice on the grid. Then you click (or tap if on a mobile device) on 2 tiles to uncover their colors. If they match, the tiles stay open permently until you start a fresh game. If they are not a match, the tiles are blacked out again the next time you click a new tile. This first version was enough to get me building something with Fable. In future versions, I plan to make it a bit more interesting by adding time challenges and levels with bigger grids among other ideas.

Dom operations through Fable to get up and running

There are a few basic things that I wanted to do with the dom for my game interaction. The following were enough for the first version of the game.

Creating a dom element with attributes

let tileDiv = Browser.document.createElement("div")
             tileDiv.className <- "col-3"
             let bc = match tile.Status with
                      | UnMatched -> tile.CoverColor
                      | AttemptingMatch -> tile.HiddenColor
                      | Matched -> tile.HiddenColor
             tileDiv.style.backgroundColor <- bc
             tileDiv.style.border <- "white solid 4px"

In the above, the Browser module is coming from the earlier Fable.Import. From this module, there is a wealth of dom related functions closely matching the standard javascript document API. Here I’m creating a div, setting its class name and a couple other properties including its border and also its background color which is a result of a pattern match to check the status of the corresponding tile in the backing model.

Adding a click event

Adding events is straight forward too. I found if I got stuck because of not knowing the function or type to use from Fable.Import.Browser, I could just look up the corresponding javascript dom function/element on the Mozilla Dom API reference and the corresponding Fable module/type/function was named similarly.
For the memory tiles game, I needed a way to add a click event to a, “Start Fresh Game” button to refresh the grid with new randomized tiles. This is shown below:

let startNewGameButton = Browser.document.createElement("button")
      startNewGameButton.addEventListener_click(fun _ -> startNewGameCallback() :> obj)
      startNewGameButton.innerText <- "Start Fresh Game"
      gameContainer.appendChild startNewGameButton |> ignore

Here, I create the button to start a new game of randomized tiles. I then add a click event which can take a regular F# function as a parameter. Here I’m passing a lambda which, when executed, will call a function called startNewGameCallback. The block of code shown here exists inside a render function in my View module and this render function takes the startNewGameCallback function as a parameter. The full source code for this is on my github MemoryTiles.fsx

I use the same Fable addEventListener_click function for handling the click event on each tile as shown below.

let eventHandler r c d = Func<Browser.MouseEvent, obj>(fun _ -> tileClickCallback r c gameBoard :> obj)
        tileDiv.addEventListener_click(eventHandler rowIndex cIndex tileDiv)

This takes a lambda wrapping another callback which has been passed to the render function in my View module. This eventually calls back to the main driver of the game – the tileClick function in my Controller module, but first let me just show the data modelling from the Model module to put this tileClick function in context.

F# typing goodness with Fable

Fable supports the standard F# data modelling constructs bringing us the F# type safety that we love so much!
For the games’ backing model, I used standard F# records and disciminated unions to represent the immutable game board which serves as a point in time state to render a point in time view and also to enable the generation of a another game board depending on user input and the value of its tiles. A tile itself is modelled as a record and the status of a tile along with the the current selection of tiles are modelled as DUs. So even though this is a game running as javascript, I have still been able to use F# types and F# immutability to express the domain and behaviour of the game. This is shown below. I added a GameBoard helper module for the state transistions of updating tiles and the selection. There is some localized mutability within the updateTile function but the function is still pure with an immutable game board going in and another one coming out. The updateSelected is also returning a new immutable game board.

type GameBoard = {
    Selection : Selection
    Board : Tile list list
}
and Tile = {
    Row : int
    Col : int
    HiddenColor : string
    CoverColor : string
    Status : TileStatus
}
and TileStatus = UnMatched | AttemptingMatch | Matched
and Selection = NoneSelected | OneSelected of Tile | TwoSelected of Tile * Tile
 
module GameBoard = 
    let updateTile r c t gb = 
        let updatedRow = gb.Board.[r] |> List.toArray |> (fun arr -> arr.[c] <- t; arr) |> List.ofArray
        {
            gb with
                Board = gb.Board |> List.toArray |> (fun arr -> arr.[r] <- updatedRow; arr) |> List.ofArray
        }
 
    let updateSelected s gb = { gb with Selection = s}

Using standard .Net events for to trigger view rendering

.Net event handling is one of my favourite features of the .Net platform and, in particular, the way it is implemented in F#. Fable allows us to use standard .Net event triggering and handling. I designed the game using a version of the model, view, controller pattern. I wanted to keep my view decoupled from the model but I still needed a way for it to be rendered whenever the game board changed. I also wanted it to remain as decoupled as possible from the controller module. The controller module passes the main engine of the game, its tileClick function, as a function parameter to the view but the view doesn’t know anything about its implementation. This is similar for the callback to start a fresh game. In order for the view to be rendered and re-rendered, I set up a an event in the in the Model module as follows.

let modelChangeEvent = (new Event<GameBoard>())

In the Controller module, there is a an initialise function which starts everything off. It publishs an observation to the above event and adds a listener, which takes a game board as a parameter and which will call render on the View whenever the a modelChangeEvent is triggered.
A startGame() function, called by initialise, simply triggers a modelChangeEvent to trigger the initial rendering of the game.

let startGame() = modelChangeEvent.Trigger (Model.generateRandomModel 4)
 
let initialise() = 
    Model.modelChangeEvent.Publish.Add(fun gameBoard -> View.render tileClick startGame gameBoard)
    startGame()

Main game logic with F# pattern matching

The main game logic is in the tileClick function of the Controller module. It receives a game board and a row and column for the clicked tile and, based on these, it determines a new game board to be created and triggers the modelChangeEvent so that the View.render function is called to re-render with the new game state.For some cases, no event is triggered and the view is not re-rendered e.g. if the same tile is clicked multiple times in succession. Standard F# pattern matching can be used here and the full function is below.

let tileClick tileRow tileCol (gameBoard: GameBoard) = 
    let board = gameBoard.Board
    let tile = board.[tileRow].[tileCol] 
    let lastSelection = gameBoard.Selection
    if tile.Status = Matched then ()
    else
        match lastSelection with
        | TwoSelected(t1, t2) when t1.Status = Matched && t2.Status = Matched && tile <> t1 && tile <> t2->
            gameBoard
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }
            |> GameBoard.updateSelected (OneSelected { tile with Status = AttemptingMatch })
            |> modelChangeEvent.Trigger
        | TwoSelected(t1, t2) when t1.Status = Matched && tile <> t1 && tile <> t2 -> 
            gameBoard
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }
            |> GameBoard.updateTile t2.Row t2.Col { t2 with Status = UnMatched }
            |> GameBoard.updateSelected (OneSelected { tile with Status = AttemptingMatch })
            |> modelChangeEvent.Trigger
        | TwoSelected(t1, t2) when t2.Status = Matched && tile <> t1 && tile <> t2 -> 
            gameBoard
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }
            |> GameBoard.updateTile t1.Row t1.Col { t1 with Status = UnMatched }
            |> GameBoard.updateSelected (OneSelected ({ tile with Status = AttemptingMatch }))
            |> modelChangeEvent.Trigger
        | TwoSelected(t1, t2) when tile <> t1 && tile <> t2 -> 
            gameBoard 
            |> GameBoard.updateTile t1.Row t1.Col { t1 with Status = UnMatched }
            |> GameBoard.updateTile t2.Row t2.Col { t2 with Status = UnMatched }
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }
            |> GameBoard.updateSelected (OneSelected ({ tile with Status = AttemptingMatch }))
            |> modelChangeEvent.Trigger           
        | OneSelected t1 when t1.HiddenColor = tile.HiddenColor && tile <> t1 -> 
            gameBoard
            |> GameBoard.updateTile t1.Row t1.Col { t1 with Status = Matched }
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = Matched }              
            |> GameBoard.updateSelected (TwoSelected ({ t1 with Status = Matched },{ tile with Status = Matched }))
            |> modelChangeEvent.Trigger
        | OneSelected t1 when tile <> t1 -> 
            gameBoard
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }              
            |> GameBoard.updateSelected (TwoSelected (t1, { tile with Status = AttemptingMatch }))
            |> modelChangeEvent.Trigger 
        | NoneSelected when tile.Status <> Matched -> 
            gameBoard
            |> GameBoard.updateTile tile.Row tile.Col { tile with Status = AttemptingMatch }              
            |> GameBoard.updateSelected (OneSelected ({ tile with Status = AttemptingMatch }))
            |> modelChangeEvent.Trigger
        | _ -> ()

Conclusion

I had loads of fun making this game and definitely want to add more to it. My daughter has been playing away with it and having great fun!
I’ll definitely be continuing on my Fable journey.
The full source code for this game is on my github:
MemoryTiles.fsx
Also the current version of the game can be played here

F# Unit Testing Simplified – Expecto with Visual Studio Code

This post is a slightly shorter follow on post to my last one, F# Has Won Me Over: Coming to .Net World from Outside .Net

In that post, I showed how to set up a basic F# development workflow using Visual Studio Code. It probably seemed a little overkill just to get simple tests up and running.I’m glad I took this longer approach though as, coming into .NET world, it made me aware of how the tools and F# projects fit together under the hood – something that wouldn’t be as clear if I went the all out IDE approach.

Since publishing that blog, I have gotten a lot of really helpful feedback from others in the F# community and I have taken a look at an F# testing tool called Expecto

The Ionide plugin for Visual Studio Code has a really nice integration for running tests using Expecto – it’s a whole lot simpler then the workflow I showed in my last post!

Following from my last post, lets add a different test project to test the MusicLibrary project and we’ll use Expecto this time to run our test.
Again, I’m running Visual Studio Code on Mac OSX in this example but the steps should be more or less the same for other operating systems.

So, back in Visual Studio Code, type cmd-shift-p to open the command palette
Type F# and select F#:New Project

Choose a console as this will give us a main entry point for Expecto to run tests.

You will be prompted for the name of the project directory.

Lets just call it MusicLibraryExpectoTests

You will then be prompted for the name of the project.

Again, MusicLibraryExpectoTests will be grand.

Now we need to add our Expecto dependencies with Paket.

So open up the newly creted MusicLibraryExpectoTests directory and open MusicLibraryExpectoTests.fsproj

Open the command palette again, cmd-shift-p
Type Paket and select Paket:Add NuGet Package (to current project)

Then type Expecto and Enter.

You will see, info in the Paket output terminal.

Afterwards, you should be able to search Expecto in the MusicLibraryExpectoTests.fsproj file and find something like

   
 ..\..\packages\Expecto\lib\net40\Expecto.dll
               

Repeat for
Expecto.BenchmarkDotNet
Expecto.FsCheck

After all this, our paket.references file within the MusicLibraryExpectoTests project should contain

FSharp.Core
Expecto
Expecto.BenchmarkDotNet
Expecto.FsCheck

Lets try out a hello world expecto test. So, open up MusicLibraryExpectoTests.fs which will have automatically been created for us when we created our MusicLibraryExpectoTests project.

Replace the contents of that file with the following:

module MusicLibraryExpectoTests

open Expecto

[<Tests>]
let tests =
  testCase "yes" <| fun () ->
    let subject = "Hello World"
    Expect.equal subject "Hello world"
                 "The strings should equal"


[<EntryPoint>]
let main args =
  runTestsInAssembly defaultConfig args

and save.
Here we have added one test case and a main function as an entry point for expecto to run our tests.
We can easily run our test simply as follows:
Open command palette, cmd-shift-p
Type Ex and choose Expecto:Run

In the expecto output terminal, we can see our failing test!

We can fix our failing test by changing that ‘w’ to upper case so our MusicLibraryExpectoTests.fs becomes:

module MusicLibraryExpectoTests

open Expecto

[<Tests>]
let tests =
  testCase "yes" <| fun () ->
    let subject = "Hello World"
    Expect.equal subject "Hello World"
                 "The strings should equal"

[<EntryPoint>]
let main args =
  runTestsInAssembly defaultConfig args

This time to run the tests, lets just use a shortcut, ctrl-f6
Now, our test passes and you should see this along with other output in the Expecto terminal output in Visual Studio Code:

[21:28:12 INF] EXPECTO?! Summary...
Passed:  1
	yes 
Ignored: 0
	Failed:  0
	Errored: 0

Now, we can use Expecto to test our addSongToLibrary function from the MusicLibrary project that I went through in my last post.
We need to link our MusicLibraryExpectoTests project to this library first. So, open the MusicLibraryExpectoTests.fsproj file again.

Then we can use the command palette, cmd-shift-p to add a project reference.

Choose MusicLibraryExpectoTests for the project to edit

and MusicLibrary as the project to reference

Your MusicLibraryExpectoTests.fsproj should now contain something like

    
ProjectReference Include="../../MusicLibrary/MusicLibrary/MusicLibrary.fsproj"

We can add our test for addSongToLibrary to MusicLibraryExpectoTests.fs as follows:

module MusicLibraryExpectoTests

open Expecto
open MusicLibrary

[<Tests>]
let tests =
  testList "test group" [
    testCase "yes" <| fun _ ->
        let subject = "Hello World"
        Expect.equal subject "Hello World"
                    "The strings should equal"

    testCase "addSongToLibrary adds song to library" <| fun _ ->
        let expected = ["Sir Duke";"Superstition"]   
        Expect.equal expected (addSongToLibrary ["Superstition"] "Sir Duke")
                    "The library list should contain the new song"
    ]

[<EntryPoint>]
let main args =
  runTestsInAssembly defaultConfig args

Now, we can run the tests with ctrl-F6

And in the Expecto terminal output in Visual Studio code,
you should see something like this along with other output

Passed:  2
	test group/yes ....
       ....
	test group/addSongToLibrary adds song to library [/Users/tom/Dropbox/software-
Ignored: 0
	Failed:  0
	Errored: 0

Conclusion

There is a lot more you can do with Expecto. I have just scratched the surface here but, even with the basics here, you can very quickly create and run test cases from within Visual Studio Code. It’s definitely a tool that I will be using a lot!

F# Has Won Me Over: Coming to .Net World from Outside .Net

F# is a language that was sitting in the corner calling to me for a long time tempting me to go take a look at it. I watched Scott Wlaschin’s fantastic talk on Functional Programming Design Patterns some time ago. This talk is a walk through of common FP design patterns with the example code being in F#. Scott put together this talk so well that it is not a blocker to getting the most from the talk if you don’t have any previous knowledge of F#. At the same time, coming from mostly programming in Scala professionally, I couldn’t help but admire the beauty of the F# code in his examples. So I had a quick look further at some F# code out there. Things like a simple syntax using whitespace, automatic currying, nice concise representations of Algebraic Data Types caught my eye immediately.

Over the Christmas holidays I dived into F# by taking courses on FSharp TV and by going through content from Scott’s wonderful blog Fsharp For Fun And Profit.

F# has quickly become my favourite language to code in along with Haskell. Along the way though, there were some stumbling blocks getting used to the .Net world. Most of these could have been avoided if I just used the Xamarin Studio IDE. However, I’m glad I didn’t and took a little pain along the way as it made be explore the .Net and F# ecosystem. I did start to use Visual Studio Code which is really a text editor with a load of great plugins out there for different languages. Its lightweight enough that you still have to find out what’s happening under the hood a bit with your F#  .Net projects but, at the same, it does make things like creating projects with common templates, building and adding references to existing projects a lot less painful. Incidentally, there is now a Haskell plugin for Visual Studio Code which uses Intero. This is very exciting indeed and I can’t wait to try it out!

How do I code in F# on a mac?!

Yep this is a question I had for a while too. That is until I discovered Mono, an open source .Net framework which is cross platform.
I found the best way to get F# set up on OSX was to install Mono but to actually use the installer that can be downloaded here. I tried installing the homebrew way first but this seems to be missing some .Net libraries. For example, one that is needed for System.Drawing. I uninstalled my homebrew mono installation and installed from the Mono download that I linked to above, and I was then able to build and draw lovely spirographs from the FSharp TV, Introduction to F# course.

How do I create an F# project?

This was a question I had too. I wanted to simply create a project with a common folder structure. I was looking for something similar to lein new in Clojure or stack new in Haskell. A great tool for this is Forge. The Visual Studio Code ionide plugin integrates with Forge to make it really easy to create new projects through the command palette in Visual Studio Code. I demo this further down.

How do I manage dependencies?

The tool for this is Paket which uses the Nuget package manager under the hood. Again, Visual Studio Code has a plugin to integrate with Paket, Ionide-Paket

How do I build an F# project

FAKE is a very popular build tool for F# and allows you to specify your build tasks through a build.fsx file using and F# DSL. Visual Studio Code has a plugin to integrate with this too as part of its ionide suite, Ionide FAKE This is demoed further down in this blog too.

How do I run a .exe on Mac!

Some of these tools I have mentioned along with the nunit testing tool (which I demo further down) require the running of a .exe to perform tasks. If the .exe is one that doesn’t run directly on Mac OS or Linux, usually it can be run using

mono name-of-your-executable.exe

once mono is installed and on your path and once name-or-your-executable.exe is actually a file that can be executed. It may seem like a simple thing but it is important to know as there are resources out there that describe how to use .Net tools assuming you are on a Windows machine.

Demo Time: TDD with F#

As there are resources out there about setting up F# with Mono and Visual Studio Code, there’s no need to re-iterate them here so I will assume that you managed to get those set up. If you have questions, feel free to comment on this blog.
My main stumbling block coming from outside the .Net world was simply setting up a unit test project and linking this to a production code project (by production code, I mean implementation code).
Lets go through that here:

So in Visual Studio Code, from an empty directory, open command palette with shift-command-p and type F#.

Then select New Project.

You will be prompted to select the type of project.

Choose classlib for our production code.
You will then be prompted to choose the project directory.

Lets specify a directory called MusicLibrary. So type in MusicLibrary, hit enter and you will be prompted to provide a project name. Lets call this MusicLibrary too.

After a second or so, you should eventually see that the ionide plugin has created three directories, .packet, MusicLibrary and packages. It will have also created a build.fsx file and a build.sh file so that we can build our new MusicLibrary project with FAKE.

In the MusicLibrary directory, you will find a newly created MusicLibrary.fs file.

This is where we are going to put our production code after we write a test for it.
For now, delete everything from there and we are going to add a function to test called addSongToLibrary which will eventually take a list of existing song titles and a new song title and return a list of song titles containing our the existing ones and our new title. For now, it just returns an empty list until we have our test ready. The contents of the MusicLibrary.fs file can be replaced with:

module MusicLibrary

let addSongToLibrary existingSongs newSong = []

Now, we should be able to build this right?. Lets give it ago.

Open the Visual Studio command palette again and type FAKE.

Select Build

You will be prompted for a FAKE build task to run.

Select build.

Now we get an error:

If you look down at the output window in Visual Studio Code, you will see something like
build.sh: line 34: syntax error: unexpected end of file

We need to convert the dos .sh file to into unix format. There is a tool for this on the Mac App Store dos2unix.

Install that.
In the terminal go into the parent directory of your MusicLibrary directory where build.sh exists and run dos2unix on the build.sh file.

dos2unix build.sh
dos2unix: converting file build.sh to Unix format...

Now follow the steps earlier to try and do a FAKE build in visual studio code.
In the output in visual studio code, you should see output about using paket to get any needed dependencies and also about the creation of dlls.
From my understanding Dlls in .Net are like jar files in the jvm world and they are the binaries created as a result of compiling F# code. The FAKE build will now have created a build directory and put the dlls in there.

You will see a MusicLibrary.dll in there.

Back in the MusicLibrary directory there is also a MusicLibrary.fsproj file which FAKE uses to find out things it needs for the build. When we write our test, we will see how we can add a reference to the MusicLibrary.dll into our .fsproj file for our unit test project. If you look in build.fsx, you will see a reference to these .fsproj files for FAKE.

let appReferences  =
    !! "/**/*.csproj"
    ++ "/**/*.fsproj"

Lets set up our test project so we can test our addSongToLibrary function!

To test our addSongToLibrary function we can set up a MusicLibraryTest project. Follow the same steps as we did for creating the MusicLibrary project in visual studio code except for the prompt to choose library type, choose fsunit

After, you should see a newly created MusicLibraryTest directory.

If you go into that directory, you will see that there has been a MusicLibraryTest.fs file created for us containing:

module MusicLibraryTest

open NUnit.Framework
open FsUnit

[<Test>]
let ``Example Test`` () =
    1 |> should equal 1

There are 2 libraries imported here for unit testing, NUnit.Framework and FsUnit.
This is where paket comes in for our test dependencies. If you go into the paket.references file within the MusicLibraryTest directory you will see

FSharp.Core
FsUnit

So, we need to link our MusicLibraryTest project to our MusicLibrary.fsproj so that we can test our addSongToLibrary function. This is taken care of by add a reference to the MusicLibrary.fsproj in the MusicLibraryTest.fsproj.

Open up the MusicLibraryTest.fsproj file.
Now, open visual studio command palette again.

Select Add Project Reference.
Then choose MusicLibraryTest as the project that you want to edit

Then choose MusicLibrary as the reference

Now, somewhere in the MusicLibraryTest.fsproj, there will be a newly created reference to the MusicLibray.fsproj. Search the MusicLibraryTest.fsproj for MusicLibrary.fsproj.

Now we can reference our MusicLibrary module and call the addSongToLibrary function.
Back in the MusicLibraryTest.fs file, we can import the MusicLibrary module which contains our addSongToLibrary function and lets write a test to test adding a song title.

Replace the contents of MusicLibraryTest.fs with:

module MusicLibraryTest

open NUnit.Framework
open FsUnit

open MusicLibrary

[<Test>]
let shouldAddASong () =
    ["Sir Duke";"Superstition"] 
    |> should equal (addSongToLibrary ["Superstition"] "Sir Duke")

How do I run my test!

First of all lets create our MusicLibraryTest.dll. This is done the same way that we created MusicLibrary.dll before by running the FAKE build in visual studio code. After running the build, there should be a MusicLibraryTest.dll in your build directory.


Actually getting tests to run was a stumbling block for me. To run tests, a FAKE task can be created in build.fsx. I will show how to do that in a little bit. First I will show how to run more manually using an nunit test runner.
The NUnit3 test runner can be downloaded as a zip file here.
To make this work with a FAKE task a bit later, first create a directory tools/Nunit in the root folder in visual studio code that you have your MusicLibrary and MusicLibraryTest projects in.
Then download NUnit.Console-3.5.0.zip into there.


Then unzip it.

Now we can use the nunit3-console.exe to run our test.
In the terminal, go into the tools/NUnit folder.
Make the nunit3-console.exe executable:

chmod 755 nunit3-console.exe

We can run our test with the command:

mono nunit3-console.exe ../../build/MusicLibraryTest.dll

This should output our failing test result:

NUnit Console Runner 3.5.0
Copyright (C) 2016 Charlie Poole

Runtime Environment
   OS Version: MacOSX 16.1.0.0
  CLR Version: 4.0.30319.42000

Test Files
    ../../build/MusicLibraryTest.dll


Errors and Failures

1) Failed : MusicLibraryTest.shouldAddASong
  Expected is <Microsoft.FSharp.Collections.FSharpList`1[System.Object]>, actual is <Microsoft.FSharp.Collections.FSharpList`1[System.String]>
  Values differ at index [0]
at FsUnit.TopLevelOperators.should[a,a] (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] f, a x, System.Object y) [0x0004e] in :0
at MusicLibraryTest.shouldAddASong () [0x0003d] in :0

Run Settings
    DisposeRunners: True
    WorkDirectory: /Users/tom/Dropbox/software-development/fsharp/NUnitDemo/tools/Nunit
    ImageRuntimeVersion: 4.0.30319
    ImageRequiresX86: False
    ImageRequiresDefaultAppDomainAssemblyResolver: False
    NumberOfTestWorkers: 8

Test Run Summary
  Overall result: Failed
  Test Count: 1, Passed: 0, Failed: 1, Inconclusive: 0, Skipped: 0
    Failed Tests - Failures: 1, Errors: 0, Invalid: 0
  Start time: 2017-01-08 13:40:33Z
    End time: 2017-01-08 13:40:33Z
    Duration: 0.142 seconds

Now lets integrate this into FAKE.

In the build.fsx file, add the following near the top underneath
open Fake

add

open Fake.Testing

so you now have

open Fake
open Fake.Testing

Add our NUnitTest task underneath the Target “Deploy” block.

let testDlls = !! (buildDir + "*Test.dll")

Target "NUnitTest" (fun _ ->
  testDlls
    |> NUnit3 (fun p -> p)

)

So we have:

Target "Deploy" (fun _ ->
    !! (buildDir + "/**/*.*")
    -- "*.zip"
    |> Zip buildDir (deployDir + "ApplicationName." + version + ".zip")
)

let testDlls = !! (buildDir + "*Test.dll")

Target "NUnitTest" (fun _ ->
  testDlls
    |> NUnit3 (fun p -> p)

)

Also add NUnitTest to the build order so the end of your build.fsx file should be

// Build order
"Clean"
  ==> "Build"
  ==> "NUnitTest"
  ==> "Deploy"

// start build
RunTargetOrDefault "Build"

Open Visual Studio Code command palette and type FAKE as before

Choose FAKE: Build

Now you should see our new NUnitTest task so select that.

This will fail because of our failing test.

The output pane in visual studio code will have the results of the nunit3-console.exe runner

1) Failed : MusicLibraryTest.shouldAddASong
  Expected is <Microsoft.FSharp.Collections.FSharpList`1[System.Object]>, actual is <Microsoft.FSharp.Collections.FSharpList`1[System.String]>
  Values differ at index [0]
at FsUnit.TopLevelOperators.should[a,a] (Microsoft.FSharp.Core.FSharpFunc`2[T,TResult] f, a x, System.Object y) [0x0004e] in :0
at MusicLibraryTest.shouldAddASong () [0x0003d] in :0

Lets make the test pass!!

Go back to the MusicLibrary.fs file and make the test pass by replacing its contents with:

module MusicLibrary

let addSongToLibrary existingSongs newSong = newSong :: existingSongs

In Visual Studio Code, use the command palette to run FAKE: Build selecting build as before and then  FAKE: Build selecting NUnitTest.

Looking through the output in visual studio code we can now see our passing test report:

Test Run Summary
  Overall result: Passed
  Test Count: 1, Passed: 1, Failed: 0, Inconclusive: 0, Skipped: 0
  Start time: 2017-01-08 13:46:29Z
    End time: 2017-01-08 13:46:29Z
    Duration: 0.132 seconds

Conclusion

Hopefully this has helped people to get started with the F# and .Net ecosystem and get up and running quickly in this amazing language.

Setting Up A Haskell Development Environment (Mac OS)

Since I started developing in Haskell, I have experimented with a few different setups. I started with Vim with no plugins and that, together with GHC in the terminal was my vanilla setup as I spent my first couple of months learning the basics of Haskell. I like to start off this way when learning a new language as it really forces me to commit the basics of the language and its syntax to memory.

As I learned more about the language and moved on to more advanced topics and more complex problems, I needed to take advantage of Haskell tooling to increase my productivity. Sometimes I find the tooling for Haskell can get a bad rep but I actually find it very good. Tools like ghc-mod,  hlint and stylish-haskell provide a lot of key features that you may be used to from working with IDEs – code completion, documentation, compile error and warnings and even suggestions to make your Haskell code more idiomatic. The tricky thing can be getting these tools integrated into your favourite editor or IDE.

I started with VIM plugins to integrate these tools with VIM and also played around with the Haskforce plugin for Intellij which is an excellent plugin. I finally settled on the Atom editor which makes integrating Haskell tools very simple and it also has a load of really great plugins to enhance your Haskell development experience.

In this post, I’m going to go through step by step how to set up a Haskell development environment using Stack (a Haskell build tool), ghc, ghc-mod and other wonderful Haskell tools, and integrating all this with the Atom editor.

To make this a bit more realistic and to make sure that I don’t miss anything, I’m stripping my laptop of any Haskell, Stack, ghc and other installations related to Haskell tooling. I will leave Atom installed but I’ll remove all my Haskell plugins and start from scratch. As I set up my environment from scratch, I will document everything with command line commands and screenshots. I am using a Mac with macOS Sierra.

Okay here goes…
Firstly, I use Homebrew for installing Stack.

Setting up Stack, Ghc and other Haskell tools

➜  ~ brew install haskell-stack
➜  ~ stack setup

This may take about 10 minutes and you will see something like the following output.

Using latest snapshot resolver: lts-7.13
Writing implicit global project config file to: /Users/tom/.stack/global-project/stack.yaml
Note: You can change the snapshot via the resolver field there.
Downloaded lts-7.13 build plan.
Fetched package index.
Populated index cache.
Preparing to install GHC to an isolated location.
This will not interfere with any system-level installation.
Downloaded ghc-8.0.1.
Installed GHC.
stack will use a sandboxed GHC it installed
For more information on paths, see 'stack path' and 'stack exec env'
To use this GHC and packages outside of a project, consider using:
stack ghc, stack ghci, stack runghc, or stack exec

We can now start up the Haskell compiler, ghc, in interactive mode, ghci
In other words lets crank up a Haskel REPL and try a Haskell expression:

➜  ~ stack ghci
Configuring GHCi with the following packages:
GHCi, version 8.0.1: http://www.haskell.org/ghc/  :? for help
Loaded GHCi configuration from /Users/tom/.ghci
Loaded GHCi configuration from /private/var/folders/tj/mtvt3dv57cq6kbzw8809_gs00000gn/T/ghci78459/ghci-script
ghci>  1 + 1
2

We can quit back out of ghci now

ghci>  :q

We can now use stack to set up the tools that will be the engine behind a lot of our Haskell development experience.

➜  ~ stack install ghc-mod hlint stylish-haskell

You will see something like

[1 of 2] Compiling Main             ( /Users/tom/.stack/setup-exe-src/setup-mPHDZzAJ.hs, /Users/tom/.stack/setup-exe-src/setup-mPHDZzAJ.o )
[2 of 2] Compiling StackSetupShim   ( /Users/tom/.stack/setup-exe-src/setup-shim-mPHDZzAJ.hs, /Users/tom/.stack/setup-exe-src/setup-shim-mPHDZzAJ.o )
Linking /Users/tom/.stack/setup-exe-cache/x86_64-osx/tmp-Cabal-simple_mPHDZzAJ_1.24.0.0_ghc-8.0.1 ...
ghc-paths-0.1.0.9: download

It will continue on and download all the packages it needs. This can take a while.

comonad-5: download
comonad-5: configure
polyparse-1.12: copy/register
cpphs-1.20.2: download
comonad-5: build
...
Progress: 54/71
...

You should eventually see something like

Copying from /Users/tom/.stack/snapshots/x86_64-osx/lts-7.13/8.0.1/bin/hlint to /Users/tom/.local/bin/hlint
Copying from /Users/tom/.stack/snapshots/x86_64-osx/lts-7.13/8.0.1/bin/stylish-haskell to /Users/tom/.local/bin/stylish-haskell

Copied executables to /Users/tom/.local/bin:
- ghc-mod
- ghc-modi
- hlint
- stylish-haskell

Stack has now added executables in /Users/tom/.local/bin (/Users/tom being my home directory or ~)

You can add this to your PATH environment variable. I do this by editing ~/.zshrc as I use Zsh for my shell. If you are using bash, you can edit .bashrc

In the line for export PATH=
add this to the start of your path
“/Users/tom/.local/bin:
except the /Users/tom part needs to be replaced with the path to your home directory.

Setting up Atom

Download and install the Atom editor. It can be downloaded from here

So lets crank up this beautiful and highly configurable editor!

To start adding our Haskell Atom packages, click “Install a Package” and then “Open Installer”

Search for haskell

Click “Install” on the following
language-haskell
haskell-ghc-mod
ide-haskell
autocomplete-haskell

These should be all you need to get going but there are also other great packages that are worth exploring.
For example, haskell-hoogle lets you get documentation from Haskell Hoogle through a context menu.

There shouldn’t be any other configuration needed.
Lets set up a Haskell Stack Hello World project and edit it in Atom.

Close Atom for now.

back in the terminal, we can create a Stack project called hello.

➜  ~ stack new hello

This will show something like this

Downloading template "new-template" to create project "hello" in hello/ ...

The following parameters were needed by the template but not provided: author-email, author-name, category, copyright, github-username
You can provide them in /Users/tom/.stack/config.yaml, like this:
templates:
  params:
    author-email: value
    author-name: value
    category: value
    copyright: value
    github-username: value
Or you can pass each one as parameters like this:
stack new hello new-template -p "author-email:value" -p "author-name:value" -p "category:value" -p "copyright:value" -p "github-username:value"

Looking for .cabal or package.yaml files to use to init the project.
Using cabal packages:
- hello/hello.cabal

Selecting the best among 9 snapshots...

* Matches lts-7.13

Selected resolver: lts-7.13
Initialising configuration using resolver: lts-7.13
Total number of user packages considered: 1
Writing configuration to file: hello/stack.yaml
All done.

You’ll see there that it prints out info about .cabal. Cabal is a Haskell build tool that Stack is built on top of.

So lets open this up in Atom and see what it looks like. My main pain point with Haskell and Atom was getting Atom to play nicely with ghc-mod through Stack. The following way of opening a Stack project in Atom is officially deprecated but I still find it the simplest way to open a Stack project in Atom and have it work with no hassle.

Go into the hello directory.

➜  ~ ls -l hello

Open Atom through Stack

➜  hello stack exec atom

Click on “Open a Project” and the the “Open a Project” button

Select your hello directory and click open.

To open the main.hs file that stack has generated for us:
cmd-p
and type in “main”

Select main.hs
You will see a haskell-ghc-mod warning appear.

This seems to be a side effect of using stack exec to open the Stack project in atom. This warning popup can be closed and ignored. I have found no bad effects of ignoring this warning anyway.

We can now see the main.hs open with our nice Haskell syntax highlighting.
I have gone ahead and typed “i” under the module declaration for and you can see the code complete for importing a module.

If I type “im” and tab, it inserts a template for module import. There a lot of these handy templates including data declarations do syntax, if then else etc

If I start typing out an import for Data.List, you can see auto complete of ghc-mod giving me my options.

I can also go ahead and start typing “put” into main and I get auto complete options again.

If I make a mistake and save, ghc-mod gives me my compile error in the bottom pane and also when I hover over the place where the error occurs.

So lets fix that and print “Hello World”

Now we can go back to our terminal and build this.

➜  hello stack build
Warning: File listed in hello.cabal file does not exist: README.md
hello-0.1.0.0: build (lib + exe)
Preprocessing library hello-0.1.0.0...
[1 of 1] Compiling Lib              ( src/Lib.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.0.0/build/Lib.o )
Preprocessing executable 'hello-exe' for hello-0.1.0.0...
[1 of 1] Compiling Main             ( app/Main.hs, .stack-work/dist/x86_64-osx/Cabal-1.24.0.0/build/hello-exe/hello-exe-tmp/Main.o )
Linking .stack-work/dist/x86_64-osx/Cabal-1.24.0.0/build/hello-exe/hello-exe ...
Warning: File listed in hello.cabal file does not exist: README.md
hello-0.1.0.0: copy/register
Installing library in
/Users/tom/hello/.stack-work/install/x86_64-osx/lts-7.13/8.0.1/lib/x86_64-osx-ghc-8.0.1/hello-0.1.0.0-2MDzoFPW8yDJLgrpJYcMkC
Installing executable(s) in
/Users/tom/hello/.stack-work/install/x86_64-osx/lts-7.13/8.0.1/bin
Registering hello-0.1.0.0...

Stack creates an executable for us called hello-exe. This name is configured in hello.cabal in our hello directory.

We can run this executable now.

➜  hello stack exec hello-exe
hello world

Finally, that ghc-mod warning that was coming up in Atom:
cmd-, will bring up the settings pane.
Go into packages, haskell-ghc-mod and Settings and scroll down and check “Suppress GHC Package Path Warning”. However, due to the warning there, it might be better not to do this and to just close out of the ghc-mod warning popup with you open up a project.

Hopefully this guide has been useful. Enjoy playing with Atom and its packages for Haskell!

Additional Note

For existing projects, if they were built with stack that had a different lts-resolver, you can get problems with atom.Easiest thing there is to delete the .stack-work directory in the root directory of your project. In terminal run this command from the project root directory:

stack init --solver --force

this creates a new stack.yaml file with the lts-resolver for your installed stack. Removing .stack-work may cause a lengthy rebuild if the existing project has a lot of dependencies.
My projects have small numbers of dependencies at the moment so it hasn’t been a problem for me.
Then rebuild your project with

stack build

and open atom with

stack exec atom

from your project’s root directory as before.

Haskell div mod – Cyclical Division, Modulus Visualised

Reading Time: 8 mins
When I first came to integer division and the modulus operation in Haskell, I was thrown a bit of a curve ball, but a good curve ball at that. While Haskell does have two forms of integer division, one of those is a lot different from how I was used to thinking about integer division in other languages like Java and Scala. This actually comes in very handy for algorithms that cycle through ranges of values either left to right or right to left. However, as I was more used to the way it is done in Java and Scala, it took a bit of a mind-shift when I came to Haskell and saw another way to handle integer division and modulus.

One way of doing integer division in Haskell is with the quot and rem functions. They can be put together with the function quotRem and giving a tuple of the result and the remainder and this works like many would be used to from Java or some other languages.

ghci> let x = 8 :: Integer
ghci> let y = 3 :: Integer
ghci> quotRem x y
(2,2)
ghci>
ghci> let x = (-8) :: Integer
ghci> let y = 3 :: Integer
ghci> quotRem x y
(-2,-2)
ghci> let x = (-8) :: Integer
ghci> let y = (-3) :: Integer
ghci> quotRem x y
(2,-2)

ghci> let y = -3 :: Integer
ghci> quotRem x y
(-2,2)
ghci>

I’ve put together  a visualisation and a set of rules that I use to reason about another way that Haskell provides for integer division and along with its modulus operation. First, lets take 8 div 3 and show the results of the different combinations of positive and negative numerators and denominators.
Haskell has a handy function called divMod which returns a tuple with the first element being the integer division result and the second being the remainder (the result of the mod operation).

8 div 3

ghci> let x = 8 :: Integer
ghci> let y = 3 :: Integer
ghci> divMod x y
(2,2)
ghci>

-8 div 3

ghci> let x = (-8) :: Integer
ghci> let y = 3 :: Integer
ghci> divMod x y
(-3,1)
ghci>

-8 div -3

ghci> let x = (-8) :: Integer 
ghci> let y = (-3) :: Integer
ghci> divMod x y 
(2,-2)
ghci>  

8 div -3

ghci> let x = 8 :: Integer
ghci> let y = (-3) :: Integer
ghci> divMod x y
(-3,-1)
ghci>

These can be surprising results especially when compared to, say, Scala. For example, if we take -8 mod 3 in Scala, we get -2 unlike the 1 modulus result we got in our Haskell result (-3,1)

scala> -8 % 3
res2: Int = -2

Visualising Cyclical Integer Division And Modulus

The visualisation works as follows:

  • We have a denominator block in blue which has a length equal to the denominator.
  •  We have a denominator range that we are trying to move the block into which ranges from the denominator , inclusive, to the 0 boundary, exclusive.
  •  We move the denominator block until it partially or completely enters the denominator range.
  •  We have a Divisions accumulator which gets incremented or decremented.
  • If our block moves so that it approaches the denominator range without passing through the 0 boundary, we increment our Divisions accumulator with each move.
  •  Otherwise, the denominator block passes over the 0 boundary and we decrement our Divisions accumulator with each move.
  •  The final Divisions accumulator is the division result and the difference between the top of the denominator block and the 0 boundary gives the remainder.

8 div 2

0 Boundary

1
2
3
4
5
6
7
8

Divisions 0

0 Boundary

1
2
3
4
5
6
7
8

Divisions 1

0 Boundary

1
2
3
4
5
6
7
8

Divisions 2

0 Boundary

1
2
3
4
5
6
7
8

Divisions 3

0 Boundary

1
2
3
4
5
6
7
8

Divisions 4

Our last move brings the denominator block fully into the denominator range so we have no remainder.
So, 8 div 2 is 4 remainder 0.

8 div 3

0 Boundary

1
2
3
4
5
6
7
8

Divisions 0

0 Boundary

1
2
3
4
5
6
7
8

Divisions 1

0 Boundary

1
2
3
4
5
6
7
8

Divisions 2

Our last move brings the denominator partially into the denominator range. Our Divisions accumulator is 3 and the difference between the top of the denominator block and the 0 boundary is 2.
So, 8 div 3 is 2 remainder 2.

Changing division direction -8 div 3

Our Divisions accumulator gets decremented this time because we will be crossing the 0 boundary.

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions 0

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions -1

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions -2

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions -3

Our last move brings the denominator partially into the denominator range. Our Divisions accumulator is -3 and the difference between the top of the denominator block and the 0 boundary is 1.
So, -8 div 3 is -3 remainder 1.

Making the denominator negative, -8 div -3

We can visualise the integer division in the same way. Except, this time the denominator range that we move the denominator block into is -3, inclusive to -1, inclusive.

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions 1

-8
-7
-6
-5
-4
-3
-2
-1

0 Boundary

1
2
3

Divisions 2

Our last move brings the denominator partially into the denominator range. Our Divisions accumulator is 2 and the difference between the top of the denominator block and the 0 boundary is -2.
So, -8 div -3 is 2 remainder -2.

8 div -3

-3
-2
-1
1
2
3
4
5
6
7
8

Divisions 0

0 Boundary


-3
-2
-1
1
2
3
4
5
6
7
8

Divisions -1

0 Boundary

-3
-2
-1
1
2
3
4
5
6
7
8

Divisions -2

0 Boundary

-3
-2
-1
1
2
3
4
5
6
7
8

Divisions -3

0 Boundary

Our last move brings the denominator partially into the denominator range. Our Divisions accumulator is -3 and the difference between the top of the denominator block and the 0 boundary is -1.
So, 8 div -3 is -3 remainder -1

Conclusion

So integer division, when treated like it is with div and mod in Haskell, is different to what many developers would be used to from Java or Scala. The modulus operation is really a way to restrict elements to a certain range as dictated by the denominator. This is why the denominator blocks in the above visualisation are always trying to get into the denominator range without moving past it. It also makes sense to me that, when the denominator block arrives in the denominator range, the remainder is the difference between its top and the 0 boundary.

There are a lot of ways to visualise and think about these cyclical division and modulus operations. I hope this blog may help others reason about the these operations and explore other ways of thinking about them.

Dip your toes in some Haskell, the water is warm here!

Reading Time: 15 mins
Reading Time with hands on trying of the examples: 30 mins approx

Haskell Warm Water

So, Haskell sounds quite daunting right? All this talk of monads and functors when all you want to do is just try out some simple problems and see what it’s like to play around with this language! Monads and functors are actually not that scary once you get into functional programming but, for today, I won’t mention them again. So, lets dip our toes in and see what it’s like.

I often hear newcomers to functional programming sighing to themselves about how difficult it is to retrain their brains to read and write code in this paradigm that is new to them. Don’t get me wrong, the sighing is understandable especially when we have spent years wrapped in our imperative, object oriented comfort blankets with for loops and mutable variables. The sighing is usually witnessed when watching an experienced OO programmer getting to grips with a new codebase written in a functional language and seeing something like this:

5 isPalindrome (x1 : xs)  = x1 == last xs && isPalindrome (init xs)

Yep, there is a bit in there especially when you may be more used to the imperative way of writing such an algorithm.

 2 
 3 public class Palindrome {
 4 
 5     public static boolean isPalindrome(String str) {
 6 
 7         int strEndIndex = str.length() - 1;
 8 
 9         for(int i = strEndIndex; i > (strEndIndex/2); i--)
10             if(str.charAt(i) != str.charAt(strEndIndex - i))
11                 return false;
12 
13         return true;
14     }
15 }

Ah, that’s better. Our old reliable Java for loop with our mutable counter. Let me take you on a small journey to give you a taster for Haskell. We’ll go from Haskell code that actually looks quite similar to that imperative Java code and gradually work towards that previous Haskell code snippet. We’ll use the good old palindrome kata to take us on that journey. The palindrome kata is a task to write a function which checks if a String is the same going from left to right as it is going right to left. Yes, I know, most languages have a library function, reverse, meaning we can simply reverse the String and compare with the original. But lets suspend our disbelief for a while so I can take you on a journey!

Quick setup to follow along

To start dipping your toes in, why not follow along with the Haskell examples in the rest of this post. Haskell quick installers for most types of OS can be found here:
After that, all you need is a text editor and your terminal.
Make a new file called DipYourToesIn.hs and add the declaration for the Haskell module where we will put our functions:

 1 module DipYourToesIn where

Now crank up your favourite command line terminal and go to the directory where you created that file.
Now, simply type: ghci and this will start up the Haskell compiler in interactive mode allowing you to enter and evaluate Haskell expressions. We will use it to simply load and compile our DipYourToesIn.hs Haskell source so that we can call our Haskell functions as we create them to see it them working.

ghci> :l DipYourToesIn.hs
[1 of 1] Compiling DipYourToesIn    ( DipYourToesIn.hs, interpreted )
Ok, modules loaded: DipYourToesIn.
ghci> isPalindromeWithRecursion "hello"
False
ghci> isPalindromeWithRecursion "racecar"
True
ghci>

The Haskell source can be loaded and compiled using :l DipYourToesIn.hs
Once it is compiled, we can now execute functions within that file by simply calling them.
In the next section, we will be creating the function, isPalindromeWithRecursion, but the above just shows how quick it is to start interacting with Haskell code as you write it. In saying that though, this shouldn’t be a substitution for tests. I would still write tests first, and use this interactive approach to experiment with the code that I am writing to make the tests pass.

Imperative Looking Haskell – looks can be deceiving!

So, lets start off with some Haskell that models the above Java solution quite closely.

 2 
 3 isPalindromeWithRecursion :: String -> Bool
 4 isPalindromeWithRecursion str =
 5   loop strEndIndex where
 6 
 7   strEndIndex = length str - 1
 8   loop i =
 9     if (i <= (div strEndIndex 2)) then True
10     else if (str !! i) /= str !! (strEndIndex - i) then False
11     else loop (i - 1)
12 

A note about function type signatures

Line 3 in the above example contains the type signature for our function right after the :: after the function name. This specifies that the function takes a String parameter and returns a Bool (a boolean). Parameters and return types are separated by the -> symbol. If there are multiple parameters, they are all separated by -> with the last type being the return type. This is because every function in Haskell is actually a one parameter function. So, when we see a multiple parameter type signature, under the hood we have a series of outer to inner functions – a concept called currying. It is not important to know all about that just yet though if you are just dipping your toes in.

In Haskell there are no mutable variables or for loops but that doesn’t stop us from writing code that looks almost like the imperative Java solution. We define an inner loop function which is called recursively acting like our for loop.

  • On line 5, the where syntax there is a way to give bindings to local values or functions – essentially creating local immutable variables.
  •  On line 8, we define the loop function which takes an immutable counter, i, meaning that the counter is “updated” by passing a new version of the counter value into the loop function every time it is called.
  •  On line 5, the loop function is called with an initial counter, i, argument being the last index of the String – just like we initialised i in the Java solution.
  • The algorithm remains the same. We check the first and last characters. If they are equal, we continue checking the next 2 inner characters and so on. If we find two that are not equal, we can return False straight away and if we get down to only one or no characters remaining, we know we have a palindrome and return True.
  • This divides the end index of the String by 2 so that we can tell we have gotten to a point of 1 or 0 characters left.
  • (div strEndIndex 2)

     

  • This gets the i’th element of a list, in this case, the i’th character in the String str.
  •  (str !! i)

     

  • This is how we compare a character going right to left with its corresponding character going left to right. /= meaning not equal to
  • (str !! i) /= str !! (strEndIndex - i)

     

As shown previously, we can load this into our GHCI and look at it in action.

ghci> :l DipYourToesIn.hs
[1 of 1] Compiling DipYourToesIn    ( DipYourToesIn.hs, interpreted )
Ok, modules loaded: DipYourToesIn.
ghci> isPalindromeWithRecursion "hello"
False
ghci> isPalindromeWithRecursion "racecar"
True
ghci>

Guard those if then elses!

Instead of building up multiple if then elses, Haskell has this lovely construct called Guards. Using, the | symbol, we can build up boolean conditional expressions with corresponding expressions, one of which will be evaluated if its corresponding boolean expression evaluates to true.

13 isPalindromeWithRecursionAndGuard :: String -> Bool
14 isPalindromeWithRecursionAndGuard str =
15   loop strEndIndex where
16 
17     strEndIndex = length str - 1
18     loop i
19       | i <= (div strEndIndex 2) = True
20       | (str !! i) /= str !! (strEndIndex - i) = False
21       | otherwise = loop (i - 1)
22  

Nothing has changed in our underlying algorithm. Also, not much has changed in the code but it is easier to read right? We still have the same set of conditions and corresponding expressions, one of which will be evaluated.

 | i <= (div strEndIndex 2)
| (str !! i) /= str !! (strEndIndex - i)

and

 | otherwise =

otherwise is the default case and its corresponding expression will be evaluated if none of the other boolean expressions have evaluated to True.

A bit about Haskell lists

A List in Haskell is a recursive data structure. A List can be empty and is simply represented as

[]

A List can also be non empty and, in this case, it is represented as a cell containing a value and a link to another List which itself can be empty or contain another cell with a value and a link to a further List. This recursive structure continues until we get an empty list. This is shown in the diagram below.

beginning-haskell-2-001

The cons function in Haskell, represented with a

:

is used to create one of these list cells by joining a value together with an existing List which may or may not be empty. Using this function, we can create the List in the above diagram as follows:

1 : 2 : 3 : []

Haskell has a nicer way though with syntactic sugar allowing us to do the same thing with this bit of code:

[1, 2, 3]

A couple of List operations to help the palindrome cause

Haskell like other functional programming languages has very nice high level operations that we can use on lists. Lets try a couple with GHCI.

First lets create a List and bind it to a name, l. This is done in GHCI using the let keyword but, in Haskell source code, the let isn’t required.

ghci> let l = [1,2,3,4,5]
ghci> l
[1,2,3,4,5]
ghci>

We’re going to need to be able to get the last element in the list so we can compare it to the first element. This is no problem though, we can simply use a function called last.

ghci> last l
5
ghci>

We will also need to get the first element in a list. We will be doing this later by refining the code with pattern matching. However, just for completeness, we do this by using a function called head.

ghci> head l
1
ghci>

Be careful though, calling head on an empty list will give an exception.

ghci> head []
*** Exception: Prelude.head: empty list
ghci>

For reasons that will become apparent soon, we will also need to use a handy function called init. This creates a new list from everything in an existing list except the last value.

ghci> init l
[1,2,3,4]
ghci>

Polishing it off with pattern matching

Pattern matching is a concept very prevalent in functional programming. It allows you specify patterns to match against a data structure, its value type and by the structure and values of its elements. This allows the data structure to be deconstructed and parts of it extracted.

Function arguments are one place where pattern matching can be used very effectively. This is done in Haskell by redefining the function multiple times to deal with the different possible argument value patterns that can be passed in when the function is called.

52 
53 isPalindrome :: String -> Bool
54 isPalindrome [] = True
55 isPalindrome (x : []) = True
56 isPalindrome (x1 : x2 : []) = x1 == x2 -- this line is not necessary, thanks Szymon 
57 isPalindrome (x1 : xs)  = x1 == last xs && isPalindrome (init xs)
58 

  • The final example above uses a mixture of pattern matching and the functions we saw earlier that we can use on lists. A String is a list of characters so we can pattern match on a list argument to the isPalindrome function.
  • Line 54 matches on the empty list. So, if an empty list is passed as an argument to the isPalindrome function, we return True straight away.
  • Line 55 pattern matches on a list with one element. We use the cons function, : , to match on a list structure which contains one cons cell with a value and a link to an empty list. The value of the cons cell can be anything and is bound to the value, x, in the pattern. So, with this pattern, we are checking if the list has only one element and, if it does, we can again return True.
  • Correction, line 56 is not necessary. Thanks for pointing this out Szymon. If a 2 element list has 2 equal values, the init on line 57 will be called on a one element List, xs, returning an empty list and on the next recursive call, line 54 will return True. Line 56 is pattern matching to check if the list argument has only 2 values. We again use the cons function to match against the list structure that we are looking for. We can also bind the first 2 values of the list to the names, x1 and x2. This allows us to check these for equality to determine if the 2 element list is a palindrome.
  • Finally, on line 57, we are back to that piece of code that is likely to make an experienced imperative programmer sigh when trying to get to grips with a functional programming codebase. When pattern matching, the first function expression with a matching pattern for the argument being passed in is the function expression that gets executed. So, if we have gotten this far, there has been no other pattern match so we know that our list argument has more then 2 values. So, now we simply use one cons function to match on a list that has a first element, x1, and a link to the rest of the list, xs. Now we can use our trusty last function to compare x1 to the last element of the list. If they are not equal, we return false as we don’t have a palindrome.
  • However, it they are equal, we need to evaluate the right hand side of the boolean &&. In this case, we literally create a new list made up of all elements of the list minus the first and last element. By calling init on xs, we are omitting the first element, x1, and getting all elements except the last. Then we recursively call isPalindrome on this newly created list so that we can continue checking further and further into the original list until we get to 2 values that are not equal or an empty or 1 element list (after all other outer elements have been “chopped off” so to speak).

Conclusion

I hope all this might encourage more developers to delve into the beautiful language of Haskell. I started dipping my toes in a while back and, pretty quickly, I took the plunge and absolutely love coding in Haskell. I feel at my most relaxed as a developer when I am writing Haskell code. Building up small composable Haskell functions with a type system that protects you but never gets in your way really does lead to a joyful coding experience for me. I will follow up this post with some more dipping of the toes in Haskell to show how we can use the type system to make our isPalindrome function handle lists of any type…well almost any type.

Haskell Flavoured Curry

curry

Reading time: 10 minutes
Currying is a very useful pattern in functional programming. It is a way of breaking up a function which takes multiple parameters in to a chain of outer to inner functions, each of which, takes one parameter. It allows arguments which are known at one point in program execution to be captured and used in a later execution of a function requiring more arguments. In some ways, it’s similar to when an Object is instantiated in OO with one or more property values which are used in later function executions involving that object.

I have found that the subject of currying can be confusing for developers who are new to functional programming. It was for me at the start anyway. I used to try and relate everything back to OO but I discovered that using a substitution model approach based on the Lambda Calculus really simplified things. I won’t go into the specifics of Lambda Calculus here but I’ll give a short example of using a substitution approach in the context of understanding currying.

So, we have a function which transforms a String by prepending a certain number, n, of characters to the start of it returning a new String.

So, in pseudocode this could look like:

prepend(n, c, s) = put n c's at the start of s

Now lets execute this function:

prepend(3, 'z', "Tom")

This would result in

"zzzTom"

This function is completely pure in that it has no observable interaction with its outside context. This is extremely useful because it means that anywhere we see this function called in a program, we can substitute the call arguments into the function’s implementation without changing the meaning of our program. Functional programmers love to say “functional programming is easier to reason about” and this isn’t just a catchphrase. It is this ability to substitute arguments for parameters in expressions, along with all the other functional programming goodness like immutability, that does actually make functional programming easier to reason about.

So, using a simple substitution model, when we call

prepend(3, 'z', "Tom")

in our program, we can replace it with its implementation expression by substituting into it the arguments 3, ‘z’ and “Tom” for the parameters n, c and s respectively.

so

prepend(3, 'z', "Tom")

becomes

put 3 'z' 's at the start of "Tom"

For a curried version of this function we can break it up into three one parameter functions: (I’m using

->

here to separate a function name with its parameter and the function’s expression body – again this is pseudocode)

prependA(n) -> prependB(c) -> prependC(s) -> put n c's at the start of s

Okay, hold a while here! I know this looks kind of weird as we have three functions, starting with the outer prependA function and returning an inner function until we reach the innermost expression. This is where, before, I would have gained initial understanding by thinking about each outer closure returning its inner closure working from the outmost function, prependA to the innermost, prependC – outer to inner functions/closures is the subject of one of my earlier blogs: Closures For OO Developers – 1.
I find that trying to think in this way can start to bend the brain a little bit as higher order functions become more complex. If we look at how this works using the substitution model, things become way easier!

Okay, so lets say we know how many characters we want to prepend to a String but we don’t know what the character is or what the String is just yet. That’s okay!
We can still call our function

prependA(n)

.
So, if n is 3, we call,

prependA(3)

and substituting we 3 for n in its implementation expression, we get

prependB(c) -> prependC(s) -> put 3 c's at the start of s

Okay now, we have a function which can take, as a parameter, the character that we are prepending. At some point, we know that the character we want to prepend is ‘z’, so we call

prependB('z')

substituting z for c in its implementation expression, we get

prependC(s) = put 3 'z' 's at the start of s

Now we have a function which can take a String, s, and prepend three ‘z’ characters to the front of it to produce a new String.

At some point, we find out that the String we want to transform is “Tom” so we call:

prependC("Tom")

Now, we substitute again and this becomes:

put 3 'z' 's at the start of "Tom"

which is back to what we had when we called the original prepend(3, ‘z’, “Tom”)

Haskell likes its Curry

In the above example, I have actually shown, in a simplified way, how the function substitution would work in the Lambda Calculus.
With Haskell being an implementation of the Lambda Calculus, all functions are curried. This doesn’t mean that all functions have to be written as one parameter functions. When a function is declared with multiple parameters, Haskell breaks this up into a series of outer to inner one parameter curried functions underneath the hood. So, our prepend function from above could be declared in Haskell in a way that doesn’t look like its curried.

prepend :: Int -> Char -> String -> String 
prepend n c s = take n (repeat c) ++ s

The expression in the implementation of the prepend function above just says:
take the character, c repeated n times and stick it onto the String s to form a new String (without mutating the original String s).
The type signature is

prepend :: Int -> Char -> String -> String 

Type signatures in Haskell show the curried nature of functions with a series of types separated by

->

with the last being the return type of the function.

The prepend function above takes three parameters, n, c, and s just like the prepend function in our earlier pseudocode. However, just like how I broke this up into a series of outer to inner one parameter functions in the pseudocode, Haskell does this automatically for us.

This allows us to do some really handy stuff!

So, say we want a function to pad a String with a number of characters to the left of the String but we know that we will always want 10 characters. We can declare this as our pad10 function below.

pad10 :: Char -> String -> String 
pad10 = prepend 10

In the above code, we have simply assigned pad10 to prepend but we are only calling the prepend function with one argument, 10. Because the pretend function is automatically curried, when we call pretend 10, we get back another function which takes a character, c which in turn, when called with a character argument, will return the innermost expression which is the expression that actually does the String prepending.

We can continue with this pattern.

So, say we always want to pad a String to the left with 10 white space characters.

pad10WhiteSpaces :: String -> String 
pad10WhiteSpaces = pad10 ' '

In the Haskell code above, we again take advantage of Haskell’s automatic currying and call pad10 with 1 argument, the

' '

character. We can follow a similar substitution to our pseudocode so:

substituting

pad10 ' '

we get

prepend 10 ' '

and substituting this, we get:

take 10 (repeat ' ') ++ s

So when we call

pad10WhiteSpaces "Tom"

this substitutes to

take 10 (repeat ' ') ++ "Tom"

when executed, will produce

"          Tom"

Haskell’s automatic currying can take a bit of getting used to but it makes for beautiful code! It really shines when you want to re-use a multi parameter function to create a function that can only take fewer or even one parameter. For example, if we had a list of Strings and we wanted to produce a list of all the these same String values but with each one padded with 10 spaces. All we need is the original prepend function and this transformation would be as simple as mapping (prepend 10 ‘ ‘) over the list of Strings:

map (prepend 10 ' ') ["I", "like", "curry", "a", "lot"]

Isn’t that handy!

Executing this results in

["          I","          like","          curry","          a","          lot"]

Function Composition Is So Damn Nice in Haskell !

Reading time: 5 minutes

Haskell has some beautiful constructs and really allows for very expressive code along with maintaining strong, static typing and purity. I’ve been taking a deep dive into Haskell and the more I learn about the language, the more I’m falling for it! There are some really beautiful ways to express functional programming concepts in this language.Function composition, especially when used with point free function references, is one of them.

So, what is function composition anyway?

This is a common approach in functional programming to build a pipeline of operations by combining functions to form new functions. Its very useful in reusing existing pure functions to produce a new transformation.

e.g
f(x) = x^2
g(x) = x + 2

Functions f and g can exist on their own but maybe we want a new function which takes a number, squares it and then adds 2 to it. We don’t want to create a whole new implementation for this when the transformation in question can simply be made up of two sub transformations, the functions f and g.
So, we create a new function, call it z, which takes, the number x, calls f on it and calls g on the result of that.
So we get
z(x) = g(f(x))

Simplified expression of composition in Haskell

Functional Programming languages usually each have their own special construct for expressing the composition of functions. This can be especially useful if there are multiple functions being composed as it cuts out the need for multiple groupings of expressions. For example, if we also have the functions,

h(x) = x * 3
k(x) = x / 4

and we decide we want a new transformation to square a number, add 2 to it, then multiply it by 3 and then divide it by 4,
we end up with a composition like this:

z(x) = k(h(g(f(x))))

I know this is just pseudocode but, even still, it is becoming hard to read! And also, bear with me. I know composing transformations as simple as arithmetic is a bit contrived and could be done with just one overall arithmetic expression. The example is deliberately contrived but if you imagine a pipeline with more complex transformations like, say, processing financial trades or something, being able to break an overall transformation up into small composable transformations becomes incredibly useful!

Haskell has a beautifully simple way of expressing function composition. Lets have a look and I’ll explain the Haskell code along the way.

module FunctionComposition where 
 
f x = x^2 
 
g x = x + 2 
 
h x = x * 3 
 
k x = x / 4 
 
 
z x = k . h . g . f $ x

In the Haskell code above I have declared the four functions, f, g, h and k that I showed earlier, along with the function z which is a composition of those four. I would usually write associated type signatures with every function I write in Haskell but I have left typing out of this example to focus on the function composition construct and also an explanation of Haskell typing definitely merits its own blog post!
The function declaration syntax in Haskell is, in itself, very concise and elegant. It is simply the name that the function is being bound to followed by a space and one or more parameters, each separated by spaces, followed by = and then the expression of the function body.
So, in the case of

 f x = x^2

the name that the function is bound to is f, there is only one parameter and this is x and the expression of the function body is after the = and is
x^2

The function composition part is in the declaration of the function, z.
z x = k . h . g . f $ x

The composition is handled by the

.

in between each of the functions being combined together.

.

is in itself a function which takes which takes 2 functions, say (in pseudocode) g(x) and f(x) and returns a new function z(x) = g(f(x)). To compose up multiple functions is so simple and elegant with this simple composition function.

The

$

is just a function which applies what is on its left to what is on its right. It has the lowest precedence though so it means the function composed of f, g, h and k on its left is fully composed before being applied to x.

Also, if you prefer the reading of the sequence of transformations from left to right instead of right to left, you can simply add this just under the module declaration in the above Haskell code.

import Control.Arrow

And now, we can use >>> instead giving

z x = f >>> g >>> h >>> k $ x

What’s this “point free” thing?

Point free means expressing functions without their arguments. This may sound kind of strange for people coming from OOP or other paradigms but it actually makes for code where the reader can concentrate on the transformation that is being expressed without the code being obscured by unnecessary argument names. It is made possible in functional programming because functions can be treated as first class values and they can be bound to names just like any other values.

So, say we compose z just from the functions g and f above, the Haskell function composition would be:
z x = g . f $ x

but the x argument serves no purpose here. All we need to do is bind to z, the function which is composed of g and f. A type signature (left out here as typing is a subject for another blog post) here would serve the purpose of expressing the type of the parameter that z expects when applied. So, using the point free style the functional composition is made even more elegant and can be expressed as:

z = g . f

and composing the 4 functions f, g, h and k simply becomes:

z = k . h . g . f

Now that really is quite beautiful!

Closures For OO Developers – 2 – FP in an OO Language

Reading Time 5 to 10 minutes

In a previous blog post Closures For OO Developers – 1, I described how I initially thought about closures when I came to Functional Programming. I related an inner closure having access to the environment of the outer closure that created it as being conceptually similar to an inner object having access to the properties of the outer object that created it.

I thought I would back up a bit and show that a closure can really just be thought of as a one method object. There is a quote floating around something like “A Closure is a poor man’s object and visa versa”. I’m not sure of the exact origin of that quote but really the point is that closures and objects are almost the same thing. Closures, in a sense, when thought of as one method objects, bridge the gap between OO programming and functional programming.

It is possible to apply functional programming principles in an object oriented language using closures as one method objects even when the language in question isn’t a fully featured functional programming language. I’m going to take an example of a very common transformation in functional programming – map. This is a function over a data structure which also takes a mapping function as a parameter and applies this mapping function to each element in the data structure to create new data structure elements and, ultimately, a new data structure.

Closures to make OO more Functional

Lets have a look at how this may have been achieved with closures in Java pre Java 8. I will then show the same thing in Java 8 using Java 8 lambdas which are closures.

 
 
    public static <T, U> List<U> map(List<T> list, Mapping<T, U> mapping) { 
 
        List<U> result = new ArrayList<U>(); 
        for (T elem : list) { 
            result.add(mapping.apply(elem)); 
        } 
        return result; 
    } 
 
 
    private interface Mapping<T, U> { 
        U apply(T element); 
    } 
 
    public static void main (String[] args) { 
        List<String> inputList = 
                Arrays.asList("functional", "programming", "with", "closures"); 
 
        Mapping<String, Integer> stringToStringLength = 
          new Mapping<String, Integer>() { 
            public Integer apply(String element) { 
                return element.length(); 
            } 
        }; 
        List<Integer> mappedList = map(inputList, stringToStringLength); 
        System.out.println("Initial List: " + inputList); 
        System.out.println("Mapped List: " + mappedList); 
    }

In the example above, there is a map function which takes a list but it also takes another object as a parameter which will decide how each element in the list will be transformed into a new element for the resulting list. In functional programming, functions are treated as first class values and can be passed as arguments into other functions in the same as any other type of value.

In order to do this in an OO language such as Java pre Java 8, this function argument, mapping, needs to be first wrapped in a one method object. In this case the one method object will be an implementation of the one method interface, Mapping. This interface has one method, apply. The method is essentially the first class function that could be passed around in a functional programming language. An instance of this one method interface can be called a closure. In functional programming, this one method object is represented simply as a function and can also be called a closure.

The main method uses this map function to transform a list of strings into a new list representing the length of each string in the original list. An instance of the Mapping interface is created with an anonymous class. This is the one method object or closure or “function” which is passed as an argument to the map function in order to tell the map function how to transform the list of strings.

Lambdas/Closures in Java 8 – Coating on one Method Objects

Now lets refine the above example a bit to show how, essentially, Java 8 hides this under the covers (but I’m sure it provides optimisations along the way) with its lambda syntax.

 
 
    public static <T, U> List<U> map(List<T> list, Function<T, U> mapping) { 
 
        List<U> result = new ArrayList<U>(); 
        for (T elem : list) { 
            result.add(mapping.apply(elem)); 
        } 
        return result; 
    } 
 
    public static void main (String[] args) { 
        List<String> inputList = 
                Arrays.asList("functional", "programming", "with", "closures"); 
 
        List<Integer> mappedList = map(inputList, s -> s.length()); 
 
        System.out.println("Initial List: " + inputList); 
        System.out.println("Mapped List: " + mappedList); 
    }

In the above example, the Mapping interface is no longer required because Java 8 has a library of one method interfaces, called Functional Interfaces, out of the box. The map function, apart from using the Java 8 Function interface, has remained the same in its implementation. In the main method, instead of having to create and instantiate an anonymous class, we can use a Java 8 lambda which is essentially a function implementation.
s -> s.length()
This is defining a function implementation which takes a String (or anything which has a .length() method and returns an Integer) and returns the length of that String. It can really be thought of as a syntax replacement for the anonymous one method class that we had to declare in the previous example.

Refined more with Java 8 Streams

Using Java 8 streams we can take this further by simply using the map method already available on Java 8 streams. It is typical for any functional programming language to provide this map method out of the box.

 
 
    public static void main (String[] args) { 
        List<String> inputList = 
                Arrays.asList("functional", "programming", "with", "closures"); 
 
        List<Integer> mappedList = inputList.stream().map(s -> s.length()).collect(toList()); 
 
        System.out.println("Initial List: " + inputList); 
        System.out.println("Mapped List: " + mappedList); 
    }

The above example is refined to use Java 8 streams. The whole transformation can now be done in the main method using the map function from the Java 8 API. The lambda which is passed to this function remains unchanged from the one passed to the earlier implementation of map.

Haskell Beauty!!

To finish, here is the same mapping transformation implemented in the fully fledged, pure functional programming language that is Haskell.

map length ["functional", "programming", "with", "closures"]

In the Haskell above, we are simply declaring that we want to map the length function over the list of strings.

Conclusion

Thanks for reading this. I hope this post brought some value to developers looking to move into the functional programming world and also to developers already doing functional programming along with anyone with any other interest in functional programming. Feel free to comment.

Declaring Vs Impering

After years of OO programming,  I had become quite used to the imperative approach to implementing algorithms. One of the beauties of functional programming is that it challenges you to adapt to a different way of thinking and to look at other ways of approaching problem solving, implementing algorithms, and indeed to designing software modules and complete systems.

Imperative programming is all about executing a sequence of steps, each of which, can mutate program state. Declarative programming, on the other hand, is more about declaring what you want a program or component to do with one or more immutable data structures and about linking these declarations together to implement declarations at higher abstraction levels or to form a pipeline of operations on immutable data structures. That difference may seem a bit abstract so lets dive into some code to show what I mean (and maybe learn some Clojure along the way if you are not already familiar with this wonderful language).

Looping

Even something as simple as a looping construct can be enough to show the difference between the imperative and declarative approach.

  
 
    public int sum(int[] nums) { 
        int result = 0; 
        for (int i = 0; i < nums.length; i++) { 
            result += nums[i]; 
        } 
        return result; 
    } 

 

This example is a typical for loop written in Java. The algorithm to sum up an array of numbers is implemented as a sequence of steps which are repeated on the updated mutable variables – the counter variable, i, and the accumulator variable, result.

The Functional Programming approach in implementing this algorithm would be to use recursion to declare the operation to be performed on the array. The following is an example of this declarative approach in Clojure. numbers here is a Vector in Clojure similar to an array.

(defn sum [numbers] 
  (let [go (fn go [numbers result] 
             (if (seq numbers) 
               (go (rest numbers) (+ result (first numbers))) 
               result))] 
    (go numbers 0)))

In the example above, there is an inner function declared called go. This function takes, as parameters, the current immutable state of the numbers collection that it is summing up and also the current immutable result. These arguments are, at runtime, immutable values created for every call to the go function. When executed, this function will return the result if there are no more numbers to process – (seq numbers) returns a falsey value in this case – or else the go function recursively calls itself passing in the rest of the numbers to process along with a new result value calculated from adding the head of the current nums vector to the current result.

Note here that (rest numbers) creates a new immutable Vector with the same numbers except omitting the first number.

Also

(+ result (first numbers))

returns a newly calculated immutable result value.

This implementation can be simplified in Clojure using the loop construct below.

(defn sum-with-loop [numbers] 
  (loop [nums numbers 
         result 0] 
    (if (seq nums) 
      (recur (rest nums) (+ result (first nums))) 
      result)))


In this code, the inner go function is replaced with (loop
which also allows its parameters to be initialized.

(recur..here is a Clojure construct which will recursively execute loop with new arguments. The benefit of using the recur construct in Clojure is that it uses tail recursion. This means that, provided the recursive call is the very last evaluation of the function, the function’s stack frame can be reused for the recursive call meaning that stack overflow can be avoided for large data structures.

Implementing a loop is a reletively low level abstraction but, even at this abstraction level, we can see the difference between the imperative and declarative approach. The imperative for loop uses mutable variables to specify, quite explicitly, a sequence of steps to be implemented. The functional declarative approach uses recursion and more so describes, at a higher level, what we want the program to do. We are literally just saying
“Is the Vector empty? if so just give us back the aggregate but, if not..”
“Produce a new aggregate by adding the top of the list to the current aggregate”
“Now just go ahead and do all that again but with the rest of the Vector as a new Vector…oh and use the new aggregate”.

Declaring At Higher Abstraction Levels

In functional programming, pure functions are composed to create new pure functions. By a pure function, I mean a function which always returns the same output value(s) for the same input value(s) for all possible input values.
So, often in functional programming, there is no need to implement low level recursive loops. Instead functions at higher abstraction levels can be used instead and there are many common functions that are provided in all functional programming languages. The declarative approach really shines when using these higher level functions. Implementations become a lot more concise and readable compared to their corresponding imperative implementations.

Lets look at an example of this. So, say we want to transform a list of numbers into a new list by taking just the even numbers and multiplying them by n.

The following is an imperative approach written in Java – this can be done in a declarative way with Java 8’s functional programming features but the example here is to show the imperative approach that is common in OO programming.

 
 
    public List<Integer> multiplyEvenNumbersBy(List<Integer> nums, int n) { 
        List<Integer> result = new ArrayList<Integer>(); 
 
        for (Integer x : nums){ 
            if(x % 2 == 0) { 
                result.add(x * n); 
            } 
        } 
 
        return result; 
    }

Now, below we implement the same function in a declarative way in Clojure.

(defn multiply-even-numbers-by [nums n] 
  (->> nums (filter even?) (map #(* % n))))

In the declarative approach, we can use these higher abstracted functions called filter and map to declare exactly what we want to do do:

“Give me a sequence of just the even numbers with each one multiplied by n”.

#(* % n) is a shorter way to write anonymous functions in Clojure with % being an argument passed to this one arg function which multiplies its argument by n.

Again everything is immutable in the declarative approach unlike the mutable variables that are used in the imperative approach.

Laziness

The other beauty about the declarative approach using map and filter is that this declarative approach is lazy. It is using lazy sequences in Clojure which are only actually evaluated if they are needed.

(->> nums (filter even?) (map #(* % n)))

The above does not actually immediately produce a new sequence. Instead, it builds up a composition of functions, a functional pipeline, which will be executed to produce elements of this new sequence as they are needed.

For example, lets take a Vector of numbers and, from it, create a new sequence with each number multiplied by 2.
This example is executed directly in the Clojure REPL (read eval print loop console that allows immediate execution of Clojure code.)
It is common to do this in Functional Programming using a utility map function. This takes a data structure and a function to be performed on each element in the data structure in order to produce a new data structure.

(def result (map (fn [x] (println "executing") (* x 2)) [1 2 3]))
=> #'clojure-blogs.core/result

In the above Clojure code, we are declaring that the functional pipeline to map the Vector [1 2 3], into a new sequence, be bound (like ‘assigned to’ in a language with mutable variables) to the Symbol (like a variable) result. A println “executing” side effect (interaction with the function’s outside environment) has been added to the mapping function in order to prove that this function is not executing until one or more elements of the resulting sequence are actually required.
The code above is executed immediately and we can see the result simply prints the the new Symbol with the namespace clojure-blogs.core and name result. The Vector has not yet been transformed and the mapping function has not yet actually been executed. All we have done here is build up a functional pipeline on the original Vector which will be later executed on one element or more elements if their resulting elements are required.

Now, lets actually use result in order to force evaluation of the mapping function and the production of our new Vector.

result 
executing 
executing 
executing 
=> (2 4 6)

In the above code, we reference the result Symbol in the REPL. Now, result needs to be evaluated meaning that the functional pipeline that it references needs to be executed on each element to produce elements of the resulting sequence.
We are transforming every element of the Vector here but we could instead ask for one or more elements at a time meaning that the pipeline is, in turn, executed on each of these elements at a time to produce a new element. We can now clearly see that our map function has been eventually executed 3 times – 1 time for each number in the original Vector.

Conclusion

The declarative approach of Functional Programming can be a bit of a mind shift when coming from a more imperative OO background. It certainly was for me but it was a very positive mind shift. It makes for much more concise code and, in my opinion, it allows us to move up abstraction levels more easily and take on more complex problems without having to keep so much in our head at once.

I would always encourage other developers to stick with what might be a new way of thinking for them as it really can reap great rewards. I know, after years of writing imperative code, seeing more declarative code can seem a bit foreign and it can seem like it will take too long to get to grips with – especially when multiple data transformations like filters and maps are chained together. It is always a good approach to make sure that each of the smaller transformation functions and also the functions passed as parameters to them, are kept small, pure and well named. If it seems like its too much trying to understand a functional pipeline with multiple maps, filters flatMaps etc, I always think…”imagine if this had been written imperatively though and all the extra loops, branches and mutable state I would have to comprehend – all that extra stuff I would have to hold in my head” – I know.. if imperative code is well abstracted and organized, it should be easier to read but, in my opinion, writing declarative code can in itself bring us a step towards code that is easier to read, understand and maintain.

The declarative approach really shines when used in conjunction with laziness – complex functional pipelines that are only executed on an element of a data structure, if it is required for that element to be evaluated. These pipelines don’t even always need to have a tangible data structure to operate on, and can even produce their own data structures one element at a time – this is the concept of infinite sequences which I will get to in an upcoming blog post.

Thanks!

Thanks so much for taking time to read this blog. I really hope it has helped in other peoples’ functional journeys.
Feel free to leave feedback in the comments.