Skip to content

Instantly share code, notes, and snippets.

@madvas
madvas / partial-right.clj
Created June 13, 2015 12:40
Clojure partial-right (Like a partial, but arguments are added to the end)
(defn partial-right
"Takes a function f and fewer than the normal arguments to f, and
returns a fn that takes a variable number of additional args. When
called, the returned function calls f with additional args + args."
([f] f)
([f arg1]
(fn [& args] (apply f (concat args [arg1]))))
([f arg1 arg2]
(fn [& args] (apply f (concat args [arg1 arg2]))))
([f arg1 arg2 arg3]
@mitranim
mitranim / flat-block-macros.clj
Last active June 7, 2021 11:49
Clojure macros for writing flat blocks with early returns, avoiding the let/if-pyramid
(defmacro return-or [sym expr] {:pre [(symbol? sym)]}
`(if (reduced? ~sym) (unreduced ~sym) ~expr))
(defn imp-block [[expr & tail-exprs]]
(match expr
(('let pattern init) :seq)
(if-not tail-exprs
(list init)
(match (imp-block tail-exprs)