inc
increments,dec
decrementstype
int
short
byte
bigint
- arithmetic functions like
+
,-
,*
,/
=
and==
=
will be more accurate so(= 3 3.0)
will returnfalse
(since floats are approximations)==
considers the integer's equivalent in floating point form, so(== 3 3.0)
will returntrue
- It can be applied to more than two numbers
(= 2 2 2)
- other comparison operators like
<
,>
,<=
,>=
str
turns almost anything into a string- It can also be used to concatenate strings together:
(str "hello " 3 " world" )
returnshello 3 world
.
- It can also be used to concatenate strings together:
re-find
finds the result of a regex in a string
user=> (re-find #"cat" "mystic cat mouse")
"cat"
user=> (re-find #"cat" "only dogs here")
nil
re-matches
will extractboolean
to return whether a value is truthy or notand
returns the first falsy value, or the last value if they are all truthyor
returns the first positive valuenot
inverts the logical sense of a valueconj
prepends a list with a new member
user=> (conj '(1 2 3) 4)
(4 1 2 3)
but also appends an element to a vector or add to a set (they are unordered anyway).
list
to construct... listsfirst
,last
andnth
return the element at the nth indexvec
turns other data structures into listsrest
returns everything but the first element,()
if there are no more elementnext
returns everything but the first element,nil
if there are no more elementcount
will tell us how big a vector issort
will sort a set in ascending ordersort-by
takes a key function to order the elementsdisj
will remove any element from a setcontains?
to check if an element is in a setset
to create a set from any collectionget
retrieves an element in a map
(get {:name "maceo" :age 14} :name) ; "maceo"
(get {:name "maceo" :age 14} :name :weight) ; ":weight" this is a default element
assoc
lets you add a key and a value to a mapmerge
will merge mapsdissoc
will remove a value from a maplet
to declare a symbol locally (within its expression)def
to declare a mutable variablefn
to declare a functiondefn
to declare a named functiondoc
to access the docstring of a functionmeta
to access the metadata of a functionsupers
returns a set of all the types that include a specific type
user=> (supers (type type))
#{clojure.lang.AFunction clojure.lang.IMeta java.util.concurrent.Callable clojure.lang.Fn clojure.lang.AFn java.util.Comparator java.lang.Object clojure.lang.RestFn clojure.lang.IObj java.lang.Runnable java.io.Serializable clojure.lang.IFn}
fn?
returns whether the argument is a function or notsource
retrieves the source code of a functionempty?
returns true if a collection is emptycons
will return a list made of the first argument, and all the elements in the second argumentif
will take a condition, then an expression to evaluate if it's true, and one if it isn'tkeyword
takes a string and returns a keywordmap
takes a function and a sequence, it applies the the function to every member of the sequence. It can also take more sequences, and will take an member from each sequence each time and pass it to the function (which should take these arguments). If one sequence is shorter than others,map
will stop at the last element of that sequence.map-indexed
passes the index first and the element of the sequence to the function it is passedpos?
returns true if a number is positiveiterate
takes a function and a starting number and will run that function indefinitely, each time taking the result of the previous function call- You can use
take
to limit the number of times the function is run. A good example would be(take 10 (iterate inc 0))
repeat
returns a sequence where every element is the same, it just takes that member to returnrepeatedly
takes a function and returns a sequence of the result of these callsrand
generates a number between 0 and 1rand-int
will also take an integer and generate a random number between 0 and that integer minus one.range
generates sequences of numbers between two points. With one argument (a number) it generates the sequences between 0 and that number. With two numbers as arguments, it will be between these two numbers. The last argument (optional) is the step.cycle
extends a sequence by repeating it forever. Here as well you can limit it withtake
concat
concatenate sequences togetherinterleave
will combine two sequences together, one element of each at a timeinterpose
will add an element between every member of the sequence it is passed (member first argument, sequence second)reverse
will reverse sequencesseq
breaks a sequence of charactersapply
andstr
together can rebuild a string from a sequence of charactershuffle
randomizes the members of a sequencedrop
removes the first n elements of a sequencetake-last
keeps the n elements at the end of the sequencedrop-last
removes the last n elements of a sequencetake-while
keeps the elements until it doesn't satisfy a condition anymore.(take-while pos? [3 2 1 0 -1 -2 10])
will return(3 2 1)
drop-while
is the same as above withdrop
split-at
will split a sequence at an indexsplit-with
will split where the function passed is no longer satisfiedfilter
takes a condition likepos?
(a function) and sequence, and filters depending on that condition (if it returns a truthy value)remove
is the opposite offilter
partition
will take a number n and a sequence and return sequence grouped by n elements togetherpartition-by
takes a function and a sequence and will group depending on whether the function returns a truthy value or notpartition-all
will partition it all, maybe returning at shorter sequence at the endfrequencies
will count how many times an element appears in a sequence.(frequencies [:meow :mrrrow :meow :meow])
will return{:meow 3, :mrrrow 1}
pprint
pretty print an objectgroup-by
group sequences by a function, for instance
user=> (pprint (group-by :first [{:first "Li" :last "Zhou"}
{:first "Sarah" :last "Lee"}
{:first "Sarah" :last "Dunn"}
{:first "Li" :last "O'Toole"}])){"Li" [{:last "Zhou", :first "Li"} {:last "O'Toole", :first "Li"}],
"Sarah" [{:last "Lee", :first "Sarah"} {:last "Dunn", :first "Sarah"}]}
Note here that the keyword is used as a function
reduce
takes a function and will run it on the first two elements (unless you pass a starting value) and then run again on the result of that function and the next value.reductions
does the same but returns a sequence will all the stepsinto
reduces elements into a collection
user=> (into {} [[:a 2] [:b 3]])
{:a 2, :b 3}
user=> (into (list) [1 2 3 4])
(4 3 2 1)
Adding an element to a list appears at the end, so it reverses the list
reduced
lets you break out of reduce earlylazy-seq
defers the execution of what is in the expression to when it is neededrealized?
lets you figure out if a function is realized or notdo
evaluate the expressions in order, and returns the value of the lastprn
prints to the consoledotime
will pass a parameter i to a function a n number of timesmerge
merges map, takes the value of the last map if there's a collisionmerge-with
merges maps with a functionkey
,val
extracts the key and value of a map's elementpartial
takes a function and arguments, returns a new function which calls the original function, with the original arguments, followed by the new arguments passed to the new functioncond
is like cases in JavaScript, it's a function returning a boolean first, then what we actually return, as many times as we want.contains?
checks if a key exists in a map, will be index if it's a vector or a list:(contains? {:a 1} :a)
=> true, and(contains? '(4 5 6) 1)
=> trueget-in
is likeget
for a map but will work for nested maps toohash-set
creates a set with its argumentsclojure.string/replace
takes a string, another one (prepended by#
-- because it's a regex) to be replaced by a last one.time
adds a log line with the time it took for the expression to run, and then returns the rest of the enclosed expressionsapply
explodes a sequence passed as argument and transforms it into rest arguments, like so(apply max [0 1 2])
=>2
complement
comp
takes function and returns a new anonymous function that is the function composition of them.((comp inc *) 2 3)
memoize
saves the arguments and the return of a function, so when you evaluate the function with the same arguments again the result is returned immediatelyassoc-in
lets you add to nested maps, such as(assoc-in {} [:cookie :monster :vocals] "Finntroll"); => {:cookie {:monster {:vocals "Finntroll"}}}
if-let
makes is so that if an expression is truthy, you bind that name to the result of that expression.doseq
is used when you want to perform side-effect operations. The first element is a vector that tells you how to attribute each element.ns-name
returns the name of a namespacens-interns
returns the interned vars andns-map
will return the full namespace maprefer
lets you merge another namespace with the current (well not really merge but the symbols from the other namespace are brought it)alias
lets you shorten a namespace name like so(clojure.core/alias 'taxonomy 'cheese.taxonomy)
zipmap
creates a map from two vectors, the first vector gives the first part (the keyword most likely), and the second vector gives the elements