Clojureの基礎〜中身確認系、クオート、その他記号系〜
「Clojureの基礎〜Clojureの特徴〜 - oknknicの日記」に引き続きClojureの基礎メモ。
中身確認系
- #' varスペシャルフォーム(シンボルをvarに解決し、Varオブジェクトそのものを返す)に展開されるリーダーマクロ
#'str ; #'clojure.core/str (meta #'str) ;メタデータの取得 ; {:arglists ([] [x] [x & ys]), :ns #<Namespace clojure.core>, :name str, :column 1, :added "1.0", :static true, :doc "With no args, returns the empty string. With one arg x, returns\n x.toString(). (str nil) returns the empty string. With more than\n one arg, returns the concatenation of the str values of the args.", :line 504, :file "clojure/core.clj", :tag java.lang.String}
- @ deref関数(参照先の値を返す)に展開されるリーダーマクロ
(def hoge (ref "hogehoge")) @hoge ; "hogehoge" (def toge "togetoge") @#'toge ; "togetoge"
クオート
その他、記号系
- ^ メタデータのリーダーマクロ
(def ^{:info "info-val"} metatest) (meta #'metatest) ; {:info "info-val", :ns #<Namespace xml.core>, :name metatest, :column 1, :line 1, :file "NO_SOURCE_PATH"} ; なお、関数定義の場合は下記のようにメタデータを記載できる (defn meta-test-fn "ドキュメント" {:metadata "メタデータ"} [] 1) (meta #'meta-test-fn) ; {:arglists ([]), :metadata "メタデータ", :ns #<Namespace xml.core>, :name meta-test-fn, :column 1, :doc "ドキュメント", :line 1, :file "NO_SOURCE_PATH"} ; :tagメタデータ(戻り値および引数の期待する型)は下記のように略記可能 (defn ^String upper [^String s] (.toUpperCase s))
- -> 第1引数を引数として第2引数の関数を実行し、その結果を引数として第3引数の関数を実行、・・・この繰り返し
(defn square [x] (* x x)) (-> 2 square square) ; 16
参考
- 作者: Stuart Halloway and Aaron Bedra,川合史朗
- 出版社/メーカー: オーム社
- 発売日: 2013/04/26
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (11件) を見る