# Curiouser and Curiouser!

'Where shall I begin, please your Majesty?' He asked. 'Begin at the beginning,' the King said, very gravely, 'and go on till you come to the end: then stop.'

## Juxt a minute

Caveat lector: skip if you're not interested in the experiences of someone learning Clojure and functional-programming.

At the moment I am reading the excellent Clojure Programming from O'Reilly. It was one of the first books I bought but I found it a little formidable as an introduction to Clojure. I'm finding it a lot more digestible now.

In it I came across an example of the excellently named `juxt` function. Reading the docs I found it a little difficult to get my head around:

``````Takes a set of functions and returns a fn that is the juxtaposition of those fns.  The returned fn takes a variable number of args, and returns a vector containing the result of applying each fn to the args (left-to-right).
``````

It's actually really simple but the use of the word juxtaposition made me think it more complicated than it is. To whit:

``````juxtapose (verb) place or deal with close together for contrasting effect: black-and-white photos of slums were starkly juxtaposed with colour images.
``````

But what was the contrasting effect?

The O'Reilly example was:

``````(juxt quot rem)
``````

as a replacement for:

``````(defn euclidian-division [x y]
[(quot x y) (rem x y)])
``````

and, as I played with the output of the function, the penny starts to drop. Then I see it, that the contrast, the juxtaposition, is in how each of the functions passed to `juxt` interprets its argument(s).

It seems to me that `juxt` is like a `map` generator. But where `map` is about mapping a function across a sequence of values, `juxt` returns a function that maps a sequence of functions across its arguments.

So now I felt I understood it but I wondered how I could make use of it myself. I use `map` all the time but in what circumstance do you need to apply a series of functions over a value?

In calculating the value of a polynomial the degrees are, essentially, functions of an argument `x` where x is raised to the degree and multiplied by a constant, e.g.

${\mathrm{2x}}^{2},{\mathrm{3x}}^{1}, or{\mathrm{-1x}}^{0}$

The result of each of these degree functions is summed to get the total value of the polynomial for `x`. In Clojure terms we could say this is summing the juxtaposition of the degree functions which might look a bit like:

``````(defn polynomial [& degrees]
(comp (partial apply +) (apply juxt (map-indexed (fn [degree multiplier]
(fn [x]
(* multiplier (Math/pow x degree)))) (rseq (vec degrees))))))
``````

`polynomial` is a function that takes as it's arguments the constant multiplier of each of the degrees of a polynomial starting with the highest and ending with the power zero. It returns a function that takes an argument `x` and returns the value of the given polynomial for `x`.

So:

``````# Calulate the value of the polynomial
#   2x^2+3x-1
# where x = 3
((polynomial 2 3 -1) 3)
# => 26.0
``````

I use `juxt` to create a function that takes an argument and return a sequence of values obtained by passing the argument to each of a sequence of anonymous functions that calculates the value of one degree of the polynomial:

``````(fn [x] (* multiplier (Math/pow x degree)))
``````

I then use `comp` to compose the function returned by `juxt` with a function that sums the individual components to give the total value for the polynomial at `x`.

I'm quite happy that I understand `juxt`, found a reasonable way of using it, and also managed to get `comp` in there too!

Update: Marshall Bockrath-Vandegrift (llasram from the #clojure IRC channel on Freenode) came up an alternative implementation using `->>` threading macro to pull the function pipeline inside out, replacing the more cumbersome `(rseq (vec degrees))` with `(reverse degrees)` and using Clojure's anonymous function literal syntax `#(..)` instead of `(fn ..)`.

``````(defn polynomial
[& degrees]
(->> (reverse degrees)
(map-indexed (fn [i m] #(* m (Math/pow % i))))
(apply juxt)
(comp (partial apply +))))
``````

I think this one is more readable and probably more idiomatic clojure. Thanks llasram.

05/02/2014 16:00 by Matt Mower | Permalink

## Not quite forevernote

I've recently allowed my Evernote premium subscription to lapse. Here's why.

When the date of my renewal arrived, Evernote sent me a somewhat brusque "Thanks and goodbye" email telling me about how I'd been downgraded and lost a bunch of features.

I'd been in two minds about renewing right up until the deadline. Perhaps a softer approach, maybe even offering me another week or so to think about it, might have won me over. Alas, we'll never know.

The reason I was having so much trouble justifying the renewal is the vast gulf between Evernote the concept and The Evernote application.

The concept of having all your information organised in notebooks that can be shared and synchronised across all your devices, even shared with other people, is a compelling one.

But there are just so many areas where it doesn't work right that a great concept is translated into an application I ended up resenting.

For me the most essential are:

• The editor is rubbish
• Sharing is a minefield
• The security story is straight out of the "How not to do it" playbook
• It's dumb, no scripting, no smarts
• You'd be forgiven for thinking one dev works on it every other weekend

The most pressing of these is the editor. For an application that is all about authoring text, the writing environment is appalling. In the Mac version there are no headings and you can't even indent text!

And worst of it is that it's just HTML. There are free HTML editors leagues ahead of this. It really is a dismal experience and they show no signs of improving it at all. It certainly hasn't improved in the 15+ months I've been using it.

Sharing notebooks is a killer feature. And it works, more or less, providing you don't need anyone else to change anything. Because if you do you will often get bizarre conflicts and stuff will get deleted or overwritten. With careful management it's possible to work together but inexperienced users are always going to get tripped up and who needs this much hassle?

Once you start putting valuable info in there you'll wonder about Evernotes security. Only it doesn't have any. Everything is in the clear. Unless you use encryption on a page-by-page basis. Only you won't because it's so painful to do.

Then again with all this information in it you'd Evernote to be really smart. To help you make connections, maybe even to be a base for user-scripts.

But no, Evernote is dumb. While they do have a web API for capturing content there's nothing for the app itself. When I think of Tinderbox and it's agents and, well, really I shall just stop here because it's sad how stupid Evernote is. And don't get me started "related" pages.

Lastly I guess we come to the final nail in the coffin: I have lost all hope that any of these shortcomings will be addressed.

In the 15 months I have been a premium subscriber the application has not improved in any area that I care about. That the editor has not improved is baffling beyond my ability to comprehend. I cannot reward such developers with my money or my attention.

So I shall be bidding Evernote goodbye over the next month or two. I'm not sure yet what I will use to replace it. I could go back to VoodooPad which is in new hands and may see some evolution but is, anyway, pretty good.

I've also been pondering the lessons of LightTable and considering building something myself using ClojureScript, node-webkit, and Dropbox.

But I've been here before and I'm not sure I have the skill & dedication to take on such a major undertaking on my own.

More later I guess.

01/02/2014 01:12 by Matt Mower | Permalink

## Can't close. Won't close.

Okay so, like most of you, I perpetually have anything from 10 to 10^30 open browser tabs. This starts to become a problem when Safari is consuming all the memory in my computer and starting to chew through the disk as well.

At that point I am presented with the problem of how do I close some of these tabs. And it is a problem because I know of only two strategies:

1. Bookmark the page URL in Pinboard to read later
2. Read the page

I like strategy 1, I use it often, but I have to acknowledge that Pinboard is pretty much where I send pages to die.

So any page that I'm not reasonably confident I do actually want to read goes to Pinboard to wait for that mythical day when I go back and read all my pinned pages.

And that leaves me with a whole bunch of tabs and strategy 2.

But here's the problem with strategy 2 beyond actually having to read this stuff:

In any page interesting enough for me to read it I invariably find links to at least two or three (and often more!) other pages that I want to read.

So I manfully struggle through closing 3-4 tabs only to find I now have something like a dozen more than I started with! And Safari slower than ever.

No it makes no sense to try and close browser tabs.

30/01/2014 17:41 by Matt Mower | Permalink

## Todays Clojure mind bender

You want the nth element of a collection, for example the 5th element of [a b c d e] or the third maybe.

How about this as a solution (from the excellent Joy of Clojure):

``````(defn fnth [n]
(apply comp
(cons first
(take (dec n) (repeat rest)))))

((fnth 5) [a b c d e])
=> e
``````

At first glance my brain wouldn't wrap itself around this function. That's my brain really coming to grips with first-class functions.

What it does is to construct (for `n=5` say) a list:

``````'(first rest rest rest rest)
``````

Then it uses `apply` to turn that list into arguments the `comp` function. `comp` takes as its arguments a list of functions and returns a new function which is their composition. In this case:

``````((comp first rest rest rest rest) list)
``````

is equivalent to

``````(first (rest (rest (rest (rest list)))))
``````

Which returns the 5th element of `list`.

It's a kind of meta-programming that isn't macros.

Mind bent!

24/01/2014 19:20 by Matt Mower | Permalink

## Clojure power-ups

A few Clojure libraries I am making use of:

What print-foo does is provide a library of debugging macros for printing useful info.

So for example you have the very complicated function:

``````(defn adder [m n] (+ m n))
``````

The `adder` function is being invoked and you'd like to see the arguments and return value. Normally this would involve instrumenting the function with println statements. However by using the print-defn macro:

``````(print-defn adder [m n] (+ m n))
``````

whenever `adder` is called it will print the values of `m`, `n`, and the return value of `adder` to the console.

``````Instarepl:  m 5
Instarepl:  n 6
Instarepl:  defn 'adder' result: 11
``````

Handily there are equivalent forms for `if`, `cond`, `let`, and the threading macros `->` and `->>`.

Have to say I am very happy to see Jay Fields in the Clojure world, I always found he made a huge amount of sense talking about Ruby.

Expectations is a testing library. I didn't hit it off straight away with `core.test` and then watched a video of someone testing with Expectations and I was off to the races.

The library is based around a single `expect` function (or, more likely, macro). Something that's great about `expect` (and maybe core.test does this too, I don't know) is the degree to which it helps you when things fail. For example:

``````failure in (core.clj:18) : sample.test.core
(expect {:foo 2} (in {:foo 1, :cat 4}))
expected: {:foo 2}
in: {:foo 1, :cat 4}
:foo expected: 2
was: 1
``````

The degree to which it figures out what exactly is wrong is really helpful. I haven't explored it much more than this but already I am finding myself testing more and more.

I've only just started to use this library but it provides some powerful tools for data validation and Clojure is all about the data.

``````(def Data
"A schema for a nested data type"
{:a {:b s/Str
:c s/Int}
:d [{:e s/Keyword
:f [s/Num]}]})

(s/validate
Data
{:a {:b "abc"
:c 123}
:d [{:e :bc
:f [12.2 13 100]}
{:e :bc
:f [-1]}]})
;; Success!
``````

In a statically typed language I guess you wouldn't need such a thing. So far I prefer dynamic typing with something like Schema to check things where I need to (so far, usually in my expectations)

I guess that's enough for now. There's other things I am playing with like core.async which a lot of other people are writing about, and far better than I could, and more esoteric stuff like Clojure-Lanterna (a library for creating console GUI's based on the Java Lanterna library).

What Clojure goodies have you found?

23/01/2014 09:59 by Matt Mower | Permalink

## Functionally illiterate

I took up programming in Clojure fairly recently. It's my second serious foray into functional programming (I put some effort into Erlang a few year ago, I enjoyed using it but didn't see how it fit into what I wanted to do) and I'm enjoying it a lot.

Starting with any Lisp involves getting over parenthesitis. It took me a few weeks to get over the initial shock and then another few months before I found it not to be an issue at all. Now, for the most part, I "see through" them.

A big boost in learning has been Chris Grangers LightTable IDE project. After an abortive 3rd try at liking Emacs, and various semi-pleasant attempts at a Vim based REPL, LightTable is a breath of fresh air.

I guess it helps that it's implemented in ClojureScript (Clojure cross-compiled to Javascript) so the authors are eating their own dog food a lot. That also makes it very amenable to hacking on and, now that 0.6 is out and officially supporting plugins, I built my first.

LightTable uses CodeMirror for editing. CodeMirror has a pretty decent Vim emulation (decent enough for a shallow Vim user like me) but one bug-bear I have is tabbing back to the editing (e.g. to run tests) and then typing 'jjjjj' all over my nice function.

My Vim-Focus plugin switches any LightTable Vim editing tabs that are in insert mode back to normal mode whenever LightTable loses focus. Et voila! Sanity is restored.

For that matter I've found testing a lot more natural in Clojure than I ever did in Ruby. I think that may be a result of the combination of functional programming, immutability, and Clojure's "everything is data" philosophy. Or not, I'm not trying to score points.

My current project is a little robots game I'm writing to explore building a Component Entity System. As I'm writing it I'm learning how little I really know. But it's fun and I'm definitely out of the conscious incompetence phase.

Next stop robots!

20/01/2014 19:59 by Matt Mower | Permalink

## Slow down and focus

I strongly recommend to go see Joe Kraus talk Slow Tech where he talks about the way we're creating a culture of distraction and some of the effects of this.

Don't worry. It's only 15 minutes long and you'll be back to browsing & email before you know it :-)

I've noticed myself that I seem less able to focus than I remember. This may be partly age I suppose, it may be ADHD (which I think I may be mildly affected by and am reading about), but I think it's also about how we consume information now.

I watched myself in a cafe last night fail to achieve any one goal be it to read a chapter of a book, read a page, write a complete document, etc...

My brain would flip from one task to another because it was so easy to do so. I'd be reading, a thought would come up, I'd switch to the browser, then get distracted by another tab, ..., finally remember the book.

It's for this reason that I no longer have an email notifier and have turned off Twitter notifications. In fact I routinely turn off notifications in iOS applications and make heavy use of the "do not disturb" feature on my phone.

The powerful thing Joe notes that where we would have once had space to think (e.g. in a queue, on the station platform, etc) we habitually reach for our phones now. Time that might have been spent in thought is now spent consuming other people's information.

I'm trying to take Joe's advice and slow down.

06/10/2013 08:27 by Matt Mower | Permalink

## Metaphobia

Someone asked me the other day if I was still making music. While it's true that my output has dropped over the last few years I'm still working and still learning. I contributed a track to the last Ember Music mixcast which has some great stuff on it.

In case anyone is interested here are a couple of pieces that I've made this year and which I am relatively happy with:

Music for Piano Glitch 1 (2:26)

Metaphobia 2 (4:18)

The second piece is made with a new Reaktor instrument ("Klimpt") that I have been building on and off that combines sampling and synthesis and is my second homage to Metaphysical Function (2010's Reichatron being my first).

06/10/2013 08:14 by Matt Mower | Permalink

## Ruby still in play

So these days I am mostly working with clojure or Objective-C when I am writing software but since most of the people I mentor and most of the clients I work for are using Ruby I like to keep my eye in.

For \$12 this play-by-play video of Katrina Owen refactoring Ruby code sounds like it might be worth a watch.

I've also still got Avdi Grimm's Exceptional Ruby to read.

06/10/2013 07:54 by Matt Mower | Permalink

## We really do have a biological clock

I just read a short, but fascinating, article on the construction of the human biological clock.

Daily rhythms of sleep and metabolism are driven by a biological clock in the suprachiasmatic nucleus (SCN), a structure in the brain made up of 20,000 neurons, all of which can keep daily (circadian) time individually.

I stopped wearing a watch about 15 years ago and I've observed that I have a pretty good sense of what time it is, most of the time. I can be spookily accurate (to within 5 minutes of wall clock time) sometimes.

What's most fascinating is to see neuroscientists starting to analyse the mechanism of our body clocks and see it described as a system of cells that synchronises itself.

I wonder if it will turn out to offer distributed systems designers any hints about how to optimise networks. Or maybe CPU designers (thinking about the problems of clockless designs).

06/10/2013 07:34 by Matt Mower | Permalink