Skip to content

Instantly share code, notes, and snippets.

@alvarogarcia7
Created August 3, 2017 22:44
Show Gist options
  • Save alvarogarcia7/d08d244e85e645b95cb4a6e58f8769a8 to your computer and use it in GitHub Desktop.
Save alvarogarcia7/d08d244e85e645b95cb4a6e58f8769a8 to your computer and use it in GitHub Desktop.
Playing with group-by and destructuring
(def numbers [0 1 2 3 4 5 6 7 8 9])
=> #'es.joaquincaro.katas.Greeting/numbers
(filter odd? numbers)
=> (1 3 5 7 9)
(group-byodd? numbers)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: group-byodd? in this context, compiling:(/private/var/folders/6p/p1cct37x19bgtnss368w24mm0000gn/T/form-init4565011324126899375.clj:1:1)
(group-by odd? numbers)
=> {false [0 2 4 6 8], true [1 3 5 7 9]}
(let [[pares even? impares odd?]] (group-by odd? numbers))
IllegalArgumentException let requires an even number of forms in binding vector in es.joaquincaro.katas.Greeting:1 clojure.core/let (core.clj:4333)
(let [pares even? impares odd?] (group-by odd? numbers))
=> {false [0 2 4 6 8], true [1 3 5 7 9]}
(let [pares (get (group-by odd? numbers) false)]
pares)
=> [0 2 4 6 8]
(let [are-they-odd (group-by odd? numbers)
pares (get are-they-odd false)]
pares)
=> [0 2 4 6 8]
(let [are-they-odd (group-by odd? numbers)
{pares false} are-they-odd]
pares)
=> [0 2 4 6 8]
(let [are-they-odd (group-by odd? numbers)
{impares (not false)} are-they-odd]
impares)
=> [1 3 5 7 9]
(let [are-they-odd (group-by odd? numbers)
{x ((comp not) false)} are-they-odd]
x)
=> [1 3 5 7 9]
(let [are-they-odd (group-by odd? numbers)
{x ((comp not not) false)} are-they-odd]
x)
=> [0 2 4 6 8]
(let [are-they-odd (group-by odd? numbers)
{x ((comp not not identity) false)} are-they-odd]
x)
=> [0 2 4 6 8]
(let [are-they-odd (group-by odd? numbers)
{x false} are-they-odd]
x)
=> [0 2 4 6 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment