Wishful Coding

Didn't you ever wish your
computer understood you?

Clojure Wiki Gitorial

At the last Amsterdam Clojurians meetup, I gave a presentation with Hubert Iwaniuk, in which I wrote a wiki in 15 minutes, and Hubert explained what I was doing.

Unfortunately no video is available, but I made a Gitorial out of it to show you what I did. Typos included.

commit 9d2f91089dadbe795a16da9d84cb0da33cea9a37

Welcome to this gitorial. I just created an empty Leiningen project with lein new wiki and initiated git with git init

commit e2156be6cbfc421e384ebb016699497501395131

I’m using Ring for serving my application, and Moustache for routing.

commit 1898d45e54d1ae6045742f8e5db3b8fbc1fb7141

I defined a router that will at this point match an empty address and return “hello world”. The -main function can be run with lein run -m wiki.core.

Browsing to http://localhost:8080/ now should display “hello world”.

commit 5e68a78f30586ad5313072201df25e5b1eea76ac

A lot is going on here, form top to bottom:

I imported some Ring middleware. These modify the request and response on the fly. Note that wrap-reload and wrap-stacktrace are for debuging only.

I added the middleware to the Moustache app.

I added routes. The first route matches and WikiLink and binds it to title. The second one redirects all other links to the MainPage.

The #’ syntax is for getting the var instead of the value, to make reloading work.

At this point, visiting the same url should redirect to /MainPage and display “hello tester”

Changing this text does not require a server restart, so we can keep it running from now on.

commit ae78c9d640420d0154b22c66df581fa684b8aa48

I lied, to add a new dependency, you need to restart the server.

Hiccup is a DSL for generating HTML.

I defined a HTML template and a function for showing it that takes a request and a title.

Note that I used the underscore to denote we’re not using the request.

Delegate is a HOF that returns a function with the title alreadu supplied. Moustache supplies the request.

Try visiting /FooBar now.

commit cd24f552f7eefb470ac7b4357d01486e0fc9c888

I added Clutch as a dependency. Clutch is a library for CouchDB.

Install CouchDB and use Futon to create a wiki database and insert a “MainPage” document with a “content” key.

I defined my own Ring middleware that takes a handler and returns another function that calls the old handler in the context of our database.

The show function now gets a document from the database and passes its content to page.

commit f0c831708f397137b9aadf397376de2202f5cd60

You can now edit and create pages.

Page now takes a revision, which is used in the web form to update.

The update function does use the request object, and uses destructuring to extracts form data as parsed by wrap-params.

Depending if a revision was supplied, a new document is created or an existing one updated. Then, the page is shown.

Note how Moustache now delegates POST requests to update and GET requests to show.

commit 11588b8f1e591ea6415515ffa21e70ed630eef91

I added a Java library for Markdown parsing.

The markup function also replaces WikiLinks with an HTML link.

commit 235c551d57786f79c1867452de37b079a59606bd

Getting ready for deployment.

I removed the debugging wrappers and the var syntax(#’wiki).

Jetty now gets the port number from the environment/Foreman. This means it now runs at port 5000.

Install Foreman with gem install foreman

I added a Procfile according to http://devcenter.heroku.com/articles/clojure

The Origin of Language

Dutch is an interesting language. It seems we’re at a crossroad of German, French and English influences.

During the Roman empire, the river Rijn was the border of the Germanic and Roman empire, of which German and Italian are direct descendants1.

Dutch is very close to German, but it was and is influenced by the ‘world languages’ of the time. (The world was a lot smaller at that time) And so it happened that during 1800-1900 or so, French was spoken by the elite, and we where part of the French empire at times.

Map of French and Roman empire

If you look at a Dutch dictionary today, you can divide most words in 3 categories, Words that are a lot like German, Archaic and chic words from France and modern words from English.

Dutch is a lot like Common Lisp or Scala2. If we’d make a cheesy map of Europe with languages and paradigms overlaid, you’d see that natural languages are complected and very multi-paradigm.

Programming languages are much more designed, and mostly unambiguous, like Esperanto or Lojban. Computers don’t like ambiguity. Some languages, like Scheme, are still designed for growth though.

</embed>

Near the end Guy Steele argues that programming languages need to be more like natural ones, and the other way around.

As we have seen earlier, natural languages grow, and grow a lot. But I know from experience that adding words to your language that are not native, feels forced at times, does not go well with grammar, and leads to extra complexity.

On the other hand, natural languages should be simpler. But I argue that this has nothing to do with syllables, but with choosing simple words with but one meaning, concerning one thing.

But do our programming languages have this property? Rich Hickey argues this is often not the case, and explains the word ‘simple’ in more detail.3

I can only wonder what future languages will look like. Since our applications will be limited by our understanding, programming will be an art of omission and simplicity.

Last video, I promise. ‘Uncle Bob’ Martin shows us what progress we have made in software development. Not much, compared to Moore’s law. We’re still programming Lisp, and doing assignment, branching and iteration.

What we did with structured programming, with object orientated programming and with functional programming, is taking stuff away. We converted conventions to rules.

Maybe future languages will have rules about simplicity?

  1. I’m not a historian, I never followed a single history lesson. Take with a spoon of salt. 

  2. More salt please; At least we’re back at programming. 

  3. I can’t seem to embed an InfoQ video. 

My Bookshelf 5/5: Making Ideas Happen

Great book, read it. And if you don’t, scan the index, it reads like a list of do’s and don’ts.

Making Ideas Happen

The book describes the Action Method, which resolves around persisting, following up, managing action steps(todo’s that start with a verb) and relentless execution. It’s not easy, but it makes sense.

I actually read this book during summer holiday. I’ve been, uh… practicing since then. On thing that stuck with me in particular is that connectivity is inverse productivity.

I found it much easier to focus on the action step at hand without all the distractions from email, IM, Twitter, etc. But when you are offline, you need to prepare yourself with all the docs and libs you need, put the whole internet on a floppy if you can1.

Internet on a Floppy

Behance developed a web application specifically for following the Action Method, but as you can imagine, having your task manager ‘in the cloud’ while working offline, is not ideal.

I need something that syncs between my laptop and my desktop, but works great offline. CouchDB seemed to fit the bill perfectly. After I started a homebrew solution and one based on the Backbone MVC framework, I found this.

Those who don’t understand UNIX are condemned to reinvent it, poorly. – Henry Spencer

Why not use mighty tools such as text files and rsync(or Dropbox)? Googling for todo.txt shows I’m not the first to have that idea. There is a more or less agreed upon format, and even a GUI.

Good luck with that, I’m going back to my task list, which features cleaning the kitchen.

  1. Half of my action steps at the time consisted of “Download X”