Skip to content

Instantly share code, notes, and snippets.

@kota
Last active December 15, 2015 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kota/5338398 to your computer and use it in GitHub Desktop.
Save kota/5338398 to your computer and use it in GitHub Desktop.
;基本
(+ 1 1)
(mod 5 4)
(< 1 2 3 4)
;if
(if true (println "True it is."))
;リスト
(list 1 2 3)
(first '(1 2 3))
(rest '(1 2 3))
;セット
(def name #{:jar-jar :chewbacca})
(name :chewbacca)
;マップ
(def mentors {:darth-vader "obi wan", :luke "yoda"})
(mentors :luke)
(:luke mentors)
;関数定義
(defn force-it [] (str "Use the force," "Luke."))
(force-it)
;バインディング
(def line [[0 0] [10 20]])
(defn line-end [ln] (last ln))
(defn line-end [[_ second]] second)
(def board [[:x :o :x] [:o :x :o] [:o :x :o]])
(defn center [[_ [_ c _] _]] c)
(def person {:name "Jabba" :profession "Gangster"})
(let [{name :name} person] (str "The persion's name is " name))
;無名関数
(def people ["Lea", "Han Solo"])
(defn twice-count [w] (* 2 (count w)))
(twice-count "Lando")
(map (fn [w] (* 2 (count w))) people)
(map #(* 2 (count %)) people)
;再帰
;recur1.clj
;recur2.clj
;シーケンス
(every? number? [1 2 3 :four])
(some nil? [1 2 nil])
(reduce + [1 2 3 4])
;遅延評価
(repeat 1)
(take 3 (repeat 1))
(take 5 (cycle [:lather :rinse :repeat]))
(take 5 (iterate inc 0))
(defn fib-pair [[a b]] [b (+ a b)])
(fib-pair [3 5])
(take 5 (iterate fib-pair [1 1]))
(take 5 (map first (iterate fib-pair [1 1])))
(nth (map first (iterate fib-pair [1 1])) 50)
;defrecordとprotocol
;compass.clj
;マクロ
(defmacro unless [test body]
(list 'if (list 'not test) body))
(macroexpand '(unless condition body))
;参照とSTM
;参照
(def movie (ref "Star Wars"))
@movie
(alter movie str ": The Empire Strikes Back")
(dosync (alter movie str ": The Empire Strikes Back"))
;アトム
(def danger (atom "Split at your own risk."))
@danger
user=> (reset! danger "Split with inpunity")
;atomecache.clj
;エージェント
(defn twice [x] (* 2 x))
(def tribbles (agent 1))
(send tribbles twice)
@tribbles
(defn slow-twice [x]
(do
(Thread/sleep 5000)
(* 2 x)))
@tribbles
(send tribbles slow-twice)
@tribbles
;フューチャ
(def finer-things (future (Thread/sleep 5000) "take time"))
@finer-things
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment