Liking cljdoc? Tell your friends :D

consistent-lib

CI cljdoc badge Clojars Project

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 ->. The consistent-lib.ocd namespace goes one step further and puts the functions that were already collection-first (conj, assoc, get, ...) under the same c/ alias, so you never have to remember which functions needed wrapping.

Motivation

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, and consistent-lib.ocd also re-exports the fns that already did, so that every call in a pipeline can start with c/.

This library was inspired by the discussion in the Clojure Google Group about function argument ordering consistency.

Installation

deps.edn:

io.github.durantschoon/consistent-lib {:mvn/version "0.3.0"}

Leiningen / Boot:

[io.github.durantschoon/consistent-lib "0.3.0"]

Requires Clojure 1.10.3+ (CI tests 1.10.3 and 1.12.3). The only runtime dependency is Clojure itself.

Usage

Require the namespace under an alias:

(ns my.app
  (:require [consistent-lib.ocd :as c]))

consistent-lib.ocd puts every collection function under the c/ prefix, so you never have to remember which ones needed wrapping: c/map and c/take are the coll-first wrappers, and c/conj, c/assoc and c/get are clojure.core's own fns, already coll-first, re-exported unchanged.

(-> {:a 0}
    (c/update :a inc)
    (c/assoc :b 2)
    c/vals
    (c/map str))
;; => ("1" "2")

If you want only the functions whose argument order changed, require consistent-lib.core instead; it is a strict subset of consistent-lib.ocd. Pick one of the two as your alias, not both. Every example below works with either, except the ones that use a c/ name only ocd has (c/conj, ...).

Do not use (:require [consistent-lib.ocd :refer :all]) (or the same with consistent-lib.core): 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)

Thread-first examples

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)).

Mistakes and error messages

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.

Available functions

All 37 public vars of consistent-lib.core (which consistent-lib.ocd re-exports), 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. Use them from clojure.core as usual, or, if you require consistent-lib.ocd, as c/concat, c/conj and so on (see What is not wrapped).

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)

Transducers

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]

What is not wrapped

  • 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.
    • apply: a function-calling primitive, not a sequence function; its trailing seq is an argument list rather than a primary collection, and (c/reduce xs max) or (c/mapcat xss identity) already cover the pipeline tails (apply max xs) and (apply concat xss) serve.
    • 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.ocd aliases the rest. It adds a c/ alias for each clojure.core function that already takes its primary collection first (conj, assoc, update, get, get-in, count, first, concat, into, ... 49 in all, so 86 vars with the 37 wrappers). An alias is the clojure.core fn itself, so it has core's behavior and docstring. ocd leaves out the exclusions above that have no clear primary collection or are not portable (merge-with, cons, tree-seq, into-array, apply, pmap, random-sample, doall, dorun).

ClojureScript

consistent-lib.core and consistent-lib.ocd are .cljc namespaces; in ClojureScript the wrappers delegate to cljs.core, and each ocd alias is the cljs.core fn of the same name. 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:

  • Docstrings name 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.

clj-kondo

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.

Development

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)

Releasing

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.3.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 account name (the name in your clojars.org/users/<name> URL), which need not match your GitHub user name
  • CLOJARS_PASSWORD: a Clojars deploy token from https://clojars.org/tokens, never your account password

First deploy to a new group. The group io.github.<github-user> must be verified once at https://clojars.org/verify/group, which needs an empty public repository github.com/<github-user>/clojars-<clojars-user>. An unverified group, like a non-token password, fails with HTTP 401 "No deploy token provided". A token scoped to the group can be created only after the group exists.

The pom's SCM tag is v<version>; tag and push that commit before deploying so cljdoc can find the sources:

git tag v0.3.0 && git push origin v0.3.0
CLOJARS_USERNAME=... CLOJARS_PASSWORD=... clojure -T:build deploy

Acknowledgements

Built with AI assistance. Agents used in creating this version of the repo: Claude Opus 5, Claude Opus 5.5, Claude Fable 5 (Anthropic).

License

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

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close