Skip to content

Instantly share code, notes, and snippets.

@cairesr
Last active August 29, 2016 00:31
Show Gist options
  • Select an option

  • Save cairesr/565ae17ae98bc06f1a3b314cb7ea96fa to your computer and use it in GitHub Desktop.

Select an option

Save cairesr/565ae17ae98bc06f1a3b314cb7ea96fa to your computer and use it in GitHub Desktop.

Revisions

  1. cairesr revised this gist Aug 29, 2016. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions clojure_structures.md
    Original file line number Diff line number Diff line change
    @@ -139,4 +139,15 @@ Binds values to new symbols. Values can be expressions. It's a way of saying: "c
    (if (> iteration 9)
    (println "Got it, exiting")
    (recur (inc iteration))))
    ; => Iteration 0
    ; => Iteration 1
    ; => Iteration 2
    ; => Iteration 3
    ; => Iteration 4
    ; => Iteration 5
    ; => Iteration 6
    ; => Iteration 7
    ; => Iteration 8
    ; => Iteration 9
    ; => Got it, exiting
    ```
  2. cairesr renamed this gist Aug 29, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions clojure_data_structures.md → clojure_structures.md
    Original file line number Diff line number Diff line change
    @@ -129,4 +129,14 @@ Binds values to new symbols. Values can be expressions. It's a way of saying: "c
    (def musics ["Phosphene Dream" "Pink Cigarrete" "Beyond the Realms of Death"])
    (let [[first-music & the-list] musics]
    [first-music the-list]) => ["Phosphene Dream" ("Pink Cigarrete" "Beyond the Realms of Death")]
    ```
    ### Loop
    ```clojure
    (loop [iteration 0]
    (println "Iteration " iteration)
    (if (> iteration 9)
    (println "Got it, exiting")
    (recur (inc iteration))))
    ```
  3. cairesr revised this gist Aug 29, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clojure_data_structures.md
    Original file line number Diff line number Diff line change
    @@ -117,7 +117,7 @@ Sets are collections of unique elements.

    ### Let

    Binds values to new symbols. Values can be expressions.
    Binds values to new symbols. Values can be expressions. It's a way of saying: "create local names for existent values"

    ```clojure
    (def musics ["Phosphene Dream" "Pink Cigarrete" "Beyond the Realms of Death"])
  4. cairesr revised this gist Aug 29, 2016. 1 changed file with 9 additions and 0 deletions.
    9 changes: 9 additions & 0 deletions clojure_data_structures.md
    Original file line number Diff line number Diff line change
    @@ -116,8 +116,17 @@ Sets are collections of unique elements.
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. - Daniel Higginbotham (Clojure for the Brave and True)

    ### Let

    Binds values to new symbols. Values can be expressions.

    ```clojure
    (def musics ["Phosphene Dream" "Pink Cigarrete" "Beyond the Realms of Death"])
    (let [couple (take 2 musics)]
    (print couple)) => (Phosphene Dream Pink Cigarrete)
    ```

    ```clojure
    (def musics ["Phosphene Dream" "Pink Cigarrete" "Beyond the Realms of Death"])
    (let [[first-music & the-list] musics]
    [first-music the-list]) => ["Phosphene Dream" ("Pink Cigarrete" "Beyond the Realms of Death")]
    ```
  5. cairesr revised this gist Aug 29, 2016. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion clojure_data_structures.md
    Original file line number Diff line number Diff line change
    @@ -113,4 +113,11 @@ Sets are collections of unique elements.
    (get #{nil 123} nil) => nil
    (get #{1 2 3} 85) = nil
    ```
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. - Daniel Higginbotham (Clojure for the Brave and True)
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. - Daniel Higginbotham (Clojure for the Brave and True)

    ### Let
    ```clojure
    (def musics ["Phosphene Dream" "Pink Cigarrete" "Beyond the Realms of Death"])
    (let [couple (take 2 musics)]
    (print couple)) => (Phosphene Dream Pink Cigarrete)
    ```
  6. cairesr revised this gist Aug 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion clojure_data_structures.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,8 @@
    ```
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`. - Daniel Higginbotham (Clojure for the Brave and True)
    For a nubie (as I am), the rule of thumb is:
    > use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`. - Daniel Higginbotham (Clojure for the Brave and True)
    ### set
    Sets are collections of unique elements.
  7. cairesr renamed this gist Aug 1, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  8. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -112,4 +112,4 @@ Sets are collections of unique elements.
    (get #{nil 123} nil) => nil
    (get #{1 2 3} 85) = nil
    ```
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. contains? may be the better option when you’re testing specifically for set membership. - Daniel Higginbotham (Clojure for the Brave and True)
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. - Daniel Higginbotham (Clojure for the Brave and True)
  9. cairesr revised this gist Aug 1, 2016. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -104,4 +104,12 @@ Sets are collections of unique elements.
    (nil #{:a :2}) => throws IllegalArgumentException - Can't call nil
    ("word" #{"word" 1}) => throws ClassCastException - String cannot be cast to clojure.lang.IFn
    (1 #{"word" 1}) => throws ClassCastException - java.lang.Long cannot be cast to clojure.lang.IFn
    ```
    ```

    ```clojure
    (get #{1 "a"} "a") => "a"
    (get #{1 1/4} 1/4) => 1/4
    (get #{nil 123} nil) => nil
    (get #{1 2 3} 85) = nil
    ```
    > Notice that using get to test whether a set contains nil will always return nil, which is confusing. contains? may be the better option when you’re testing specifically for set membership. - Daniel Higginbotham (Clojure for the Brave and True)
  10. cairesr revised this gist Aug 1, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -102,4 +102,6 @@ Sets are collections of unique elements.
    ```clojure
    (:a #{:a :2}) => :a finds the element using the keyword as the function and the set as the argument for that function
    (nil #{:a :2}) => throws IllegalArgumentException - Can't call nil
    ("word" #{"word" 1}) => throws ClassCastException - String cannot be cast to clojure.lang.IFn
    (1 #{"word" 1}) => throws ClassCastException - java.lang.Long cannot be cast to clojure.lang.IFn
    ```
  11. cairesr revised this gist Aug 1, 2016. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -97,4 +97,9 @@ Sets are collections of unique elements.
    ```clojure
    (contains? #{1 2} 1) => true
    (contains? #{nil "a"} nil) => true
    ```

    ```clojure
    (:a #{:a :2}) => :a finds the element using the keyword as the function and the set as the argument for that function
    (nil #{:a :2}) => throws IllegalArgumentException - Can't call nil
    ```
  12. cairesr revised this gist Aug 1, 2016. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -75,20 +75,26 @@
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`. - Daniel Higginbotham (Clojure for the Brave and True)
    ### hash-set
    ### set
    Sets are collections of unique elements.
    ```clojure
    #{"awesome" 1 :simbolo 1/4} => #{"awesome" 1 :simbolo 1/4} creates a set literal
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a set
    #{"awesome" 1 :simbolo 1/4} => #{"awesome" 1 :simbolo 1/4} creates a hash-set literal
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a hash-set
    ```

    ```clojure
    (hash-set 1 1 2 2) => #{1 2}
    (hash-set [1 1 2 2]) => #{[1 1 2 2]}
    (hash-set [1 1 2 2] [1 1 2 2]) => #{[1 1 2 2]}
    #{1 1 2 2} => throws IllegalArgumentException (Duplicate key)
    ```

    ```clojure
    (set [1 1 2 2]) => #{1 2} creates a set with the given vector
    ```

    ```clojure
    (contains? #{1 2} 1) => true
    (contains? #{nil "a"} nil) => true
    ```
  13. cairesr revised this gist Aug 1, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -82,7 +82,13 @@ Sets are collections of unique elements.
    #{"awesome" 1 :simbolo 1/4} => #{"awesome" 1 :simbolo 1/4} creates a set literal
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a set
    ```

    ```clojure
    (hash-set 1 1 2 2) => #{1 2}
    #{1 1 2 2} => throws IllegalArgumentException (Duplicate key)
    ```

    ```
    (contains? #{1 2} 1) => true
    (contains? #{nil "a"} nil) => true
    ```
  14. cairesr revised this gist Aug 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -83,5 +83,6 @@ Sets are collections of unique elements.
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a set
    ```
    ```clojure
    #{1 1 2 2} => #{1 2}
    (hash-set 1 1 2 2) => #{1 2}
    #{1 1 2 2} => throws IllegalArgumentException (Duplicate key)
    ```
  15. cairesr revised this gist Aug 1, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -81,4 +81,7 @@ Sets are collections of unique elements.
    ```clojure
    #{"awesome" 1 :simbolo 1/4} => #{"awesome" 1 :simbolo 1/4} creates a set literal
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a set
    ```
    ```clojure
    #{1 1 2 2} => #{1 2}
    ```
  16. cairesr revised this gist Aug 1, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -75,5 +75,10 @@
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`. - Daniel Higginbotham (Clojure for the Brave and True)
    ### sets
    Sets are collections of unique elements.
    ### hash-set
    Sets are collections of unique elements.
    ```clojure
    #{"awesome" 1 :simbolo 1/4} => #{"awesome" 1 :simbolo 1/4} creates a set literal
    (hash-set "awesome 1 :simbolo 1/4) => #{"awesome" 1 :simbolo 1/4} creates a set
    ```
  17. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -73,8 +73,7 @@
    ```
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > - Daniel Higginbotham (Clojure for the Brave and True)
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`. - Daniel Higginbotham (Clojure for the Brave and True)
    ### sets
    Sets are collections of unique elements.
  18. cairesr revised this gist Aug 1, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -73,8 +73,8 @@
    ```
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > Daniel Higginbotham - Clojure for the Brave and True
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > - Daniel Higginbotham (Clojure for the Brave and True)
    ### sets
    Sets are collections of unique elements.
  19. cairesr revised this gist Aug 1, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,8 @@
    ```
    ### vectors x lists
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > Daniel Higginbotham - Clojure for the Brave and True
    ### sets
    > Sets are collections of unique elements.
    Sets are collections of unique elements.
  20. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@
    ```
    ### vectors x lists
    For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    > For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    ### sets
    > Sets are collections of unique elements.
  21. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -76,4 +76,4 @@
    For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    ### sets
    #### Sets are collections of unique elements.
    > Sets are collections of unique elements.
  22. cairesr revised this gist Aug 1, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -73,4 +73,7 @@
    ```
    ### vectors x lists
    For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
    ### sets
    #### Sets are collections of unique elements.
  23. cairesr revised this gist Aug 1, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,7 @@
    ```
    ```clojure
    (conj '(1 2 3) 4 5 6) ;; => (6 5 4 1 2 3) (elements are added at the beginning of the list)
    ```
    ```
    ### vectors x lists
    For a nubie (as I am), the rule of thumb is: use a `list` when you need to add `elements` at the beginning of a `sequence`. Or when you write a `macro`. Otherwise, use `vector`.
  24. cairesr revised this gist Aug 1, 2016. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -42,9 +42,9 @@
    ### vector
    ```clojure
    [1 2 3] ;; creates a vector literal
    (vector 1 2 3) ;; creates a vector through the vector function
    (vector [1 {a: "opa" b: 1/2} "da string" 23/39]) ;; a vector can accept elements of any type
    [1 2 3] ;; => [1 2 3] creates a vector literal
    (vector 1 2 3) ;; [1 2 3] creates a vector through the vector function
    (vector [1 {a: "opa" b: 1/2} "da string" 23/39]) ;; => [1 {a: "opa" b: 1/2} "da string" 23/39] a vector accepts any type
    ```
    ```clojure
  25. cairesr revised this gist Aug 1, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -61,7 +61,8 @@
    ### lists
    ```clojure
    '(1 2 3 4) ;; creates a list literal
    '(1 2 3 4) ;; => (1 2 3 4) creates a list literal
    (list 1 :simbolo {:name "Cartman" :age 7} 1/2) ;; => (1 :simbolo {:name "Cartman" :age 7} 1/2) creates a list
    ```
    ```clojure
    (nth '(1 2 3 4) 0) ;; => 1
  26. cairesr revised this gist Aug 1, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -56,7 +56,7 @@
    ```
    ```clojure
    (conj [1 2 3] 4 5 6) ;; => [1 2 3 4 5 6]
    (conj [1 2 3] 4 5 6) ;; => [1 2 3 4 5 6] (elements are added at the end of the vector)
    ```
    ### lists
    @@ -66,4 +66,7 @@
    ```clojure
    (nth '(1 2 3 4) 0) ;; => 1
    (nth '(1 2 3 4) 4) ;; => throws IndexOutOfBoundException
    ```
    ```clojure
    (conj '(1 2 3) 4 5 6) ;; => (6 5 4 1 2 3) (elements are added at the beginning of the list)
    ```
  27. cairesr revised this gist Aug 1, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -62,4 +62,8 @@
    ### lists
    ```clojure
    '(1 2 3 4) ;; creates a list literal
    ```
    ```clojure
    (nth '(1 2 3 4) 0) ;; => 1
    (nth '(1 2 3 4) 4) ;; => throws IndexOutOfBoundException
    ```
  28. cairesr revised this gist Aug 1, 2016. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -46,10 +46,20 @@
    (vector 1 2 3) ;; creates a vector through the vector function
    (vector [1 {a: "opa" b: 1/2} "da string" 23/39]) ;; a vector can accept elements of any type
    ```
    ```clojure
    (get [25 35 300] 0) ;; => 1 (0th element)
    (get [25 35 300] 2) ;; => 300 (2nd element)
    (get [25 35 300] 3) ;; => nil (there's no 3rd element)
    ([25 35 300] 0) ;; => 1 (0th element)
    ([25 35 300] 3) ;; => throws IndexOutOfBoundsException (different behaviour from `(get [25 35 300] 3)`)
    ```
    ```clojure
    (conj [1 2 3] 4 5 6) ;; => [1 2 3 4 5 6]
    ```
    ### lists
    ```clojure
    '(1 2 3 4) ;; creates a list literal
    ```
  29. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -44,6 +44,7 @@
    ```clojure
    [1 2 3] ;; creates a vector literal
    (vector 1 2 3) ;; creates a vector through the vector function
    (vector [1 {a: "opa" b: 1/2} "da string" 23/39]) ;; a vector can accept elements of any type
    ```
    ```clojure
    (get [25 35 300] 0) ;; => 1 (0th element)
  30. cairesr revised this gist Aug 1, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion clojure_facts.md
    Original file line number Diff line number Diff line change
    @@ -50,5 +50,5 @@
    (get [25 35 300] 2) ;; => 300 (2nd element)
    (get [25 35 300] 3) ;; => nil (there's no 3rd element)
    ([25 35 300] 0) ;; => 1 (0th element)
    ([25 35 300] 0) ;; => throws IndexOutOfBoundsException (different behaviour from `(get [25 35 300] 3)`)
    ([25 35 300] 3) ;; => throws IndexOutOfBoundsException (different behaviour from `(get [25 35 300] 3)`)
    ```