CLOVER🍀

That was when it all began.

doc、find-doc、source、apropos

Clojureについては、普段はClojureDocsとかGoogleを使って調べていますが、REPLで使えるマクロ/関数を使ってもよさそう。

まー、ClojureDocsだとサンプルとかも載っているので、それなりに効果はあるのですが。

docマクロ

指定した関数のdoc-stringを表示するマクロです。

user=> (doc pmap)
-------------------------
clojure.core/pmap
([f coll] [f coll & colls])
  Like map, except f is applied in parallel. Semi-lazy in that the
  parallel computation stays ahead of the consumption, but doesn't
  realize the entire result unless required. Only useful for
  computationally intensive functions where the time of f dominates
  the coordination overhead.
nil

sourceマクロ

指定した関数のソースを表示するマクロです。

user=> (source pmap)
(defn pmap
  "Like map, except f is applied in parallel. Semi-lazy in that the
  parallel computation stays ahead of the consumption, but doesn't
  realize the entire result unless required. Only useful for
  computationally intensive functions where the time of f dominates
  the coordination overhead."
  {:added "1.0"
   :static true}
  ([f coll]
   (let [n (+ 2 (.. Runtime getRuntime availableProcessors))
         rets (map #(future (f %)) coll)
         step (fn step [[x & xs :as vs] fs]
                (lazy-seq
                 (if-let [s (seq fs)]
                   (cons (deref x) (step xs (rest s)))
                   (map deref vs))))]
     (step rets (drop n rets))))
  ([f coll & colls]
   (let [step (fn step [cs]
                (lazy-seq
                 (let [ss (map seq cs)]
                   (when (every? identity ss)
                     (cons (map first ss) (step (map rest ss)))))))]
     (pmap #(apply f %) (step (cons coll colls))))))
nil

find-doc関数

指定したキーワードを、doc-string内から検索してくれる関数です。

user=> (find-doc "filter")
-------------------------
clojure.core/doseq
([seq-exprs & body])
Macro
  Repeatedly executes body (presumably for side-effects) with
  bindings and filtering as provided by "for".  Does not retain
  the head of the sequence. Returns nil.
-------------------------
clojure.core/filter
([pred coll])
  Returns a lazy sequence of the items in coll for which
  (pred item) returns true. pred must be free of side-effects.
-------------------------
clojure.core/filterv
([pred coll])
  Returns a vector of the items in coll for which
  (pred item) returns true. pred must be free of side-effects.
-------------------------

 〜省略〜

-------------------------
clojure.repl/source-fn
([x])
  Returns a string of the source code for the given symbol, if it can
  find it.  This requires that the symbol resolve to a Var defined in
  a namespace for which the .clj is in the classpath.  Returns nil if
  it can't find the source.  For most REPL usage, 'source' is more
  convenient.

  Example: (source-fn 'filter)
nil

正規表現OK。

user=> (find-doc #".*map")

apropos関数

ロードされている範囲の名前空間で、パターンに一致する関数を検索します。String、もしくは正規表現が使用可能。

user=> (apropos "map")
(sorted-map ns-unmap zipmap mapv map mapcat sorted-map-by map? amap struct-map proxy-mappings pmap map-indexed ns-map array-map hash-map)

user=> (apropos #".*map$")
(sorted-map ns-unmap zipmap map amap struct-map pmap ns-map array-map hash-map)

…ちょっとずつ、使っていこうっと。