ClojureDocs

Community-powered documentation and examples for Clojure

Example of the dayclojure.core/map
;; Be careful with `map`, closures, and anonymous functions

(defn typical-closure []
  (let [names (atom [])]
    (fn [arg] (swap! names conj arg) @names)))

;; This works as expected: one function gets created and mapped

(defmacro list-maker [fun args & body]
  `(defn ~fun ~args
     (map (typical-closure) '~body)))

(list-maker foo [x] 1 2 3 4 5)
(foo 100) ; ([1] [1 2] [1 2 3] [1 2 3 4] [1 2 3 4 5])

;; This version recomputes the function for each mappand!

(defmacro list-maker-oops [fun args & body]
  `(defn ~fun ~args
     (map #((typical-closure) %) '~body)))

;; facepalm
(list-maker-oops foo [x] 1 2 3 4 5)
(foo 100) ; ([1] [2] [3] [4] [5])
Clojure/JVMClojureScriptbabashka
Recently Updated
msyds authored an example for clojure.core/line-seq 18 days ago.
charlot-shaw authored an example for clojure.java.io/resource 124 days ago.
RezendeComZ authored an example for clojure.string/starts-with? 150 days ago.
Contribute to ClojureDocs

Give feedback, add examples, or add link related vars.

Learn how to pitch in →
clojure.core, by purpose
Full quick reference →

Sequences76

seq
Returns a seq on the collection. If the…
sequence
Coerces coll to a (possibly empty)…
eduction
Returns a reducible/iterable…
repeat
Returns a lazy (infinite!, or length n…
replicate
DEPRECATED: Use 'repeat' instead. …
range
Returns a lazy seq of nums from start…
repeatedly
Takes a function of no args, presumably…
iterate
Returns a lazy (infinite!) sequence of…
lazy-seq
Takes a body of expressions that…
lazy-cat
Expands to code which yields a lazy…

Maps26

hash-map
keyval => key val Returns a new hash…
array-map
Constructs an array-map. If any keys…
zipmap
Returns a map with the keys mapped to…
sorted-map
keyval => key val Returns a new…
sorted-map-by
keyval => key val Returns a new…
bean
Takes a Java object and returns a…
frequencies
Returns a map from distinct items in…
assoc
assoc[iate]. When applied to a map,…
assoc-in
Associates a value in a nested…
dissoc
dissoc[iate]. Returns a new map of the…

Vectors10

vec
Creates a new vector containing the…
vector
Creates a new vector containing the…
vector-of
Creates a new vector of a single…
conj
conj[oin]. Returns a new collection…
peek
For a list or queue, same as first, for…
pop
For a list or queue, returns a new…
get
Returns the value mapped to key,…
assoc
assoc[iate]. When applied to a map,…
subvec
Returns a persistent vector of the…
rseq
Returns, in constant time, a seq of the…

Sets7

hash-set
Returns a new hash set with supplied…
set
Returns a set of the distinct elements…
sorted-set
Returns a new sorted set with supplied…
sorted-set-by
Returns a new sorted set with supplied…
conj
conj[oin]. Returns a new collection…
disj
disj[oin]. Returns a new set of the…
get
Returns the value mapped to key,…

clojure.string

Clojure String utilities It is poor form to (:use clojure.string).

splitjoinreplacetrimblank?

clojure.set

Set operations such as union/intersection.

uniondifferenceindexrename-keys

clojure.walk

This file defines a generic tree walker for Clojure data structures.

postwalkprewalkkeywordize-keys

clojure.java.io

This file defines polymorphic I/O utility functions for Clojure.

readerwriterfileresource

clojure.test

A unit testing framework.

deftestistestingare