ListやVectorに対する再帰
例: index-of
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn index-of [value coll] | |
(loop [index 0 coll coll] | |
(cond (empty? coll) nil | |
(= value (first coll)) index | |
:else (recur (inc index) (rest coll))))) |
Destructuringを使うとこんな感じで書ける。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn index-of' [value coll] | |
(loop [index 0 [head & tail :as coll] coll] | |
(cond (empty? coll) nil | |
(= value head) index | |
:else (recur (inc index) tail)))) |
firstとrestがなくなりました。
例: end
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn end [coll] | |
(if (empty? (rest coll)) | |
(first coll) | |
(recur (rest coll)))) |
Destructuringは関数の引数部に直接書ける。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn end' [[head & tail]] | |
(if (empty? tail) | |
head | |
(recur tail))) |
リスト全体を束縛する変数を省略出来ました。
MapやRecordに対するDestructuring
レコードに対する操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defrecord Complex [real imaginary]) |
例: abs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn abs [complex] | |
(Math/sqrt (+ (Math/pow (:real complex) 2) | |
(Math/pow (:imaginary complex) 2)))) |
Destructuringを使うとこんな感じ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn abs' [{:keys [real imaginary]}] | |
(Math/sqrt (+ (Math/pow real 2) | |
(Math/pow imaginary 2)))) |
{:keys [foo bar ...]}はフィールドを列挙することでその値を束縛します。
例: add
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn add [complex complex'] | |
(assoc complex | |
:real (+ (:real complex) (:real complex')) | |
:imaginary (+ (:imaginary complex) (:imaginary complex')))) |
フィールドと違った名前の変数名を付ける場合は{foo' :foo bar' :bar ...}という記法が使えます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn add' [{:keys [real imaginary] :as complex} | |
{real' :real imaginary' :imaginary}] | |
(assoc complex | |
:real (+ real real') | |
:imaginary (+ imaginary imaginary'))) |
Destructuringを使うことで要素の取得、束縛を簡素にしませんか?