In Clojure We Trust

A piece of intertube about the Clojure programming language, algorithms and Emacs.

Four Haskell Books

In 2013 and 2014 I read four Haskell books. In this post I’m making mini-reviews for each of them, chronologically ordered.

Learn you a Haskell for Great Good

Learn you a Haskell for Great Good is one of the best computer science book I had the occasion to read, and I even admit I was a little sad once I finished it! It exposes the main concepts of Haskell in a very pedagogic way and clear writing style. After reading the book, functors, applicative and monads become easily understandable. The only drawback is that this book is not enough to start with Haskell, there are no explanations of how to create a Haskell project using Cabal, use libraries, test your application etc. I think it’s okay for an introductory book but one should be informed. The book is suited for people without experience with Haskell or functional programming.

Developing Web Applications with Haskell and Yesod

I read Developing Web Applications with Haskell and Yesod to discover other ways of building web applications, as I mostly had experience with Clojure and Compojure. I enjoyed reading this book which is short but contains a lot, going from the frontend to the backend and explaining a lot of subjects: HTML templates, sessions, authentification, persistence with databases etc. You discover how a statically typed language can help you building a web application with more guarantees. The only drawback for me was that even after reading one book on Haskell, some of the type signatures of functions were still hard to understand. Some familiarity with monad transformers prior to reading this book may help.

Real World Haskell

The goal of Real World Haskell was to bring Haskell to a less academic audience by showing how Haskell could be used for “real world” applications, at a time where there weren’t so many Haskell books there. In this sense I think the book succeed. There are a lot of interesting subjects tackled in this book, like profiling and performance analysis but I did not really enjoy reading it. Either the examples were a bit boring or the writing style was too dry for me, in the end I had to fight to finish this very long book (~700 pages!). I nonetheless appreciate that this book exist and I may use it in the future as a reference. The book is a bit outdated and some code are not valid anymore ; it was not a problem for me since I didn’t try out the examples but this should be considered if you want to learn Haskell with it.

Beginning Haskell

Beginning Haskell is a paradoxical book. The truth is this book should not have been published because its edition and proof-reading are too bad. What do you think about a book where the first code sample is wrong? map listOfThings action instead of map action listOfThings on page 4, come one… Also the subtitle reads “a Project-Based approach” but this seems to be exaggerated since you will only get some code samples there and there… That being said, I really enjoyed reading this book! I don’t know if it’s a good introductory book but it’s surely is a correct second book on Haskell. It explains how to use Cabal, test an application, use popular libraries like Conduit, build parsers with attoparsec, building a Web Application with Scotty and Fay etc. It includes advanced topics like type programming (enforcing more constraints at the type-level) and even a small introduction to Idris. Conclusion: either burn it or read it.

Conclusion

There are more and more books on Haskell newly published or coming, this makes me happy as this broaden the Haskell community, shows that it is growing and makes it easier to learn the language. Reading is not enough to learn so at the moment I’m writing small Haskell projects to go from a beginner to intermediate level.

Playing With Haskell

After reading a few books on Haskell I had to face the reality: learning by doing a project was necessary!

I chose a project which was easy enough to be finished in a few weeks or months (but still slightly challenging) and had a practical utility.

My project is a JSON command-line utility loosely inspired by underscore-cli and Hawk. Arbitrary Lens expressions can be used to filter or transform the input.

If you don’t know what Lens are, think of them as getters/setters/filters/functions combinators similar to JQuery or CSS selectors but for any type of data-structures. I’m still a beginner regarding Lens.

The challenge for me was to learn how to dynamically evaluate Haskell expressions. This is uncommon since Haskell is statically typed. The library I used to do that is naturally limited in its functionality in comparison to a Lisp but the final result is all but disappointing.

For the purpose of my program I implemented a pretty printer for JSON similar to aeson-pretty but with colors. Maybe I should package it for Hackage?

Once I had hdevtools setup for cabal sandboxes, programming in Haskell was enjoyable. Refactoring is easy thanks to the strong type system. I was stuck once or twice with the type system but the people on the #haskell channel were helpful. The code has a certain form of esthetic even if I feel more knowledge would allow me to be cleaner. For example I wonder if it is possible to avoid pattern matching on Left and Right for multiple calls which return something like IO (Either x y), since both IO and Either are monads.

You can have a look at the project here:

https://github.com/kototama/jp

A Simple Emacs Configuration

In this post I introduce a simple, effective and maintainable structure for the .emacs.d configuration directory.

I have a mix feeling about pre-defined Emacs configurations such are Emacs Live or Prelude. In one hand they build a nice showcase, demonstrating powerful modes and features, in the other hand they seem complex in their structure and use their own abstractions (“modules” for Prelude, “pack” for Emacs Live). That is why I still prefer to write my own configuration which I can easily fix. Of course I still dig in people configuration to get ideas. Basically I have reached the same conclusion as technomancy which stopped its Emacs Starter Kit.

In the last weeks I have redone my configuration removing El-Get, which I found too slow, in favor of MELPA. My initial switch to El-Get was because Marmalade was often down ; I didn’t experience the same issues with MELPA.

The configuration is based on package+ (the name is hard to search on the web!) and the great use-package package. A makefile installs the few git submodules needed, download user-defined packages and byte-compiles the packages. User and machines specific configurations can be defined in the $HOME/.eroles/login@hostname/ directory. The auto-async-byte-compile mode ensures that Emacs Lisp files are always byte-compiled.

All packages configurations are done in a lazy way, thanks to use-package, and organized with orgstruct-mode in an org-like structure.

I stole a few ideas here, there and there for the configuration.

The configuration is simple yet effective, I hope it can inspire beginners or even advanced users for how to structure an .emacs.d directory effectively.

See my .emacs.d configuration on GitHub and happy hacking!

An Emacs Minor Mode to Mute Email Threads

Emacs is best know to be a powerful text editor. It is also a powerful note editing system with org-mode. Since emails are mostly composed of text it also makes sense to use Emacs for reading and writing them ; automating common tasks is possible with Emacs Lisp and taking notes and referencing emails within org-mode is easy.

To read my emails with Emacs I use the notmuch program. It indexes emails very quickly and functions with tags. Searching is super fast, even with a huge number of emails.

Since Emacs is almost infinitely customizable, I hacked a minor mode for notmuch that allow me to mute email threads. You can mark a thread of emails as ‘killed’ and all incoming email belonging to this thread will be automatically marked as read upon arrival.

The mode is not very efficient for the moment but you are encouraged to try it on your own and see if it improves your workflow.

You can find the mode on GitHub. Happy hacking!

Linux Distributions as a Service

One of my colleague installed Ubuntu on his Mac computers and was surprised by the varieties of Ubuntu distributions offered. There are different distributions and differents architectures and he was wondering how they managed this complexity.

This gave me an idea. What about having even more distributions? What about having personalized distribution customized for your particular machine ?

A usage scenario could be like this:

  • you go on the website of a Linux distribution,
  • start an application that analyze your hardware,
  • answer a few questions about your requirements (security, network, etc),
  • choose the graphical environment, choose or customize some graphical themes (for a desktop distribution) etc.
  • you then get a link where you could download your own customized distribution, tailored for your needs and hardware. Or maybe you enter an email and you get a link via email when the build is finished.

Common choices and typical hardware configurations could be shared by the community.

It would require a good infrastructure but it could be a service people would be ready to pay for.

Currently only expert Linux users can tailor their distributions for their needs, Linux distributions as a service (or “on demand”) could bring more power and more control to a broader public.

Migrating Away From Google Blogger

This blog is moving away from Google Blogger. Update your RSS reader.

Moving to a self-hosted platform will allow me to:

  • have a better control of my data. My blog can now be versioned on GitHub and do not depend of Google anymore.
  • have a better workflow. I can now blog in plain text with Emacs, Octopress and org-octopress.

Of course the reliability may not be as good as the Google services but in general I think I will enjoy the journey!

Searching in Clojure and ClojureScript Files With Ack

“Ack is a tool like grep, optimized for programmers”. I knew the existence of Ack since a few years but I never gave it try. Now that I did, I’m positively surprised: it’s easy to use and fast.

I was always fighting in Emacs between grep (what is the syntax?), igrep-find (why did it suddenly stopped to prompt for directories?), tags-search (ooops some of my files are not tagged) and rgrep (oh it did exist?). I installed ack-and-a-half for Emacs and its great. Ack-and-half tries to find automatically the root directory of your project but you can set ack-and-a-half-prompt-for-directory to true with (setq ack-and-a-half-prompt-for-directory t) for it to ask which directory to search in.

The Ack version on the website provides support for Clojure files. For ClojureScript files, you need to add the following lines to your ~/.ackrc file:


--type-add
clojure=.cljs

Et voila, enjoy!

Duplicating S-expressions on a Line

Duplicating a line is extremely useful when editing text and specially when programming. There is no native command in Emacs to achieve that but it’s easy to add one.

It’s easy to get addicted to the use of this new command but a problem remains when programming in Lisp with paredit.el: duplicating sometimes lead to invalid s-expressions being inserted.

I decided to give it a try and made an implementation that duplicate the s-expressions after the point (cursor). For instance in Clojure if you are editing this code:

(ns myns
(:use [clojure.string :only [escape]]))
You can duplicate the first vector by placing the cursor on the square bracket, invoking the command with one keystroke and then easily obtain this code:
(ns myns
(:use [clojure.string :only [escape]]
[clojure.set :only [union]]))
Here is the code to duplicate the line; to my knowledge there is no such command in paredit.el:

Not really pretty but it does the work, feel free to provide a nicer implementation in the comments or add it to your ~/.emacs.d/init.el file with:

(eval-after-load "paredit"
'(progn (define-key paredit-mode-map (kbd "C-S-d") 'paredit-duplicate-after-point)))
Edit: Here a fix for when the sexp is the last expression in the buffer.

Comments

Neale Swinnerton
Nice idea. Have you tried expand-region ? It doesn't do quite what you're trying here, but it handles most of the 'sexp that straddles a line ending issues'

An Emacs Minor Mode for the ClojureScript Compilation

When programming with ClojureScript, one has to lunch “lein cljsbuild auto” to automatically compiles its ClojureScript sources. It is convenient but a problem remains: the output of the compilation process must be manually read to see if any errors or warnings occur.

To solve this problem I programmed a simple minor mode for Emacs. If you start the cljsbuild command in a terminal buffer, it will watch the ouput and popup the window if an error or a warning occurs. It can also automatically hide the process when the compilation succeed, so that you are able to concentrate on what matters, the code.

It’s available on Marmalade or Github.

Feedback and patches are welcomed!

Implementing a Lisp

To extend my knowledge and to have a better understanding of the Lisp’s foundations, I implemented a simple Lisp in C. It supports symbols, integers, a few primitives, anonymous functions, closures, macros and garbage collection. The implementation follows the KISS principles and is around 1400 lines of code.


Discussion: http://news.ycombinator.com/item?id=3919685
Code: https://github.com/kototama/kml

Comments

postfuturist
Hey, cool project. I just started something similar, here: https://github.com/deliciousrobots/lisp . I'm intrigued by your implementation of garbage collection… an area I punted on (just using Boehm's GC), but eventually would like to implement myself. I'll be reading through your code for inspiration and comparison, so far I like what I'm seeing. You can email me (steveth45 at the google mail service).