Skip to main content

Open Source Consultant, Software Developer and System Administrator

If you are interrested in hiring a consultant for the effective use of open source software on an enterprise grade, take a look around in the About section to see, what I have to offer.

Blog and snippets

Various snippets or code parts I found useful, so I keep them here for reference.

Turning paging into a lazy sequence with Clojure tranducers

When dealing with APIs we are often confronted with some sort of paging to access lists. Yet if we need to access all of the data, it would be nice to have them as a lazy sequence.

This can be done nicely with transducers:

(defn page-seq
  "Flat sequence of items loaded via the given page loading fn"
  [get-page-fn]
  (eduction
   (map get-page-fn)
   (take-while seq)
   cat
   (range)))

Add some tests:

(def pages
  (into [] (partition-all 20 (range 56))))

(defn get-page
  [page]
  (println "Loading page" page)
  (nth pages page ()))

(page-seq get-page)
;Loading page 0
;Loading page 1
;Loading page 2
;Loading page 3
; => (0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 ... 54 55)

Now see, if that is really lazy:

(take 10 (page-seq get-page))
;Loading page 0
;Loading page 1
; => (0 1 2 3 4 5 6 7 8 9)

Well, no. We should only see Loading page 0 and not the one for page 1. The reason for this is chunking. Clojure internally loads itself chunks of 32. So side effects will be problematic. But for just lazily loading the whole lot having some mismatch in the used paging and the chunking is good enough for now.

How to start a Clojure REPL since the Specs Alpha change

With the recent change in Clojure to use dedicated artifacts for Spec in an alpha sub-namespace, it is no longer easy to start a simple REPL just with the regular jar (e.g. java -jar $CLOJURE_JAR clojure.main). So how to start it now?

As of before you are better off to do a lein new somewhere, change the org.clojure/clojure dependency to the version you like and do a lein deps :tree to fetch all that is needed and display what things org.clojure/core now depends on. E.g.

[org.clojure/clojure "1.9.0-alpha17"]
   [org.clojure/core.specs.alpha "0.1.10" :exclusions [[org.clojure/clojure] [org.clojure/spec.alpha]]]
   [org.clojure/spec.alpha "0.1.123" :exclusions [[org.clojure/clojure]]]

All your artifacts end up in ~/.m2/repository. So next you have to build up your classpath to call the REPL. You can either run a lein with-profile uberjar cp in that dummy project to get the class path Leiningen is using (which often can be quite long depending on all the plugins you run. Or you can guess the locations from the versions above to build up the params for -cp for yourself.

M2="$HOME/.m2/repository"
java -cp $M2/org/clojure/clojure/1.9.0-alpha17/clojure-1.9.0-alpha17.jar:$M2/org/clojure/spec.alpha/0.1.123/spec.alpha-0.1.123.jar:$M2/org/clojure/core.specs.alpha/0.1.10/core.specs.alpha-0.1.10.jar:. clojure.main

This is a very basic setup to get a basic REPL going, but paired with rlwrap this is a few times faster to fire up than a lein repl, which makes it perfect to quickly fool around with some ideas.

On the horizon to streamline this, there is https://github.com/clojure/tools.deps.alpha.

Generic Vaadin8 field for converted values

In Vaadin8 the conversion between the model data type and the field is done in the Binder. This prevents automatic mapping for the models as some manual step must be done beforehand. But since writing custom fields now is way more reasonable, here is a first draft for a generic converter field. Just provide the backing Field and an Converter.

https://gist.github.com/christoph-frick/947fec59cfd09f7140933b52102cc041