Saturday, May 21, 2011

Division in Clojure

Clojure includes a Ratio type which we can access using a ratio literal like this
(+ 2/3 3/4)
=> 17/12


You need to be careful because the '/' operator will return a ratio if the result is not a whole number (most languages return an integer)
Ruby: 16/7
=> 2
Clojure: (/ 16 7)
=> 16/7


The % sign means something different (the first argument to an anonymous function actually) rather than being a modulus operator
(% 16 7)
java.lang.IllegalStateException: arg literal not in #()


If you want to do integer division or get the mod, you need to use the quot (quotient) and rem (remainder) functions
(quot 16 7)
=> 2

(rem 16 6)
=> 4


You can also use decimal numbers to do decimal division
(/ 16.0 7)
=> 2.2857142857142856

Monday, May 16, 2011

Early Explorations in Clojure

Having never programmed in Lisp before, and never really been able to start making sense of the code snippets I've seen, this is all very new to me.  However, I've got the following figured out:

Function calls are surrounded with parenthesis and arguments are separated by spaces not commas.  String literals are surrounded by quotes.  

Print "hello world" to stdout:
(println "hello world")

Single quotes cannot be used for string literals though
(println 'hello world')
java.lang.Exception: Unmatched delimiter: )

Clojure provides access to the core java libraries - albeit with some slight syntax changes...

Java:
System.out.println(System.getProperty("user.dir"));


Clojure:
(println (System/getProperty "user.dir"))

You can use defn to declare a function.  Notice the argument list in square brakets. 
(defn foo [bar baz] (str bar "," baz))
(foo 1 2)
==> "1,2"

The str function concatenates the strings passed.


Sunday, May 15, 2011

Clojure

I'm probably biting off more than I can chew, but I've started looking at Clojure as well as JavaScript. I've been thinking of learning a functional language for some time now and JavaScript's focus on functions over classes and objects in the classical sense has inspired me to give it a go.

JavaScript Books

I'm finding twitter increasingly useful now that my network has grown to include people I know from Scottish Developers events and Code Retreats and I'm having conversations rather than just lurking on discussions.


My JavaScript doesn't extend much beyond the obvious syntax (variables, loops etc) and a rudimentry understanding of the DOM.  I clearly need to brush up a bit before digging into node.js and jasmine.  I don't want to even thing about coffee script before I understand the project it solves. 


So where should I start?  I asked the twitterverse:




Within minutes I had a response! 


Matthew Gillard and Bill Caputo both recommended JavaScript: The Good Parts by Douglas Crockford.  They also recommend:




Certainly a lot to be getting on with


Tools-wise, I downloaded chrome and Firefox and installed the Firebug plugin. 


That was a week ago.  So far, I prefer Chrome.  I like it's mimimal style.  I also like the way firebug highlights the relevant area on the page when you click the markup.


The book is truly awesome.  I would definitely recommend it.









Scottish Ruby Conference 2011

So here we go.  I've been meaning to start a blog about all the things I do outside Agile data management for a while.  Hopefully this will work out.  Let's see...


The Scottish Ruby Conference this year was just what I needed.  As ever, it was really inspirational and motivated me to kick-start my learning.  Someone said that if you heard something mentioned 3 times at a conference, those are the things you should be learning.


My 3+ list is 
Strange that the first three are Javascript technologies, but I guess that's just the way it worked out this time.

I guess I had best brush up on my javascript then.