Archive for the 'Clojure' Category

Order of Operations in comp

Monday, February 24th, 2020

I’m digging back into Transducers in Clojure. This time, I think I might actually understand them.

One thing you have to get used to is replacing your reliance on the threading macros -> and ->> with the (comp) function.

TL;DR — (comp) runs in the same order as threading.

(sequence 
  (comp
    (map #(str % "a"))
    (map #(str % "b")))
  ["0", "1", "2"])

=> ("0ab", "1ab", "2ab")

The same applies when you (comp) a (comp); the order follows.

(let [p1 (comp (map #(str % "a"))  
               (map #(str % "b")))
      p2 (comp (map #(str % "y"))
               (map #(str % "z")))]
  (sequence (comp p1 p2) 
            ["0" "1" "2"]))

=> ("0abyz", "1abyz", "2abyz")

Clojure: Inverting a nested hash map

Friday, October 26th, 2018

We all know and love (assoc-in a kv v)

How can we take an existing map and invert it to the assoc-in commands that would create it?

I’ve been struggling with this problem for far too long. I finally found a nice solution by Jay Fields

Datomic: Relationships and refs within a transaction

Wednesday, April 12th, 2017

Not being able to figure this out is what killed all my previous attempts to use Datomic.

You want to transact two Entities that have a relationship between them. You’ve tried all the different variations that Google shows you, but they all give you :db.error/not-an-entity

What you want is the TWO ARGUMENT version of #db/id

Here, Verna gets a Temporary ID of -1 in this transaction. We can then reference that same Temporary ID later under :mother

This of course assumes that you have a Schema that defines :mother as a Datomic Reference, such as:

Also note that #db/id is simply a Tagged Element (similar to a reader macro), not an inline function or symbol reference or anything else.

Notes on Component (Part 1)

Tuesday, April 11th, 2017

Stuart Sierra’s Component library for Clojure took awhile for me to really understand. Hence these notes.

(more…)

Luminus directories: src/clj, src/cljs, src/cljc

Saturday, January 28th, 2017

When you use the lein plugin luminus to create a web application based on the Luminus Web Stack, it creates three directories under src: clj, cljs, and cljc.

What are these for?
(more…)