Remainders - oliyh/learning-clojure GitHub Wiki

Unused bits

##Types

Some other useful literals and things that may surprise you

  • #"^[ab].*" -- a regular expression pattern
  • (/ 3 4) -- a ratio, example below
assertEquals(1.0, ((1.0 / 49) * 49)); // false - expected:<1.0> but was:<0.9999999999999999> - precision is lost
(= 1N (* 49 (/ 1 49))) ;; true - the ratio type preserves precision

##Functions and variables

The shorter shortcut

(def yo #(println "Yo, World!"))
(yo) ;; Yo, World!

The short form binds arguments to %n where n is the (1-based) index of the argument, e.g

(def birthday-greetings #(println "Happy Birthday" %1 "you're" %2))
(birthday-greetings "Anakin" 25) ;; Happy Birthday Anakin you're 25

##Sequences

Testing presence of an element (in this case, at index 1)

(contains? [1 2 3] 0) ;; true
(contains? [1 2 3] 3) ;; false

Iteration

Iterate through a sequence, presumably for side effects

(doseq [x [1 2 3]] (println "x:" x)) ;; x: 1 x: 2 x: 3

Sets

Can be operated on with all the same functions with the uniqueness becoming apparent in contains?

(contains? #{1 2 3} 0) ;; false
(contains? #{1 2 3} 3) ;; true

The idiomatic way of adding to and removing from a map is assoc and dissoc.

(assoc {:a 1 :b 2} :c 3) ;; {:c 3, :a 1, :b 2}
(dissoc {:a 1 :b 2} :b) ;; {:a 1}
⚠️ **GitHub.com Fallback** ⚠️