clojureland
clojureland
Clojure Land
12 posts
Don't wanna be here? Send us removal request.
clojureland · 7 years ago
Text
Cond vs Case
Cond will evaluate each test and return the first one that’s truthy, not running any further. if nothing matches, nil is returned.
(cond   (= “cats” “dogs”) “They’re equal!”   (> 2 1) “I believe it”   (= “bob” 1) “this will never run”)
When to use: evaluating multiple conditions
Case will evaluate for identity instead of equality (things like (> 1 2.. etc). When no match is found, it throws an exception. If you include a trailing expression ("Cats are cool” in this case) it will evaluate to that instead of throwing an error in the absence of a match.
(case cats   0 “sadness, no cats here”   1 “hurray! a cat!”   4 “More kitties!”   “Harold” “It’s just Harold”   “Cats are cool”)
(def cats “meow”) => “Cats are cool”
If you include a trailing expression ("Cats are cool” in this case) it will evaluate to that if no match is found.
When to use: identifying a symbol with specific value
Here’s to hoping this ends my confusion... might add condp and cond-> later for ✨more confusion ✨
0 notes
clojureland · 8 years ago
Text
Digit Splitting
The easiest way I’ve found to split a number into individual digits.
(defn split-digits [number]   (map #(Character/digit % 10) (str number))) 
(split-digits 123456) => (1 2 3 4 5 6)
0 notes
clojureland · 8 years ago
Text
keep, keep-indexed, and map-indexed
Really great explanation of these: http://www.spacjer.com/blog/2015/11/24/lesser-known-clojure-keep-and-keep-indexed-functions/
some examples from the post:
keep:
(keep f col)
(keep :a '({:a 1} {:b 3} {:a 8} {:z 7})) => (1 8)
instead of:
(filter :a '({:a 1} {:b 3} {:a 8} {:z 7})) => ({:a 1} {:a 8})
(map :a '({:a 1} {:b 3} {:a 8} {:z 7})) => (1 nil 8 nil)
keep-indexed:
(keep-indexed (fn [index item]   [index (:a item)]) '({:a 1} {:b 3} {:a 8} {:z 7}))
=> ([0 1] [1 nil] [2 8] [3 nil])
(keep-indexed (fn [index item]   (when (even? index)     (:a item))) '({:a 1} {:a 5} {:b 3} {:a 8} {:a 12} {:z 7}))
=> (1 12)
map-indexed:
(map-indexed (fn [index item] [index item]) '(:a :b :c :d :e :f))
=> ([0 :a] [1 :b] [2 :c] [3 :d] [4 :e] [5 :f])
0 notes
clojureland · 9 years ago
Text
Apply
Apply allows you to put a function in front of a vector.
(=  (apply + [1 2 3 4]) (+ 1 2 3 4))
0 notes
clojureland · 9 years ago
Text
Mutating
When mutating use “!”.
(defn update-atom! [new-vals] (swap! the-atom (fn [old-val] new-vals)))
0 notes
clojureland · 9 years ago
Text
Exit Clause
Always put your exit condition first in an if statement. If it’s second, do if-not.
(if true exit do-something) (if-not false exit do-something)
0 notes
clojureland · 10 years ago
Text
cljs - js calling methods and properties
to call a method of a js object in clojurescript use .methodName, to get a property from that object .-methodName
0 notes
clojureland · 10 years ago
Text
:pre and :post
:pre creates a check before the body runs, if it passes, it runs the body, if it fails the body doesn’t run.
    ((fn [a] {:pre [(pos? a)]} (println a) (- a)) 1)     => -1     ((fn [a] {:pre [(pos? a)]} (println a) (- a)) -1)     => java.lang.AssertionError: Assert failed: (pos? a)
:post is a check made after the code is run
    ((fn [a] {:post [(pos? %)]} (println a) (- a)) -1)     => 1     ((fn [a] {:post [(pos? %)]} (println a) (- a)) 1)     => java.lang.AssertionError: Assert failed: (pos? %)
http://blog.fogus.me/2009/12/21/clojures-pre-and-post/
1 note · View note
clojureland · 10 years ago
Text
if and not or if-not when
if if thing is true, do one thing, if false do another.
(if [true]    (do this)    (otherwise, this))
and
both or all things must be true to run function
(and (this must be true)        (so must this)        (and any number of things must be true)) => true (and true true false) => false
not returns true if statement is false
(not (= 1 2)) => true (not (= 1 1)) => false
or if either of the statements are true
(or true false) => true (or (this is true)     (this is false)) => false
if-not flip if from true statements to false
(if-not (this is false)     (run this if statement evals to false)     (otherwise run this)) => (run this if statement evals to false)
when constantly checks and when it finds true, runs
(when (true)     (+ 1 1)) => 2
when-not runs when the statement evals to false
(when-not (false)     (println ”hi”)) => hi
1 note · View note
clojureland · 10 years ago
Text
-in
assoc-in nests a map in a map by only providing a vector of keys
(assoc-in cats [:type :fluffy :colour] “black”) => {:type {:fluffy {:colour “black”}}}
get-in navigates the nested structure to return a value
(get-in cats [:type :fluffy]) => {:colour “black”}
update-in finds a value and updates it in a nested structure
(update-in cats [:type :fluffy :colour] “purple”) => {:type {:fluffy {:colour “purple}}}
0 notes
clojureland · 10 years ago
Text
Comment macros
; comments out code until the end of the line
;; I am a comment
#_ comments out a form
#_ (everything (in (this) (gets) commented) out) but not this
1 note · View note
clojureland · 10 years ago
Text
What I’m doing
I’m learning Clojure, I’m going to take notes and write things here how I understand them.
0 notes