Show your fun one-liners in Clojure

Came up with interesting, cute, sexy, cool, neat, or elegant one-liners lately?

Let’s have some fun :slight_smile:

Explain it if you want, it might be educational or inspirational for others.

I’ll start:

 (map (apply juxt columns) list-of-maps) 


  (def list-of-maps
    [{:a 1 :b 2}
     {:a 3 :b 4}
     {:a 5 :b 6}
     {:a 7 :b 8}])

  (def columns
    [:a :b])

  (map (apply juxt columns) list-of-maps) ;;=> ([1 2] [3 4] [5 6] [7 8]) 

Converts a list of maps to a two-dimensional list, according to keys in columns (ie. output can be fed into for CSV generation)

I like here, how we use juxt applied to a bunch of keywords (keywords -crucially- are functions!), resulting in a function that calls each keyword in columns on the input (a map in this case), effectively doing a map lookup for each key, and returning the result in a vector.


A fun one by Borkdude

$ bb -e '(map (juxt first count) (partition-by identity "aaaabbbcca"))'
;; => ([\a 4] [\b 3] [\c 2] [\a 1])

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.