Skip to content

Instantly share code, notes, and snippets.

@dexterous
Created May 6, 2025 20:34
Show Gist options
  • Save dexterous/37b98c7e17e81bbe133b855f0070e830 to your computer and use it in GitHub Desktop.
Save dexterous/37b98c7e17e81bbe133b855f0070e830 to your computer and use it in GitHub Desktop.

Revisions

  1. dexterous created this gist May 6, 2025.
    38 changes: 38 additions & 0 deletions fns-should-be-1-form.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    ;; From https://github.com/ring-clojure/ring/blob/a915a44/ring-core/src/ring/middleware/session/cookie.clj#L29
    (defn- hmac
    "Generates a Base64 HMAC with the supplied key on a string of data."
    [key data]
    (let [mac (Mac/getInstance hmac-algorithm)]
    (.init mac (SecretKeySpec. key hmac-algorithm))
    (codec/base64-encode (.doFinal mac data))))

    ;; I would prefer to write it like this.
    (defn- hmac
    "Generates a Base64 HMAC with the supplied key on a string of data."
    [key data]
    (codec/base64-encode
    (-> (doto (Mac/getInstance hmac-algorithm)
    (.init (SecretKeySpec. key hmac-algorithm)))
    (.doFinal data))))

    ;; Expands as follows
    (pprint (clojure.walk/macroexpand-all
    '(defn- hmac
    "Generates a Base64 HMAC with the supplied key on a string of data."
    [key data]
    (codec/base64-encode
    (-> (doto (Mac/getInstance hmac-algorithm)
    (.init (SecretKeySpec. key hmac-algorithm)))
    (.doFinal data))))))

    (def hmac
    (fn*
    ([key data]
    (codec/base64-encode
    (.
    (let*
    [G__136 (Mac/getInstance hmac-algorithm)]
    (. G__136 init (new SecretKeySpec key hmac-algorithm))
    G__136)
    doFinal
    data)))))