Welcome to Lits!
Lits is a Lisp dialect made to work well in a browser or Node environment. It's heavily inspired by Clojure, most of the core functions from Clojure have been ported.
Some outstanding features / shortcommings worth mentioning.
All datatypes in Lits are immutable.
All functions are pure , unless the built-in function name ends with a !. See write! or rand! for example.
All datatypes in Lits mapps directly to Javascript's types.
No lazy evaluation.
No quotes.
No macros.
No keyword symbols. :foo is just a shorthand for "foo" .
Dynamic scoping, no lexical scoping
100% test coverage
You can see some examples and find documentation of all built-in function to the left.
For more instruction on how to install and use Lits as a cli or a typescript lib, checkout https://github.com/YouCruit/lits
Happy coding!
Examples
Simple Lisp expression
A super simple example.
Show in playground
Params in use
Simple example using params.
Show in playground
Phone number formatter
Pretty prints a US phone number.
Show in playground
Factorial
A recursive implementation of the factorial function.
Show in playground
Sort
Sort an array of numbers.
Show in playground
Many arities
Function with multiple arities.
Show in playground
Translations lib
A Lits take on i18n.
Show in playground
Is ISO date string
Check if string is formatted as an ISO date string.
Show in playground
label-from-value
Find label to corresponding value in array of {label value}-objects.
Show in playground
labels-from-values
Find labels to corresponding values in array of {label value}-objects.
Show in playground
Returns number of elements in coll .
Syntax
Arguments coll: collection | string
Examples
▶ (count [1 2 3]) => 3
▶ (count []) => 0
▶ (count (object :a 1)) => 1
▶ (count "") => 0
▶ (count "Albert") => 6
Returns value in coll mapped at key .
Syntax
get coll key default(optional) => any
Arguments coll: collection
key: string or integer
default: any
Examples
▶ (get [1 2 3] 1) => 2
▶ (get [] 1) => null
▶ (get [] 1 "default") => "default"
▶ (get (object :a 1) :a) => 1
▶ (get (object :a 1) :b) => null
▶ (get (object :a 1) :b "default") => "default"
▶ (get nil :a) => null
▶ (get nil :b "default") => "default"
Returns the value in a nested collection, where keys is an array of keys. Returns default if the key is not present. If default is not set, nil is returned.
Syntax
get-in coll keys default(optional) => any
Arguments coll: collection
keys: Array
default: any
Examples
▶ (get-in [[1 2 3] [4 {:a "Kalle"} 6]] [1 1 :a 0]) => "K"
▶ (get-in [[1 2 3] [4 {:a "Kalle"} 6]] [1 1 :b 0]) => null
▶ (get-in [[1 2 3] [4 {:a "Kalle"} 6]] [1 1 :b 0] "Lisa") => "Lisa"
Returns true if collection contains key , otherwise returns false .
Syntax
contains? coll key => boolean
Arguments coll: collection
key: string or number
Examples
▶ (contains? [] 1) => false
▶ (contains? [1] 1) => false
▶ (contains? [1 2 3] 1) => true
▶ (contains? {} :a) => false
▶ (contains? {:a 1 :b 2} :a) => true
Returns true if collection has value , otherwise returns false .
Syntax
Arguments coll: collection
key: string or number
Examples
▶ (has? [1 2 3] 1) => true
▶ (has? [1 2 3] 0) => false
▶ (has? {:a 1 :b 2} 1) => true
▶ (has? {:a 1 :b 2} 0) => false
▶ (has? "Albert" :A) => true
▶ (has? "Albert" :a) => false
Returns true if collection has any of the elements in keys , otherwise returns false .
Syntax
has-some? coll keys => boolean
Arguments coll: collection
keys: Seq
Examples
▶ (has-some? [] []) => false
▶ (has-some? [1 2 3] []) => false
▶ (has-some? [1 2 3] [0]) => false
▶ (has-some? [1 2 3] [0 1]) => true
▶ (has-some? (object :a 1 :b 2) [0]) => false
▶ (has-some? (object :a 1 :b 2) [0 1]) => true
▶ (has-some? "Albert" "xyz") => false
▶ (has-some? "Albert" "xyzl") => true
▶ (has-some? [:a :b :c :d] "xyz") => false
▶ (has-some? [:a :b :c :d] "xyzc") => true
Returns true if collection has all of the elements in keys , otherwise returns false .
Syntax
has-every? coll keys => boolean
Arguments coll: collection
keys: Seq
Examples
▶ (has-every? [] []) => true
▶ (has-every? [1 2 3] []) => true
▶ (has-every? [1 2 3] [0 1]) => false
▶ (has-every? [1 2 3] [1 2]) => true
▶ (has-every? (object :a 1 :b 2) [0 1]) => false
▶ (has-every? (object :a 1 :b 2) [1 2]) => true
▶ (has-every? "Albert" "xyz") => false
▶ (has-every? "Albert" "treblA") => true
▶ (has-every? [:a :b :c :d] "xyz") => false
▶ (has-every? [:a :b :c :d] "dcba") => true
Sets value on specified element of coll .
Syntax
assoc coll key value => collection
Arguments coll: collection
key: string or number
value: any
Examples
▶ (assoc [1 2 3] 1 "Two") => [1,"Two",3]
▶ (assoc [1 2 3] 3 "Four") => [1,2,3,"Four"]
▶ (assoc {:a 1 :b 2} :a "One") => {"a":"One","b":2}
▶ (assoc {:a 1 :b 2} :c "Three") => {"a":1,"b":2,"c":"Three"}
Associates a value in a nested Coll, where keys is an array of keys and value is the new value and returns a new nested structure. If any levels do not exist, objects will be created - and the corresponding keys must be of type string.
Syntax
assoc-in coll keys value => collection
Arguments coll: collection
keys: Array
value: any
Examples
▶ (assoc-in {} [:a :b :c] "Albert") => {"a":{"b":{"c":"Albert"}}}
▶ (assoc-in [1 2 [1 2 3]] [2 1] "Albert") => [1,2,[1,"Albert",3]]
▶ (assoc-in [1 2 {"name" "albert"}] [2 "name" 0] :A) => [1,2,{"name":"Albert"}]
Concatenates array or object arguments into one collection.
Syntax
concat value(zero or more) => collection
Arguments
Examples
▶ (concat [1 2] [3 4]) => [1,2,3,4]
▶ (concat [] [3 4]) => [3,4]
▶ (concat [1 2] []) => [1,2]
▶ (concat [1 2] [3 4] [5 6]) => [1,2,3,4,5,6]
▶ (concat []) => []
▶ (concat {:a 1 :b 2} {:b 1 :c 2}) => {"a":1,"b":1,"c":2}
▶ (concat {} {:a 1}) => {"a":1}
Returns null if coll is empty, otherwise coll .
Syntax
not-empty coll => boolean
Arguments coll: collection or string
Examples
▶ (not-empty []) => null
▶ (not-empty [1 2 3]) => [1,2,3]
▶ (not-empty {}) => null
▶ (not-empty {:a 2}) => {"a":2}
▶ (not-empty "") => null
▶ (not-empty "Albert") => "Albert"
Returns true if all entries in coll pass the test implemented by finder , otherwise returns false.
Syntax
every? coll finder => boolean
Arguments coll: Coll
finder: function
Examples
▶ (every? string? ["Albert" "Mojir" 160 [1 2]]) => false
▶ (every? (fn [x] (> x 10)) [50 100 150 200]) => true
▶ (every? number? []) => true
▶ (every? number? "") => true
▶ (every? number? {}) => true
▶ (every? #(even? (second %1)) {:a 2 :b 4}) => true
▶ (every? #(even? (second %1)) {:a 2 :b 3}) => false
Returns true if all entries in coll pass the test implemented by finder , otherwise returns false.
Syntax
not-every? coll finder => boolean
Arguments coll: Coll
finder: function
Examples
▶ (not-every? string? ["Albert" "Mojir" 160 [1 2]]) => true
▶ (not-every? (fn [x] (> x 10)) [50 100 150 200]) => false
▶ (not-every? number? []) => false
▶ (not-every? number? "") => false
▶ (not-every? number? {}) => false
▶ (not-every? #(even? (second %1)) {:a 2 :b 4}) => false
▶ (not-every? #(even? (second %1)) {:a 2 :b 3}) => true
Returns true if any entry in coll pass the test implemented by finder , otherwise returns false.
Syntax
any? coll finder => boolean
Arguments coll: Coll
finder: function
Examples
▶ (any? string? ["Albert" "Mojir" 160 [1 2]]) => true
▶ (any? (fn [x] (> x 10)) [50 100 150 200]) => true
▶ (any? number? []) => false
▶ (any? number? "") => false
▶ (any? number? {}) => false
▶ (any? #(even? (second %1)) {:a 2 :b 3}) => true
▶ (any? #(even? (second %1)) {:a 1 :b 3}) => false
Returns false if any entry in coll pass the test implemented by finder , otherwise returns true.
Syntax
not-any? coll finder => boolean
Arguments coll: Coll
finder: function
Examples
▶ (not-any? string? ["Albert" "Mojir" 160 [1 2]]) => false
▶ (not-any? (fn [x] (> x 10)) [50 100 150 200]) => false
▶ (not-any? number? []) => true
▶ (not-any? number? "") => true
▶ (not-any? number? {}) => true
▶ (not-any? #(even? (second %1)) {:a 2 :b 3}) => false
▶ (not-any? #(even? (second %1)) {:a 1 :b 3}) => true
Updates a value in collection, where key is a key and fn is a function that will take the old value and any supplied args and return the new value, and returns a new collection. If the key does not exist, nil is passed as the old value.
Syntax
update coll key fn args(zero or more) => Coll
Arguments coll: Coll
key: string | number
fn: function
args: Any
Examples
▶ (def x {:a 1 :b 2}) (update x :a inc) => {"a":2,"b":2}
▶ (def x {:a 1 :b 2}) (update x :c (fn [val] (if (nil? val) 0 (inc val)))) => {"a":1,"b":2,"c":0}
'Updates' a value in coll , where keys is an array of keys (string or number) and fn is a function that will take the old value and any supplied args and return the new value, and returns a new Coll. If any levels do not exist, objects will be created - and the corresponding keys must be of type string.
Syntax
update-in coll keys fn args(zero or more) => Coll
Arguments coll: Coll
keys: Arr
fn: function
args: Any
Examples
▶ (update-in {:a [1 2 3]} [:a 1] (fn [val] (when (nil? val) 0))) => {"a":[1,null,3]}
▶ (update-in {:a [1 "Albert" 3]} [:a 1 0] (fn [val] (if (nil? val) "?" "!"))) => {"a":[1,"!lbert",3]}
Makes new array from values .
Syntax
Arguments
Examples
▶ (array 1 2 3) => [1,2,3]
▶ (array (array nil false true)) => [[null,false,true]]
▶ [] => []
▶ [1 2 3] => [1,2,3]
▶ [[nil false true]] => [[null,false,true]]
▶ [] => []
▶ ([1 2 3] 1) => 2
▶ ([1 2 3 4 5 6 7 8 9] 3) => 4
Create an array with a range of numbers. If only one argument: 0...a , otherwise: a...b . step defaults to 1 .
Syntax
range a length(optional) length(optional) => array
Arguments a: number
length: number
length: number
Examples
▶ (range 4) => [0,1,2,3]
▶ (range 1 4) => [1,2,3]
▶ (range 0.4 4.9) => [0.4,1.4,2.4,3.4,4.4]
▶ (range 0.25 1 0.25) => [0.25,0.5,0.75]
Returns an array with value repeated count times.
Syntax
repeat count value => array
Arguments count: non negative integer
value: any
Examples
▶ (repeat 3 10) => [10,10,10]
▶ (repeat 0 10) => []
▶ (repeat 5 "Albert") => ["Albert","Albert","Albert","Albert","Albert"]
Takes a nested array and return a flat array. If input isn't an array, an empty array is returned.
Syntax
Arguments
Examples
▶ (flatten [1 2 [3 4] 5]) => [1,2,3,4,5]
▶ (flatten [1 2 [3 [4 [5]]] 6]) => [1,2,3,4,5,6]
▶ (flatten 12) => []
Returns the result of applying concat to the result of applying map to mapper and arrays .
Syntax
mapcat mapper arrays(one or many) => Array
Arguments mapper: function
arrays: Array
Examples
▶ (mapcat reverse [[3 2 1 0] [6 5 4] [9 8 7]]) => [0,1,2,3,4,5,6,7,8,9]
▶ (mapcat reverse [[3 2 1 0] [6 [5] 4] [9 8 7]]) => [0,1,2,3,4,[5],6,7,8,9]
▶ (defn foo [n] [(- n 1) n (+ n 1)]) (mapcat foo [1 2 3]) => [0,1,2,1,2,3,2,3,4]
▶ (mapcat #(remove even? %1) [[1 2] [2 2] [2 3]]) => [1,3]
Accesses element index of input . Accessing out-of-bounds indices returns notFound or nil .
Syntax
nth input index notFound(optional) => any
Arguments input: string | array | nil
index: integer
notFound: any
Examples
▶ (nth [1 2 3] 1) => 2
▶ (nth [1 2 3] 3) => null
▶ (nth [1 2 3] -1) => null
▶ (nth [1 2 3] 3 99) => 99
▶ (nth "A string" 1) => " "
▶ (nth "A string" 3) => "t"
▶ (nth "A string" -3) => null
▶ (nth "A string" 30 :X) => "X"
▶ (nth nil 1) => null
▶ (nth nil 1 "Default value") => "Default value"
Pushes values to the end of array .
Syntax
push array values(one or more) => array
Arguments array: array
values: array
Examples
▶ (push [1 2 3] 4) => [1,2,3,4]
▶ (push [1 2 3] 4 5 6) => [1,2,3,4,5,6]
▶ (def l [1 2 3]) (push l 4) l => [1,2,3]
Removes and returns then last item of array . If array is empty, nil is returned.
Syntax
Arguments
Examples
▶ (pop [1 2 3]) => [1,2]
▶ (pop []) => []
Inserts values at the beginning of array .
Syntax
unshift array values(one or more) => array
Arguments array: array
values: array
Examples
▶ (unshift [1 2 3] 4) => [4,1,2,3]
▶ (unshift [1 2 3] 4 5 6) => [4,5,6,1,2,3]
▶ (def l [1 2 3]) (unshift l 4) l => [1,2,3]
Removes and returns the first item of array . If array is empty, nil is returned.
Syntax
shift array value => array
Arguments
Examples
▶ (shift [1 2 3]) => [2,3]
▶ (shift []) => []
Returns a shallow copy of a portion of array into a new array selected from index start (inclusive) to index end (exclusive). If start is not provided it defaults to 0 . If end is not provided, the rest of the array will be copied.
Syntax
slice array start(optional) end(optional) => array
Arguments array: array
start: number
end: number
Examples
▶ (slice [1 2 3 4 5] 2 4) => [3,4]
▶ (slice [1 2 3 4 5] 2) => [3,4,5]
Returns an array of the intermediate values of the reduction (see reduce ) of seq by reducer .
Syntax
reductions array reducer startValue => array
Arguments array: array
reducer: function
startValue: any
Examples
▶ (reductions + [1 2 3]) => [1,3,6]
▶ (reductions + 0 [1 2 3]) => [0,1,3,6]
▶ (reductions + 0 []) => [0]
▶ (reductions (fn [result value] (+ result (if (even? value) value 0))) 0 [1 2 3 4 5 6 7 8 9]) => [0,0,2,2,6,6,12,12,20,20]
Runs reducer function on each element of the seq , passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the seq is a single value.
Syntax
reduce reducer startValue(optional) seq => sequence
Arguments reducer: function
startValue: any
seq: Seq
Examples
▶ (reduce + [1 2 3]) => 6
▶ (reduce + 0 [1 2 3]) => 6
▶ (reduce + 0 []) => 0
▶ (reduce (fn [result value] (+ result (if (even? value) value 0))) 0 [1 2 3 4 5 6 7 8 9]) => 20
Runs reducer function on each element of the seq (starting from the last item), passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the seq is a single value.
Syntax
reduce-right reducer startValue seq => sequence
Arguments reducer: function
startValue: any
seq: Seq
Examples
▶ (reduce-right str [:A :B :C] "") => ["A","B","C"]
Creates a new array populated with the results of calling mapper on every elements in the calling sequence s.
Syntax
map mapper sequence(one or many) => array
Arguments mapper: function
sequence: Seq
Examples
▶ (map str ["Albert" "Mojir" 42]) => ["Albert","Mojir","42"]
▶ (map str []) => []
▶ (map + [1 2 3] [1 2 3]) => [2,4,6]
▶ (map max [2 6 3] [2 4 7] [1 6 2]) => [2,6,7]
Creates a new array with all elements that pass the test implemented by filter .
Syntax
filter array filter => array
Arguments array: array
filter: function
Examples
▶ (filter string? ["Albert" "Mojir" 160 [1 2]]) => ["Albert","Mojir"]
▶ (filter (fn [x] (> x 10)) [5 10 15 20]) => [15,20]
Returns the index of the first elements that pass the test implemented by finder . If no element was found, nil is returned.
Syntax
position array finder => number
Arguments array: array
finder: function
Examples
▶ (position string? ["Albert" "Mojir" 160 [1 2]]) => 0
▶ (position (fn [x] (> x 10)) [5 10 15 20]) => 2
▶ (position (fn [x] (> x 100)) [5 10 15 20]) => null
Returns the index of value in array. If element is not present in array nil is returned.
Syntax
index-of array value => number
Arguments
Examples
▶ (index-of ["Albert" "Mojir" 160 [1 2]] "Mojir") => 1
▶ (index-of [5 10 15 20] 15) => 2
▶ (index-of [5 10 15 20] 1) => null
Returns the first elements that pass the test implemented by finder . I no element was found, nil is returned.
Syntax
some array finder => array
Arguments array: array
finder: function
Examples
▶ (some string? ["Albert" "Mojir" 160 [1 2]]) => "Albert"
▶ (some (fn [x] (> x 10)) [5 10 15 20]) => 15
▶ (some (fn [x] (> x 10)) [1 2 3 4]) => null
▶ (some (fn [x] (> x 10)) []) => null
If input is an array, creates a new array with the elements from input in reversed order. If input is a string, returns new reversed string.
Syntax
reverse input => array or string
Arguments
Examples
▶ (reverse ["Albert" "Mojir" 160 [1 2]]) => [[1,2],160,"Mojir","Albert"]
▶ (reverse []) => []
▶ (reverse "Albert") => "treblA"
Returns the first element of array . If array is empty, nil is returned.
Syntax
Arguments
Examples
▶ (first ["Albert" "Mojir" 160 [1 2]]) => "Albert"
▶ (first []) => null
Returns the second element of array . If array has less than two elements, nil is returned.
Syntax
Arguments
Examples
▶ (second ["Albert" "Mojir" 160 [1 2]]) => "Mojir"
▶ (second [1]) => null
▶ (second []) => null
Returns the last element of array . If array is empty, nil is returned.
Syntax
Arguments
Examples
▶ (last ["Albert" "Mojir" 160 [1 2]]) => [1,2]
▶ (last [1 2]) => 2
▶ (last [1]) => 1
▶ (last []) => null
If input is an array, returns a new array with all but the first element from input . If input has less than two elements, an empty array is returned. For string input returns all but the first characters in input .
Syntax
rest input => array | string
Arguments
Examples
▶ (rest ["Albert" "Mojir" 160 [1 2]]) => ["Mojir",160,[1,2]]
▶ (rest ["Albert"]) => []
▶ (rest []) => []
▶ (rest "Albert") => "lbert"
▶ (rest :A) => ""
▶ (rest "") => ""
If input is an array, returns a new array with all but the first count elements from input . For string input returns all but the first count characters in input .
Syntax
Arguments
Examples
▶ (nthrest ["Albert" "Mojir" 160 [1 2]] 2) => [160,[1,2]]
▶ (nthrest "Albert" 3) => "ert"
▶ (nthrest "Albert" 6) => ""
▶ (nthrest [] 0) => []
▶ (nthrest "" 0) => ""
If input is an array, returns a new array with all but the first element from input . If input has less than two elements, nil is returned. For string input returns all but the first characters in input . If length of string input is less than two, nil is returned.
Syntax
Arguments
Examples
▶ (next ["Albert" "Mojir" 160 [1 2]]) => ["Mojir",160,[1,2]]
▶ (next ["Albert"]) => null
▶ (next []) => null
▶ (next "Albert") => "lbert"
▶ (next :A) => null
▶ (next "") => null
If input is an array, returns a new array with all but the first count elements from input . If input has less or equal than count elements, nil returned. For string input returns all but the first count characters in input . If length of string input is less or equal than count , nil is returned.
Syntax
Arguments
Examples
▶ (nthnext ["Albert" "Mojir" 160 [1 2]] 2) => [160,[1,2]]
▶ (nthnext "Albert" 3) => "ert"
▶ (nthnext "Albert" 6) => null
▶ (nthnext [] 0) => null
▶ (nthnext "" 0) => null
Constructs a new array with element as first element and rest as the rest.
Syntax
Arguments
Examples
▶ (cons "Hi" ["Albert" "Mojir" 160 [1 2]]) => ["Hi","Albert","Mojir",160,[1,2]]
▶ (cons "Hi" []) => ["Hi"]
Constructs a new array/string with the count first elements from input .
Syntax
take count input => array | string
Arguments count: integer
input: array | string
Examples
▶ (take 3 [1 2 3 4 5]) => [1,2,3]
▶ (take 0 [1 2 3 4 5]) => []
▶ (take 2 "Albert") => "Al"
▶ (take 50 "Albert") => "Albert"
Constructs a new array with the count last elements from array .
Syntax
take-last array count => array
Arguments array: array
count: integer
Examples
▶ (take-last 3 [1 2 3 4 5]) => [3,4,5]
▶ (take-last 0 [1 2 3 4 5]) => []
Returns the members of array in order, stopping before the first one for which predicate returns a falsy value.
Syntax
take-while predicate array => array
Arguments predicate: function
array: array
Examples
▶ (take-while (fn [x] (< x 3)) [1 2 3 2 1]) => [1,2]
▶ (take-while (fn [x] (> x 3)) [1 2 3 2 1]) => []
Constructs a new array/string with the count first elements dropped from input .
Syntax
drop count input => array | string
Arguments count: integer
input: array | string
Examples
▶ (drop 3 [1 2 3 4 5]) => [4,5]
▶ (drop 0 [1 2 3 4 5]) => [1,2,3,4,5]
▶ (drop 2 "Albert") => "bert"
▶ (drop 50 "Albert") => ""
Constructs a new array with the count last elements dropped from array .
Syntax
drop-last count array => array
Arguments count: integer
array: array
Examples
▶ (drop-last 3 [1 2 3 4 5]) => [1,2]
▶ (drop-last 0 [1 2 3 4 5]) => [1,2,3,4,5]
Returns the members of array in order, skipping the fist elements for witch the predicate returns a truethy value.
Syntax
drop-while predicate array => array
Arguments predicate: function
array: array
Examples
▶ (drop-while (fn [x] (< x 3)) [1 2 3 2 1]) => [3,2,1]
▶ (drop-while (fn [x] (> x 3)) [1 2 3 2 1]) => [1,2,3,2,1]
Returns a new Seq with the elements from seq sorted according to comparer . If no comparer is supplied, builtin compare will be used.
Syntax
sort comparer(optional) seq => array
Arguments comparer: function
seq: Seq
Examples
▶ (sort [3 1 2]) => [1,2,3]
▶ (sort (fn [a b] (cond (< a b) -1 (> a b) 1 true -1)) [3 1 2]) => [1,2,3]
▶ (sort (fn [a b] (cond (> a b) -1 (< a b) 1 true -1)) [3 1 2]) => [3,2,1]
Returns a sorted sequence of the items in seq , where the sort order is determined by comparing (keyfn item) . If no comparer is supplied, uses builtin compare .
Syntax
sort-by keyfn comparer(optional) seq => array
Arguments keyfn: function
comparer: function
seq: Seq
Examples
▶ (sort-by count ["Albert" "Mojir" "Nina"]) => ["Nina","Mojir","Albert"]
▶ (sort-by lower-case "Albert") => "Abelrt"
Returns a new string by concatenating all of the elements in array , separated by delimiter .
Syntax
join array delimiter => string
Arguments array: array of strings
delimiter: string
Examples
▶ (join ["Albert" "Mojir"] " ") => "Albert Mojir"
▶ (join (map number-to-string [0 1 2 3 4 5 6 7 8 9]) ", ") => "0, 1, 2, 3, 4, 5, 6, 7, 8, 9"
Returns an array. Each element from array has the probability prob to be included in the result.
Syntax
random-sample! prob(between 0 and 1) array => array
Arguments prob: number
array: array
Examples
▶ (random-sample! 0.5 [1 2 3 4 5 6 7 8 9 10]) => [8]
▶ (random-sample! 0.5 "Albert") => "Albt"
Returns a random element from seq . Returns nil if seq is empty.
Syntax
Arguments
Examples
▶ (rand-nth! [1 2 3 4 5 6 7 8 9 10]) => 4
▶ (rand-nth! "Albert") => "l"
▶ (rand-nth! []) => null
Returns a shuffled copy of input .
Syntax
Arguments
Examples
▶ (shuffle! [1 2 3 4 5 6 7 8 9 10]) => [3,9,10,2,4,1,5,7,6,8]
▶ (shuffle! "Albert Mojir") => "MoitA lbrerj"
▶ (shuffle! [1 2]) => [2,1]
▶ (shuffle! [1]) => [1]
▶ (shuffle! []) => []
Returns a copy of input with no duplicates.
Syntax
Arguments
Examples
▶ (distinct [1 2 3 1 3 5]) => [1,2,3,5]
▶ (distinct "Albert Mojir") => "Albert Moji"
▶ (distinct []) => []
▶ (distinct "") => ""
Returns a new sequence of items in input for witch (pred item) returns a falsy value.
Syntax
Arguments pred: Function
input: Seq
Examples
▶ (remove even? [1 2 3 1 3 5]) => [1,3,1,3,5]
▶ (remove #(has? "aoueiyAOUEIY" %1) "Albert Mojir") => "lbrt Mjr"
Returns a new sequence of all items in input except item at index .
Syntax
remove-at index input => Seq
Arguments
Examples
▶ (remove-at 0 [1 2 3 1 3 5]) => [2,3,1,3,5]
▶ (remove-at -1 [1 2 3 1 3 5]) => [1,2,3,1,3,5]
▶ (remove-at 6 "Albert Mojir") => "AlbertMojir"
Returns a new array/string of [(take pos input) (drop pos input)] .
Syntax
split-at pos input => Seq
Arguments
Examples
▶ (split-at 2 [1 2 3 4 5]) => [[1,2],[3,4,5]]
▶ (split-at 2 "Albert") => ["Al","bert"]
Returns a new array/string of [(take-while pos input) (drop-while pos input)] .
Syntax
split-with pos input => Seq
Arguments
Examples
▶ (split-with #(> %1 3) [1 2 3 4 5]) => [[],[1,2,3,4,5]]
▶ (split-with #(<= %1 :Z) "Albert") => ["A","lbert"]
Returns an object from distinct items in seq to the number of times they appear. Note that all items in seq must be valid object keys i.e. strings.
Syntax
Arguments
Examples
▶ (frequencies ["Albert" "Mojir" "Nina" "Mojir"]) => {"Albert":1,"Mojir":2,"Nina":1}
▶ (frequencies "Pneumonoultramicroscopicsilicovolcanoconiosis") => {"P":1,"n":4,"e":1,"u":2,"m":2,"o":9,"l":3,"t":1,"r":2,"a":2,"i":6,"c":6,"s":4,"p":1,"v":1}
Returns an object of the elements of seq keyed by the result of fn on each element. The value at each key will be an array of the corresponding elements.
Syntax
Arguments
Examples
▶ (group-by "name" [{"name" "Albert"} {"name" "Albert"} {"name" "Mojir"}]) => {"Albert":[{"name":"Albert"},{"name":"Albert"}],"Mojir":[{"name":"Mojir"}]}
▶ (group-by (fn [char] (if (has? "aoueiAOUEI" char) "vowel" "other")) "Albert Mojir") => {"vowel":["A","e","o","i"],"other":["l","b","r","t"," ","M","j","r"]}
Returns an array of sequences of n items each, at offsets step apart. If step is not supplied, defaults to n . If a pad array is supplied, use its elements as necessary to complete last partition upto n items. In case there are not enough padding elements, return a partition with less than n items.
Syntax
partition n step(optional) pad(optional) seq => Seq
Arguments n: number
step: number
pad: Arr
seq: Seq
Examples
▶ (partition 4 (range 20)) => [[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15],[16,17,18,19]]
▶ (partition 4 (range 22)) => [[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15],[16,17,18,19]]
▶ (partition 4 6 (range 20)) => [[0,1,2,3],[6,7,8,9],[12,13,14,15]]
▶ (partition 4 3 (range 20)) => [[0,1,2,3],[3,4,5,6],[6,7,8,9],[9,10,11,12],[12,13,14,15],[15,16,17,18]]
▶ (partition 3 6 [:a] (range 20)) => [[0,1,2],[6,7,8],[12,13,14],[18,19,"a"]]
▶ (partition 4 6 [:a] (range 20)) => [[0,1,2,3],[6,7,8,9],[12,13,14,15],[18,19,"a"]]
▶ (partition 4 6 [:a :b :c :d] (range 20)) => [[0,1,2,3],[6,7,8,9],[12,13,14,15],[18,19,"a","b"]]
▶ (partition 3 1 [:a :b :c :d :e :f]) => [["a","b","c"],["b","c","d"],["c","d","e"],["d","e","f"]]
▶ (partition 10 [1 2 3 4]) => []
▶ (partition 10 10 [1 2 3 4]) => []
▶ (partition 10 10 [] [1 2 3 4]) => [[1,2,3,4]]
▶ (partition 10 10 nil [1 2 3 4]) => [[1,2,3,4]]
▶ (partition 5 "superfragilistic") => ["super","fragi","listi"]
▶ (partition 5 5 nil "superfragilistic") => ["super","fragi","listi","c"]
▶ (def foo [5 6 7 8]) (partition 2 1 foo foo) => [[5,6],[6,7],[7,8],[8,5]]
Returns an array of sequences like partition, but may include partitions with fewer than n items at the end.
Syntax
partition-all n step(optional) seq => Seq
Arguments n: number
step: number
seq: Seq
Examples
▶ (partition-all 4 [0 1 2 3 4 5 6 7 8 9]) => [[0,1,2,3],[4,5,6,7],[8,9]]
▶ (partition 4 [0 1 2 3 4 5 6 7 8 9]) => [[0,1,2,3],[4,5,6,7]]
▶ (partition-all 2 4 [0 1 2 3 4 5 6 7 8 9]) => [[0,1],[4,5],[8,9]]
Applies fn to each value in seq , splitting it each time fn returns a new value. Returns an array of sequences.
Syntax
partition-by fn seq => Seq
Arguments
Examples
▶ (partition-by #(= 3 %1) [1 2 3 4 5]) => [[1,2],[3],[4,5]]
▶ (partition-by odd? [1 1 1 2 2 3 3]) => [[1,1,1],[2,2],[3,3]]
▶ (partition-by identity "Leeeeeerrroyyy") => ["L","eeeeee","rrr","o","yyy"]
Computes sum of numbers .
Syntax
+ numbers(zero or more) => number
Arguments
Examples
▶ (+) => 0
▶ (+ 1) => 1
▶ (+ 2 4) => 6
▶ (+ 1 2 3 4) => 10
▶ (+ (+ 2 3) (+ 5 6)) => 16
Computes difference between first value and sum of the rest. When called with only one argument, it does negation.
Syntax
- numbers(zero or more) => number
Arguments
Examples
▶ (-) => 0
▶ (- 1) => -1
▶ (- 2 4) => -2
▶ (- 4 3 2 1) => -2
Computes product of numbers .
Syntax
* numbers(zero or more) => number
Arguments
Examples
▶ (*) => 1
▶ (* 2) => 2
▶ (* 2 4) => 8
▶ (* 1 2 3 4) => 24
Computes division or reciprocal. When called with one argument it computes reciprocal. When called with two or more arguments it does compute division of the first by the all remaining numbers .
Syntax
/ numbers(zero or more) => number
Arguments
Examples
▶ (/) => 1
▶ (/ 2) => 0.5
▶ (/ 2 4) => 0.5
▶ (/ 4 3 2 1) => 0.6666666666666666
Modulus of dividend and divisor . Truncates toward negative infinity.
Syntax
mod dividend divisor => number
Arguments dividend: number
divisor: number
Examples
▶ (mod 5 3) => 2
▶ (mod 5.2 3.1) => 2.1
▶ (mod -5 3) => 1
▶ (mod 5 -3) => -1
▶ (mod -5 -3) => -2
Remainder of dividing dividend and divisor .
Syntax
rem dividend divisor => number
Arguments dividend: number
divisor: number
Examples
▶ (rem 5 3) => 2
▶ (rem 5.2 3.1) => 2.1
▶ (rem -5 3) => -2
▶ (rem 5 -3) => 2
▶ (rem -5 -3) => -2
Quotient of dividing dividend and divisor .
Syntax
quot dividend divisor => number
Arguments dividend: number
divisor: number
Examples
▶ (quot 5 3) => 1
▶ (quot 5.2 3.1) => 1
▶ (quot -5 3) => -1
▶ (quot 5 -3) => -1
▶ (quot -5 -3) => 1
Adds one to number .
Syntax
Arguments
Examples
▶ (inc 0) => 1
▶ (inc 1) => 2
▶ (inc 100.1) => 101.1
Subtracts one from number .
Syntax
Arguments
Examples
▶ (dec 0) => -1
▶ (dec 1) => 0
▶ (dec 100.1) => 99.1
Computes square root of number .
Syntax
Arguments
Examples
▶ (sqrt 0) => 0
▶ (sqrt 9) => 3
▶ (sqrt 2) => 1.4142135623730951
Computes cube root of number .
Syntax
Arguments
Examples
▶ (cbrt 0) => 0
▶ (cbrt 27) => 3
▶ (cbrt 2) => 1.2599210498948732
▶ (cbrt 1) => 1
Computes returns base-number raised to the power-number .
Syntax
pow base-number power-number => number
Arguments base-number: number
power-number: number
Examples
▶ (pow 2 3) => 8
▶ (pow 2 0) => 1
▶ (pow 2 -3) => 0.125
▶ (pow -2 3) => -8
▶ (pow -2 -3) => -0.125
Computes e rasied to the power-number .
Syntax
exp power-number => number
Arguments
Examples
▶ (exp 3) => 20.085536923187668
▶ (exp 0) => 1
▶ (exp -3) => 0.049787068367863944
▶ (exp 3) => 20.085536923187668
Returns rounded number . If decimals is provided it return a number with that many decimals.
Syntax
round number decimals(optional) => number
Arguments number: number
decimals: integer
Examples
▶ (round 2) => 2
▶ (round 2.49) => 2
▶ (round 2.5) => 3
▶ (round -2.49) => -2
▶ (round -2.5) => -2
▶ (round -2.501) => -3
▶ (round 1.23456789 4) => 1.2346
Returns the integer part of number by removing any fractional digits.
Syntax
Arguments
Examples
▶ (trunc 2) => 2
▶ (trunc 2.49) => 2
▶ (trunc 2.5) => 2
▶ (trunc -2.49) => -2
▶ (trunc -2.5) => -2
▶ (trunc -2.501) => -2
Returns the largest integer less than or equal to number .
Syntax
Arguments
Examples
▶ (floor 2) => 2
▶ (floor 2.49) => 2
▶ (floor 2.5) => 2
▶ (floor -2.49) => -3
▶ (floor -2.5) => -3
▶ (floor -2.501) => -3
Returns the smallest integer larger than or equal to number .
Syntax
Arguments
Examples
▶ (ceil 2) => 2
▶ (ceil 2.49) => 3
▶ (ceil 2.5) => 3
▶ (ceil -2.49) => -2
▶ (ceil -2.5) => -2
▶ (ceil -2.501) => -2
Returns the smallest number of the arguments.
Syntax
min numbers((one or many)) => number
Arguments
Examples
▶ (min 2 0 1) => 0
▶ (min 2 -1 1) => -1
▶ (min 2.5) => 2.5
Returns the largest number of the arguments.
Syntax
max numbers((one or many)) => number
Arguments
Examples
▶ (max 2 0 1) => 2
▶ (max 2 -1 1) => 2
▶ (max 2.5) => 2.5
Returns the absolute value of number .
Syntax
Arguments
Examples
▶ (abs -2.3) => 2.3
▶ (abs 0) => 0
▶ (abs 2.5) => 2.5
Returns 1 if number > 0 , -1 if number < 0 , 0 if number = 0 or -0 if number = -0 .
Syntax
Arguments
Examples
▶ (sign -2.3) => -1
▶ (sign -0) => 0
▶ (sign 0) => 0
▶ (sign 12312) => 1
Returns a number representing positive positive-infinity.
Syntax
positive-infinity => number
No arguments
Examples
▶ (positive-infinity) => null
Returns a number representing negative infinity.
Syntax
negative-infinity => number
No arguments
Examples
▶ (negative-infinity) => null
Returns a number representing the maximum safe integer.
Syntax
max-safe-integer => number
No arguments
Examples
▶ (max-safe-integer) => 9007199254740991
Returns a number representing the minimum safe integer.
Syntax
min-safe-integer => number
No arguments
Examples
▶ (min-safe-integer) => -9007199254740991
Returns a number representing the maximum numeric value.
Syntax
No arguments
Examples
▶ (max-value) => 1.7976931348623157e+308
Returns a number representing the smallest positive numeric value.
Syntax
No arguments
Examples
Returns a number representing the difference between 1 and the smallest floating point number greater than 1.
Syntax
No arguments
Examples
▶ (epsilon) => 2.220446049250313e-16
Returns a number representing Not-A-Number.
Syntax
No arguments
Examples
Returns Euler's number, the base of natural logarithms, e.
Syntax
No arguments
Examples
▶ (e) => 2.718281828459045
Returns Pi, the ratio of the circumference of a circle to its diameter.
Syntax
No arguments
Examples
▶ (pi) => 3.141592653589793
Returns the natural logarithm (base e) of number .
Syntax
Arguments
Examples
▶ (log 0.01) => -4.605170185988091
▶ (log (exp 12)) => 12
▶ (log 2.5) => 0.9162907318741551
Returns the base 2 logarithm of a number.
Syntax
Arguments
Examples
▶ (log2 0.01) => -6.643856189774724
▶ (log2 (pow 2 12)) => 12
▶ (log2 2.5) => 1.3219280948873624
Returns the base 2 logarithm of a number.
Syntax
Arguments
Examples
▶ (log10 0.01) => -2
▶ (log10 (pow 10 12)) => 12
▶ (log10 2.5) => 0.3979400086720376
Returns a semi random number between 0 (inclusive) and number (default 1) (exclusive).
Syntax
rand! number(optional) => number
Arguments
Examples
▶ (rand! 1) => 0.3982850796273931
▶ (rand! 0.01) => 0.002873506564909625
▶ (rand! 2.5) => 1.6869433233293645
Returns a semi random integer between 0 (inclusive) and number (exclusive).
Syntax
rand-int! number(optional) => number
Arguments
Examples
▶ (rand-int! 1) => 0
▶ (rand-int! 10.12) => 2
▶ (rand-int! 123) => 91
Returns the sine of angle . angle must be specified in radians.
Syntax
Arguments
Examples
▶ (sin 0) => 0
▶ (sin 1) => 0.8414709848078965
▶ (sin (pi)) => 1.2246467991473532e-16
▶ (sin -0.5) => -0.479425538604203
Returns the cosine of angle . angle must be specified in radians.
Syntax
Arguments
Examples
▶ (cos 0) => 1
▶ (cos 1) => 0.5403023058681398
▶ (cos (pi)) => -1
▶ (cos -0.5) => 0.8775825618903728
Returns the tangent of angle . angle must be specified in radians.
Syntax
Arguments
Examples
▶ (tan 0) => 0
▶ (tan 1) => 1.5574077246549023
▶ (tan (pi)) => -1.2246467991473532e-16
▶ (tan -0.5) => -0.5463024898437905
Returns the arcsine (in radians) of value .
Syntax
Arguments
Examples
▶ (asin 0) => 0
▶ (asin 1) => 1.5707963267948966
▶ (asin -0.5) => -0.5235987755982989
Returns the arccosine (in radians) of value .
Syntax
Arguments
Examples
▶ (acos 0) => 1.5707963267948966
▶ (acos 1) => 0
▶ (acos -0.5) => 2.0943951023931957
Returns the arctangent (in radians) of value .
Syntax
Arguments
Examples
▶ (atan 0) => 0
▶ (atan 1) => 0.7853981633974483
▶ (atan -0.5) => -0.4636476090008061
Returns the hyperbolic sine of value .
Syntax
Arguments
Examples
▶ (sinh 0) => 0
▶ (sinh 1) => 1.1752011936438014
▶ (sinh -0.5) => -0.5210953054937474
Returns the hyperbolic cosine of value .
Syntax
Arguments
Examples
▶ (cosh 0) => 1
▶ (cosh 1) => 1.5430806348152437
▶ (cosh -0.5) => 1.1276259652063807
Returns the hyperbolic tangent of value .
Syntax
Arguments
Examples
▶ (tanh 0) => 0
▶ (tanh 1) => 0.7615941559557649
▶ (tanh -0.5) => -0.46211715726000974
▶ (tanh 50) => 1
Returns the hyperbolic arcsine of value .
Syntax
Arguments
Examples
▶ (asinh 0) => 0
▶ (asinh 0.9) => 0.8088669356527824
▶ (asinh -0.5) => -0.48121182505960347
Returns the hyperbolic arccosine of value .
Syntax
Arguments
Examples
▶ (acosh 1) => 0
▶ (acosh 2) => 1.3169578969248166
▶ (acosh 100) => 5.298292365610484
Returns the hyperbolic arctangent of value .
Syntax
Arguments
Examples
▶ (atanh 0) => 0
▶ (atanh 0.9) => 1.4722194895832204
▶ (atanh -0.5) => -0.5493061443340548
Call supplied function with specified arguments.
Syntax
Arguments
Examples
▶ (apply + [1 2 3]) => 6
▶ (apply (fn [x y] (sqrt (+ (* x x) (* y y)))) [3 4]) => 5
Returns value .
Syntax
Arguments
Examples
▶ (identity 1) => 1
▶ (identity "Albert") => "Albert"
▶ (identity {:a 1}) => {"a":1}
▶ (identity nil) => null
Takes a function fn and fewer (or equal) than the normal arguments to fn , and returns a function that takes a variable number of additional args. When called, the returned function calls f with args + additional args.
Syntax
partial fn args(zero or more) => function
Arguments
Examples
▶ (partial + 100) => <function λ>
▶ (def addHundred (partial + 100)) (addHundred 10) => 110
Takes a set of functions and returns a fn that is the composition of those. The returned functions takes a variable number of arguments, applies the rightmost function to the args, the next function (right-to-left) to the result, etc.
Syntax
comp fn(zero or more) fns(optional) => function
Arguments fn: function
fns: function[]
Examples
▶ (def negative-quotient (comp - /)) (negative-quotient 9 3) => -3
▶ (#((apply comp first (repeat %2 rest)) %1) [1 2 3 4 5 6 7] 3) => 4
▶ (def x {"bar" {"foo" 42}}) ((comp "foo" "bar") x) => 42
Returns a function that takes any number of arguments and returns value .
Syntax
constantly value => function
Arguments
Examples
▶ (def always-true (constantly true)) (always-true 9 3) => true
▶ (#((apply constantly first (repeat %2 rest)) %1) [1 2 3 4 5 6 7] 3) => <function first>
Takes variable number of functions and returns a function that is the juxtaposition of those functions . The returned function takes a variable number of args, and returns a vector containing the result of applying each function to the args (left-to-right).
Syntax
juxt functions(one or more) => Function
Arguments
Examples
▶ ((juxt + * min max) 3 4 6) => [13,72,3,6]
▶ ((juxt :a :b) {:a 1, :b 2, :c 3, :d 4}) => [1,2]
▶ (apply (juxt + * min max) (range 1 11)) => [55,3628800,1,10]
Takes a function and returns a new function that takes the same arguments as f, has the same effects, if any, and returns the opposite truth value.
Syntax
complement function => Function
Arguments
Examples
▶ ((complement >) 1 3) => true
▶ ((complement <) 1 3) => false
▶ ((complement +) 1 3) => false
▶ ((complement +) 0 0) => true
Takes a number of predicates and returns a function that returns true if all of the predicates return a truthy true value against all of its arguments, else it returns false.
Syntax
every-pred predicates(one or more) => Function
Arguments
Examples
▶ ((every-pred string? #(> (count %1) 3)) "Albert" "Mojir") => true
▶ ((every-pred string? #(> (count %1) 3)) "Albert" :M) => false
▶ ((every-pred string? #(> (count %1) 3)) "Albert" [1 2 3]) => false
Takes a number of predicates and returns a function that returns true if at least one of the predicates return a truthy true value against at least one of its arguments, else it returns false.
Syntax
some-pred predicates(one or more) => Function
Arguments
Examples
▶ ((some-pred string? #(> (count %1) 3)) "Albert" "Mojir") => true
▶ ((some-pred string? #(> (count %1) 3)) :A :M) => true
▶ ((some-pred string? #(> (count %1) 3)) :A [1 2 3]) => true
▶ ((some-pred string? #(> (count %1) 3)) [1 2 3] [2]) => false
Takes a function fn , and returns a function that calls fn , replacing a nil argument to fn with the corresponding param .
Syntax
fnil fn param(one or more) => Function
Arguments
Examples
▶ ((fnil + 1 2) 0 0) => 0
▶ ((fnil + 1 2) nil 0) => 1
▶ ((fnil + 1 2) 0 nil) => 2
▶ ((fnil + 1 2) nil nil) => 3
▶ ((fnil + 1 2) nil nil 3 4) => 10
Result is true if no two values are equal to each other, otherwise result is false . Note that only two argument version result is negation of = function, that is (not= a b) is same as (not (= a b)) .
Syntax
Arguments
Examples
▶ (not= 3) => true
▶ (not= 3 2) => true
▶ (not= :3 3) => true
▶ (not= 3 3 2) => false
▶ (not= :3 :2 :1 :0) => true
▶ (not= 0 -0) => false
Compares values according to 'equal' predicate. Result is true if every specified value is equal to each other, otherwise result is false .
Syntax
Arguments
Examples
▶ (= 1 1) => true
▶ (= 1.01 1) => false
▶ (= :1 1) => false
▶ (= :2 :2 :2 :2) => true
▶ (= 2 2 1 2) => false
Compares values according to 'less than' predicate. Each (overlapping) pair of the values is compared by it. The result is true if all compared pairs satisfy comparison.
Syntax
< values(one or many) => boolean
Arguments
Examples
▶ (< 0 1) => true
▶ (< 1 1.01) => true
▶ (< 1 1) => false
▶ (< 1 2 2 3) => false
▶ (< :a :b) => true
▶ (< [9] [1 2]) => true
Compares values according to 'greater than' predicate. Each (overlapping) pair of the values is compared by it. The result is true if all compared pairs satisfy comparison.
Syntax
> values(one or many) => boolean
Arguments
Examples
▶ (> 1 0) => true
▶ (> 1.01 1) => true
▶ (> 1 1) => false
▶ (> 4 3 2 1) => true
▶ (> 3 2 2 1) => false
Compares values according to 'less than or equal' predicate. Each (overlapping) pair of the values is compared by it. The result is true if all compared pairs satisfy comparison.
Syntax
<= values(one or many) => boolean
Arguments
Examples
▶ (<= 0 1) => true
▶ (<= 1 1.01) => true
▶ (<= 1 1) => true
▶ (<= 1 2 3 4) => true
▶ (<= 1 2 2 3) => true
Compares values according to 'greater than or equal' predicate. Each (overlapping) pair of the values is compared by it. The result is true if all compared pairs satisfy comparison.
Syntax
>= values(one or many) => boolean
Arguments
Examples
▶ (>= 1 0) => true
▶ (>= 1.01 1) => true
▶ (>= 1 1) => true
▶ (>= 4 3 2 1) => true
▶ (>= 3 2 2 1) => true
Computes logical negation. Note that any other value than false , 0 , nil and '' is considered as true .
Syntax
Arguments
Examples
▶ (not 3) => false
▶ (not true) => false
▶ (not "A string") => false
▶ (not 0) => true
▶ (not false) => true
▶ (not nil) => true
▶ (not "") => true
It console.log the values and then returns the last element of the values array. If called with no arguments nil is returned.
Syntax
Arguments
Examples
▶ (write! "A string") => "A string"
▶ (write! 100 "items") => "items"
▶ (write! (object :a 10)) => {"a":10}
▶ (write! [:a :b :c]) => ["a","b","c"]
▶ (write! #"^start") => {"__REGEXP__":true,"debugInfo":{"code":"(write! #\"^start\")","line":1,"column":9},"source":"^start","flags":""}
▶ (write! nil true false) => false
Returns milliseconds elapsed since the UNIX epoch.
Syntax
No arguments
Examples
▶ (inst-ms!) => 1683389426447
Returns milliseconds elapsed since the UNIX epoch to date-time .
Syntax
iso-date-time->inst-ms date-time => number
Arguments
Examples
▶ (iso-date-time->inst-ms "2022-04-12T09:37:10.899Z") => 1649756230899
▶ (iso-date-time->inst-ms "1980-01-01") => 315532800000
Returns IOS date time string from ms (milliseconds elapsed since the UNIX epoch).
Syntax
inst-ms->iso-date-time ms => string
Arguments
Examples
▶ (inst-ms->iso-date-time 1649756230899) => "2022-04-12T09:37:10.899Z"
▶ (inst-ms->iso-date-time 0) => "1970-01-01T00:00:00.000Z"
If no params, prints context stack, otherwise prints value details.
Syntax
Arguments
Examples
▶ (debug!) => null
▶ (debug! #(> %1 2)) => <function λ>
Coerces value to boolean.
Syntax
boolean value => true | false
Arguments
Examples
▶ (boolean 0) => false
▶ (boolean 1) => true
▶ (boolean nil) => false
▶ (boolean "Albert") => true
Compares two values. Returns -1 if a < b, 1 if a > b and 0 if a and b have the same sort order.
Syntax
compare a b => 1 | -1 | 0
Arguments
Examples
▶ (compare 0 1) => -1
▶ (compare "Albert" "Mojir") => -1
▶ (compare 1 :1) => -1
▶ (compare [1 2 3] [2 3]) => 1
▶ (compare [1 2 3] [2 3 4]) => -1
▶ (compare {:a 1 :b 2} {:a 1}) => 1
▶ (compare {:a 1} [2 3]) => 1
▶ (compare + -) => 0
Returns the lits version.
Syntax
No arguments
Examples
▶ (lits-version!) => "1.0.0"
Returns UUID string.
Syntax
No arguments
Examples
▶ (uuid!) => "de367c5d-025a-4106-963c-67450a0ca808"
Returns true if a and b are structually equal.
Syntax
Arguments
Examples
▶ (equal? {:a 10 :b 20} {:b 20 :a 10}) => true
▶ (equal? [1 true nil] [1 true nil]) => true
▶ (equal? {:a 10 :b [1 2 {:b 20}]} {:b [1 2 {:b 20}] :a 10}) => true
▶ (equal? {:a 10 :b [1 2 {:b 20}]} {:b [1 2 {:b 21}] :a 10}) => false
▶ (= 0.3 (+ 0.1 0.2)) => false
▶ (equal? 0.3 (+ 0.1 0.2)) => true
Return shallow copy of object with attribute attr deleted.
Syntax
dissoc object attr => value
Arguments object: object
attr: string
Examples
▶ (dissoc (object :x 10 :y 20) :x) => {"y":20}
▶ (dissoc { :x 10 } :y) => {"x":10}
▶ (def o { :a 5 }) (dissoc o :a) o => {"a":5}
Constructs a new object. Object members are created from key - value pairs. Requires an even number of arguments.
Syntax
object [key value](zero or more) => object
Arguments [key value]: [string any]
Examples
▶ (object) => {}
▶ (object :x 10 :y true :z "A string") => {"x":10,"y":true,"z":"A string"}
▶ {} => {}
▶ {:a 1 :b 2} => {"a":1,"b":2}
Returns array of all keys in object .
Syntax
Arguments
Examples
▶ (keys (object)) => []
▶ (keys (object :x 10 :y true :z "A string")) => ["x","y","z"]
Returns array of all values in object .
Syntax
Arguments
Examples
▶ (vals (object)) => []
▶ (vals (object :x 10 :y true :z "A string")) => [10,true,"A string"]
Returns nested array of all key - value pairs in object .
Syntax
entries object => array of [key value] - paris
Arguments
Examples
▶ (entries (object)) => []
▶ (entries (object :x 10 :y true :z "A string")) => [["x",10],["y",true],["z","A string"]]
Returns entry for key , or nil if key not present in object .
Syntax
Arguments object: object
key: string
Examples
▶ (find (object :a 1 :b 2) :b) => ["b",2]
▶ (find (object :a 1 :b 2) :c) => null
Returns a new object created by merging together all arguments.
Syntax
merge object(one or many) => object
Arguments
Examples
▶ (merge (object :x 10) (object :y 20)) => {"x":10,"y":20}
▶ (merge (object :x 10) (object :x 15 :y 20)) => {"x":15,"y":20}
Returns a new object created by merging together all arguments. If two keys appears in more than one object fn is used to calculate the new value.
Syntax
merge-with fn object(one or many) => object
Arguments fn: Function
object: object
Examples
▶ (merge-with + (object :x 10) (object :y 20)) => {"x":10,"y":20}
▶ (merge-with + (object :x 10) (object :x 15 :y 20)) => {"x":25,"y":20}
▶ (merge-with - (object :x 10) (object :x 20) (object :x 30) (object :x 40)) => {"x":-80}
Returns a new object created by mapping keys to values .
Syntax
zipmap keys values => object
Arguments keys: Array
values: Array
Examples
▶ (zipmap [:a :b :c] [10 nil [1 2 3]]) => {"a":10,"b":null,"c":[1,2,3]}
▶ (zipmap [:a :b :c] [1]) => {"a":1}
▶ (zipmap [] [10 nil [1 2 3]]) => {}
Returns an object containing only those entries in object whose key is in keys .
Syntax
select-keys object keys => object
Arguments object: Object
keys: Array
Examples
▶ (select-keys {:a 1 :b 2 :c 3} [:a :b]) => {"a":1,"b":2}
▶ (select-keys {:a 1} [:a :b]) => {"a":1}
Returns true if value is a boolean , otherwise false .
Syntax
boolean? value => boolean
Arguments
Examples
▶ (boolean? true) => true
▶ (boolean? false) => true
▶ (boolean? [1 2 3]) => false
▶ (boolean? 0) => false
▶ (boolean? "A string") => false
Returns true if value is nil , otherwise false .
Syntax
Arguments
Examples
▶ (nil? nil) => true
▶ (nil? false) => false
▶ (nil? [1 2 3]) => false
▶ (nil? 0) => false
▶ (nil? "A string") => false
Returns true if value is a number, otherwise false .
Syntax
Arguments
Examples
▶ (number? 0) => true
▶ (number? 2) => true
▶ (number? -0.12) => true
▶ (number? false) => false
▶ (number? [1 2 3]) => false
▶ (number? "A string") => false
Returns true if value is a string, otherwise false .
Syntax
Arguments
Examples
▶ (string? "") => true
▶ (string? "A string") => true
▶ (string? (if true "A string" false)) => true
▶ (string? false) => false
▶ (string? [1 2 3]) => false
▶ (string? 100) => false
Returns true if value is a function, otherwise false .
Syntax
function? value => boolean
Arguments
Examples
▶ (function? +) => true
▶ (function? /) => true
▶ (function? (fn [x y] (+ x y))) => true
▶ (function? false) => false
▶ (function? "false") => false
▶ (function? [1 2 3]) => false
Returns true if value is an integer, otherwise false .
Syntax
integer? value => boolean
Arguments
Examples
▶ (integer? 0) => true
▶ (integer? -12) => true
▶ (integer? 42) => true
▶ (integer? 10.1) => false
▶ (integer? (fn [x y] (+ x y))) => false
▶ (integer? false) => false
▶ (integer? "false") => false
▶ (integer? [1 2 3]) => false
Returns true if value is an array, otherwise false .
Syntax
Arguments
Examples
▶ (array? []) => true
▶ (array? [1 2 3]) => true
▶ (array? (object :a 10)) => false
▶ (array? 42) => false
▶ (array? 10.1) => false
▶ (array? (fn [x y] (+ x y))) => false
Returns true if value is an object, otherwise false .
Syntax
Arguments
Examples
▶ (object? (object :a 10)) => true
▶ (object? (object)) => true
▶ (object? 42) => false
▶ (object? 10.1) => false
▶ (object? (fn [x y] (+ x y))) => false
▶ (object? #"^start") => false
▶ (object? "false") => false
▶ (object? [1 2 3]) => false
Returns true if value is a Coll i.e. an array, an object or a string, otherwise false .
Syntax
Arguments
Examples
▶ (coll? []) => true
▶ (coll? [1 2 3]) => true
▶ (coll? (object :a 10)) => true
▶ (coll? "Albert") => true
▶ (coll? 42) => false
▶ (coll? 10.1) => false
▶ (coll? (fn [x y] (+ x y))) => false
Returns true if value is a Seq i.e. an array or a string, otherwise false .
Syntax
Arguments
Examples
▶ (seq? []) => true
▶ (seq? [1 2 3]) => true
▶ (seq? (object :a 10)) => false
▶ (seq? "Albert") => true
▶ (seq? 42) => false
▶ (seq? 10.1) => false
▶ (seq? (fn [x y] (+ x y))) => false
Returns true if value is a regexp, otherwise false .
Syntax
Arguments
Examples
▶ (regexp? (regexp "^start")) => true
▶ (regexp? #"^start") => true
▶ (regexp? -12) => false
▶ (regexp? (object)) => false
▶ (regexp? 10.1) => false
▶ (regexp? (fn [x y] (+ x y))) => false
▶ (regexp? false) => false
▶ (regexp? "false") => false
▶ (regexp? [1 2 3]) => false
Returns true if number is 0 , otherwise false .
Syntax
Arguments
Examples
▶ (zero? 0) => true
▶ (zero? -0.0) => true
▶ (zero? 1) => false
▶ (zero? 0.1) => false
Returns true if number is greater than 0 , otherwise false .
Syntax
Arguments
Examples
▶ (pos? 0) => false
▶ (pos? -0.0) => false
▶ (pos? 1) => true
▶ (pos? -0.1) => false
Returns true if number is less than 0 , otherwise false .
Syntax
Arguments
Examples
▶ (neg? 0) => false
▶ (neg? -0.0) => false
▶ (neg? 1) => false
▶ (neg? -0.1) => true
Returns true if number is even, otherwise false .
Syntax
Arguments
Examples
▶ (even? 0) => true
▶ (even? -0.0) => true
▶ (even? -1) => false
▶ (even? 2.1) => false
Returns true if number is odd, otherwise false .
Syntax
Arguments
Examples
▶ (odd? 1.0) => true
▶ (odd? 1.001) => false
▶ (odd? -1) => true
▶ (odd? 2.1) => false
Returns true if number is finite, otherwise false .
Syntax
finite? number => boolean
Arguments
Examples
▶ (finite? 1.0) => true
▶ (finite? (/ 1 0)) => false
▶ (finite? (/ -1 0)) => false
▶ (finite? (sqrt -1)) => false
Returns true if number is NaN (not a number), otherwise false .
Syntax
Arguments
Examples
▶ (nan? 1.0) => false
▶ (nan? (/ 1 0)) => false
▶ (nan? (/ -1 0)) => false
▶ (nan? (sqrt -1)) => true
Returns true if number equals negative infinity, otherwise false .
Syntax
negative-infinity? number => boolean
Arguments
Examples
▶ (negative-infinity? 1.0) => false
▶ (negative-infinity? (/ 1 0)) => false
▶ (negative-infinity? (/ -1 0)) => true
▶ (negative-infinity? (sqrt -1)) => false
Returns true if number equals positive infinity, otherwise false .
Syntax
positive-infinity? number => boolean
Arguments
Examples
▶ (positive-infinity? 1.0) => false
▶ (positive-infinity? (/ 1 0)) => true
▶ (positive-infinity? (/ -1 0)) => false
▶ (positive-infinity? (sqrt -1)) => false
Returns true if value is true , otherwise false .
Syntax
Arguments
Examples
▶ (false? false) => true
▶ (false? true) => false
▶ (false? 1) => false
▶ (false? 0) => false
Returns true if value is true , otherwise false .
Syntax
Arguments
Examples
▶ (true? false) => false
▶ (true? true) => true
▶ (true? 1) => false
▶ (true? 0) => false
Returns true if coll is empty, otherwise false .
Syntax
Arguments coll: collection or string
Examples
▶ (empty? []) => true
▶ (empty? [1 2 3]) => false
▶ (empty? {}) => true
▶ (empty? {:a 2}) => false
▶ (empty? "") => true
▶ (empty? "Albert") => false
Returns false if coll is empty, otherwise true .
Syntax
not-empty? coll => boolean
Arguments coll: collection or string
Examples
▶ (not-empty? []) => false
▶ (not-empty? [1 2 3]) => true
▶ (not-empty? {}) => false
▶ (not-empty? {:a 2}) => true
▶ (not-empty? "") => false
▶ (not-empty? "Albert") => true
Creates a RegExp from pattern and flags .
Syntax
regexp pattern flags => RegExp
Arguments pattern: string
flags: string
Examples
▶ (regexp "^\s*(.*)$") => {"__REGEXP__":true,"debugInfo":{"code":"(regexp \"^\\s*(.*)$\")","line":1,"column":2},"source":"^\\s*(.*)$","flags":""}
▶ #"^\s*(.*)$" => {"__REGEXP__":true,"debugInfo":{"code":"#\"^\\s*(.*)$\"","line":1,"column":1},"source":"^\\s*(.*)$","flags":""}
▶ (regexp "albert" :i) => {"__REGEXP__":true,"debugInfo":{"code":"(regexp \"albert\" :i)","line":1,"column":2},"source":"albert","flags":"i"}
▶ #"albert"ig => {"__REGEXP__":true,"debugInfo":{"code":"#\"albert\"ig","line":1,"column":1},"source":"albert","flags":"gi"}
Matches string against pattern . If string matches, a match -array is returned, otherwise nil .
Syntax
match pattern string => array
Arguments pattern: RegExp
string: string
Examples
▶ (match (regexp "^\s*(.*)$") " A string") => [" A string","A string"]
▶ (match #"albert"i "My name is Albert") => ["Albert"]
▶ (match #"albert"i "My name is Ben") => null
Returns a new string with some or all matches of pattern replaced by replacement .
Syntax
replace string pattern replacement => array
Arguments string: string
pattern: RegExp
replacement: string
Examples
▶ (replace "Duck" (regexp :u) :i) => "Dick"
▶ (replace "abcABC" (regexp :a "gi") "-") => "-bc-BC"
▶ (replace "abcABC" #"a"gi "-") => "-bc-BC"
Special Expression
Computes logical 'and' function. forms evaluation starts from left. Value from the first form that decides result is returned so forms at end of argument array may not evaluated.
Syntax
Arguments
Examples
▶ (and 1 1) => 1
▶ (and (> 3 2) "string") => "string"
▶ (and (< 3 2) "string") => false
▶ (and true true true true) => true
▶ (and true true 0 true) => 0
Special Expression
Computes logical 'or' function. forms evaluation starts from left. Value from the first form that decides result is returned so forms at end of argument array may not evaluated.
Syntax
Arguments
Examples
▶ (or 1 1) => 1
▶ (or (> 3 2) "string") => true
▶ (or (< 3 2) "string") => "string"
▶ (or true true true true) => true
▶ (or 1 2 3 4) => 1
Special Expression
Bind value to variable . If variable isn't defined, a new global variable is created.
Syntax
def variable value => any
Arguments variable: name
value: any
Examples
▶ (def a (object)) => {}
▶ (def a (object :x 10 :y true :z "A string")) => {"x":10,"y":true,"z":"A string"}
Special Expression
Creates a variable with name set to variable evaluated and value set to value . If a variable with name variable isn't found a new global variable is created.
Syntax
defs variable value => any
Arguments variable: form
value: any
Examples
▶ (defs :a :b) => "b"
▶ (defs (str :a :1) (object :x 10 :y true :z "A string")) a1 => {"x":10,"y":true,"z":"A string"}
▶ (defs :a :b) (defs a :c) b => "c"
Special Expression
Binds local variables. The variables lives only within the body. It returns evaluation of the last expression in the body.
Syntax
Arguments bindings: bindings
body: lisp expressions
Examples
▶ (let [a (+ 1 2 3 4) b (* 1 2 3 4)] (write! a b)) => 24
Special Expression
Binds one local variable. If it evaluates to a truthy value the then-form is executed with the variable accessable. If the bound variable evaluates to false, the then-form is evaluated (without variable accessable).
Syntax
if-let bindings then else(optional) => any
Arguments bindings: bindings
then: any
else: any
Examples
▶ (if-let [a (> (count "Albert") 4)] (write! (str a "is big enough")) (write! "Sorry, not big enough.")) => "trueis big enough"
▶ (if-let [a (> (count "Albert") 10)] (write! (str a "is big enough")) (write! "Sorry, not big enough.")) => "Sorry, not big enough."
Special Expression
Binds one local variable. If it evaluates to a truthy value the then-form is executed with the variable accessable. If the bound variable evaluates to false, nil is returned.
Syntax
when-let bindings then => any
Arguments bindings: bindings
then: any
Examples
▶ (when-let [a (> (count "Albert") 4)] (write! (str a "is big enough")) (write! "Sorry, not big enough.")) => "Sorry, not big enough."
▶ (when-let [a (> (count "Albert") 10)] (write! (str a "is big enough")) (write! "Sorry, not big enough.")) => null
Special Expression
Roughly the same as (when (seq xs) (let [x (first xs)] body)) .
Syntax
when-first bindings then => any
Arguments bindings: bindings
then: any
Examples
▶ (when-first [x [1 2 3]] (write! 10) (write! 20) x) => 1
▶ (when-first [x "Albert"] x) => "A"
▶ (when-first [x ""] x) => null
▶ (when-first [x []] x) => null
Special Expression
Creates a function. When called, evaluation of the last expression in the body is returned.
Syntax
fn arguments body => function
Arguments arguments: arguments
body: lisp expressions
Examples
▶ (fn [a b] (sqrt (+ (* a a) (* b b)))) => <function λ>
▶ ((fn [a b] (sqrt (+ (* a a) (* b b)))) 3 4) => 5
Special Expression
Creates a named global function. When called, evaluation of the last expression in the body is returned.
Syntax
defn name arguments body => function
Arguments name: name
arguments: arguments
body: lisp expressions
Examples
▶ (defn hyp [a b] (sqrt (+ (* a a) (* b b)))) hyp => <function hyp>
▶ (defn hyp [a b] (sqrt (+ (* a a) (* b b)))) (hyp 3 4) => 5
▶ (defn sumOfSquares [& s] (apply + (map (fn [x] (* x x)) s))) (sumOfSquares 1 2 3 4 5) => 55
Special Expression
Creates a named global function with its name set to name evaluated. When called, evaluation of the last expression in the body is returned.
Syntax
defns name arguments body => function
Arguments name: form
arguments: arguments
body: lisp expressions
Examples
▶ (defns "hyp" [a b] (sqrt (+ (* a a) (* b b)))) hyp => <function hyp>
▶ (defns (str :h :y :p) [a b] (sqrt (+ (* a a) (* b b)))) (hyp 3 4) => 5
▶ (defns "sumOfSquares" [& s] (apply + (map (fn [x] (* x x)) s))) (sumOfSquares 1 2 3 4 5) => 55
Special Expression
Executes tryExpression. If that throws, the catchBlock gets executed. See examples for details.
Syntax
try tryExpression catchBlock => any
Arguments tryExpression: form
catchBlock: catchBlock
Examples
▶ (try (/ 2 4) (catch error (/ 2 1))) => 0.5
▶ (try (/ 2 0) (catch error (/ 2 1))) => null
▶ (try (/ 2 0) (catch error error)) => null
Special Expression
Throws UserDefinedError with message set to message evaluated. message must evaluate to a string .
Syntax
Arguments
Examples
▶ (try (throw "You shall not pass!") (catch e e)) => UserDefinedError: You shall not pass!
(try (throw "You shall not pass!") (catch e e))
^
▶ (try (throw (subs "You shall not pass!" 0 3)) (catch e e)) => UserDefinedError: You
(try (throw (subs "You shall not pass!" 0 3)) (catch e e))
^
▶ (try (throw "You shall not pass!") (catch error error)) => UserDefinedError: You shall not pass!
(try (throw "You shall not pass!") (catch error error))
^
Special Expression
Either then or else branch is taken. Then branch is selected when test result is truthy. I test is falsy and no else branch exists, nil is returned.
Syntax
if test then else(optional) => any
Arguments test: any
then: any
else: any
Examples
▶ (if true (write! "TRUE") (write! "FALSE")) => "TRUE"
▶ (if false (write! "TRUE") (write! "FALSE")) => "FALSE"
▶ (if true (write! "TRUE")) => "TRUE"
▶ (if false (write! "TRUE")) => null
Special Expression
Either then or else branch is taken. Then branch is selected when test result is falsy. I test is falsy and no else branch exists, nil is returned.
Syntax
if-not test then else(optional) => any
Arguments test: any
then: any
else: any
Examples
▶ (if-not true (write! "TRUE") (write! "FALSE")) => "FALSE"
▶ (if-not false (write! "TRUE") (write! "FALSE")) => "TRUE"
▶ (if-not true (write! "TRUE")) => null
▶ (if-not false (write! "TRUE")) => "TRUE"
Special Expression
Used for branching. Variants are tested sequentially from the top. If no branch is tested truthy, nil is returned.
Syntax
Arguments
Examples
▶ (cond false (write! "FALSE") nil (write! "nil") true (write! "TRUE")) => "TRUE"
▶ (cond false (write! "FALSE") nil (write! "nil")) => null
Special Expression
If test yields a thruthy value, the forms are evaluated in order from left to right and the value returned by the last form is returned. Otherwise, if test yields a falsy value, the forms are not evaluated, and nil is returned. If no form is provided, nil is returned.
Syntax
when test form(zero or more) => any
Arguments
Examples
▶ (when true (write! "Hi") (write! "There")) => "There"
▶ (when false (write! "Hi") (write! "There")) => null
▶ (when true) => null
▶ (when false) => null
Special Expression
If test yields a falsy value, the forms are evaluated in order from left to right and the value returned by the last form is returned. Otherwise, if test yields a truthy value, the forms are not evaluated, and nil is returned. If no form is provided, nil is returned.
Syntax
when-not test form(zero or more) => any
Arguments
Examples
▶ (when-not true (write! "Hi") (write! "There")) => null
▶ (when-not false (write! "Hi") (write! "There")) => "There"
▶ (when-not true) => null
▶ (when-not false) => null
Special Expression
Calls forms in the order they have been written. Resulting value is the value of the last form.
Syntax
Arguments
Examples
▶ (do (write! "Hi") (write! "Albert")) => "Albert"
▶ (do) => null
Special Expression
Recursevly calls enclosing function or loop with its evaluated forms .
Syntax
Arguments
Examples
▶ (defn foo [n] (write! n) (when (not (zero? n)) (recur (dec n)))) (foo 3) => null
▶ ((fn [n] (write! n) (when (not (zero? n)) (recur (dec n)))) 3) => null
▶ (loop [n 3] (write! n) (when (not (zero? n)) (recur (dec n)))) => null
Special Expression
Executes body with initial bindings . The bindings will be replaced with the recur parameters for subsequent recursions.
Syntax
loop bindings body => any
Arguments bindings: bindings
body: lisp expressions
Examples
▶ (loop [n 3] (write! n) (when (not (zero? n)) (recur (dec n)))) => null
▶ (loop [n 3] (write! n) (if (not (zero? n)) (recur (dec n)) n)) => 0
Special Expression
Prints the time it took to evaluate form . Returns form evaluated.
Syntax
Arguments
Examples
▶ (defn fib [x] (if (<= x 2) 1 (+ (fib (dec x)) (fib (- x 2))))) (time! (fib 10)) => 55
Special Expression
Same syntax as for , but returns nil . Use for side effects. Consumes less memory than for .
Syntax
doseq bindings expression => nil
Arguments bindings: bindings
expression: form
Examples
▶ (doseq [x [1 2 4]] (write! x)) => null
Special Expression
List comprehension. Takes an array of one or more bindings , each followed by zero or more modifiers, and returns an array of evaluations of expression . Collections are iterated in a nested fashion, rightmost fastest. Supported modifiers are: &let &while and &when.
Syntax
for bindings expression => Array
Arguments bindings: bindings
expression: form
Examples
▶ (for [x "Al" y [1 2]] (repeat y x)) => [["A"],["A","A"],["l"],["l","l"]]
▶ (for [x {:a 10 :b 20} y [1 2]] (repeat y x)) => [[["a",10]],[["a",10],["a",10]],[["b",20]],[["b",20],["b",20]]]
▶ (for [x [1 2] y [1 10]] (* x y)) => [1,10,2,20]
▶ (for [x [1 2] &let [z (* x x x)]] z) => [1,8]
▶ (for [x [0 1 2 3 4 5] &let [y (* x 3)] &when (even? y)] y) => [0,6,12]
▶ (for [x [0 1 2 3 4 5] &let [y (* x 3)] &while (even? y)] y) => [0]
▶ (for [x [0 1 2 3 4 5] &let [y (* x 3)] &while (odd? y)] y) => []
▶ (for [x [1 2 3] y [1 2 3] &while (<= x y) z [1 2 3]] [x y z]) => [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3]]
▶ (for [x [1 2 3] y [1 2 3] z [1 2 3] &while (<= x y)] [x y z]) => [[1,1,1],[1,1,2],[1,1,3],[1,2,1],[1,2,2],[1,2,3],[1,3,1],[1,3,2],[1,3,3],[2,2,1],[2,2,2],[2,2,3],[2,3,1],[2,3,2],[2,3,3],[3,3,1],[3,3,2],[3,3,3]]
Special Expression
Returns true if name is a declared variable or a builtin function.
Syntax
declared? name => boolean
Arguments
Examples
▶ (declared? foo) => false
▶ (def foo :foo) (declared? foo) => true
▶ (declared? +) => true
▶ (def foo nil) (declared? foo) => true
▶ (declared? if) => true
Special Expression
Sort of like or with a couple of differences. 1. test does not need to be declared (defaults to nil ) 2. Accept exactly one or two parameters.
Syntax
?? test default(optional) => any
Arguments
Examples
▶ (?? foo) => null
▶ (def foo :foo) (?? foo) => "foo"
▶ (?? +) => <function +>
▶ (def foo nil) (?? foo) => null
▶ (?? foo 1) => 1
▶ (?? "") => ""
▶ (?? 0) => 0
▶ (?? 0 1) => 1
▶ (?? 2 1) => 2
Extracts characters from indexStart up to but not including indexEnd .
Syntax
subs string indexStart indexEnd(optional) => string
Arguments string: string
indexStart: integer
indexEnd: integer
Examples
▶ (subs "A string" 2) => "string"
▶ (subs "A string" 2 5) => "str"
▶ (subs "A string" 2 100) => "string"
▶ (subs "A string" 100) => ""
Repeates string count times.
Syntax
string-repeat string count => number
Arguments string: string
count: integer
Examples
▶ (string-repeat "*" 10) => "**********"
▶ (string-repeat "***" 0) => ""
Concatenats values into one string . If value equals nil empty string is returned.
Syntax
str value(zero or more) => number
Arguments
Examples
▶ (str "A string" ", and another string" " ...and more") => "A string, and another string ...and more"
▶ (str "Just one string") => "Just one string"
▶ (str) => ""
▶ (str 0 false true nil #"^kalle" [1 2 3] {:a :a}) => "0falsetrue[object Object][1,2,3]{\"a\":\"a\"}"
Parses string to a number.
Syntax
Arguments
Examples
▶ (number "10") => 10
▶ (number "010") => 10
▶ (number "-1.01") => -1.01
Converts number to a string. If base is not equal to 10 nu mber must be a non negative integer.
Syntax
number-to-string number base => string
Arguments number: number
base: number: 2, 8, 10 or 16
Examples
▶ (number-to-string 10) => "10"
▶ (number-to-string -1.01) => "-1.01"
▶ (number-to-string -.01) => "-0.01"
▶ (number-to-string 15 2) => "1111"
▶ (number-to-string 15 8) => "17"
▶ (number-to-string 15 16) => "f"
Returns string converted to lower case.
Syntax
lower-case string => string
Arguments
Examples
▶ (lower-case "Albert") => "albert"
▶ (lower-case "") => ""
Returns string converted to upper case.
Syntax
upper-case string => string
Arguments
Examples
▶ (upper-case "Albert") => "ALBERT"
▶ (upper-case "") => ""
Returns a new string with leading and trailing whitespaces removed.
Syntax
Arguments
Examples
▶ (trim " Albert ") => "Albert"
▶ (trim " ") => ""
▶ (trim "") => ""
Returns a new string with leading whitespaces removed.
Syntax
trim-left string => string
Arguments
Examples
▶ (trim-left " Albert ") => "Albert "
▶ (trim-left " ") => ""
▶ (trim-left "") => ""
Returns a new string with trailing whitespaces removed.
Syntax
trim-right string => string
Arguments
Examples
▶ (trim-right " Albert ") => " Albert"
▶ (trim-right " ") => ""
▶ (trim-right "") => ""
Pads from the start of string with padString (multiple times, if needed) until the resulting string reaches the given length .
Syntax
pad-left string length padString(optional) => string
Arguments string: string
length: integer
padString: string
Examples
▶ (pad-left "Albert" 20) => " Albert"
▶ (pad-left "Albert" 20 "-*-") => "-*--*--*--*--*Albert"
▶ (pad-left "Albert" 5) => "Albert"
▶ (pad-left "Albert" -1) => "Albert"
Pads from the start of string with padString (multiple times, if needed) until the resulting string reaches the given length .
Syntax
pad-right string length padString(optional) => string
Arguments string: string
length: integer
padString: string
Examples
▶ (pad-right "Albert" 20) => "Albert "
▶ (pad-right "Albert" 20 "-*-") => "Albert-*--*--*--*--*"
▶ (pad-right "Albert" 5) => "Albert"
▶ (pad-right "Albert" -1) => "Albert"
Divides string into an array of substrings. The division is done by searching for delimiter . If limit as provided, at most limit number of substrings are returned.
Syntax
split string delimiter limit(optional) => string
Arguments string: string
delimiter: string
limit: non negative integer
Examples
▶ (split "Albert Mojir" " ") => ["Albert","Mojir"]
▶ (split "abcdefghijklmnopqrstuvw" (regexp "[aoueiy]")) => ["","bcd","fgh","jklmn","pqrst","vw"]
▶ (split "0123456789" "") => ["0","1","2","3","4","5","6","7","8","9"]
▶ (map number (split "0123456789" "" 5)) => [0,1,2,3,4]
Applies placeholders to a string. Support for basic pluralization - see examples. If pluralization is used, first placeholder must be a number.
Syntax
template templateString placeholder(zero to nine) => string
Arguments templateString: string
placeholder: string
Examples
▶ (template "Hi, $1 and $2" "Carl" "Larry") => "Hi, Carl and Larry"
▶ (template "Hi $1, $2, $3, $4, $5, $6, $7, $8 and $9" :A :B :C :D :E :F :G :H :I) => "Hi A, B, C, D, E, F, G, H and I"
▶ (template "$1 book||||$1 books" 0) => "0 books"
▶ (template "$1 book||||$1 books" 1) => "1 book"
▶ (template "$1 book||||$1 books" 2) => "2 books"
▶ (template "No book||||$1 book||||$1 books" 0) => "No book"
▶ (template "No book||||$1 book||||$1 books" 1) => "1 book"
▶ (template "No book||||$1 book||||$1 books" 10) => "10 books"
▶ (template "No book||||One book||||Two books||||Three books||||$1 books" 0) => "No book"
▶ (template "No book||||One book||||Two books||||Three books||||$1 books" 1) => "One book"
▶ (template "No book||||One book||||Two books||||Three books||||$1 books" 2) => "Two books"
▶ (template "No book||||One book||||Two books||||Three books||||$1 books" 3) => "Three books"
▶ (template "No book||||One book||||Two books||||Three books||||$1 books" 4) => "4 books"
Return code point for first character in input .
Syntax
to-char-code input => number
Arguments
Examples
▶ (to-char-code :A) => 65
▶ (to-char-code "Albert") => 65
Return character for code point code .
Syntax
from-char-code code => string
Arguments
Examples
▶ (from-char-code 65) => "A"
▶ (from-char-code 0) => "\u0000"
Returns a Base64 encoded string from value .
Syntax
encode-base64 value => string
Arguments
Examples
▶ (encode-base64 "Albert 🐻") => "QWxiZXJ0IPCfkLs="
Returns a Base64 decoded string from base64string .
Syntax
decode-base64 base64string => string
Arguments
Examples
▶ (decode-base64 "QWxiZXJ0IPCfkLs=") => "Albert 🐻"
Returns an escaped uri string.
Syntax
encode-uri-component uri => string
Arguments
Examples
▶ (encode-uri-component "Hi everyone!? 👍") => "Hi%20everyone!%3F%20%F0%9F%91%8D"
Returns an un-escaped uri string.
Syntax
decode-uri-component uri => string
Arguments
Examples
▶ (decode-uri-component "Hi%20everyone!%3F%20%F0%9F%91%8D") => "Hi everyone!? 👍"
Shifts number arithmetically left by count bit positions if count .
Syntax
bit-shift-left number count => integer
Arguments number: integer
count: non negative integer
Examples
▶ (bit-shift-left 1 10) => 1024
▶ (bit-shift-left -4 2) => -16
Shifts number arithmetically right by count bit positions if count .
Syntax
bit-shift-right number count => integer
Arguments number: integer
count: non negative integer
Examples
▶ (bit-shift-right 2048 10) => 2
▶ (bit-shift-right 4 10) => 0
Returns bitwise not of number .
Syntax
bit-not number => integer
Arguments
Examples
▶ (bit-not 0) => -1
▶ (bit-not 255) => -256
Returns bitwise and of all arguments.
Syntax
bit-and number(two or more) => integer
Arguments
Examples
▶ (bit-and 0b0011 0b0110) => 2
▶ (bit-and 0b0011 0b0110 0b1001) => 0
Returns bitwise and with complement of all arguments. (bit-and-not x y) <==> (bit-and x (bit-not y)).
Syntax
bit-and-not number(two or more) => integer
Arguments
Examples
▶ (bit-and-not 0b0011 0b0110) => 1
▶ (bit-and-not 0b0011 0b0110 0b1001) => 0
Returns bitwise or of all arguments. Return 0 if no arguments.
Syntax
bit-or number(two or more) => integer
Arguments
Examples
▶ (bit-or 0b0011 0b0110) => 7
▶ (bit-or 0b1000 0b0100 0b0010) => 14
Returns bitwise xor of all arguments.
Syntax
bit-xor number(two or more) => integer
Arguments
Examples
▶ (bit-xor 0b0011 0b0110) => 5
▶ (bit-xor 0b11110000 0b00111100 0b10101010) => 102
Flips bit number index .
Syntax
bit-flip number index => integer
Arguments number: integer
index: non negative integer
Examples
▶ (bit-flip 0b0011 1) => 1
▶ (bit-flip 0b1100 1) => 14
Clears bit number index .
Syntax
bit-clear number index => integer
Arguments number: integer
index: non negative integer
Examples
▶ (bit-clear 0b0011 1) => 1
▶ (bit-clear 0b1100 1) => 12
Sets bit number index .
Syntax
bit-set number index => integer
Arguments number: integer
index: non negative integer
Examples
▶ (bit-set 0b0011 1) => 3
▶ (bit-set 0b1100 1) => 14
Checks if bit number index is set.
Syntax
bit-test number index => boolean
Arguments number: integer
index: non negative integer
Examples
▶ (bit-test 0b0011 1) => true
▶ (bit-test 0b1100 1) => false
If value is falsy it throws AssertionError with message . If no message is provided, message is set to value .
Syntax
assert value message(optional) => any
Arguments value: any
message: string
Examples
▶ (try (assert 0 "Expected a positive value") (catch e e)) => AssertionError: Expected a positive value
(try (assert 0 "Expected a positive value") (catch e e))
^
If first is not the same as second it throws AssertionError.
Syntax
assert= first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert= 0 1 "Expected same values") (catch e e)) => AssertionError: Expected 0 to be 1. "Expected same values"
(try (assert= 0 1 "Expected same values") (catch e e))
^
▶ (try (assert= 0 1) (catch e e)) => AssertionError: Expected 0 to be 1.
(try (assert= 0 1) (catch e e))
^
▶ (try (assert= 1 1) (catch e e)) => null
If first is the same as second it throws AssertionError.
Syntax
assert-not= first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert-not= 0 0 "Expected different values") (catch e e)) => AssertionError: Expected 0 not to be 0. "Expected different values"
(try (assert-not= 0 0 "Expected different values") (catch e e))
^
▶ (try (assert-not= 0 0) (catch e e)) => AssertionError: Expected 0 not to be 0.
(try (assert-not= 0 0) (catch e e))
^
▶ (try (assert-not= 0 1) (catch e e)) => null
If first is not deep equal to second it throws AssertionError.
Syntax
assert-equal first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert-equal { :a 1 } { :a 2 } "Expected equal values") (catch e e)) => AssertionError: Expected
{
"a": 1
}
to deep equal
{
"a": 2
}. "Expected equal values"
(try (assert-equal { :a 1 } { :a 2 } "Expected equal values") (catch e e))
^
▶ (try (assert-equal { :a 1 } { :a 2 }) (catch e e)) => AssertionError: Expected
{
"a": 1
}
to deep equal
{
"a": 2
}.
(try (assert-equal { :a 1 } { :a 2 }) (catch e e))
^
▶ (try (assert-equal { :a 1 } { :a 1 }) (catch e e)) => null
If first is not deep equal to second it throws AssertionError.
Syntax
assert-not-equal first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert-not-equal { :a 2 } { :a 2 } "Expected different values") (catch e e)) => AssertionError: Expected {"a":2} not to deep equal {"a":2}. "Expected different values"
(try (assert-not-equal { :a 2 } { :a 2 } "Expected different values") (catch e e))
^
▶ (try (assert-not-equal { :a 2 } { :a 2 }) (catch e e)) => AssertionError: Expected {"a":2} not to deep equal {"a":2}.
(try (assert-not-equal { :a 2 } { :a 2 }) (catch e e))
^
▶ (try (assert-not-equal { :a 1 } { :a 2 }) (catch e e)) => null
If first is not greater than second it throws AssertionError.
Syntax
assert> first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert> 0 1 "Expected greater value") (catch e e)) => AssertionError: Expected 0 to be grater than 1. "Expected greater value"
(try (assert> 0 1 "Expected greater value") (catch e e))
^
▶ (try (assert> 0 0) (catch e e)) => AssertionError: Expected 0 to be grater than 0.
(try (assert> 0 0) (catch e e))
^
▶ (try (assert> 1 0) (catch e e)) => null
If first is not less than second it throws AssertionError.
Syntax
assert< first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert< 1 0 "Expected smaller value value") (catch e e)) => AssertionError: Expected 1 to be less than 0. "Expected smaller value value"
(try (assert< 1 0 "Expected smaller value value") (catch e e))
^
▶ (try (assert< 1 1) (catch e e)) => AssertionError: Expected 1 to be less than 1.
(try (assert< 1 1) (catch e e))
^
▶ (try (assert< 0 1) (catch e e)) => null
If first is less than second it throws AssertionError.
Syntax
assert>= first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert>= 0 1 "Expected greater value") (catch e e)) => AssertionError: Expected 0 to be grater than or equal to 1. "Expected greater value"
(try (assert>= 0 1 "Expected greater value") (catch e e))
^
▶ (try (assert>= 0 1) (catch e e)) => AssertionError: Expected 0 to be grater than or equal to 1.
(try (assert>= 0 1) (catch e e))
^
▶ (try (assert>= 1 1) (catch e e)) => null
If first is grater than second it throws AssertionError.
Syntax
assert<= first second message(optional) => nil
Arguments first: any
second: any
message: string
Examples
▶ (try (assert<= 1 0 "Expected smaller value value") (catch e e)) => AssertionError: Expected 1 to be less than or equal to 0. "Expected smaller value value"
(try (assert<= 1 0 "Expected smaller value value") (catch e e))
^
▶ (try (assert<= 1 0) (catch e e)) => AssertionError: Expected 1 to be less than or equal to 0.
(try (assert<= 1 0) (catch e e))
^
▶ (try (assert<= 1 1) (catch e e)) => null
If first is not true it throws AssertionError.
Syntax
assert-true first message(optional) => nil
Arguments first: any
message: string
Examples
▶ (try (assert-true false "Expected true") (catch e e)) => AssertionError: Expected false to be true. "Expected true"
(try (assert-true false "Expected true") (catch e e))
^
▶ (try (assert-true false) (catch e e)) => AssertionError: Expected false to be true.
(try (assert-true false) (catch e e))
^
▶ (try (assert-true true) (catch e e)) => null
If first is not false it throws AssertionError.
Syntax
assert-false first message(optional) => nil
Arguments first: any
message: string
Examples
▶ (try (assert-false true "Expected false") (catch e e)) => AssertionError: Expected true to be false. "Expected false"
(try (assert-false true "Expected false") (catch e e))
^
▶ (try (assert-false true) (catch e e)) => AssertionError: Expected true to be false.
(try (assert-false true) (catch e e))
^
▶ (try (assert-false false) (catch e e)) => null
If first is not truthy it throws AssertionError.
Syntax
assert-truthy first message(optional) => nil
Arguments first: any
message: string
Examples
▶ (try (assert-truthy false "Expected truthy") (catch e e)) => AssertionError: Expected false to be truthy. "Expected truthy"
(try (assert-truthy false "Expected truthy") (catch e e))
^
▶ (try (assert-truthy false) (catch e e)) => AssertionError: Expected false to be truthy.
(try (assert-truthy false) (catch e e))
^
▶ (try (assert-truthy 0) (catch e e)) => AssertionError: Expected 0 to be truthy.
(try (assert-truthy 0) (catch e e))
^
▶ (try (assert-truthy nil) (catch e e)) => AssertionError: Expected null to be truthy.
(try (assert-truthy nil) (catch e e))
^
▶ (try (assert-truthy "") (catch e e)) => AssertionError: Expected to be truthy.
(try (assert-truthy "") (catch e e))
^
▶ (assert-truthy true) => null
▶ (assert-truthy 1) => null
▶ (assert-truthy :x) => null
▶ (assert-truthy []) => null
▶ (assert-truthy {}) => null
If first is not falsy it throws AssertionError.
Syntax
assert-falsy first message(optional) => nil
Arguments first: any
message: string
Examples
▶ (try (assert-falsy true "Expected falsy") (catch e e)) => AssertionError: Expected true to be falsy. "Expected falsy"
(try (assert-falsy true "Expected falsy") (catch e e))
^
▶ (try (assert-falsy :x) (catch e e)) => AssertionError: Expected x to be falsy.
(try (assert-falsy :x) (catch e e))
^
▶ (try (assert-falsy []) (catch e e)) => AssertionError: Expected to be falsy.
(try (assert-falsy []) (catch e e))
^
▶ (try (assert-falsy {}) (catch e e)) => AssertionError: Expected [object Object] to be falsy.
(try (assert-falsy {}) (catch e e))
^
▶ (try (assert-falsy 1) (catch e e)) => AssertionError: Expected 1 to be falsy.
(try (assert-falsy 1) (catch e e))
^
▶ (assert-falsy false) => null
▶ (assert-falsy 0) => null
▶ (assert-falsy nil) => null
▶ (assert-falsy "") => null
If first is not nil it throws AssertionError.
Syntax
assert-nil first message(optional) => nil
Arguments first: any
message: string
Examples
▶ (assert-nil nil) => null
▶ (try (assert-nil true "Expected nil") (catch e e)) => AssertionError: Expected true to be nil. "Expected nil"
(try (assert-nil true "Expected nil") (catch e e))
^
▶ (try (assert-nil :x) (catch e e)) => AssertionError: Expected x to be nil.
(try (assert-nil :x) (catch e e))
^
▶ (try (assert-nil []) (catch e e)) => AssertionError: Expected to be nil.
(try (assert-nil []) (catch e e))
^
▶ (try (assert-nil {}) (catch e e)) => AssertionError: Expected [object Object] to be nil.
(try (assert-nil {}) (catch e e))
^
▶ (try (assert-nil 1) (catch e e)) => AssertionError: Expected 1 to be nil.
(try (assert-nil 1) (catch e e))
^
▶ (try (assert-nil false) (catch e e)) => AssertionError: Expected false to be nil.
(try (assert-nil false) (catch e e))
^
▶ (try (assert-nil 0) (catch e e)) => AssertionError: Expected 0 to be nil.
(try (assert-nil 0) (catch e e))
^
▶ (try (assert-nil "") (catch e e)) => AssertionError: Expected to be nil.
(try (assert-nil "") (catch e e))
^
If func does not throw, it throws AssertionError.
Syntax
assert-throws func message(optional) => nil
Arguments func: Function
message: string
Examples
▶ (assert-throws #(throw "Error")) => null
▶ (try (assert-throws #(identity "Error")) (catch e e)) => AssertionError: Expected function to throw.
(try (assert-throws #(identity "Error")) (catch e e))
^
If func does not throw expectedMessage , it throws AssertionError.
Syntax
assert-throws-error func expectedMessage message(optional) => nil
Arguments func: Function
expectedMessage: string
message: string
Examples
▶ (assert-throws-error #(throw "Error") "Error") => null
▶ (try (assert-throws #(throw "Not Error")) (catch e e)) => null
▶ (try (assert-throws #(identity "Error")) (catch e e)) => AssertionError: Expected function to throw.
(try (assert-throws #(identity "Error")) (catch e e))
^
If func throws, it throws AssertionError.
Syntax
assert-not-throws func message(optional) => nil
Arguments func: Function
message: string
Examples
▶ (assert-not-throws #(identity "Error")) => null
▶ (try (assert-not-throws #(throw "Error")) (catch e e)) => AssertionError: Expected function not to throw.
(try (assert-not-throws #(throw "Error")) (catch e e))
^