Tagged: Clojure

funcjure

funcjureI’ll soon be starting a new permanent job, and in preparation I’m making it clear what is my prior intellectual property. In this case, it’s not so I can make “oodles of money” in the future, but it’s actually so I can release it as Open Source.

So what I’ll be explaining here is not something I’ve finished, but an idea which I’ve actually been playing around with for a year or so, but never gotten around to fully implementing. This will also help bring together my thinking on this and hopefully inspire me to really get going on this project :-)

 

What is funcjure?

funcjure is a “functional syntax wrapper” around Clojure. What do I mean by that? Well, Clojure is a great language, especially if you’re either used to or prepared to adapt to prefix notation. ie instead of typing 1+1, you type (+ 1 1) which although relatively easy to understand, can get a bit harder as things get more complex, like 9+5*7/4+62/6 which would translate as (+ 9 (*5 7) (/ 62 6) etc…

The logical question is Why? (do I have to type prefix notation) The Clojure and Lisp people will say “well, that’s the way it is, so just get used to it”, which I’m fine with as I wrote my first Lisp program over 3 decades go. Even still, I would prefer to use infix notation (which is what we’re taught for maths) and see no reason why we shouldn’t as computers are great at doing the sort of rote translation required to convert infix to prefix notations.

Clojure is a great language, which also has a fantastic ecosystem and community, and is written in Java which has a HUGE ecosystem. Furthermore, Clojure can call Java code, which has helped tremendously by giving Clojure “out of the box” access to so many libraries and products. Finally, because Clojure is a Lisp, its strong point is Symbolic Manipulation which is exactly what is required for translating infix to prefix structures in order to implement funcjure!

What would this look like? Let’s take some typical Clojure and then show what it would look like in funcjure:

"Hello World!"            ; Minimal "Hello World!"
; "Hello World!"

(println "Hello World!" ) ; Standard "Hello World!"
; println("Hello World!)
; Clojure((println "Hello World")) ; Execute some Clojure code
(def a "test")            ; Define a variable
; a="test"

(def mylist '(1 2 4 5 6)) ; Define a variable list
; mylist='(1 2 3 4 5 6)

(println a mylist)        ; Print our variables
; println(a mylist)

(first (rest '(1 2 3 )))  ; Get the 2nd element of the list
; first(rest('(1 2 3)))

(.println System/out "Hello World from Java!")
; System.out.println("Hello World from Java!")
; or Java(System.out.println("Hello World from Java!"))

(defn factorial           ; Now let's do the classic Factorial function
  ([n]                    ; when only one argument is passed in
    (factorial n 1))
  ([n acc]                ; when two arguments are passed in, with recursion
    (if  (= n 0)  acc
    (recur (dec n) (* acc n)))))
(factorial 6)             ; And test it
; factorial(n) = factorial(n 1)
; factorial(n, acc) = if((n==0), acc, recur(dec(n), n*acc))
; factorial(6)

The overall purpose is to make code much more accessible and ‘natural’ to write as we’re essentially taught infix notation as our “second language” when we study even the most basic mathematics. None of this is really new – in the beginning, things look like our old friend BASIC, with direct assignment and loose typing, which easily map to Clojure. You’ll also notice that access to Clojure and Java is provided by the respective functions. Speaking of functions, these are written similar to Prolog and other languages (eg Erlang) which allow for “pattern matching” in function definitions. In the beginning, the capabilities would be mapped directly to Clojure (as shown above), but eventually it would be nice to go to a full Prolog style, so the factorial function could be written like

factorial(0) = 1                ; The base termination case
factorial(n) = n*factorial(n-1) ; Iteration by recursion

which I think is way more elegant.

That’s about it for this post, as this really contains the base concepts for what I’d like to do. There’s obviously a lot more scope and subtlety to this, some of which I’m aware of and probably a lot more which I’m not, but I personally would much prefer to use a language like this and have access to Clojure and Java when needed for efficiency or easy code porting.

What do you think? All comments / suggestions / critiques would be gratefully accepted as I haven’t really done much more work than this, other than having a 1/2 working translator for this syntax which I’m going to (hopefully) get to work on in the next few weeks before I start my job.

Is Java the new C?

JavaLogoI started programming in Java a few years after it was released. I had been doing C++ for a number of years and just come off a C++ project which failed because they (who were a pretty good team of programmers) couldn’t scale it because of the two common killers: Threads and Memory Management. Near the end I was reading James Gosling’s original paper on Java which addresses the major problems of C++ and explaining how Java addressed them. I was obviously quickly sold! As a result I spent over a decade programming in Java and J(2)EE, in the beginning implementing (mostly web) projects way faster than the “C++ laggards” could.

Now, I’ve been doing Solution and SOA Architecture, occasionally whipping up a prototype in Java, or doing some Perl but quite often working with WSDL, XSD’s and just plain models. Recently though, I’ve been playing around with Clojure in my own time and am amazed at how much fun it is. It really reminds me about my early experiences with Java, but that would be the subject of another blog post…

Recently it has struck me just how many languages have versions or been constructed to run on the Java JVM. A list from Wikipedia shows:

Java LanguagesThat’s quite a few languages! What’s more, a number of them have plugins to an IDEs such as IntelliJ or more commonly eclipse. It struck me that these languages were effectively using Java and often IDE’s as a runtime / development platform, much like many high level languages used C.

Which begs the question – where is Java headed in the long-term? Yes, they’re adding Lamda’s in Java 8, but why would I want that when I can work with Clojure which has had this from the beginning and has a whole bunch of good extra stuff… Is it destined to become a “low level language and environment” upon which more sophisticated languages will be built?

The Almost Unbearable Lightness of LightTable

Note: This post is in Draft, I’ll be updating it a bit more…

LightTableSeesawAs I’ve been tweeting, I’m very interested in LightTable, the amazingly cool IDE being developed by Chris Granger @ibdknox and his gang. It’s a bit like the black spaceship from Hitch-Hikers Guide To The Galaxy though: “It’s the weird colour scheme that freaks me. Every time you try to operate one of these weird black controls, which are labeled in black on a black background, a small black light lights up black to let you know you’ve done it”. The next major release (in a month or so) will be addressing “user experience”, but in the mean time, what if you want to give it a serious try?

That is what the rest of this post is going to try and help you do also – get LightTable up and running so you can incorporate other projects and frameworks, which is really needed if you’re to proceed beyond trivial playing with it. But first, a warning from our sponsors (that’s me :). The version on LightTable says it all – 0.2.7. If you want a seamless install / development experience, especially if you have other installations of Clojure, IDE’s, lein, … then it’s probably best to come back later… They’re working quickly to address things, but this is a Real Agile Project that released very early and is now releasing often, so major kudos to them for doing so!

Finally, for some context, I’d rate myself as an “Enthusiastic Clojure Amateur” – I don’t do it for a day job, but I know my way around Unix, Maven, (now) Lein and Clojure through other environments. If you’re at this level or above then this post could be for you, and hopefully save you some time and frustration. You should also note that I use a Mac (is there any other platform to use for serious development? ;) so these instructions assume that. I won’t be doing a Windows post of this, but if anyone wants to then feel free to copy and change what I’ve written.

There are 3 key activities for setting up the latest version (0.2.7 or above) of Clojure:

  1. Clean Installation of Lein2
  2. Clean Installation of LightTable
  3. Creating a LightTable Project with Dependencies using Lein2

Note: I’m assuming you’ve got Maven 2 running, because you’re some sort of Java dev – I may add instructions for this later…

I’m sure all of these issues will be addressed in subsequent releases, but for the meantime, here’s how I did it – Note: it’s assumed that you’ve installed to your local user. If this is global (Unix) or Windows you’ll have to use different paths.

1. Clean Installation of Lein2

LeiningenThis was probably the most difficult and time-consuming thing to work out as I was running a version of Lein 1 that I’d installed ages ago, which is an absolute no-no. LightTable won’t work with this configuration and give you all sorts of weird errors. Whatever version of Lein you’re running it’s probably best to clean out your Maven directory:

rm -r ~/.m2/*

If you’re running an “old version” 1.x of Lein you’ll have to upgrade, which means that you should clean out your .lein directory:

rm -r ~/.lein

Then you’ll have to do a “Bootstrap Install” of Lein2 which isn’t that hard. Firstly, remove you’re old shellscript which in my case was in my ~/bin directory that is on my Unix path:

rm ~/bin/lein

Github LeiningenThen you’ll need to get the latest and greatest version of Lein 2.0.0 – this matters, as the default when you grab it from GitHub is the 2.1-SNAPSHOT (at this time of writing) version which is on the “master” branch, but unfortunately this won’t currently work with LightTable and give you weird connection errors. I originally got this, then changed it to Stable (2.0). In my environment, everything is in ~/Gits.

The final step for this is to then install Lein2:

cd ~/Gits/leiningen
lein install
lein version
>> Leiningen 2.0.0 on Java 1.6.0_37 Java HotSpot(TM) 64-Bit Server VM

2. Clean Installation of LightTableLightTableIcon

Once you have the right version of Lein2 going, it’s actually pretty easy to get LightTable going either if you haven’t installed it or are running an earlier version. To reset your configuration, just remove your .lighttable directory and contents:

rm -r ~/.lighttable

If you don’t have a recent version of LightTable, i.e. your icon doesn’t look like the above one, then you’ll need the latest installation of LightTable, which is pretty easy – just go to http://www.lighttable.com and download the correct launcher

Once you’ve reset your configuration, just run LightTable, it should then get the latest version and tell you to restart. When you’ve done this you can check the version from the “command line” at the bottom by typing “version” which should show 0.2.7 or whatever the latest (higher) version is. If it’s less then somethings gone wrong – Maybe you didn’t delete the entire directory? Maybe you have an old version of the launcher? You’ll have to do some digging if this is the case…

3. Creating a LightTable Project with Dependencies using Lein2

Now you have the right infrastructure set up, it gets very easy. To create a new project, you just create a default template:

cd ~/Projects/LightTable/
lein new guitesting

start up LightTable and connect to your project

lighttable-connect

during which you’ll see connecting and retrieving messages

lighttable-retrieving

and hopefully a brief connected message

lighttable-connected

If you get any blue error windows popping up on the left, then something has gone wrong, probably with your installation of Lein2 so you should go through and check the above process again.

Ah, but there is “one more thing”, which was the key capability I was looking for originally with LightTable, and that’s the ability to include extra projects / dependencies. Now that you’re up and running, this should be comparatively easy. You just add whatever you want to your project.clj, probably using LightTable now you have it running

(defproject guitesting "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [seesaw "1.4.0"]])

which in my case was seesaw that is a development framework for Swing GUI’s that I talked about in my previous post on Easy UI development in Clojure with Seesaw. If you have an instance of LightTable running then you should quit and re-start it to pick up the new dependencies when you connect and that’s it! You should now be able to use LightTable like you see on all the latest articles and videos

Finally, I will be doing an “Unbearable Lightness of LightTable” when LightTable progresses more in installation to the stage where the above instructions will not be necessary. In the mean time, I hope the above has helped you if like me, you’ve been looking at all the latest LightTable videos and wondering why you can’t do these things.

PS If I you have a different experience then please let me know via the comments and I’ll correct or add to the post

ClojureX 2012 London

Yesterday I attended the Skills Matter ClojureX conference – one of the best I’ve been to. It had the same feeling as JavaOne before it got all commercial and Apple’s WWDC before every person and their animal started attending… As one person remarked, an “ordinary conference” would of taken two days to deliver what this one did in one!

The standard of talks was consistently high, and only one speaker payed the price of “live coding” with things not quite going to plan. Even that session was still fun as the audience shouted out possible solutions to her problem (think group programming :). A small price to pay when you consider how much live coding increases the quality of the presentations – one of the advantages of the REPL.

What were my favourites?

  • Live Programming with Clojure – this was the intro presentation and literally ROCKED!
  • The Refined Clojurist – I’ve been thinking about working with Prolog and Clojure. No More! Now I know about Core.Logic and the fact that it’s effectively a “mini Prolog”. I’ll definitely be coding and writing more about this.
  • The Language of the System – Hey, this is Rich Hickey’s talk, so it goes without saying that it’s interesting and covers a lot of thought provoking ground.

I must also mention (as I did in a tweet) that almost 1/2 the people at the conference were working on commercial products for Banks, Media companies, Start-ups and I’m sure a whole lot more. It wasn’t until near the end of the day that I personally met someone who wasn’t a commercial Clojure programmer! Anyone who dis’s Clojure as “academic” or “too geeky” had better watch out because it’s in your rear-view mirror and it’s a Ferrari! If your competitors start using it (well) then you won’t know what hit you. Overall I came away feeling extremely positive for the prospects of Clojure in the industry.

The videos are certainly worth watching, but if you don’t have time, here are the takeaways I got as a mind-map (which I’ve been using for the past few years to take notes – I find it more effective). As I knew that the talks would be on-line (some of them were within an hour or so!) my notes are about topics of immediate interest to me – mostly around new tools to investigate or articles to read, so if one of the topics really interests you then it will be worth watching the related video.

ClojureX

and also in list form if you’re in to that ;-)

  • ClojureX
    • Core.Logic
      • Post Functional
        • Talk from TechMesh
        • miniKanren
        • Constraint Logic Programming
      • Ciao!
        • Lien repl :headless
        • How about working on Definite Clause
          Grammars?
    • USwitch
      • Mississippi
        • Lib for data validation?
      • Part of Forward Group
      • Charts
        • D3.AS
        • C2
    • Playing Nice
    • Google+
    • Persistent Data Structures
      • Book: Purely functional data structures
      • Phil Bagwell
    • The Language of the System
      • Riak
      • Armstrong Thesis
      • Coursera
      • Eden
        • Better than json
      • ZooKeeper
      • Use java.util.concurrent.queue
    • Music
      • Monome
      • MaxMSP
      • JazzMutant Lemur
      • Arc
      • Performance
      • Sifuka
        • On iPad
      • Ableton Live
      • jsui
    • ClojureScript
      • MongoDB
      • Express – application
      • Hiccups – library
      • Catnip – CS IDE?
    • Sam Aaron
      • Freesound Website
      • EMACS Live
    • API design
      • Clojure Training
        • 23-25 Jan ’13
    • Web
      • James Reeves