incincrements,decdecrementstypeintshortbytebigint- 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
<,>,<=,>= strturns 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-findfinds the result of a regex in a string
user=> (re-find #"cat" "mystic cat mouse")
"cat"
user=> (re-find #"cat" "only dogs here")
nilre-matcheswill extractbooleanto return whether a value is truthy or notandreturns the first falsy value, or the last value if they are all truthyorreturns the first positive valuenotinverts the logical sense of a valueconjprepends 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).
listto construct... listsfirst,lastandnthreturn the element at the nth indexvecturns other data structures into listsrestreturns everything but the first element,()if there are no more elementnextreturns everything but the first element,nilif there are no more elementcountwill tell us how big a vector issortwill sort a set in ascending ordersort-bytakes a key function to order the elementsdisjwill remove any element from a setcontains?to check if an element is in a setsetto create a set from any collectiongetretrieves an element in a map
(get {:name "maceo" :age 14} :name) ; "maceo"
(get {:name "maceo" :age 14} :name :weight) ; ":weight" this is a default elementassoclets you add a key and a value to a mapmergewill merge mapsdissocwill remove a value from a mapletto declare a symbol locally (within its expression)defto declare a mutable variablefnto declare a functiondefnto declare a named functiondocto access the docstring of a functionmetato access the metadata of a functionsupersreturns 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 notsourceretrieves the source code of a functionempty?returns true if a collection is emptyconswill return a list made of the first argument, and all the elements in the second argumentifwill take a condition, then an expression to evaluate if it's true, and one if it isn'tkeywordtakes a string and returns a keywordmaptakes 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,mapwill stop at the last element of that sequence.map-indexedpasses the index first and the element of the sequence to the function it is passedpos?returns true if a number is positiveiteratetakes 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
taketo limit the number of times the function is run. A good example would be(take 10 (iterate inc 0)) repeatreturns a sequence where every element is the same, it just takes that member to returnrepeatedlytakes a function and returns a sequence of the result of these callsrandgenerates a number between 0 and 1rand-intwill also take an integer and generate a random number between 0 and that integer minus one.rangegenerates 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.cycleextends a sequence by repeating it forever. Here as well you can limit it withtakeconcatconcatenate sequences togetherinterleavewill combine two sequences together, one element of each at a timeinterposewill add an element between every member of the sequence it is passed (member first argument, sequence second)reversewill reverse sequencesseqbreaks a sequence of charactersapplyandstrtogether can rebuild a string from a sequence of charactershufflerandomizes the members of a sequencedropremoves the first n elements of a sequencetake-lastkeeps the n elements at the end of the sequencedrop-lastremoves the last n elements of a sequencetake-whilekeeps 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-whileis the same as above withdropsplit-atwill split a sequence at an indexsplit-withwill split where the function passed is no longer satisfiedfiltertakes a condition likepos?(a function) and sequence, and filters depending on that condition (if it returns a truthy value)removeis the opposite offilterpartitionwill take a number n and a sequence and return sequence grouped by n elements togetherpartition-bytakes a function and a sequence and will group depending on whether the function returns a truthy value or notpartition-allwill partition it all, maybe returning at shorter sequence at the endfrequencieswill count how many times an element appears in a sequence.(frequencies [:meow :mrrrow :meow :meow])will return{:meow 3, :mrrrow 1}pprintpretty print an objectgroup-bygroup 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
reducetakes 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.reductionsdoes the same but returns a sequence will all the stepsintoreduces 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
reducedlets you break out of reduce earlylazy-seqdefers the execution of what is in the expression to when it is neededrealized?lets you figure out if a function is realized or notdoevaluate the expressions in order, and returns the value of the lastprnprints to the consoledotimewill pass a parameter i to a function a n number of timesmergemerges map, takes the value of the last map if there's a collisionmerge-withmerges maps with a functionkey,valextracts the key and value of a map's elementpartialtakes 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 functioncondis 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-inis likegetfor a map but will work for nested maps toohash-setcreates a set with its argumentsclojure.string/replacetakes a string, another one (prepended by#-- because it's a regex) to be replaced by a last one.timeadds a log line with the time it took for the expression to run, and then returns the rest of the enclosed expressionsapplyexplodes a sequence passed as argument and transforms it into rest arguments, like so(apply max [0 1 2])=>2complementcomptakes function and returns a new anonymous function that is the function composition of them.((comp inc *) 2 3)memoizesaves the arguments and the return of a function, so when you evaluate the function with the same arguments again the result is returned immediatelyassoc-inlets you add to nested maps, such as(assoc-in {} [:cookie :monster :vocals] "Finntroll"); => {:cookie {:monster {:vocals "Finntroll"}}}if-letmakes is so that if an expression is truthy, you bind that name to the result of that expression.doseqis used when you want to perform side-effect operations. The first element is a vector that tells you how to attribute each element.ns-namereturns the name of a namespacens-internsreturns the interned vars andns-mapwill return the full namespace mapreferlets you merge another namespace with the current (well not really merge but the symbols from the other namespace are brought it)aliaslets you shorten a namespace name like so(clojure.core/alias 'taxonomy 'cheese.taxonomy)zipmapcreates a map from two vectors, the first vector gives the first part (the keyword most likely), and the second vector gives the elements