Merge branch 'Matt-Practice' of https://github.com/matthewDavidson1997/clojure-koans into Matt-Practice
This commit is contained in:
commit
e5ec500e01
5 changed files with 24 additions and 24 deletions
File diff suppressed because one or more lines are too long
|
|
@ -4,22 +4,22 @@
|
|||
|
||||
(meditations
|
||||
"Wrap a quote around a list to suppress evaluation"
|
||||
(= (quote '(1 2 3 4 5)) '(1 2 3 4 5))
|
||||
(= (quote (1 2 3 4 5)) '(1 2 3 4 5))
|
||||
|
||||
"There is a shortcut too!"
|
||||
(= (quote ) '(1 2 3 4 5))
|
||||
(= (quote (1 2 3 4 5)) '(1 2 3 4 5))
|
||||
|
||||
"You can quote symbols as well as lists... without evaluation!"
|
||||
(= __ (let [age 9] (quote age)))
|
||||
(= 'age (let [age 9] (quote age)))
|
||||
|
||||
"You can use a literal list as a data collection without having Clojure try to call a function"
|
||||
(= (cons 1 (__ (2 3))) (list 1 2 3) (cons 1 [2 3]))
|
||||
(= (cons 1 (quote (2 3))) (list 1 2 3) (cons 1 [2 3]))
|
||||
|
||||
"The quote affects all of its arguments, not just the top level"
|
||||
(= (list 1 __) '(1 (+ 2 3)))
|
||||
(= (list 1 '(+ 2 3)) '(1 (+ 2 3)))
|
||||
|
||||
"Syntax-quote (`) acts similarly to the normal quote"
|
||||
(= (list __ __ __) `(1 2 3) '(1 2 3))
|
||||
(= (list 1 2 3) `(1 2 3) '(1 2 3))
|
||||
|
||||
"Unquote (~) within a syntax-quoted expression lets you mark specific expressions as requiring evaluation"
|
||||
(= (list __ __) `(1 ~(+ 2 3)) '(1 5)))
|
||||
(= (list 1 5) `(1 ~(+ 2 3)) '(1 5)))
|
||||
|
|
|
|||
|
|
@ -3,19 +3,19 @@
|
|||
|
||||
(meditations
|
||||
"To split a collection you can use the partition function"
|
||||
(= '((0 1) (2 3)) (__ 2 (range 4)))
|
||||
(= '((0 1) (2 3)) (partition 2 (range 4)))
|
||||
|
||||
"But watch out if there are not enough elements to form n sequences"
|
||||
(= '(__) (partition 3 [:a :b :c :d :e]))
|
||||
(= '([:a :b :c]) (partition 3 [:a :b :c :d :e]))
|
||||
|
||||
"You can use partition-all to include any leftovers too"
|
||||
(= __ (partition-all 3 (range 5)))
|
||||
(= '((0 1 2) (3 4)) (partition-all 3 (range 5)))
|
||||
|
||||
"If you need to, you can start each sequence with an offset"
|
||||
(= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 __ (range 13)))
|
||||
(= '((0 1 2) (5 6 7) (10 11 12)) (partition 3 5 (range 13)))
|
||||
|
||||
"Consider padding the last sequence with some default values..."
|
||||
(= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [__] (range 7)))
|
||||
(= '((0 1 2) (3 4 5) (6 :hello)) (partition 3 3 [:hello] (range 7)))
|
||||
|
||||
"... but notice that they will only pad up to the given sequence length"
|
||||
(= '((0 1 2) (3 4 5) __) (partition 3 3 [:these :are "my" "words"] (range 7))))
|
||||
(= '((0 1 2) (3 4 5) (6 :these :are)) (partition 3 3 [:these :are "my" "words"] (range 7))))
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
(:require [koan-engine.core :refer :all]))
|
||||
|
||||
(defn get-odds-and-evens [coll]
|
||||
(let [{odds true evens false} (group-by __ coll)]
|
||||
(let [{odds true evens false} (group-by odd? coll)]
|
||||
[odds evens]))
|
||||
|
||||
(meditations
|
||||
"To categorize a collection by some function, use group-by."
|
||||
(= __ (group-by count ["hello" "world" "foo" "bar"]))
|
||||
(= {3 ["foo" "bar"] 5 ["hello" "world"]} (group-by count ["hello" "world" "foo" "bar"]))
|
||||
|
||||
"You can simulate filter + remove in one pass"
|
||||
(= (get-odds-and-evens [1 2 3 4 5])
|
||||
|
|
@ -15,7 +15,7 @@
|
|||
[[1 3 5] [2 4]])
|
||||
|
||||
"You can also group by a primary key"
|
||||
(= __
|
||||
(= {1 [{:id 1 :name "Bob"} {:id 1 :last-name "Smith"}] 2 [{:id 2 :name "Jennifer"}]}
|
||||
(group-by :id [{:id 1 :name "Bob"}
|
||||
{:id 2 :name "Jennifer"}
|
||||
{:id 1 :last-name "Smith"} ]))
|
||||
|
|
@ -23,13 +23,13 @@
|
|||
"But be careful when you group by a non-required key"
|
||||
(= {"Bob" [{:name "Bob" :id 1}]
|
||||
"Jennifer" [{:name "Jennifer" :id 2}]
|
||||
__ [{:last-name "Smith" :id 1}]}
|
||||
nil [{:last-name "Smith" :id 1}]}
|
||||
(group-by :name [{:id 1 :name "Bob"}
|
||||
{:id 2 :name "Jennifer"}
|
||||
{:id 1 :last-name "Smith"}]))
|
||||
|
||||
"The true power of group-by comes with custom functions"
|
||||
(= __
|
||||
(= {:naughty-list [{:name "Jimmy" :bad true} {:name "Joe" :bad true}] :nice-list [{:name "Jane" :bad false}]}
|
||||
(group-by #(if (:bad %) :naughty-list :nice-list)
|
||||
[{:name "Jimmy" :bad true}
|
||||
{:name "Jane" :bad false}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@
|
|||
|
||||
(defmacro infix-concise [form]
|
||||
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
|
||||
__
|
||||
__))
|
||||
~(first form)
|
||||
~(nth form 2)))
|
||||
|
||||
(defmacro recursive-infix [form]
|
||||
(cond (not (seq? form))
|
||||
|
|
@ -27,13 +27,13 @@
|
|||
|
||||
(meditations
|
||||
"Macros are like functions created at compile time"
|
||||
(= __ (hello "Macros!"))
|
||||
(= "Hello, Macros!" (hello "Macros!"))
|
||||
|
||||
"I can haz infix?"
|
||||
(= __ (infix (9 + 1)))
|
||||
(= 10 (infix (9 + 1)))
|
||||
|
||||
"Remember, these are nothing but code transformations"
|
||||
(= __ (macroexpand '(infix (9 + 1))))
|
||||
(= '(+ 9 1) (macroexpand '(infix (9 + 1))))
|
||||
|
||||
"You can do better than that - hand crafting FTW!"
|
||||
(= '(* 10 2) (macroexpand '(infix-concise (10 * 2))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue