July 23, 2014

Clojure Weekly, July 23rd, 2014

Welcome to another issue of Clojure Weekly! Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

clojure.core/gen-class · Grimoire gen-class creates a Java class and spits that to disk. It can be instructed with all sort of Java goodies, like implemented interfaces, methods, ctor plus some goodies like a post-init constructor. One of the main reasons of gen-class is Java interoperability, where it is important that the class is not generated on the fly in a deeply nested classloader and is reachable through normal class path lookup such as Class/forName calls. gen-class has only value during AOT compilation and does nothing at the REPL.

Transit – A format for conveying values between different languages | Hacker News Transit is a new data transfer format from the Cognitect crew. What it offers and what adds to the plate of an already rich horizon of data transfer format was not clear to me after reading the official presentation page. As usual HN does a great job comparing with the existing stuff and hence my link to it. Rich, Fogus and Swanndonnette are all in the thread, so you can see that the competitive advantage of Transit over other formats seems to be about the compression mechanism, pluggable human-readability, json interoperability and the extension mechanism. EDN isn’t deprecated or anything like that, but it sounds like for future work you better look at Transit instead.

cemerick/austin Austin is the glue between the lein building process of a ClojureScript based project and the browser. With Austin things like evaluation of ClojureScript in the repl is possible, with several options, headless or fully integrated with Chrome for example. I think the most compelling use case during development is making a change and have the browser to automatically update the changes, no refresh required. Other compelling reason is full access to the dom at the repl for debugging and experimentation.

One Div Zero: A Brief, Incomplete, and Mostly Wrong History of Programming Languages Totally cracked up by this humorous history of programming languages :) All the stereotypes are there. Probably the one I liked the most is Perl. I think ARC got mentioned instead of Clojure because in 2009-ish it was still considered the new Lisp. History turned out to be different.

CLiki: Naming conventions These are the naming convention guidelines you were all waiting for! It’s common lisp, but hey, it is not that different from the same kind of naming conventions of Clojure sources. Of course they are not all applicable, but they help a lot giving just a bit of semantic to a function name in case Clojure doesn’t.

Okasaki: Ten Years of Purely Functional Data Structures A blog post from Okasaki, author of Purely Functional Data Structures, describing the history behind the book that is still popular these days. Okasaki also answers a few frequently asked questions. StandardML was selected for specific reasons related to its control of lazy VS strict evaluation but it won’t be the same choice today: the book would likely be re-written in OCaml. Why not a dynamically typed language? Because Okasaki thinks that functional data structures are better explained when types are explicit. Just in case you don’t know, Clojure is implementing and extending some of the persistent data structures described there (in Java). If you want a preview of the book before buying, keep in mind that Okasaki’s thesis is available online with a very similar content. Worth learning SML just to follow this book! Unfortunately, no 2nd edition is in sight.

How to write a dissoc-in command for clojure? - Stack Overflow There is no dissoc-in in the standard library. There used to be one in clojure.contrib that got dispersed when libraries entered their own repos. But it is also simple to replicate with an update-in as suggested in this SO answer. You just need to stop one key before in the list of inner keys and add it to a dissoc invocation for the corresponding map: (update-in {:a {:b 0 :c 1}} [:a] dissoc :b)

Comments (View)
July 15, 2014

Clojure Weekly, Jul 15th, 2014

Welcome to another issue of Clojure Weekly! Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

Functional Geekery Episode 12 – Adi BolboacaFunctional Geekery | Functional Geekery Controversial (in a good sense) Functional Geekery episode. When speaking about style and idiomatic Clojure, I tend to be inspired by one of the most popular source of Clojure wisdom: clojure/core.clj. The style there leans toward essentiality, where variable names are just a few letters and they tend to follow an implicit convention (f, g are function names, xs is a collection of x and so on). But uncle Bob clean-code style is much more verbose in that respect, suggesting more speaking names. I agree with all the rest of the discussion, in term of SOLID principles applied to FP and the overall idea to dedicate a code-retreat format to FP style programming. But I’m not sure about naming. What determines understandability in a language is also the culture and conventions that are stratified into it. Clojure is inspired by CL in many ways, including naming and it looks unproductive to me to try to change 50+ years of conventional wisdom to another convention.

rseq · clojure.core For the “standard library in small pills” series, here’s the mostly unknown rseq. It works like seq, transforming something sortable into a sequence but at the same time it is reversing its order. Use cases? None at hand, but consider that it is doing it O(1). How can it be? Little trickeries: the PersistentVector underneath is unchanged, but is wrapped in an RSeq object that is counting backward when next() is required, starting from the end! Good luck garbage collector when I unroll 1M of these :)

protocols - How does clojure’s defrecord method name resolution work? - Stack Overflow Enlightening quick SO answer from Christophe Grand about potential name clashing in protocol definitions. defprotocol and defrecord are a powerful tool provided by Clojure to replicate some object oriented runtime polymorphism. They’ve got rough edges as soon as you start pushing too far with them, something you can experience if you try to port a complex object model from Java “literally”. defprotocol for instance, creates the function definitions inside the namespace they are declared in, with the consequence that there shouldn’t be other “normal” functions with the same name and no other protocols definitions with the same name, let alone using function names from clojure.core. At the same time if you define protocols in different namespaces you can incur in weird require+import constrains to use them in other places. Add the fact that other than the basic examples, there is no throughout guide to “build yourself a CLOS in Clojure”. We can certainly discuss if this is needed at all…

lein test-refresh If only I knew about this a couple months ago… I wouldn’t have ported the test suite to Midje with autotest, my favourite unit test framework. Or maybe not, because I’m quite fluent with the Midje API and got used to the expressiveness it can provide. Anyway, if you are on clojure.test, this lein plugin is an automatic runner that runs in the background and send you notifications about the status of the tests while you are typing in the editor. It is my favourite workflow at the moment, which includes the REPL running for experimentation and solutions while the overall functional interface is driven by tests. I don’t use the same for legacy code, where somebody else wrote it, although I tend to cover “after” with test the part I don’t understand.

L(λ)THW Learn Lisp The Hard Way Interesting initiative that could be ported to Clojure and has already been ported many times to many languages. Zed Shaw started it all (I think it was Learn C The Hard Way) and then produced another bunch of them. The template to write books using the same style is publicly available and Zed Shaw is encouraging others to write their own. This is the “brave” Lisp attempt, considering the amount of top quality books around about programming in Lisp. The author explained the effort behind this achievement on HackerNews. This is not a couple of days project and it is coming out nicely. Remember the following rule of thumb: learning Lisp is providing you the all principle (and often nomenclature and conventions) behind Clojure.

clojure.core/cycle Cycle is similar to clojure.core/repeat, but it operates on a sequence by recursively merging it to a copy of itself, ad infinitum. Cycle is your friend on things that needs to be provided in a round-robin fashion, including assignment of a finite set of identifiers or simply creating a css zebra list of some sort. Repeat is less useful here since it will repeat a single item instead. Always use with clojure.core/take on front!

Comments (View)
July 9, 2014

Clojure Weekly, July 9th, 2014

Welcome to another issue of Clojure Weekly! I’ve got tons of ideas and resources to follow coming out from EuroClojure, I’ll be digesting them here during the following weeks. If you don’t know what this is, Clojure Weekly collects a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

My Top Clojure Articles - Adam Bard and his magical blog Signalling a nice collection of Clojure related articles by Adam Bard. They are divided by level of proficiency with the language, so it makes it a very good start to pick up Clojure from scratch.

EuroClojure’s Videos on Vimeo Unless they are already displayed when you read this, keep an eye on the EuroClojure channel on Vimeo because videos will appear very soon here. Go for David Nolen for the inspirational talk, Tommy Hall for the entertaining one or yours truly (Renzo) because this is a shameless plug!

Thoughtworks technology radar The last technology radar sees ClojureScript in the trial phase, up to the top close to the adopt section. Clojure the core language is not present, while Scala is in the adopt section. Clojure (Om-ClojureScript) is present in the explanation section as well. My impression is that TW is not endorsing Clojure as a server side language but as client side, which is amazing and disturbing at the same time (or as someone else put it on twitter, it is given Clojure as implicit adopt?). There are other well known technologies and platforms, but there are also new entires I’d like to have a look at.

Grimoire Grimoire is a new documentation service and community contributed example repo similar to ClojureDocs.org. ClojureDocs.org still comes up high in google searches despite it is documenting Clojure 1.3, by providing examples that are not easy to find elsewhere. Grimoire is instead up to date and also gives access to all the examples in clojuredocs. Grimoire is open to community contribution over git pull requests and will likely be improved in the future to make contributions easier.

Difference between defrecord and extend - Google Groups This interesting thread finally solved my problem. Extending a type to a protocol is not equivalent to a defrecord definition that implements that same protocol. They generate different kind of polymorphism. The first main difference is that extending a type with a protocol is not adding functions to its interface (and this you can’t invoke them on a record instance). Second main difference is that extending the type with a protocol allows for an implementation swap at any point, while defrecord definition of functions are forever.

Clojure - todo Another small bit of history, a Clojure todo list back from 2009. Is not very important today but I want to keep an handle to things I discover randomly just in case. Interesting to notice that 90% of what we are talking about nowadays with faster compilers for mobile or Cloj in Cloj were already there. Others like Datalog were not baked in directly in the language but in Datomic.

Clojure Gazette 1.81 - Nicola Mometto I’m very interested in everything compiler related these days and of-course I’m following the amazing work Nicola is doing to port Clojure in Clojure. This Clojure Gazette is an interview with Nicola that explains a little but of the background about his work, past, present and future. I hope we’ll be soon meeting Nicola at some Clojure conference speaking about the work he’s doing, but as he said in the interview, he’s mostly spending his time coding and the better way to know about his work is to follow the public activity on github.

Comments (View)
July 1, 2014

Clojure Weekly, July 1st, 2014

Welcome to another issue of Clojure Weekly! I’ve got tons of ideas and resources to follow coming out from EuroClojure, I’ll be digesting them here during the following weeks. If you don’t know what this is, Clojure Weekly collects a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

midje-doc Midje-doc generates beautiful documentation starting from midje facts, which are treated as examples to show how code works. There is some directives you can give during namespace declaration to tell midje-doc how do you want your tests to be translated into “live documentation”. Midje-doc supports the midje :autotest philosophy and can show how the documentation shapes up while facts are being written. There is a 14’ screencast to demonstrate all of this on the github repo page.

[GSoC 2014] Lean compiler: first iteration - Clojure on Android Quick one to signal that project Skummet is now able to output static declaration of Vars in the generated Clojure byte-code, one of the several optimisations envisioned for Android environment that could have also beneficial impact for quick development feedback at the REPL. I tried the build on my own projects without noticing any errors.

Lisp in Small Pieces: Christian Queinnec Just ordered a copy of this (quite pricey) book about Lisp implementations. I found the book linked in many places, including rhickey suggesting it on IRC (5 years ago). The content is about typical problems and solutions for implementation of lispy-like languages (Scheme is the one used in the book), including compiled output, intermediate bytecodes or interpretation. The Rich Reference Implementation of Clojure is indeed all about this sort of problem.

metaprogramming - How do you evaluate a string as a clojure expression? - Stack Overflow I had to use something similar recently, so I could pass around a macro to be used in a different place from where it appeared in the code. With normal functions you can always use (var fn) or the equivalent #’ reader macro to create, for example, a map of keys - fns. But macros need special evaluation and thus something like (@#’clojure.repl/doc str) doesn’t work while (@#’clojure.lang/+ 1 1) is perfectly fine. The only (not very nice) way I found was to use (eval (read-string “clojure.repl/doc str”)) to make it work. Mind that there are very few cases where you need something like this, which is also suffering from security problems if eval-ing user input.

Euroclojure 2014 Here are the definitive notes for EuroClojure. Phil Potter wrote all of the notes at smoking speed over the conference, except during his own talk (I’m sure he’ll fix this annoying detail next year :) By the way, talks were amazing, with tons of ideas and links to follow that you can read on this gist as well. Thanks Phil!

Comparing Clojure Testing Libraries: Output - Jake McCrary An interesting comparison between output of testing libraries. Expectations is the winner of this informal but still useful comparison, mainly because of the care that put into the correct formatting of output between different data types. Midje is coming last with a promise (see comments below) by Brian Marick to make it better than ever. Can’t wait.

quil/quil Quil is a Clojure wrapper around processing.org, which is a drawing language and environment written in Java. With a few more wrapping layers on top, we are talking about driving openGL from Clojure. Quil is suitable for all your graphical and animation needs down to the pixel level, with a huge library of primitive available for more abstract drawing. I saw it in action during the interlude lunch-time lightning talk, showing tracks of a sequencer on the screen in a simple and effective way.

Comments (View)
June 24, 2014

Clojure Weekly, Jun 24th, 2014

Welcome to another stellar issue of Clojure Weekly! This one is just before my talk at EuroClojure. Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

lazy evaluation - Clojure: rest vs. next - Stack Overflow Nice discussion on StackOverflow about next VS rest. It can get quite philosophical and reading the sources is not helping a lot (I could only track that ASeq.rest() is calling next() inside but each collection has a different implementation of this). One sure thing is that to return nil next needs to look ahead and realise at least the next element or the next chunk of a sequence. So rest is more lazy than next.

clojure-compiler/src/net/n01se/clojure_compiler.clj at master · Chouser/clojure-compiler Another bit of Clojure history. Chris Houser is one of the protagonist of the Clojure scene and one of the earliest receptor of Rich’s ideas about the future of Clojure. Apart from his very good book (manning.com/fogus) he should also be credited for a couple of important early attempts: ClojureScript and Clojure in Clojure. Although his work didn’t end up being part the current related efforts, it is stil inspirational. Linked here is a clojure compiler in clojure from 2009. If you look at the bottom, it is tested by parsing the compiler itself.

Pelure - Shell Scripting in Clojure with Pallet github.com/pallet/stevedore is an internal clojure DSL that emits bash shell scripting. Personally I don’t write a lot of shell scripting, just the right amount when needed and when that happens I’m definitely going to look up yet again how I need to implement a condition or a loop. No more with Stevedore. I just write my favourite Clojure with some imperative style and voila, I can output shell scripting. Maybe that could be the base skeleton to start with. It allows advanced “macro style” scripting, with quoting pure Clojure to express some more complicated stuff that doesn’t necessarily belong to the DSL language. Cool.

ClojureDocs - clojure.core/group-by And just when you were abandoning all further attempts and accepted to use a fairly understandable “reduce” here comes the group-by. The name speaks quite clearly: give it a list of maps and a key and group-by will group those maps by the value of that key. The reduce is still there in the sources but handed gracefully and with an eye on performances by the standard library. If you open the sources you’ll see that it is assoc! a transient map to build up results and make it persistent at the end.

More Open-Source Clojure Systems, Please uSwitch is a neat Clojure shop in London and they recently decided to share some of the goodies they are producing over there. This post also illustrates a few of the Clojure infrastructure best practices and tools they adopted, including a component system, riemann monitoring and much more. The post is also an invite for all the other Clojure shops to open-source more of their work. I couldn’t agree more. At the Dailymail I started taking care of this part a little more and open sourced the first of a series of libraries/tools that are part of our daily practice. But we still have a lot to do in this direction. Stay tuned and share more.

ClojureDocs - clojure.core/comment comment is a macro that ignores the given body and returns nil. When comment is used in normal code it still needs to go through the reader before macro expansion inside the compiler when it is finally yielding nil. This has the important consequence that what is enclosed in a comment needs to be well formed, at least for the syntax checking that the Clojure reader is applying. For example the following: (comment {:a a :b}) won’t pass the MapReader check, because the reader knows nothing about the behaviour of the comment macro. The #_ reader macro ignores the next form and doesn’t force any compiler step, so it is usually a better choice.

Comments (View)
June 12, 2014

Clojure Weekly, Jun 12th, 2014

Welcome to another stellar issue of Clojure Weekly! Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

how is it possible to intern macros in clojure? - Stack Overflow Interning is the operation of storing a new symbol to var mapping pair in a namespace instance. The var can point to a function or a macro or something else. After interning the new symbol starts to work exactly like any other created using def/defn. Interning can be useful when the definition is done at runtime rather than compile time (a special case indeed, but similar to what is called meta-programming in other languages). This stack overflow question was answered to understand what to do in case of a macro, which turns out to be just some metadata away. The interned macro is then used in the context of the namespace it was defined into.

Automated Browser-based Testing with Clojure — An Architect’s View When the time will come to have some browser automation covering the main feature of our app, I want to be ready. I’m very reluctant to have a comprehensive suite of browser based tests and make it part of a top-down process, but I’m happy to have a sanity automation click-around that tells me if something important is severely damaged. It looks like Gregoire’s clj-webdriver is that nifty lib, a Selenium WebDriver wrapper that can be used easily from Clojure. This post by Sean Corfield explains and to set it up.

ClojureDocs - clojure.core/with-redefs with-redefs is the less sophisticated binding cousin. It changes the var root of a var for all the executing threads instead of being selective as binding is. It does this to all vars, those marked :^dynamic and those that are not. This brutal approach doesn’t have a lot of application in normal code, since it implies a global state change that other portion of the code might not be aware of. This is the reason why it is preferable to binding to mock out a function in testing. with-redefs will operate as expected even in case where tests are concurrent.

ClojureDocs - clojure.core/not-empty This little function seems useless but it enables better idiomatic Clojure. not-empty on a collection returns nil when the collection is empty. As we know, Clojure collection emptiness does not equal nil, that is (= nil []) is false. But (= nil (not-empty [])) is true. Why would you need that? To enable when-let forms on collections. If a function needs to create/process a collection first thing and only proceed if there is at least one element in the collection (maybe doing expensive stuff) (when-let [mycoll (not-empty (fetch-elements))]) only evaluates when there is something in the collection.

arrdem/oxcart Oxcart is another (GSoC) attempt to speed-up Clojure at the price of a reduced or removed dynamicity. It is a Clojure rewrite of the reader, analyzer, emitter triad that compared to the tools.* approach doesn’t seek Clojure compliance (other than the pure runtime). Compared instead to Alexander Yakushev’s Skummet or the Rich’ fastload branch, this is written in Clojure while the formers are taking it from the current Java implementation. Several interesting ideas in this project, but mainly the compilation of functiond belonging to the same namespace to a single class (instead of a class per function) or the entire program to a single class, thus allowing drastic optimisations once the entire requirements in term of bytecode are all collected in the same place.

clojure-classes/graph-w-legend.png at master · Chouser/clojure-classes Another great class diagram by CHouser. It’s from 2009 but it’s still a great “getting-around” reference considering the amount of classes and their relationship in Clojure core.

Comments (View)
June 3, 2014

Clojure Weekly, June 3rd, 2014

Welcome to another issue of Clojure Weekly! Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

fastload Branch Commits · clojure/clojure Glad to see Rich is back working on speedup of Clojure. There are many people at the moment working on improving Clojure bootstrap. Maybe the trigger for this is that as the Clojure projects grow in size it is more and more difficult to deal with a fast feedback loop, even with a REPL and components frameworks. Another big demand is coming from mobile, where Clojure can be a compelling language to work with.

Clojure Cheat Sheet (Clojure 1.3 - 1.6, sheet v13) An enhanced version of the official clojure.org/cheatsheet with added popup tooltip and search box! The cheatsheet is quite useful to have a glance of the standard library functions grouped by functionality type. With this one you just hover over a function to see the docs and typing in the search box will filter down the list. Thanks Andy Fingerhut for posting this in the group list.

ClojureDocs - clojure.core/vector-of Vector-of creates a Vector, one of the Clojure data structures we are used to with [] that is using primitive types underneath. I couldn’t find compelling examples or decent documentation so I’ve opened up the sources. gvec.clj is offering a PersistentVector behaviour implemented on top of native Java arrays, when PersistentVector.java is using Object[] as a node instead. In that sense gvec is a translation of PersistentVector.java to Clojure, with primitive types as leaves (of the trie). When to use? Performances. Use vector-of when a persistent data structure is still important (because if not, you could just go transient or even int-array and friends). Of course you need to be sure you’ll never store anything else than a primitive type in it, that now that I think about it, it happens quite often to me.

ClojureDocs - clojure.core/pmap pmap is a dead easy way to parallelise a map operation in Clojure. It comes with trade-offs you really want to be aware of, before it’s too late. The model assigns each element of the map to a future, where the map operation will take place. Pmap is not a good idea for big collections and fast map operations, since at that point any possible speed gain will be overtaken by the creation of threads. For more big data things, reducers are a better solution.

ClojureDocs - clojure.core/with-meta Metadata in the form of a map can be attached to symbols or collections. Metadata are mainly designed to pass semantic information down to the compiler phases, so there is a list of them with a special meaning that is used by the clojure internals. Metadata can also be used by library or framework providers, to mark functions (symbol-Var pairs pointing to functions) for example for testing. Of course more creative use is possible, but implementing business logic as metadata is possibly a bad idea (ask yourself why that information needs to be hidden instead of being part of the code contract). With-meta is the extended form, but the reader macro ^{:doc “How obj works!”} is what you’ll probably most often see.

ClojureDocs - clojure.core/symbol The symbol fn takes a name or a ns name pair and return a new instance of clojure.lang.Symbol .There is no global table of created Symbols like what happens for Keywords instead, so every time you invoke (symbol “blah”) you always get a different (in the Java sense of object reference) symbol instance. If we are talking about equality instead, they just need to have same name and namespace, so: (= (symbol “asdf”) ‘asdf (clojure.lang.Symbol/intern “asdf”)) are all equivalent ways to express equivalent symbols. Symbol main purpose in Clojure is to pair up a name and a namespace and be used to lookup “things” like Vars. You can even invoke them! (‘+ #{‘+ ‘- ‘* ‘/}) will lookup itself in the set.

Life: Nasty, Brutish, and Short I know, couldn’t resist. No clojure here, but this is where you can get with APL on a typical matrix-based problem like the Game of Life. I don’t understand a line, but the explanation is fascinating nonetheless. The article explores APL solution to the Game of Like from the 1970 onward, with the goal of finding the shortest. A golf-programming exercise at the end, but an entertaining one. The final version is 9 tokens.

Comments (View)
May 29, 2014

Clojure Weekly, May 29th, 2014

Welcome to another issue of Clojure Weekly! Here I collect a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

Functional Geekery Episode 9 – William E. Byrd Enjoyed listening to this episode of the Functional Geekery podcast (and all the others by the way). I had the pleasure to attend a couple of pair-talks by Wll Byrd and Daniel Friedman and appreciated their style presenting dense topics with a bit of humour. I’m fascinated by logic programming and what you can achieve declaratively. I think the big jump from FP to logic happens when you stop thinking about input and output as two different things. In logic programming you can very well feed the output of a function (or the output evaluation of a compiler) back as input and generate a set of all the solutions that can produce that output. William also remembers us that despite not conquering the entire world, logic programming has found specific niches where it has no rivals, like embedded query engines like the datalog engine in Datomic.

Session — Erlang, Lisp and Haskell — Medium Session is a REPL on steroids, an all-evaluable Clojure scratchpad that remembers LightTable but also allow for graphics and other fancy output, not just text. It is backed by a Datomic database, so sessions are persistent and stored. It uses some other fancy new tech like Om and designed to be extensible and modular (such as, replace the rendering engine if you need to). One application that comes into mind is creating custom editors for specific DSL, replacing the need of editing configuration files for example.

How Clojure works: a simple namespace : Deep Blue Lambda This is probably the best article so far of the series “clojure internals”. It explains what happens in case of a bare bone new namespace declaration. One of the most interesting facts is that a new namespace always trigger first thing a push of a new class loader into the thread bound table of Vars. Translated it means that from that moment on, all the functions declaration that will follow in the namespace will be loaded by a new instance of DynamicClassLoader custom class loader. I suppose this is a wise decision, since the dynamicity of Clojure will allow me to redefine functions at anytime in that namespace, resulting in a class being thrown away and a new class being generated and loaded. The DynamicClassLoader contains a weak reference cache that makes this process easier. Not sure about all the details yet, but very interesting stuff.

MailOnline/s-metric Quick and hopefully useful self-promotion. This little library implements a couple of easy algorithms for string matching and scoring. The typical use case is to compare a first string to another one and see how visually similar they are. For this the “naive” implementation presented here can be sufficient. But string matching theory is way more complicated than this :) so other interesting algos will be added soon. Clojure here shines for quick implementation of fairly complex recursive behaviour. If you need to push performances to the next level though, it takes some more Java knowledge and pre-processing with useful data structures.

Getting Started with Textmate | Clojure Project | Assembla Linking here another piece of abandoned Clojure history. The Assembla wiki was adopted some point in 2009 and then abandoned to move under the clojure.org umbrella. There is not a lot there, except a few design scratch pad about some of the Clojure features built at that point. The page about the clojure compiler in clojure is a summary about some of the desired features of a a new compiler, nowadays tools.analyzer and friends.

ClojureDocs - clojure.core/take-while take-while is often the answer to the question “how do I cut short a loop” for people coming from a Java background. The lazy way to “cut short” can be implemented with a take-while that stops a sequence the first time the given predicate becomes true. It can be used perfectly fine with the for comprehension (which is just another way to create a lazy sequence) without the need for a “break” construct.

galdolber/clojure-core-java Quite an interesting investigation project from the same author of clojure-objc. This is what happens if you take a very simple Clojure hello world, even just a namespace declaration and AOT compile it. You’ll see a bunch of classes are generated. This project is the correponding Java source for the byte code that Clojure generates. By no mean this should be used, but it gives an impression of what is going on under the hood. You can obtain similar results by decompiling Clojure, but this is already done for you if you need a quick glance.

Comments (View)
May 20, 2014

Clojure Weekly, May 20th, 2014

Welcome to another issue of Clojure Weekly! i collect here a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention in the clojure-sphere. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

xach/linj Let’s add LinJ to the series of the forgotten Lisp2Java experiments that thanks to Github have now found a place to live for the eternity. LinJ is what Clojure was at some very early point in life, a Java source generator. This approach has a lot of practical downsides, above all the double compilation step that kills any REPL-like interactivity. On the good side instead, the fact that what is generated can be easily inspected, something that in Clojure is possible but difficult (through decompilation). LinJ also tried to imitate CL as much as possible, something that Clojure did not, proving that what is important is the Lisp philosophy, not necessarily the list-everywhere syntax. Lot of lessons coming from the past, it’s good to remember.

The Weird and Wonderful Characters of Clojure Ha! Indeed, I struggled to google for some of the un-googeable clojurisms found in this page. Mostly reader macros, but also unquote-quote, splicing, deref and many others are part of the Clojure Reader power tools that make our life easier, but when it comes to search for them it is sometimes a pain, especially if you don’t know/remember their english name.

ClojureDocs - clojure.core/select-keys Just when you were considering rolling your own reduce, here’s a quick helper when you need to consider just some keys of a bigger hash map. Always good to remember what’s available.

clojure - Update the values of multiple keys - Stack Overflow On the other hand (see link above), there is no std lib function to achieve this. Reduce to the rescue! Reduce can be used in a non-reducing way every time a sequence-able collection needs multiple updates at once, for example a map. In this case multiple values need to be sent for update. Reduce allows the partial updates to be gradually accumulated and sent over to the next element in the sequence until the final result.

Why You Should Avoid the Jargon Tail Recursion Nice short summary of the meaning of tail recursion and the danger of using the term in a semi-religious way. First of all the mandatory SICP reference. Looping constructs are described there as a patch for “bogus” compilers unable to optimise iterative recursion (recursion with proper tail call). The article suggests that we should stop using a TCO because of its too many confusing meanings. There is no direct reference to Clojure in this post, but I’ve heard the argument many times that Clojure doesn’t have TCO so it’s not a Good Language (™). Of course having to loop-recur is tedious but as a trade-off is not too bad. There is some discussing about Java 9 to finally support TCO, let’s hope that’s the case.

Loper OS - Thumbs Down for Clojure This is quite a popular post, one that articulates well what are the main Clojure design flaws, although what is defined as design flaw here is a design goal in Clojure: pragmatisms. Unfortunately the argumentation is not accepting the trade-off, dismissing Clojure as “revolting”. As of why this post is still popular these days is pretty clear: it is one of the few completely negative review of Clojure while the rest of the world, maybe with some reservations, is accepting it as innovative and useful. I think we should still read it in a constructive way: can we “patch” the Java compromise? Can we push more of the Lisp purity in Clojure and draw a clear demarcation line between its surface and its internal implementation details? Can we fix the nasty stack traces (that is, translate the Java nature of Clojure back into Lisp when things go wrong?)

Google Groups - comp.lang.lisp Let’s not forget about an important aspect (if not predominant) of Clojure. comp.lang.lisp is the venerable Lisp list, where universals have been discussed at least twice along with all of the most important functional concepts. I think it’s a good idea if you’re serious about Clojure to make some room to learn Lisp and follow the discussions going on here.

Comments (View)
May 14, 2014

Clojure Weekly, May 14th, 2014

Welcome to another issue of Clojure Weekly! Skipped a week, hopefully you didn’t miss it that much :) The following are just a few links, normally 4/5 urls, pointing at articles, docs, screencasts, podcasts and anything else that attracts my attention. I add a small comment so you can decide if you want to look at the whole thing or not. That’s it, enjoy!

gtrak/no.disassemble For all of you uber-geeking with Clojure, this little lein plugin and library will let you dump out at the REPL the decompiled version of the byte code that Clojure is generating on the fly when you create a function (or other Clojure snippets). It needs to plug an agent into the JVM bootstrap sequence to intercept byte code loading related to Clojure on the fly, which should not get in your way unless you are already using another agent (unlikely in dev mode).

Running out of memory when using filter? - Google Groups This is an old thread that contains some wisdom toward the end about why lazy sequences are not a silver bullet. The problem highlighted in this thread is solved now. But while reading I realised I also had the assumption that using lazy sequences prevents out of memory errors “completely”. That “Completely” is not always true, even in case of careful laziness usage. As Rich states in a message they do make working with data bigger than memory transparent but that’s not their only intent. There are also performance considerations involved, like how much to cache during processing. There is always a design tradeoff that can break under specific circumstances (and throw the dreaded out of memory). It is the responsibility of the language implementor (i.e. Rich) to find the good balance between laziness, performances and the typical usage expected for a language, that in the case of Clojure is very likely to be a translation between an imperative-built collection (Java) and the purely functional world. Skip most of the details at the beginning and go where Rich chimes in.

papers-we-love/papers-we-love Oh, this is neat neat idea. Papers we love is a Github repo to collect CS papers and an initiative to organise meet-ups to discuss them (this last bit is seriously more important). Papers are a great source of inspiration. They often are the common ground where academia meets industry. Many ideas in Clojure are coming from academic work and relevant papers are often linked in the mailing list or by Rich at conferences. Have a look at the Clojure and functional programming folders to start with. The related London meet-up already started if you want to join.

Clojure expression benchmarks for Clojure versions up thru 1.6.0 Quite interesting benchmark that Andy Fingerhut put together on this page that was posted to clojure-dev. JDK 8 is not particularly changing the game although in a couple of cases is doing significantly better. Maybe what should be important to look at is what Clojure is doing through versions and being sure the lines don’t tend to go up toward the right. There are a few charts showing degraded performances toward the bottom, presumably related to the new hashing algorithms.

clojure - Is defn thread-safe? - Stack Overflow Here’s another good example that shows how Vars, Namespaces and Interning work together. You might think that the function result that didn’t change inside the vector is a surprising result, but is not. What was added to the vector is the value of the Var my-func not the Var itself. Defn are just def followed by a (fn) function definition. When defn is invoked the following happens: a java class implementing IFn is defined in the current class loader with the body executing the content of the function. A Var instance is also created if not extent already that is pointing at the instance of the function. The Var is added to a table of definitions for the current namespace (interning). All the Vars defined for the a ns can be seen with (ns-interns ns). When the vector is created and passed the Var my-func the Var is dereferenced and the IFn instance is taken. When the Var is defined again, replacing its root binding, the initial function instance is still referenced by the vector. If you really wanted the Var to enter the vector then you should have done [#’my-func]. As of thread safety, the map of all the Vars for a Namespace is kept inside a PersistenArray wrapped by an AtomicReference, so all updates are re-tried until success with compareAndSet.

The Curse of the Excluded Middle - ACM Queue tl;dr no true functional programming is achievable without monads (or similar state wrapping). This essay by Erik Meijer is quite extreme in its conclusions, describing why an hybrid approach cannot work. Erik considers OO languages with added closures doomed to fail because state creeps in unexpected ways: interleaving of operations due to laziness, catch clauses not operating correctly, with examples in C#. I think Clojure demonstrates that an convention based on immutability with a strong API around state is a winner even without enforcing a monadic approach. Too bad the article is not mentioning Clojure.

Comments (View)