Clojureを学ぶ 4clojure編 2
以前、ちょっとだけかじった事があった Clojure を、もういっかい勉強中。
4clojureは以前やったことはあったけど、すっかり忘れているので、最初からやり直してます。
今回やったのは、16問目~25問目
- Sequences: map
- Sequences: filter
- Local bindings
- Let it Be
- Regular Expressions
- Intro to Reduce
- Simple Recursion
- Rearranging Code: ->
- Recurring Theme
- Rearranging Code: ->>
メモ
覚えておきたい、知らなかった内容のメモ
re-seq
Regular Expression に該当した文字列の遅延SEQuence を返す。
(re-seq #"\d+" "11.2 3")
=> ("11" "2" "3")
;
(re-seq #"\w+" "I have a pen")
=> ("I" "have" "a" "pen")
apply
複数の引数をとる関数に、引数が入った配列を適用させる?
(str \a \b \c)
=> "abc"
(str [\a \b \c])
=> "[\\a \\b \\c]"
(apply str [\a \b \c])
=> "abc"
->,->>
-> 最初の引数に適用する
->> 最後の引数に適用する
(-> 4 (/ 2)) ;(/ 4 2)
=> 2
(->> 4 (/ 2)) ;(/ 2 4)
=> 1/2
;
(-> 18 (/ 3 2)) ;(/ 18 3 2)
=> 3
(->> 18 (/ 3 2)) ;(/ 3 2 18)
=> 1/12
「Simple Recursion」
(= __ ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5))
(conj nil 1)の戻り値は list。
(foo 0) ;(when)を通らないのでnilが返る
=> nil
(foo 1) ;(conj nil 1)
=> (1)
(type (conj nil 1))
=> clojure.lang.PersistentList
loop / recur
Clojure で 末尾再帰最適化するときは、これを使う
ディスカッション
コメント一覧
まだ、コメントがありません