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)
April 29, 2014

Clojure Weekly, April 29th, 2014

Welcome to another issue of Clojure Weekly, my small routine blog contribution to the Clojure sphere! These are just a few links, normally 4/5 urls, pointing at articles, documentation, screencasts, podcasts or 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!

Euroclojure Programme The program for Euroclojure is finalized. Many interesting talks including yours truly speaking just before Rich (argh). Also from Cognitect we’re going to have Stuart Sierra and David Nolen. There is also an impressive number of Londoneers presenting. See you there.

tree-seq - Clojure v1.6 API documentation Tree-seq is a generic depth-first walk function for tree-like structures that returns a lazy sequence of all the nodes in a tree. It takes two fns and a start-root. The two fns are all that is needed to walk any kind of tree. branch? is a function that takes a node and return false when this is a leaf. children is a function that given a node is expecting the list of other nodes down that path. An already implemented use in the std lib is file-seq, which is returning a list of all the nodes when iterating java.io.File types.

Lisp Flavored Erlang LFE (not to be confused with left fold enumerators) is a lisp implemented on top of the Erlang VM. It is not strictly related to Clojure except that I believe they share some of the difficulties in parsing and adapting a Lisp to run on a VM specifically designed for another language (Java or Erlang in this case). LFE is closer to Common Lisp than Clojure (which deliberately is trying to improve Lisp and drastically enhancing its syntax). Trivia: LFE is a lisp-2 (Clojure is a lisp-1). I don’t think LFE achieved the same popularity that Clojure did and my guess is that who wanted to leverage the Erlang VM did that in Erlang directly (that despite the syntax oddities is still a great functional language).

Clojure - Statistics Overview If you ever wonder how much interest Clojure is generating around, here are the clojure.org web stats. This is not definitive answer, it just gives an idea about popularity based on page views and a break down by country, not much more. The average uniques are steadily increasing, now about 3500-5000 per day. There was a huge spike 6th January 2013, wondering what that was.

tonsky/datascript Interesting ClojureScript idea, implementing a Datalog query engine on top of Clojure immutable data structures. It is meant to run in the browser with a lifecycle starting at page load and ending the next page load. It is technically an in-memory database in the browser. When to use? When some widgetry on the page requires some more structured data (thinking about complex questionaries, single page apps processing data and so on). If “searching” through those data requires some constant array loop it could be a nice idea to put datalog on top instead.

Lisp50 Lisp50 was a once in a lifetime event for many (but I should be able to attend Lisp100!) It was a co-conference with OOPSLA 2008 collecting for the first time in many years all the brilliant minds in the Lisp community, including McCarthy (by phone), Guy Steele and many more. This series of 5 article is a write-up of what happened there and is full of wisdom. Rich Hickey was last talking there. We should not forget that Clojure main goal was not just to imitate but to be a better Lisp, an attempt that has been made so many times without success. Previous presenters before Rich created a sort pessimistic atmosphere, looking at Lisp big ideas in the distant past without an opportunity of redemption for the present. You can imagine what happened when Rich introduced Clojure to that mix of bearded Lisp hackers and a few younger nostalgic fellows! Clojure is really the only re-incarnation of Lisp nowadays that is getting traction. Kudos to Rich for solving decades of Lisp problems with simple and pragmatic choices.

jfli / Mailing Lists Another bit of Clojure history. Clojure was announced to the public on October 17th 2007, after a couple of years of stealth development, on the jfli mailing list (another RH’s project). The google groups was created at that time and about one year later in 2008 was already counting 700+ users and Rich was already invited at several important conferences (notably, Lisp50 celebrating 50 years of Lisp). Also about one year later the release 1.0 was very close, with AOT compilation as its main big new feature.

clojure - Put an element to the tail of a collection - Stack Overflow Why there is no such a function in Clojure that always append at the end? The official answer seems to be that you need to know your stuff. For example the implications of modifying a sequence implemented as a list or as a vector. Lately, I started to rely less on vectors and more on lazy seqs. What I like about sequences in general is that they are and stay lazy if you pay attention not to realize them, a nice feature that allows a lot of processing in code before actually processing the sequence for real. But what to use (vector VS seq) it all depends: is the collection going to be accessed sequentially or not? Is it going to be sorted often? And so on.

What does the leading arrow in a name mean in clojure - Stack Overflow Ah, got it. I was wondering from where that arrow was coming from, considering a defrecord could always be created with a post-fixed dot as for Java interop (since what is created is actually a Java class). So the defrecord macro is responsible for creating a function ->name-of-the-record that invokes the constructor of the record. The macro spits out a lot of other magic that is not easy to read, including hashCode and equals implementations and map like behaviour.

Comments (View)