Collection-first versions of clojure.core sequence functions, for Clojure and
ClojureScript. Every function here is a pure rename of the clojure.core function
of the same name: same behavior, with the primary collection moved to argument 1
(any further collections stay after the function, in core's order), so a whole
pipeline threads with ->.
In clojure.core, some functions take the collection first (conj, assoc,
subvec) and the sequence functions take it last (map, filter, partition,
take). Mixing them forces you to switch between -> and ->> mid-pipeline.
consistent-lib wraps the sequence functions so the collection always comes first.
This library was inspired by the discussion in the Clojure Google Group about function argument ordering consistency.
deps.edn:
io.github.durantschoon/consistent-lib {:mvn/version "0.2.0"}
Leiningen / Boot:
[io.github.durantschoon/consistent-lib "0.2.0"]
Requires Clojure 1.10.3+ (CI tests 1.10.3 and 1.12.3). The only runtime dependency is Clojure itself.
Require the namespace under an alias:
(ns my.app
(:require [consistent-lib.core :as c]))
Do not use (:require [consistent-lib.core :refer :all]): almost every name here
shadows a clojure.core function, so a blanket refer replaces map, filter,
reduce, take and the rest with the collection-first versions throughout the
namespace, and the compiler warns about every replaced var. If you :refer
specific names, exclude them from clojure.core with
(:refer-clojure :exclude [...]).
(require '[consistent-lib.core :as c])
;; Instead of (partition 2 [1 2 3 4])
(c/partition [1 2 3 4] 2)
;; => ((1 2) (3 4))
;; Instead of (take-while odd? [1 3 4 5 7])
(c/take-while [1 3 4 5 7] odd?)
;; => (1 3)
;; Instead of (map inc [1 2 3])
(c/map [1 2 3] inc)
;; => (2 3 4)
;; Instead of (reduce + 10 [1 2 3])
(c/reduce [1 2 3] + 10)
;; => 16
Functions that take several collections keep the first collection first, then the function, then the remaining collections:
;; Instead of (map + [1 2 3] [10 20 30])
(c/map [1 2 3] + [10 20 30])
;; => (11 22 33)
;; Instead of (mapcat reverse [[1 2] [3 4]])
(c/mapcat [[1 2] [3 4]] reverse)
;; => (2 1 4 3)
With clojure.core, a pipeline that mixes collection-first and collection-last
functions has to switch threading macros:
(-> {:scores [3 9 4 8 1]}
:scores
(->> (filter even?) ; switch to ->> for the sequence fns
(map inc))
(conj 100)) ; back to -> for conj
;; => (100 5 9)
With consistent-lib, everything threads with ->:
(-> {:scores [3 9 4 8 1]}
:scores
(c/filter even?)
(c/map inc)
(conj 100))
;; => (100 5 9)
More pipelines:
(-> (range 10)
(c/partition 2)
(c/map #(apply + %))
(c/partition-all 3))
;; => ((1 5 9) (13 17))
(-> [0 1 2 3 4 5 6 7 8 9]
(c/drop-while #(< % 3)) ; drop numbers less than 3
(c/take 4) ; take the next 4
(c/partition 2)) ; group in pairs
;; => ((3 4) (5 6))
(-> [1 2 3 4 5 6 7 8 9 10]
(c/partition-by #(< % 4)) ; split where the predicate flips
(c/map #(apply + %)) ; sum each run
(c/sort-by -)) ; largest first
;; => (49 6)
(-> ["apple" "avocado" "banana" "blueberry" "cherry"]
(c/group-by first)
(c/reduce-kv (fn [m k v] (assoc m k (count v))) {}))
;; => {\a 2, \b 2, \c 1}
partial still works inside a pipeline, on the function you pass to a wrapper:
(-> (range 10)
(c/partition 2)
(c/map (partial apply +)) ; curried: same as #(apply + %)
(c/partition-all 3))
;; => ((1 5 9) (13 17))
What does not work is partial on a wrapper itself: (partial c/partition 2) binds
2 to the collection slot, because the collection comes first. To reuse a whole
pipeline as a function, wrap the thread instead:
#(-> % (c/partition 2) (c/map (partial apply +)) (c/partition-all 3)).
Calling a wrapper with clojure.core's argument order does not produce a helpful
error; the collection lands where the wrapper expects a number or a function. In
a REPL (Clojure 1.10.3, JDK 21):
user=> (c/take 3 [1 2 3])
Error printing return value (ClassCastException) at clojure.lang.Numbers/isPos (Numbers.java:121).
class clojure.lang.PersistentVector cannot be cast to class java.lang.Number (clojure.lang.PersistentVector is in unnamed module of loader 'app'; java.lang.Number is in module java.base of loader 'bootstrap')
user=> (c/map inc [1 2 3])
Error printing return value (IllegalArgumentException) at clojure.lang.RT/seqFrom (RT.java:557).
Don't know how to create ISeq from: clojure.core$inc
The mitigation is the clj-kondo hook the library ships (see clj-kondo below). With it imported, clj-kondo reports both mistakes, and wrong-arity calls, before you run them (here the two calls above sit on lines 4 and 5 of a consumer file):
src/demo/core.clj:4:30: error: Expected: seqable collection, received: positive integer.
src/demo/core.clj:4:32: error: Expected: natural integer, received: vector.
src/demo/core.clj:5:28: error: Expected: seqable collection, received: function.
A one-argument call such as (c/map [1 2 3]) is a legal transducer call, so
neither the compiler nor clj-kondo reports it. The mistake surfaces only when the
transducer is applied, with the same error clojure.core's own (map [1 2 3])
gives: (into [] (c/map [1 2 3]) [1 2 3]) throws IndexOutOfBoundsException,
because the vector is called as the mapping function.
All 37 public vars of consistent-lib.core, each wrapping the clojure.core function
of the same name. Each var's docstring is the clojure.core docstring plus a note,
and its :arglists show the wrapper's own (collection-first) argument order.
A function is wrapped only when clojure.core does not already take its primary
collection first; concat, interleave, subvec, conj and the like are not
wrapped, so use them from clojure.core as usual.
Splitting and chunking:
(partition coll n), (partition coll n step), (partition coll n step pad)(partition-all coll n), (partition-all coll n step)(partition-by coll f)(split-at coll idx)(split-with coll pred)(group-by coll f)Taking and dropping:
(take coll n), (drop coll n)(take-nth coll n)(take-while coll pred), (drop-while coll pred)(take-last coll n)(drop-last coll), (drop-last coll n)Transforming:
(map coll f), (map coll f c2), (map coll f c2 c3), (map coll f c2 c3 & colls)(mapv coll f), (mapv coll f c2), (mapv coll f c2 c3), (mapv coll f c2 c3 & colls)(map-indexed coll f)(mapcat coll f & colls)(filter coll pred), (filterv coll pred), (remove coll pred)(keep coll f), (keep-indexed coll f)(replace coll smap)(sort coll), (sort coll comp)(sort-by coll keyfn), (sort-by coll keyfn comp)(interpose coll separator)Reducing:
(reduce coll f), (reduce coll f init)(reductions coll f), (reductions coll f init)(reduce-kv coll f init)(run! coll proc) (for side effects; returns nil)Predicates:
(some coll pred)(every? coll pred), (not-every? coll pred), (not-any? coll pred)Where clojure.core has a one-argument transducer arity, the wrapper has it too,
passed through unchanged: (c/map f) returns the same transducer as
(clojure.core/map f). These sixteen have one: (partition-all n),
(partition-by f), (take n), (drop n), (take-nth n), (take-while pred),
(drop-while pred), (interpose separator), (remove pred), (map f),
(filter pred), (mapcat f), (keep f), (map-indexed f), (keep-indexed f),
(replace smap).
The functions that apply transducers are wrapped collection-first:
(sequence coll), (sequence coll xform), (sequence coll xform & colls)(transduce coll xform f), (transduce coll xform f init)(eduction coll & xforms)So a whole transducer pipeline threads with ->:
;; Instead of (sequence (comp (map inc) (filter even?) (take 3)) (range 10))
(-> (range 10)
(c/sequence (comp (c/map inc) (c/filter even?) (c/take 3))))
;; => (2 4 6)
;; Instead of (transduce (comp (map inc) (filter even?)) + 0 (range 10))
(-> (range 10)
(c/transduce (comp (c/map inc) (c/filter even?)) + 0))
;; => 30
;; Instead of (vec (eduction (map inc) (filter even?) (range 10)))
(-> (range 10) (c/eduction (c/map inc) (c/filter even?)) vec)
;; => [2 4 6 8 10]
Deliberate exclusions. These functions have no wrapper:
pmap: JVM only (cljs.core has no pmap), so a .cljc wrapper would have
nothing to delegate to in ClojureScript.doall, dorun: the usual one-argument form already takes only the
collection.random-sample: its result is random (it filters on rand), so its output
cannot be checked against clojure.core's.partitionv, partitionv-all, splitv-at: added in Clojure 1.12, newer
than the declared 1.10.3 floor.into, zipmap, merge-with, cons, tree-seq, into-array: which
argument is the primary collection is ambiguous (into's to or from,
zipmap's keys or vals, and so on), so the library does not guess.The public API is append-only: wrappers may be added in later releases, and adding one is not a breaking change for code that requires the namespace under an alias.
Only the functions listed above are wrapped. Everything else in clojure.core
is unchanged and still available under its usual name.
consistent-lib.core is a .cljc namespace; in ClojureScript the wrappers delegate
to cljs.core. The wrappers are generated by a JVM-side macro
(consistent-lib.impl, loaded through :require-macros), so a ClojureScript
consumer needs nothing extra. Known differences:
clojure.core. The macro reads the docstring from the JVM
var, so in ClojureScript a wrapper's docstring is the clojure.core docstring,
not the cljs.core one.reduce over an empty collection with a function lacking a 0-arity. On the
JVM, (c/reduce [] -) throws an ArityException, exactly as (reduce - [])
does; ClojureScript does not check arity, so it returns NaN (and max/min
return nil), exactly as cljs.core/reduce does. Each wrapper matches its own
platform's core.consistent-lib.impl (the macro namespace) is internal: it is not part of the
public API and is outside the append-only promise (public vars of
consistent-lib.core are never renamed or removed), so it may change in any release.
The library ships a clj-kondo config in its jar. Import it, then analyze your
dependencies with it active (two passes). The import needs a .clj-kondo/
directory in your project: without one, the first pass prints "No configs copied
because config dir (.clj-kondo) does not exist.", exits 0 and imports nothing, so
create it first:
mkdir -p .clj-kondo
clj-kondo --lint "$(clojure -Spath)" --dependencies --copy-configs --skip-lint
clj-kondo --lint "$(clojure -Spath)" --dependencies
clj-kondo --lint src test # your own code: this is where findings appear
The two --dependencies passes only prepare clj-kondo and print nothing. When you
then lint your own sources, the imported hook teaches clj-kondo what each wrapper is,
so a misspelled c/mapp is reported as an unresolved var and a wrong-arity call such
as (c/partition [1 2 3]) is reported as an arity error. Lein users substitute lein classpath for
clojure -Spath.
The gates (also run by CI on every push and pull request to main, see
.github/workflows/ci.yml):
clojure -X:test # JVM tests, including generative equivalence tests
clj-kondo --lint src test # static analysis, 0 errors and 0 warnings expected
clojure -M:cljs-test # ClojureScript tests on node (needs node on PATH)
CI also runs cljfmt check as an advisory (non-blocking) step, and runs the JVM
tests on both the declared floor (Clojure 1.10.3) and Clojure 1.12.3.
The wrapper macro's arity-coverage check (every clojure.core arity is wrapped) runs
only in this repo's :test and :cljs-test aliases, which set
-Dconsistent-lib.check-arities=true; it is off in consumers' builds, so a newer
Clojure cannot break them. A REPL started without one of those aliases does not check
arities.
After editing src/consistent_lib/impl.clj (the wrapper macro), delete the
ClojureScript build output before running the cljs tests; the runner does not notice
a changed macro and would test the stale build:
rm -rf target/cljs-test-runner-out && clojure -M:cljs-test
To run the tests from a REPL started with clojure -M:test:
(require 'clojure.test 'consistent-lib.core-test)
(clojure.test/run-tests 'consistent-lib.core-test)
Build tooling is in build.clj (tools.build, :build alias). The version lives in
build.clj (version); update CHANGELOG.md and the install snippets above with it.
clojure -T:build jar # target/consistent-lib-0.2.0.jar, pom inside the jar
clojure -T:build install # jar, then install into the local Maven repository
clojure -T:build deploy # jar, then deploy to Clojars
deploy needs two environment variables:
CLOJARS_USERNAME: your Clojars user nameCLOJARS_PASSWORD: a Clojars deploy token (not your account password)The pom's SCM tag is v<version>; tag and push that commit before deploying so
cljdoc can find the sources:
git tag v0.2.0 && git push origin v0.2.0
CLOJARS_USERNAME=... CLOJARS_PASSWORD=... clojure -T:build deploy
Built with AI assistance. Agents used in creating this version of the repo: Claude Opus 5, Claude Opus 5.5, Claude Fable 5 (Anthropic).
Copyright © 2024-2026 Durant Schoon
Distributed under the Eclipse Public License version 2.0 (see LICENSE).
Can you improve this documentation?Edit on GitHub
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |