「そもそも概念が分からない」という方に向けた説明です.
簡略化のため大幅に説明を省略しています. ご容赦ください.
誤りは御指摘いただければ幸いです.
「ファンクタ」, 「アプリカティブ」, 「モナド」 などは Haskell に限定された概念・用語ではありませんが,
(map #(.getName %) (.getMethods java.io.File))
; -> ("equals" "toString" "hashCode" "compareTo" "compareTo" "getName" "length" "getParent" "isAbsolute" "getCanonicalPath" "setReadOnly" "list" "list" "delete" "getParentFile" "getPath" "getAbsolutePath" "getAbsoluteFile" "getCanonicalFile" "toURL" "toURI" "canRead" "canWrite" "exists" "isDirectory" "isFile" "isHidden" "lastModified" "createNewFile" "deleteOnExit" "listFiles" "listFiles" "listFiles" "mkdir" "mkdirs" "renameTo" "setLastModified" "setWritable" "setWritable" "setReadable" "setReadable" "setExecutable" "setExecutable" "canExecute" "listRoots" "getTotalSpace" "getFreeSpace" "getUsableSpace" "createTempFile" "createTempFile" "wait" "wait" "wait" "getClass" "notify" "notifyAll")
(def prime-numbers | |
((fn f [x] | |
(cons x | |
(lazy-seq | |
(f (first | |
(drop-while | |
(fn [n] | |
(some #(zero? (mod n %)) | |
(take-while #(<= (* % %) n) prime-numbers))) | |
(iterate inc (inc x)))))))) |
(defn factorize [n] | |
((fn f [n [h & r :as ps]] | |
(cond (< n 2) '() | |
(zero? (mod n h)) (cons h (lazy-seq (f (quot n h) ps))) | |
:else (recur n r))) | |
n prime-numbers))) |
Assumed that you have set leiningen up and can use it.
Copy project.clj
, repl-test.cljs
and brepl-test.html
from this gist
or git clone this gist and move or copy repl-test.cljs under src
directory.
$ git clone https://gist.github.com/6183122.git
$ cd 6183122/
Prepare an environment according to A minimum setting to use browser REPL of ClojureScript.
Input lines of dom-example.cljs
to the REPL.
Here's a SCREEN CAST.
Enjoy!
(def s "(- a b)"))
のような文字列があるとき, 実行時に決まる値で a や b を束縛して, この式を評価したいとする.
(let [a 4 b 3] (eval (read-string s)))
(require '[clojure.test :refer (with-test is run-tests)]) | |
(with-test | |
(defn tails [coll] | |
(take-while seq (iterate rest coll))) | |
(is (= (tails '(:a :b :c)) | |
'((:a :b :c) (:b :c) (:c))))) |
(require '[clojure.test :refer (with-test are run-tests)]) | |
(with-test | |
(defn map-between [f s] | |
(map (fn [[a b]] (f a b)) (partition 2 1 s))) | |
(are [f s _ r] (= (map-between f s) r) | |
#(- %2 %1) '() -> '() | |
#(- %2 %1) '(0) -> '() | |
#(- %2 %1) '(0 1) -> '(1) |