http://stackoverflow.com/questions/1768567/how-does-one-start-a-thread-in-clojure
Clojure fns are Runnable
clojureでは関数はRunnable
なんですね!!
便利!!
リンク貼ったstackoverflowに書いてあるようにfn
で作った関数でも、もちろんdefn
でも一緒
user=> (defn f [] (println "clojure!")) #'user/f user=> (.start (Thread. f)) nil user=> clojure!
これだけで定義した好きな関数を別スレッドで動かせる。便利。
以下、補足
ちなみに最初見た時わからなかったけど、.start
はjavaの関数呼び出す時の書き方。
clojureプログラミング入門-5 Javaの呼び出し - すにぺっと
.に続けてjavaのメソッドを書いて、その後にインスタンスを書くと呼べる。変数とかも同じ。
(doc .) ------------------------- . (.instanceMember instance args*) (.instanceMember Classname args*) (Classname/staticMethod args*) Classname/staticField Special Form The instance member form works for both fields and methods. They all expand into calls to the dot operator at macroexpansion time. Please see http://clojure.org/java_interop#dot
で、Thread.
っていうのは(new Thread)
と同じ。
user=> (doc new) ------------------------- new (Classname. args*) (new Classname args*) Special Form The args, if any, are evaluated from left to right, and passed to the constructor of the class named by Classname. The constructed object is returned. Please see http://clojure.org/java_interop#new
docも便利