Alister Bullman once told me "You can learn any language in a week - its the API's that take all the time". This was after I learned PHP to work on some stuff for Alister. Then Alister made me learn Zend. And I almost had a heart attack / anuerism. Welll. ....... Now I'm doing some Clojure work to finish off my thesis. And big Al was right - its the API's that get you.
It just turns out that, nowadays, to build a production quality app, you have to have one of those god-forsaken frameworks - be it Zend, Maven, JBoss, Django, Grails, whatever.... Standard old hello worlds with the basic language primitives, even for new languages like Clojure (yeah, it really is new...) , just don't cut it anymore.
Why Clojure ? There are a million Blogs out there addressing that issue. I'm assuming whoever is reading this blog is sick and tired of needlessly over-statefull methods and applications, and actually WANTS to learn clojure.
So, to ease the pain of Leinigan for begginers, I've created a VERY simple set of instructions here for you :
1) Follow the Leiningan instructions online https://github.com/technomancy/leiningen at git hub. Set it up and install it.
2) Type "lein repl" into your terminal .
3) Now, you can code some simple clojure in the REPL. Do a simple hello world, like the one here : (def hello (fn [] "Hello world"))
4) Okay, so that works --- now wheres the magic ? Wheres the API that makes everything so wonderful and amazing ? Exit out of REPL, or if you're smart and have two terminals open, go to the one without REPL running. type "lein new myNewProject". Now you have a perfectly configured clojure project, with a README, a starter .clj script, and a dependency/configuration file. Your'e all set. Just run an "ls" and go to the src directory if you dont believe me.
5) Okay, now cd to the project. Add apache String Utils to the project build file. For those of you that don't know what that is, its just a java library for doing simple things in java that, unfrotunately, are not part of the standard api. For example, joining an array into strings.
StringUtils.join(new String[]{"a","b"}); will yield a nice & neat string "ab".
The beauty of clojure is that its easy to take java APIs that already exist and use them in a functional idiom... so...
Now please edit your project.clj to look like this (that is, add commons-lang, in the last line, to the dependencies.
(defproject myNewProject "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.2.1"][commons-lang "2.3"] ])
If you look at the last [] inside of :dependencies, you'll see [commons-lang, "2.3"]. What is that you ask ? Its a maven dependency .
" What ? MAVEN ? Maven dependencies are supposed to look like this .... " you might say.
commons-lang
commons-lang
2.3
Yes....... But lein simplifies everything for you. It doesnt force you to write out redundant group/artifact headings. Its simpley
[group artifact version] or
[group version] (when group=artifact)
6) So thats it. Now just get run "lein deps" from the projects root directory, created by lein in step 4. Lein will magically go to maven, get you're stringutils jar file, and add it to the internal classpath. And you can run StringUtils inside your clojure scripts in this project now. hell yeah ;
7) To test this out, add this function to the core.clj script in your source folder.
(defn makeCap "This makes capitals" [inString]
(StringUtils/capitalize inString))
Conclusion
The nice thing here is that you can easily integrate Clojure with external jars from maven that we all know and love, in a functional idiom, and furthermore, the fact that this is entirely "mavenized" for you by a "maven-like" dialect that is native to Clojure, called leiningan.
Yay for Frameworks !
It just turns out that, nowadays, to build a production quality app, you have to have one of those god-forsaken frameworks - be it Zend, Maven, JBoss, Django, Grails, whatever.... Standard old hello worlds with the basic language primitives, even for new languages like Clojure (yeah, it really is new...) , just don't cut it anymore.
Why Clojure ? There are a million Blogs out there addressing that issue. I'm assuming whoever is reading this blog is sick and tired of needlessly over-statefull methods and applications, and actually WANTS to learn clojure.
So, to ease the pain of Leinigan for begginers, I've created a VERY simple set of instructions here for you :
1) Follow the Leiningan instructions online https://github.com/technomancy/leiningen at git hub. Set it up and install it.
2) Type "lein repl" into your terminal .
3) Now, you can code some simple clojure in the REPL. Do a simple hello world, like the one here : (def hello (fn [] "Hello world"))
4) Okay, so that works --- now wheres the magic ? Wheres the API that makes everything so wonderful and amazing ? Exit out of REPL, or if you're smart and have two terminals open, go to the one without REPL running. type "lein new myNewProject". Now you have a perfectly configured clojure project, with a README, a starter .clj script, and a dependency/configuration file. Your'e all set. Just run an "ls" and go to the src directory if you dont believe me.
5) Okay, now cd to the project. Add apache String Utils to the project build file. For those of you that don't know what that is, its just a java library for doing simple things in java that, unfrotunately, are not part of the standard api. For example, joining an array into strings.
StringUtils.join(new String[]{"a","b"}); will yield a nice & neat string "ab".
The beauty of clojure is that its easy to take java APIs that already exist and use them in a functional idiom... so...
Now please edit your project.clj to look like this (that is, add commons-lang, in the last line, to the dependencies.
(defproject myNewProject "1.0.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.2.1"][commons-lang "2.3"] ])
If you look at the last [] inside of :dependencies, you'll see [commons-lang, "2.3"]. What is that you ask ? Its a maven dependency .
" What ? MAVEN ? Maven dependencies are supposed to look like this .... " you might say.
Yes....... But lein simplifies everything for you. It doesnt force you to write out redundant group/artifact headings. Its simpley
[group artifact version] or
[group version] (when group=artifact)
6) So thats it. Now just get run "lein deps" from the projects root directory, created by lein in step 4. Lein will magically go to maven, get you're stringutils jar file, and add it to the internal classpath. And you can run StringUtils inside your clojure scripts in this project now. hell yeah ;
7) To test this out, add this function to the core.clj script in your source folder.
(defn makeCap "This makes capitals" [inString]
(StringUtils/capitalize inString))
Conclusion
The nice thing here is that you can easily integrate Clojure with external jars from maven that we all know and love, in a functional idiom, and furthermore, the fact that this is entirely "mavenized" for you by a "maven-like" dialect that is native to Clojure, called leiningan.
Yay for Frameworks !




0 comments:
Post a Comment