Clojure Templates Are Easy With Leiningen

Using templates to create your Clojure projects can save you a lot of setup time and ensure your team is using the same base configuration and dependencies. There are templates on Clojars.org, however I’ll show you how easy it is to create your own with Leiningen.

I’ll create a simple template based on the leiningen default template, adding a section in the project.clj to give a custom propmt when run in the repl.

Templates used to be a Leinigen pluging called lein-newnew and its repo was the only doucmentation I found and was a little outdated. The plugin is now part of Leiningen and there are a few built in templates. There is also information via lein help new.

If you want to create a template in a more automatic way from a more complete project you created, take a look at the lein-create-template Leinignen plugin.

Read More

Clojure Development With LightTable 0.2.x

LightTable is a kickstarter project to create new kind of developer tool for Clojure development, inspired by the Inventing on Principle talk by Bret Victor.

LightTable aims to give developers instant feedback about their code, showing how any change affects their applications. Giving you a developer “surface” to work on, which will bring information to the places you need it the most. The principles of the LightTable design include:

  • Documentation there when you need it, no need to search
  • Edit anywhere and anything - not just text and not just as files
  • Discover by doing, changes produces instantaneous results
  • Shine a light on related pieces of code

Read More

EuroClojure - Developers Get Musical With Overtone

Sam Aaron and Jeff Rose gave a whirlwind tour of creating music with Overtone, an open source music generator written in Clojure.

You can define your own instruments, map keyboards and other synthesiser hardware, all to make some funky sounds - although you probably want to have headphones on when experimenting!

@samaaron with overtone you can sit on a train and make musicI had fun creating my first overtone project from scratch at the last Overtone Hackday. Have a look at how I set up my environment.

The Design of Overtone

Music is not a very easy concept to define in software. Typically you start with a synthesiser and work your way up to notes and chords. Eventually you may get to a music piece, but this is often driven by a hardware keyboard and recorded.

The difficulty is that everyone has a different idea of how to describe music.

Overtone comes in two parts. The Super-Collider generates all the sounds from over 500 midi building blocks, essentially you create a directed graph that returns values to represent those sounds. The clojure project part allows you to define instruments (synthesisers) and orchestrate these instruments together.

Basic approach to making music

Overtone generally works on the principle of subtractive synthesis. You create a number of different sounds by defining individual instruments and by adjusting the time and frequency of the sound wave to vary the sounds produced.

Once you have some instruments, then adding an envelope generator will give you a changing sound through time by, essentially multiplying the sound by the envelope.

Join sounds together by creating a player function that takes a time and plays the instruments - adding durations to the sound.

To spice up your sounds you can then experiment with playing two different frequencies at the same time, referred to as multi-channel expansion. A resident low pass filter is also fun to experiment with.

Sam and Geoff showed off what they call the stepinator, which seems to emulate a square wave form which steps through a series of values over time. This created some Buck Rogers style music.

Eventually you will want to use an external keyboard or some hardware device to pay your music as calling functions over and over again from within the REPL will only get you so far. If you map functions, frequencies, etc to the external player controls then you can play your clojure code..

Getting Visual

To make the music come alive even more, you can use the Java processing framework. Instead of calling processing directly, you can use the clojure project Quil to visualise the overtone sounds, creating a sphere and controlling the size of the sphere with the different frequencies of the sounds.

Get collaborative

Sam and Geoff are trying out different ways of sharing the REPL so they can jam together. Many people are sharing their sounds on freesound.org, a collaborative database of Creative Commons Licensed sounds. Browse, download and share sounds

Get started

Read the Overtone documentation to get started or have a look at my setup on Ubuntu. Dont forget to experiment.

Thank you.
@jr0cket


This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License, including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket
Creative Commons License

ClojureScript - the Hard Truth - London Clojurians March 2012 Dojo

The March edition of the London Clojurians coding dojo all the suggested dojo challenges were to be carried out with ClojureScript. After a long list of ideas we voted to do either Conway’s Game of Life or Monty Carlo graphics.

Getting started with ClojureScript seemed reminiscent of the challenge the group faced a couple of years ago when first trying out Clojure. Although getting started with Clojure itself is pretty easy these days, it feels like ClojureScript still has a way to go in terms of a great developer experience.

I looked at ClojureScript One and was put off a little by the amount of git projects it was downloading as part of its bootstrap process. I am sure its a great project, but seemed too much for the dojo and my netbook!

We settled on lein-cljsbuild project and used the simple example that comes with it. We fired the example up okay and had a working webserver thanks to some Ring Clojure magic and a tiny bit of JavaScript.

lein deps
lein ring server-headless 3000

Whilst we could display text in a web page and a JavaScript popup, we could not do anything that required working with the DOM - specifically setting up a graphic area or adding buttons.

In hindsight, perhaps we should have looked at the advanced example that came with lein-cljsbuild, although we would probably have also run out of time with that too…

Perhaps if we had spent a bit of time before the dojo with the basics things would have gone better, however it was good to discover as a group the level of challenge involved and it made it easier to get started as we could draw from a range of experiences.

So what else did I learn?

You need a JavaScript resource

I started to become a bit weary when it was mentioned that we should have someone in the team with JavaScript experience. Even though we had someone with experience we still had lots of challenges.

Google Closure libraries

The Closure Library is a JavaScript library that provides cross-browser functions for DOM manipulations and events, AJAX and JSON, as well as more high-level objects such as User Interface widgets and controls.

The Google Closure library looks really great, but there seems to be a few challenges to get it to work with ClojureScript. Again this is down to our limited time to get to grips with several APIs, so we had little luck finding something that worked.

Where’s my DOM

Our group got stuck on trying to find elements in the DOM via ClojureScript, repeatedly getting nil when asking for elements in the DOM. We postulated that is was a timing problem, but were not able to code around the problem.

We fired up the Chrome browser developer tools to see the errors, but couldn’t get any of the many fixes we found on Google to work.

Using jQuery to load things up

By the end of the night I had a strong impression that you cant do ClojureScript without knowing a lot of JavaScript. This goes against what I thought was possible, to write a Clojure style syntax that you could run inside a JavaScript engine. Yes, I expected quite a bit of interop, having lots of doto calls to chain some JavaScript calls, but didnt figure on jQuery being there or so essential.

Find an example that works

Find an example ClojureScript project that works and is easy enough to understand - without having to spend an hour setting up Leiningen plugins and dependencies or having to download lots of things from the Internet. This was tricky to find in the time we had.

Some blogs to and projects to review

In Summary

So first impressions of the experience suggest I need to read some good tutorials on the subject and review code of some more projects. I plan on doing some more projects around Noir, so I’ll try and see where the advantages of using ClojureScript are when using a set of Clojure web frameworks.

I am still excited about ClojureScript, but its one of those things where I need to find more time than I have to get to grips with it. If anyone has any other blog or project recomendations, please let me know.

Thank you.
@jr0cket


This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License, including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket
Creative Commons License

Learning Clojure Through Practice - London Clojurians February Dojo

Another chance to practice Clojure with the help of the great people that are part of the London Clojurians community. Each person at the dojo has a different experience with Clojure and functional programming, so there is always something different to learn. It still amazes me how much I learn and how confident each dojo makes me (by the end of the night anyway).

Read More

Battleships at London Clojure Coding Dojo - January 2012

Its not quite Global Thermo Nuclear War, but battleships was a great choice for a coding dojo topic. Its a simple enough game and therefore a challenge that you feel you can tackle within one evening. Its also a game that most people know and have fond memories, so the discussions have lots of context.

If you are not familiar with the Battleships game, please see the Wikipedia page on Battleships.

At the January 2012 dojo we used a battleships server created by Neill Alexander and Robert Rees kindly facilitated the night. The battleships server allows you to submit your “player” and proceeds to play battleship games against is own player - CPU1 (shame the player is not called Master Control Program so I could slip in a Tron reference).

To get started with the dojo I forked the project on Github to my own account and cloned the project repository to my local machine. I still use the command line to clone remote repositories, its pretty straight forward:

git clone url local-folder-name

For my fork of the battleships game the command becomes

git clone https://github.com/jr0cket/battleships

When I clone a github repository that I have forked from someone elses repository I prefix the local folder name with my username so I know its my fork and not the original - saves a lot of hassle wondering why I cant push changes back to github directly

The project is on my local computer I can fire up leiningen build tool and get the project running. First thing to do is to make sure I have all the libraries the project depends upon. Leiningen will download the Internet of jars for me (just like maven) with the following command:

lein deps

The battleships project uses Clojail to create a sandbox, so its important to set the Java runtime environment security permissions. There is a handy lein task for this courtesy of Robert Rees that creats a .java-policy document in the … file:

lein policy

Or you can just create the file with any handy text editor.

I then fire up Emacs from within the top level project directory (makes it quicker to find my project files) and opened emacs with the project.clj file to see how the project is set up.

emacs project.clj &

Clojure has a repl for working with the language dynamically, so I fire the REPL server up using emacs (of course). Adding clojure-mode to emacs 24 gives you the swank REPL server - allowing you to call clojure-jack-in and fire up a swank REPl server using the lein project.clj project definition. I defined a keyboard shortcut Ctrl-c, Ctrl-j for the M-x clojure-jack-in command in the .emacs.d/config.el emacs configuration file.

I then open the relevant clojure code using the keyboard shortcut Ctrl-c Ctrl-f. For the dojo I just needed to work with the demo.clj file that defines a battleship player: jr0cket-battleships/src/battleships/demo.clj

Once the project is loaded into emacs and swank is running, load the battleships namespace into the swank server using the (use ...) function.

Note that (use) will load in all the required dependencies at once whereas (requires) will also require you add all the dependencies yourself.

user+> (use :reload-all '[battleships.client :as client])
nil

For the dojo we had a central server that we all submitted to. I also spun up a local battleships server so I could do some testing.

lein ring server

Now I am in the namespace I can submit the player I created - initially this was just the default demo.clj player as I was interested in a baseline player to work with. By default the demo.clj player will shoot and place your ships at random, with no intelligence to these
actions

user> (submit-player "src/battleships/demo.clj" "baseline" "http://localhost:3000")
Submitting to http://localhost:3000/create
    {:status 200, :headers {"date" "Tue, 31 Jan 2012 21:24:18 GMT", "content-type" "text/html; charset=utf-8", "connection" "close", "server" "Jetty(6.1.25)"}, :body "player1912"}

When submitting your “enhanced” player you should give it a name you will remember, different from other players. As the game does not replay any matches, its probably worth submitting a new player rather than updating an existing one (there would have to be a lot of players to overload the server!).

user> **(submit-player "src/battleships/demo.clj" "Masher001" "http://localhost:3000")**
Submitting to http://localhost:3000/create
{:status 200, :headers {"date" "Tue, 31 Jan 2012 21:24:18 GMT",
"content-type" "text/html; charset=utf-8", "connection" "close",
"server" "Jetty(6.1.25)"}, :body "player1912"}

If you are playing against each other in teams, then the server address will be the IP address of a shared server.

Now the clojure fun begins. Using the demo.clj as a basis, modify your player so it wins all the games, or at least stop it from sucking more than everyone else’s player.

So if you want to have some Clojure fun with Battleships, go make your own fork of the Battleships server and get coding!

Thank you.
@jr0cket


This work is licensed under a Creative Commons Attribution 4.0 ShareAlike License, including custom images & stylesheets. Permissions beyond the scope of this license may be available at @jr0cket
Creative Commons License