São simples
'(1 2 3)
Melhores para trabalhar com indices
[1 2 3]
Útil para armazenar dados e valores
{:foo 1 :bar 2}
Útil para armazenar dados não repetidos
#{1 2 3 4}
(def foo "bar")
(let [foo "whatever"] foo)
(fn [] (str "foo"))
(#(str "foo" %1 %2) "foo" "deu")
(defn foo [arg1 arg2] )
(ns foo)
*ns*
(require 'foo')
(require '[namespace :as foo])
(ns foo
(:require [namespace :as foo]))
(ns wonderland
(:require [clojure.set :as s]))
(defn common-fav-foods [foods1 foods2]
(let [food-set1 (set foods1)
food-set2 (set foods2)
common-foods (s/intersection food-set1 food-set2)]
(str "Common Foods: " common-foods)))
(common-fav-foods [:jam :brownies :toast]
[:lettuce :carrots :jam])
(= 1 2)
(not= 1 2)
(true? 'foo')
(false? 'foo')
(nil? 'foo')
(empty? [])
(every? (fn [x] (= x :drinkme)) [:drinkme :drinkme])
(every? #(= % :drinkme) [:drinkme :drinkme])
(not-any? #(= % :drinkme) [:poison :poison])
(some #(> % 3) [1 2 3 4 5])
(if (= :drinkme :drinkme)
"Try it"
"Don't try it")
(if-let [need-to-grow-small (> 5 1)]
"drink bottle"
"don't drink bottle")
(defn drink [need-to-grow-small]
(when need-to-grow-small "drink bottle"))
(drink true)
(drink false)
(let [bottle "mystery"]
(cond
(= bottle "poison") "don't touch"
(= bottle "drinkme") "grow smaller"
(= bottle "empty") "all gone"
:else "unknown"))
(let [bottle "drinkme"]
(case bottle
"poison" "don't touch"
"drinkme" "grow smaller"
"empty" "all gone"
"default"))
(defn grow [name direction]
(if (= direction :small)
(str name " is growing smaller")
(str name " is growing bigger")))
((partial grow "Alice") :small)
(defn toggle-grow [direction]
(if (= direction :small) :big :small))
(defn oh-my [direction]
(str "Oh My! You are growing " direction))
(oh-my (toggle-grow :small))
(defn surprise [direction]
((comp oh-my toggle-grow) direction))
(surprise :small)
With array
(let [[color size] ["blue" "small"]]
(str "The " color " door is " size))
(let [[color [size] :as original] ["blue" ["small"]]]
{:color color :size size :original original})
;; -> {:color "blue", :size "small", :original ["blue" ["small"]]}
With map
(let [{flower1 :flower1 flower2 :flower2}
{:flower1 "red" :flower2 "blue"}]
(str "The flowers are " flower1 " and " flower2))
(let [{flower1 :flower1 flower2 :flower2 :or {flower2 "missing"}}
{:flower1 "red"}]
(str "The flowers are " flower1 " and " flower2))
(let [{flower1 :flower1 :as all-flowers}
{:flower1 "red"}]
[flower1 all-flowers])
Shorthand with maps
(let [{:keys [flower1 flower2]}
{:flower1 "red" :flower2 "blue"}]
(str "The flowers are " flower1 " and " flower2))
with functions
(defn flower-colors [{:keys [flower1 flower2]}]
(str "The flowers are " flower1 " and " flower2))
(flower-color {:flower1 'blue' :flower2 'red'})
(take 5 (range))
;; -> (0 1 2 3 4)
(repeat 3 "rabbit")
;; -> ("rabbit" "rabbit" "rabbit")
(take 5 (repeat "rabbit"))
;; -> ("rabbit" "rabbit" "rabbit" "rabbit" "rabbit")
(rand-int 10)
;; -> 3
(repeat 5 (rand-int 10))
;; -> (7 7 7 7 7)
(repeatedly 5 #(rand-int 10))
;; -> (1 5 8 4 3)
(def adjs ["normal"
"too small"
"too big"
"is swimming"])
(defn alice-is [in out]
(if (empty? in)
out
(alice-is
(rest in)
(conj out
(str "Alice is " (first in))))))
(alice-is adjs [])
(defn alice-is [input]
(loop [in input
out []]
(if (empty? in)
out
(recur (rest in)
(conj out
(str "Alice is " (first in)))))))
(alice-is adjs)
(defn countdown [n]
(if (= n 0)
n
(recur (- n 1))))
(countdown 100000)
(def animals
["mouse" "duck" "dodo" "lory" "eaglet"])
(def colors
["brown" "black" "blue" "pink" "gold"])
(defn gen-animal-string [animal color]
(str color "-" animal))
(map gen-animal-string animals colors)
(map gen-animal-string animals (cycle ["brown" "black"]))
(reduce (fn [r x] (if (nil? x) r (conj r x)))
[]
[:mouse nil :duck nil nil :lory])
Filter
(filter keyword? [:mouse nil :duck nil])
Remove
(remove nil? [:mouse nil :duck nil])
For
(for [animal [:mouse :duck :lory]
color [:red :blue]
:let [animal-str (str "animal-"(name animal))
color-str (str "color-"(name color))
display-str (str animal-str "-" color-str)]] display-str)
When
(for [animal [:mouse :duck :lory]
color [:red :blue]
:let [animal-str (str "animal-"(name animal))
color-str (str "color-"(name color))
display-str (str animal-str "-" color-str)]
:when (= color :blue)]
display-str)
Flatten
(flatten [ [:duck [:mouse] [[:lory]]]])
Vec and Into
(vec '(1 2 3))
;; -> [1 2 3]
(into [] '(1 2 3))
;; -> [1 2 3]
(sorted-map :b 2 :a 1 :z 3)
;; -> {:a 1, :b 2, :z 3}
(into (sorted-map) {:b 2 :c 3 :a 1})
;; -> {:a 1, :b 2, :c 3}
(into {} [[:a 1] [:b 2] [:c 3]])
;; -> {:a 1, :b 2, :c 3}
(into [] {:a 1, :b 2, :c 3})
;; -> [[:c 3] [:b 2] [:a 1]]
Partition
(partition 3 [1 2 3 4 5 6 7 8 9])
;; -> ((1 2 3) (4 5 6) (7 8 9))
(partition-all 3 [1 2 3 4 5 6 7 8 9 10])
;; -> ((1 2 3) (4 5 6) (7 8 9) (10))
(partition-by #(= 6 %) [1 2 3 4 5 6 7 8 9 10])
;; -> ((1 2 3 4 5) (6) (7 8 9 10))
(def who-atom (atom :caterpillar))
(defn change [state]
(case state
:caterpillar :chrysalis
:chrysalis :butterfly
:butterfly))
(swap! who-atom change)
@who-atom
(defn inc-print [val]
(println val)
(inc val))
(def counter (atom 0))
(let [n 2]
(future (dotimes [_ n] (swap! counter inc-print)))
(future (dotimes [_ n] (swap! counter inc-print)))
(future (dotimes [_ n] (swap! counter inc-print))))
(def x (ref 1))
(def y (ref 1))
(defn new-values []
(dosync
(alter x inc)
(ref-set y (+ 2 @x))))
(let [n 2]
(future (dotimes [_ n] (new-values)))
(future (dotimes [_ n] (new-values))))
(def who-agent (agent :caterpillar))
(defn change [state]
(case state
:caterpillar :chrysalis
:chrysalis :butterfly
:butterfly))
(defn change-error [state]
(throw (Exception. "Boom!")))
(defn err-handler-fn [a ex]
(println "error " ex " value is " @a))
(send who-agent change)
(send-off who-agent change)
(send who-agent change-error)
(restart-agent who-agent :caterpillar)
(send who-agent change-error)
(set-error-mode! who-agent :continue)
(set-error-handler! who-agent err-handler-fn)
caterpillar".toUpperCase(); (.toUpperCase "caterpillar")
new String("Hi!!"); (String. "Hi!!")
host.getHostName(); (.getHostName host)
with defmulti
(defmulti who-are-you class)
(defmethod who-are-you java.lang.String [input]
(str "String - who are you? " input))
(defmethod who-are-you clojure.lang.Keyword [input]
(str "Keyword - who are you? " input))
(defmethod who-are-you java.lang.Long [input]
(str "Number - who are you? " input))
(defmethod who-are-you :default [input]
(str "I don't know - who are you? " input))
(who-are-you :alice)
;; -> "Keyword - who are you? :alice"
(who-are-you "Alice")
;; -> "String - who are you? Alice"
(who-are-you 123)
;; -> "Number - who are you? 123"
(who-are-you true)
(defmulti eat-mushroom (fn [height]
(if (< height 3)
:grow
:shrink)))
(defmethod eat-mushroom :grow [_]
"Eat the right side to grow.")
(defmethod eat-mushroom :shrink [_]
"Eat the left side to shrink.")
(eat-mushroom 1)
“;; -> "Eat the right side to grow.”
(eat-mushroom 9)
;; -> "Eat the left side to shrink.
with protocol
(defprotocol BigMushroom
(eat-mushroom [this]))
(extend-protocol BigMushroom
java.lang.String
(eat-mushroom [this]
(str (.toUpperCase this) " mmmm tasty!"))
clojure.lang.Keyword
(eat-mushroom [this]
(case this
:grow "Eat the right side!"
:shrink "Eat the left side!"))
java.lang.Long
(eat-mushroom [this]
(if (< this 3)
"Eat the right side to grow"
"Eat the left side to shrink")))
(eat-mushroom "Big Mushroom")
;; -> "BIG MUSHROOM mmmm tasty!
(eat-mushroom :grow)
;; -> "Eat the right side!
(eat-mushroom 1)
;; -> "Eat the right side to grow
with defrecord
(defrecord Mushroom [color height])
(def regular-mushroom (Mushroom. "white and blue polka dots" "2 inches"))
(.-color regular-mushroom)
;; -> "white and blue polka dots"
(.-height regular-mushroom)
;; -> "2 inches