From Vim to Emacs
I’ve started to choose emacs as the editor for my clojure project development. Overall vim was the big fit for the last years, but the VimClojure REPL is really not that much powerful (regarding usability, …). I’ve installed emacs as a debian package, following the rules from the emacs website nad installed the emacs starter kit from technomancy which ships with nearly everything you need.
I’ve added a few entries to my emacs init to get it up and working. See:
$ emacs ~/.emacs.d/init.el
(require 'package)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/") t)
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
;; Add in your own as you wish:
(defvar my-packages '(starter-kit starter-kit-lisp starter-kit-bindings clojure-mode)
"A list of packages to ensure are installed at launch.")
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
(global-set-key (kbd "C-x <up>") 'windmove-up)
(global-set-key (kbd "C-x <down>") 'windmove-down)
(global-set-key (kbd "C-x <right>") 'windmove-right)
(global-set-key (kbd "C-x <left>") 'windmove-left)
(fset 'cljinit
"\C-xo\C-x1\C-x2\C-x^\C-x^\C-x^\C-x^\C-x^\C-x^\C-x^\C-x^\C-x^\C-x^\C-xo\C-xbslime-r\C-m")
(global-set-key (kbd "C-c i") 'cljinit)
The global key bindings make geometric movement with the specified hotkeys possible. After viewing a file and M-x clojure-jack-in the REPL is opened at the right side with 50% size. The shortcut C-c i moves it to the bottom with only ~30% size.
Short keys are for example:
C-c C-k (compile the file)
C-x e (compile last sexp)
(ns x.y) << switching namespace in REPL.
Clojure Web Application
I build up a Clojure stack for web application development.
First Results can be found here: https://github.com/kremers/Clojure-Memcached-Connector
Clojure + Sandbar + Enlive + Stencil spymemcached on membase server + MongoDB
Membase vs. other K,V Stores
- Not Master,Slave! Peer Replication
- Fastest, Supported and Tested Platform
- Also available at Heroku (5mb for free)
But no tested support for collections, missing query api, …
Mongodb will also get a chance. (With monger instead of congomongo).
Enlive vs Hiccup
- Pure DRY code should be separated from the page markup.
- HTML should be semantic markup. CSS should work for the design.
Stencil vs. Enlive
- Templates free of logic
- Same API works in other languages for years
- No CSS selector and modifier hell
- Sandbar creates stateful sessions in memory, but we want to scale. Let’s store the sessions in our k,v store. So we need an implementation of ring / ring-core / src / ring / middleware / session / store.clj for memcached
If I choose to go on with mongodb, there are already existing ring session adapters.
Logging
- Finally: Simple logging (Category, Message) to memcached, quota with flush to file
First experiments with Heroku
Heroku is an excellent cloud application platform. Because the Java platform has so much impact, I’ve never tried to write plain ruby applications. Now they have introduced Java, Node.js, Python, Scala and Clojure support. Cause I wanted to try Clojure anyway, this seems to be a nice place to run examples and prototypes. What I really like is the deep integration of Git into the project structure. Git commands are used to create / publish data on Heroku. Also the concept of omitting filesystem storage for scalability seems to be a big win.
I had problems installing the Heroku CLI on Debian (lenny) because of SSL Certificate problems. Heroku CLI developers commented out the “error” line with the important message, explaining what caused the error.
~> export RESTCLIENT_LOG=stdout ~> heroku create --stack cedar Enter your Heroku credentials. Email: xxx Password: RestClient.post "https://api.heroku.com/login", "password=hidden&amp;username=kremers%40act-online.de", "Accept"=>"application/json", "Accept-Encoding"=>"gzip, deflate", "Content-Length"=>"50", "Content-Type"=>"application/x-www-form-urlencoded", "User-Agent"=>"heroku-gem/2.15.1", "X-Heroku-API-Version"=>"2", "X-Ruby-Platform"=>"i486-linux", "X-Ruby-Version"=>"1.8.7" /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/auth.rb:96:in `ask_for_credentials': undefined method `[]' for nil:NilClass (NoMethodError) from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/auth.rb:133:in `ask_for_and_save_credentials' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/auth.rb:71:in `get_credentials' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/auth.rb:51:in `user' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/auth.rb:11:in `client' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/command/base.rb:22:in `heroku' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/command/apps.rb:122:in `create' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/command.rb:114:in `send' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/lib/heroku/command.rb:114:in `run' from /usr/lib/ruby/gems/1.8/gems/heroku-2.15.1/bin/heroku:14 from /usr/bin/heroku:19:in `load' from /usr/bin/heroku:19
After uncommenting the following Line (564 in /usr/lib/ruby/gems/1.8/gems/heroku-2.15.3/lib/heroku/client.rb):
error "WARNING: Unable to verify SSL certificate for #{host}\nTo disable SSL verification, run with HEROKU_SSL_VERIFY=disable"
After executing “export HEROKU_SSL_VERIFY=disable” everything worked.
Give Clojure a try
The first time I had contact with Clojure was in 2009, while writing my final piece of coursework for my degree on dynamic programming languages in big projects, focusing on Groovy. It was a bit hard for me to get to grips with Clojure, because the learning curve is quiet different to that of Groovy (which is very shallow if you come from a Java background). One of Groovy’s painful aspects appeared during performance tests. Possibly because the language hasn’t got the same academic background as LISP, Haskell or C++.
Why I have not choosen Scala
I tried Scala because I excepted it to be more academic and exact. A major reason was that I have been impressed by Martin Odersky working on Scala. My first functional attempts at Scala were unsuccessful, due to the lack of JVM TCO. It may be due to my lack of experience that the Scala code seemed to look like “write only” Perl code. Programming Scala felt more like writing in Java, but with a lot more capabilities and primarily more complexity. The language is really hard to understand, even the complexity of its type system is astounding. I’ve been quite happy using this blog post from Stephen Colebourne to help me. I had some of the same issues after some experiments with Scala. After seeing the answer from Martin Odersky I was a bit disappointed by how he deals with criticism. Blaming people who are investing time to point out weaknesses of a language and its problems to spread FUD.
How I started learning Clojure
Getting into a complete new environment with its own paradigms is hard. Especially if you can not reuse your knowledge from imperative / procedural and object oriented programming languages. Having seen a few real world Clojure applications, I was able to get an understanding of its Java interoparability. With the help of tools like “leiningen” it was easy to build my first applications.
Why I except it to be worth a try
Clojures built-in functionality to deal with the lack of TCO in the JVM (“recur, lazy sequences, …”). I searched a long time in the Scala API for such a feature. It’s not just a wide mix of object orientation with a few functional concepts, which feels like an advantage to me. The community is also adopting good libraries like these for website templates (Enlive, …).
Where to go from here
I will check out the samples for Heroku and Clojure, since Heroku is one of the most popular cloud providers and all of the infrastructure management is done for you. And now they are properly supporting Clojure. There are also goals to play around with the language such as the “petshop” and examples with database / NoSQL connectivity. Once I have tried writing these things myself, I’ll write another blog entry on it. One of my goals is to ensure that Clojure can be used in bigger web projects. I’m not sure Clojure should be recommended for such business projects yet.
[1] http://stackoverflow.com/questions/310974/what-is-tail-call-optimization
[2] http://blog.joda.org/2011/11/scala-feels-like-ejb-2-and-other.html
[3] http://entwickler.com/itr/news/psecom,id,58955,nodeid,82.html
[4] http://en.wikipedia.org/wiki/Write-only_language
[5] http://de.wikipedia.org/wiki/Fear,_Uncertainty_and_Doubt
[6] https://github.com/technomancy/leiningen
[7] https://github.com/cgrand/enlive/wiki/
[8] http://blog.heroku.com/archives/2011/7/5/clojure_on_heroku/
[9] http://thecomputersarewinning.com/post/clojure-heroku-noir-mongo
