Follow posts tagged #leiningen in seconds.
Sign upClojure weekly, 09/11 to 09/18 2012
Welcome to this issue of Clojure Weekly. Clojure Weekly are just a few links to clojure-related posts, blogs, screencasts, presentations or podcasts I found over the Internet. I added a summary to each one of them so you can decide if you want to spend the time to look at the original! That’s it. Enjoy.
Monad Tutorial, Part 1 (Monads as Deciders / Identity Monad) on Vimeo This is the Marick version of the Monad tutorial. Absolutely brilliant series of 4 videos dissecting what monads are starting from the simplest possible example. Every step is explained or it shouldn’t take more than a few minutes of reasoning to understand what’s going on. Still, the complexity grows exponentially. Episode 4 is the core of the treatment, where everything comes together and is not that easy to follow. I still have to watch that last part a few more times to completely understand. Basically Marick is re-implementing https://github.com/clojure/algo.monads step by step to show what those hight level macros like domonad are there for. I should mention that reading the first few chapter of SICP helped me a lot quickly understanding many of the concept present here, for example the let form. Enjoyed every single frame, highly recommended.
ClojureDocs - clojure.core/reductions Here’s a nifty trick, useful to debug that reduce single liner you are so proud of. If you don’t understand what’s going on inside and why a reduce operation returns that odd result, you may want to replace it with reductions and look at each step performed during the reduce.
leiningen/doc/PLUGINS.md at master · technomancy/leiningen · GitHub Writing plugins for leiningen is pretty straightforward and well documented. Many of the tools coming with leiningen are plugins as well. That maybe explains the good documentation and debugging hooks. I especially like the .lein-classpath entry so that you can develop the plugin directly from the first project that requires it, without the need for a lein install of the plugin. This wiki page contains everything you need to get started, like “eval-in-project” key function used to evaluate code from the plugin in the context of the project where it runs, giving access to the same classpath as seen by the hosting project. This is a good starting point though, I had to search for plugins to understand how things really work when you need something more complicated. (see lein-midje for a good example).
macourtney/drift Drift is te closest of the library I’ve found to mimic Rails migrations for Clojure projects. I’m used to the way Rails handle migrations so started searching around for something like this. I know there is something similar in https://github.com/slagyr/joodo by Micah Martin, a Clojure web framework deeply inspired by Rails but I don’t know if it can be extracted without using the rest of the framework. I had less luck searching for something like Machinist or Factory Girl for integrated testing and seed data. For this reason I’m kind of growing my own little lib and lein plugin. Hope they’ll be public soon.
Raynes/bultitude The name of this library doesn’t speak for itself :( but it is certainly an useful utility as soon as you have to play with the classpath seen by Clojure. Bultitude shows you all available namespaces from the current classpath or inspecting a specific jar or folder. Bultitude is considered an improvement over certain limitations of clojure.tools.namespace (see the readme). clojure.tools.namespace can be found now as https://github.com/clojure/tools.namespace (it was previously a classic clojure contrib).
first steps into clojure
i’ve been looking for a new language to learn.
javascript and java are currently part of my job, so all the new things I have to learn in those areas I normally do them every day. I had peaked into Scala a couple of times already but I’m (still?) not convinced of the whole “scalable language” concept. it feels like a language, like a tool, should aim at one kind of problems. maybe it’s immaturity on my part but since I’m only 31, there’s still hope for me I guess.
the other option was clojure.
I had Lisp in university and I enjoyed it a lot. my master was in logics and artificial intelligence, and even though used Prolog heavily at the time, the same philosophy of declarative programming was a bliss to embrace. but perhaps even more than that, I remember that my good friend Hubert was a big fan of clojure. and since he was one of the few truly inspiring people with whom I worked in tomtom, I guess it kind of makes sense to follow his lead on this one.
so how to start?
so I’ll start by describing the general setup you’ll want to have so that you can play with clojure in two different ways:
- using the REPL, a command line environment where you can just try clojure commands;
- having the ability to build a small application and run tests on it, kind of an equivalent to a maven project, where I can get my code, my tests, build it and have a deliverable at the end.
so here we go!
- leiningen (http://leiningen.org) — leiningen (lein for friends) is the clojure equivalent to maven. and it integrates with maven in such a beautiful that it’s not even real. get it the old-fashioned way since package managers are still using the older clojure 1.3.0.
- so, since lein is like maven, it downloads whatever dependencies you may need, one of them being… clojure. it’s dependencies come from clojars, the clojure equivalent to maven central and… the actual maven central.
- now, it turns out that with lein you immediately have both the REPL command line prompt and a way to create new project. to try the prompt just do lein repl.
- to create a new project try lein new awesome-project. that is the equivalent to mvn archetype:generate, except it doesn’t ask you for anything — and creates a proper project.
- with that project created, cd into its directory and try lein test, lein clean, lein install. lein help will give you a list of options you can use but the one that’s really nice is lein pom — yes, it generates a pom file for your project, which means that you can now import it into IntelliJ. so let’s do that then.
- la clojure (it’s part of the intellij plugins) — head over o intellij, open the plugins, search for clojure, choose La Clojure, install and restart. now import your project from the pom file you created above.
my first observation so far is that things just seem to work.
you get leiningen and it is working without any configuration. you create a pom file and it’s valid and intellij reads it. the project that was generated with lein new has a documentation folder with a doc written in markdown.
it’s like there’s people in the world who follow this precept of “convention over configuration” and they are clojure developers.
so my next question is how to approach learning this.
I’ve seen a lot of lisp exercises out there but I also keep seeing that clojure is not exactly lisp, so they may not be the option.
I really loathe project euler since I’m not trying to exercise my brain or remember mathematics here — I’m looking for programming exercises, algorithmic problems, that make me understand the way you do things in clojure. something that would guide me through the standard API, while at the same time forcing me to solve little problems.
4 clojure may be a good candidate, although it looks a lot like project euler.
I thought of following the python challenge, has the kind of problems I’m talking about. another option is Land of Lisp, where you create a game every chapter - but again, it uses common Lisp instead of clojure…
so, given the current dilemma, I’m just going to go out for a walk.
(bye)
Clojure weekly Aug 09-16 2012
Welcome to this issue of Clojure Weekly. Clojure Weekly are just a few links to clojure-related posts, blogs, screencasts, presentations or podcasts I found over the Internet. I added a summary to each one of them so you can decide if you want to spend the time to look at the original! That’s it. Enjoy.
065 RR Functional vs Object Oriented Programming with Michael Feathers This time the Rougues are confronting a delicate argument, OO vs FP. Unfortunately I’m not happy about the way they handled it, starting from the fact that didn’t invite someone from the FP community to join the talk. There are also the usual common places of OO guys talking about what FP is: use immutable data structures to avoid memory overload and list comprehensions are not the essence of functional programming. BUT, but, there is some good point to reason about. OO was not translated properly from the original Alan Key propositions of components sending messages between each other. Today we are talking about “Tell don’t ask” to remember our self that objects should be communicating asynchronously. The Actors model of computation is typically an OO pattern that implements message passing. It’s strange nowadays to see actors being used as a strong argument to adopt FP instead :) A lot of incidental complexity in OO was introduced over the years. OO gave up reusing in the small. OO ended up reusing architectures and frameworks instead. It’s so difficult to tell when FP or OO is more suitable to solve a problem that the best thing is trying to put evidence together. What evidence do we have these days? Michael Feathers’ blog post (http://michaelfeathers.typepad.com/michael_feathers_blog/2012/03/tell-above-and-ask-below-hybridizing-oo-and-functional-design.html) describes the situation where FP works at the bottom while it becomes OO rising the abstraction at the top. It seem that OO is great at modeling large systems because we’ve seen very large OO systems. What are the large FP systems instead? Can we demonstrate the same for FP in handling large systems? Maybe there are no large FP systems because ‘data’ is large in FP. You take all the data complexity out and put it in an hashmap of some sort. All the complexity in FP seems to move to functions. To summarise: both FP and OO are going to have their in programming but nobody at the moment knows where exactly.
Nathan Marz: “Cascalog: Making Data Processing Fun Again” | Clojure on Blip I’m impressed by this presentation of cascalog, although I was overwhelmed by the increasing complexity of the queries half way through. But I don’t think that’s important. Queries are going to be related to a business domain that requires a bit to grasp. What’s important is to see how much SQL you just avoided by comfortable stay inside your beloved Clojure. Declaraive programming allows you to work your query complexity over functional decomposition without the need to become a sql guru (because I suppose you can do most of that in sql as well). Also consider you can test drive cascalog and use your favourite debugging setup. I cannot imagine how to do that in some proprietary sql environment. Something I need to understand better is how everything in cascalog seem to map and reduce. How exactly a query gets map reduced? Is that always possible? Finally, a concern I have is that cascalog, the clojure implementation of datalog, datomic query language all seems to solve the same problem with a slightly different syntax. Is this the right time to think about an uniform standard?
Jay Fields’ Thoughts: Lessons Learned while Introducing a New Programming Language I enjoyed this reading, a very insightful guide to migrate your team to a new language (in this case Clojure over Java). The first step is parallel development of features off work and comparison during office hours. Non production code is a good place to start: tests, log parsers, simulations, migration scripts. You should be prepare to re-write the new part back to the old language if somebody is having a lot of difficulties. Something else I didn’t think about: it’s okay if part of the team is working on the old language and other people are working with the new if they don’t want to follow. I’d like to add: if your company has a teaching/learning department engage with them to offer Clojure courses, presentations, dojos and so on. Another comment: there is a fight on two fronts. From the above: management should recognise the business advantages of the new language. From the bottom: developers should scream their frustration with the old language and ask to move to the new. There are many other takeways. 1. the new language must be almost as fast as java 2. if the company is already JVM based, it must be on the JVM 3. Causing the least amount of pain possible to teammates, adapting to current development env (for example, run on IntelliJ, right click run tests, CI integration, profiling)
leiningen/sample.project.clj at master · technomancy/leiningen The other day I had the following question for Google: how can I tell leiningen that there are other sources which are not in the default src folder? I had a good 15 minutes search and then found the all-you-can-eat version of the project.clj from the leiningen repo itself. This is all you need to understand what you can do with leiningen, and the list of options is long and well documented in this sample project.clj. My question, for instance, is answered by the :source-paths option. At the bottom you’ll see that lein is going to obey a few of the usual Java environment variable suspects that will be passed to the JVM, like jvm_opts. Well done.
Homebrew で Clojure を使う
Homebrew を使っている場合、% brew install clojure で Clojure をインストールできるのですが、leiningen 経由でインストール・管理することにしました。Leiningen も homebrew でインストールできて、
% brew install leiningen
==> Downloading http://github.com/technomancy/leiningen/tarball/1.6.1.1
==> /Volumes/Data/homebrew/Cellar/leiningen/1.6.1.1/bin/lein self-install
==> Caveats
Standalone jar and dependencies installed to:
$HOME/.m2/repository
==> Summary
/Volumes/Data/homebrew/Cellar/leiningen/1.6.1.1: 3 files, 36K, built in 15 seconds
$HOME/.m2/repository 以下に依存ファイルがインストールされるようです。
ためしに、新規 Clojure プロジェクト seven-languages を作成して REPL を立ち上げてみると、
~/work % lein new seven-langeages
Created new project in: /Users/passingloop/work/seven-langeages
~/work % cd seven-langeages/
~/work/seven-langeages % ls
README project.clj src test
~/work/seven-langeages % lein repl
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.pom from central
Downloading: org/clojure/clojure/1.2.1/clojure-1.2.1.jar from central
Copying 1 file to /Volumes/Data/work/seven-langeages/lib
REPL started; server listening on localhost port 19034
user=>
何やら Clojure 本体がダウンロードされてようです。$HOME/.m2/repository を確認すると、
~/work/seven-langeages % ls -R ~/.m2/repository/
org
/Users/passingloop/.m2/repository//org:
clojure
/Users/passingloop/.m2/repository//org/clojure:
clojure
/Users/passingloop/.m2/repository//org/clojure/clojure:
1.2.1
/Users/passingloop/.m2/repository//org/clojure/clojure/1.2.1:
clojure-1.2.1.jar clojure-1.2.1.jar.sha1 clojure-1.2.1.pom clojure-1.2.1.pom.sha1
ファイルを確認できました。
How to set leiningen2's proxy.
There’s an old post out there that documents how to configure leiningen to use a proxy. It’s fantastic and points to all the right information, but for the old version of leiningen.
With leiningen2 in the slow process of being released, it strikes me as an appropriate time to update that documentation, as it’s now entirely incorrect as leiningen no longer use Maven, but Aether, to resolve dependencies.
Simple Route: Set your environmentshttp_proxy and https_proxy correctly.
Leiningen now knows how to honor those variables. When it attempts to grab dependencies and push things to Clojars, etc, it will default to the values found there. Note that those variables are both the host and the port, i.e. 123.456.789.101:89.
:jvm-opts section in your user profile.
If you need leiningen to have a custom proxy that won’t work for other components in your system, then you can’t set the whole she-bang with an environment variable.
Enter profiles.
Profiles are the preferred way to change the configuration of many projects or provide plugins that are useful mainly to you and not to everyone that might work on the project (like lein-swank, for instance).
There are a few profiles active at all times unless configured not to be active: :dev, :user, and :default. That means that you can configure any of those in the ~/.lein/profiles.clj and it will take effect for all runs.
To configure the :user profile, open up or create the ~/.lein/profiles.clj and enter something like the following:
{:user {:jvm-opts ["-Dhttp.proxyHost=165.226.204.104" "-Dhttp.proxyPort=8080"]}}
Note a couple of things:
- In good Clojure tradition, configuration is done via a plain map.
- Specifying which profile your configuring is done by specifying that profile as a keyword. To define more, you just add another entry to the map. A profile can be any valid keyword.
:jvm-optsis a key that leiningen knows about which is avectorofstrings that will be passed to the JVM that leiningen starts. Thinkclojure.java.shell/shcall where multiple arguments are specified as multiplestrings.
Short but sweet! Hope it helps.
Emacs + leiningen + REPL で Clojure を使う
Emacs の inferior Lisp process として REPL を走らせるには、.emacs に
(setq inferior-lisp-program "lein repl")
と設定して、M-x cd で Clojure プロジェクトのディレクトリに移動して、M-x run-lisp するだけ。これで、新しいバッファに
REPL started; server listening on localhost port 26413
user=>
REPL が起動する。Emacs info の 31.11 Running an External Lisp を参考にした。
Learning Clojure
Last week I started learning Clojure with the goal of creating an http server in the language. Previously when I first started my internship I started doing the Clojure Koans, however I soon changed to work with ruby instead and I never finished them. To create the server I needed to learn more about the language.
In this post I will give a short overview of Clojure and try go over some of the best resources I used to learn the language.
What is Clojure?
Clojure (pronounced closure) is a relatively new dialect of the Lisp programming language created by Rich Hickey. Clojure source code is compiled into Java Virtual Machine (JVM) bytecode which means it can run on any JVM enabled device. Clojure is an impure functional programming language, it gives the programmer the tools needed to avoid mutable (changing) state. It regards functions as first-class citizens.
How to learn Clojure
Clojure is not an easy language to learn compared to languages like Ruby and C# which are the other two languages I know. With some good tools and resources it can be easier. Clojure (like Lisp) has a lot of parentheses in its syntax which can be very confusing in the beginning. Over time it gets easier to follow, especially with a good editor.
Before starting to learn how to write Clojure, I think it’s a good idea to get a more thorough overview of the language. This series of videos on youtube is a good starting point.
At this point I felt more ready to try out some Clojure myself and I went through the Clojure Koans (finishing them all this time).
Currently I am doing the labs found here. They are an excellent resource that offer explanations and allow you to code a good amount yourself. I recommend doing these after the koans since they can be challenging if you don’t have experience with this type of programming.
How to set up Clojure for custom projects
Both the koans and the labs come with Clojure, so you still need to set something up for running your own projects. You can go to the Clojure website, download the language core and set up a startup script to run Clojure projects. There is an easier way though, using Leiningen.
Leiningen is a tool used to manage your Clojure project and its dependencies. When you use Leiningen to start a new project it will create a basic structure for files and directories. It will also add Clojure as a dependency. Having the Clojure core being a dependency managed by Leiningen is nice because you don’t need to make sure it’s installed and that it is in the right version. Leiningen will do that for you. The following commands are the basic steps to create a new project.
lein new myawesomeproject
cd myawesomeproject
lein deps
For more detailed instructions on Leiningen read this blog post by Colin Jones.
I still have a lot to learn about Clojure and functional programming in general, but I think that I now have a good basic knowledge.
Clojure weekly Aug 2-9 2012
Phil Hagelberg: “Making Leiningen Work for You” | Clojure on Blip Phil Hagelberg (aka techmomancy) goes through the main features of Leiningen, which is the workhorse tool for developing Clojure application. The presentation is quite old, so some of the new features you’ll see here are already there now, especially with version 2.0-preview that is available today. Many changes since the previous release: dependencies stay in place where they are in the maven repository. Plugins are now more modular and flexible for the installation. A few things I didn’t know: the /checkouts folder can be used to override dependencies with out-of-the-trunk projects. This is useful for parallel hacking while developing your own project instead of jumping to the directory where you have the dependency to work on a patch. You can write your own task (to be expected) something that I never used myself. And many more. At the end Phil lists what makes a good open source project good, like leiningen with its 50+ active contributors is.
Blog | Rich Hickey - Podcast Episode 014 | Relevance An easy listening get-to-know-the-inventor-of-clojure podcast. Rich is a very smart guy and he’s the main responsible for my interest in Clojure, not the language. Let me explain. When I was a full time Ruby programmer I use to follow (and I still do) Stuart Halloway blogs, presentation and books. When I saw his interest in Clojure I was intrigued as well and I promptly found Rich’s Clojure introductions on Video. It was just fascinating to hear him speaking about the concept of time and identity. What?!? What time and identity has to do with OO and functional programming? It snowballed from there. So if I can give a suggestion for the next language inventor is: think carefully about the principles behind your language, document them and possibly send out easily accessible videos that result at the top of the google page. In this interview Rich talks about his background, how he came up with Clojure, his past as audio engineer and C++ guru. He’s also working on a third “big thing” other than Clojure and Datomic: Machine Listening (http://en.wikipedia.org/wiki/Machine_listening).
Learning Clojure/Special Forms - Wikibooks, open books for an open world Is a special form really special? Yeah, kind of, because they are all special in the same way. Special means that it looks like a normal function but it doesn’t behave as such. What usually is different is the was parameters are evaluated and in which way the function is dependent on the surrounding context. The clojure reader needs to apply a special treatment for these forms: if ,quote, do, try, fn, let to name a few. They usually differ from functions and macros provided by the standard library in the sense that they are not built on top of other Clojure primitives. This wikibook page contains a nice summary of the most common Clojure special forms.
Block Comments in Clojure - Stack Overflow coming from a Ruby background I searched for some form of block comment in clojure on day one. I was surprised not to find a proper one. A definition of proper is a sintactic form that the compiler totally ignore so I can for example embed some raw XML without escaping for further processing as a string. You can comment with ‘;’ but it doesn’t work that well. There is a ‘comment’ macro that can enclose multiple lines but it needs to be proper clojure. The #_ macro is a shortcut for the ‘comment’ block that requires less typing but again it must enclose proper clojure. A patch was submitted at some point to allow raw text but it was deemed as useless by Rich Hickey in person. That’s the meaning of having a benevolent dictatorship in charge of the language!
Clojure Libs and Namespaces: require, use, import, and ns | 8th Light By far this is the best introduction to clojure tooling for namespaces manipulation. I came across this a while ago and I keep getting back to it when I need a quick refresh. The well crafted article by Colin Jones is a gradual introduction to all the flavours of require, use and lately refer. There are many powerful options and unfortunately it can confuse at the beginning. With the introduction of Clojure 1.4 ‘use’ has been replaced by require plus :refer to obtain similar results. Everything adds then together with the ns macro where you can list all of them at once. My preferred way to include clojure namespaces at the moment (and avoid quite a bit of thinking) is to always prefix them by qualifying them like: (require ‘[clj-json.core :as json]) so it’s always clear what I’m talking about: json/generate-string for example.