Clojureå®å ¨ã«ãã¹ã¿ã¼ãã話
TL;DR
- Leiningenã¤ã³ã¹ã³ãã¦IntelliJ IDEAã«Cursiveãã©ã°ã¤ã³å ¥ããã¨Leiningenã§çæããããã¸ã§ã¯ãæ®éã«èªã¿è¾¼ãããæ¬å¼§è¾ºãã®åé¡ãã ãã¶å¿«é©ã«ãªããã
- ãã¹ãç°å¢ä½ãããªãã¦ãã¨ããããæ´ã£ã¦ãã£ã½ã
- å®å ¨ã«ãã¹ã¿ã¼ã¨ã¯HelloWorldãã£ããæ®éã®FizzBuzzãã£ããåºæ¥ãããæãé èª
çµç·¯
ã¯ãã¼ã¸ã£ã¼åºæ¥ãªãã®ã§ã¯ãã¼ã¸ã£ã¼ã®ãªã¹ãã«å ¥ãããã¦ãªããã»ã¼ãã
@kamekoopa è¦ééãã§ãªããã°å ¥ã£ã¦ããæ°ããã¾ãããw
@kamekoopa ã¨ã¼ã¾ãããã¨åããªã¹ãã«...
@kamekoopa ã¨ãããã¨ã§ãã²...
æåã®ä¸æ©
Clojureã®ãµã¤ãããzipè½ã¨ãã¦ãã¦å±éãä¸ã« clojure-1.7.0.jar ãå ¥ã£ã¦ãã®ã§
java -cp clojure-1.7.0.jar clojure.main
ããã¨REPLãèµ·åãã¦ãããã試ãã
REPLãããã¼ã
ç¢å°ãã¼ãå¹ããªãã£ããå±¥æ´æ©è½ãç¡ãã£ããã¨ã«ããä½ãæ¸ãã«ãã ãããªã£ã¦æã£ã¦ããrlwrapã¨ããã®ããããã¨æãã¦ãããã¾ããã
ããåãããªãã§ããã©ãã¾ããã¨ã©ãããã¦ããæãã«ãã¦ãããã¨ã®ãã¨ã
rlwrap java -cp clojure-1.7.0.jar clojure.main
ã¨è¨ãæãã§REPLèµ·åããã¨å±¥æ´ãã¤ãããç¢å°ãã¼ä½¿ããããã¦ããæãã«ãªãã¾ããã
rlwrapã®ã¤ã³ã¹ã³æ¹æ³ã¯èª¿ã¹ã¦ãã ãããã ãããããã±ã¼ã¸ããã¼ã¸ã£ã§å
¥ãã¨æãã¾ãã
Leiningenã®ã¤ã³ã¹ã³
Javaã§è¨ãæã®MavenãScalaã§è¨ãæã®activator(sbt)ã¿ãããªãã®ãããã§ããã©ã¤ãã©ãªã®ä¾åé¢ä¿ãã£ã¦ãããããããã¸ã§ã¯ãã®éå½¢ä½ã£ã¦ããããã³ã³ãã¤ã«ããããã¦ããããClojureçã§ã¯ã¿ããªå½ç¶ä½¿ã£ã¦ãã奴ã¨ããæãã£ã½ãã§ãã
ã¤ã³ã¹ã³ã¯é©å½ãªãã£ã¬ã¯ããªã§
wget https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein
ãã¦ãDLãããã¡ã¤ã«ã«ãã¹éãã°çµããã§ã
Cursiveã®ã¤ã³ã¹ã³
IntelliJ IDEAã®Clojureãã©ã°ã¤ã³ã§ãã
åæã«æ¬å¼§ã®å¯¾å¿åã£ã¦ãããã対å¿ããæ¬å¼§ã«è²ããªè²ä»ãã¦ããããè£å®åºãã¦ããããLeiningenã¨é£æºãã¦ãããããã¦Clojureã³ã¼ãã£ã³ã°ã©ã¤ããå¼·åã«ãµãã¼ããã¦ãããã¨ã®ãã¨ã
ã¤ã³ã¹ã³ã¯ Cursive: Getting Started ã«ããéããèªåã®IntelliJ IDEAã®ãã¼ã¸ã§ã³ã«åã£ãplugin.xmlããã©ã°ã¤ã³ãªãã¸ããªã¨ãã¦ç»é²ãããã¤ãã®æ§ã«ãã©ã°ã¤ã³æ¤ç´¢ãã¦ã¤ã³ã¹ãã¼ããã°OKã§ãã
ã§ããã¡ããæ§åã§ãã
ãã¹ã
ãªããæåãããã¹ãããç°å¢ãæ´ã£ã¦ããããã§ããããããè¨ãããè¦ãã»ããæ©ãã®ã§ãã¡ãã©ããã
åç»ã¿ãããªæãã§ãã¹ããæ¸ãã¾ããClojure REPL(ãã¼ã«ã«ãµã¼ã)ãèµ·åãã¦ããã¦ãã«ã¼ã½ã«ä½ç½®ã«ããã³ã¼ã(ãã¹ã)ãREPLãµã¼ãã«éã£ã¦ã¡ããã¡å®è¡ãã¦ãã¾ãã
REPLã¸ã®ã³ã¼ãã®éãã®ã¯ãã³ã¼ãã®é©å½ãªä½ç½®ãå³ã¯ãªãã¯ãã¦REPLâRun 'hoge' in REPLãé¸ã¶ãããEnter action or option nameãã®ãã¤ã¢ãã°ã ãã¦ãrun replãã¨ãã§æ¤ç´¢ããã¨åºã¦ãããRun test under caret in REPLããå®è¡ãããããã¨åºæ¥ã¾ãã
é¢æ°æ¸ãâãã¹ãæ¸ãâã«ã¼ã½ã«ä½ç½®ã®ãã¹ããREPLã«éãâ失æããâé¢æ°å®è£ ããâã«ã¼ã½ã«ä½ç½®ã®ãã¹ããREPLã«éãâã¤ã«ç¹°ãè¿ã
ãªãããã£ãããã§ãã(æ¬å¼§ã ãã«)ã
FizzBuzz
ãã£ãã®ç»åã§ãã©è¦ããã¦ã¾ãããåºæ¥ãã³ã¼ãããã¡ãã§ãã
(ns cljtest.core) (defn peek' [x] (do (println x) x)) (defn gen [n] (range 1 (+ n 1))) (defn fizzbuzz [x] (if (= (mod x 15) 0) "FizzBuzz" (if (= (mod x 3) 0) "Fizz" (if (= (mod x 5) 0) "Buzz" x)))) (defn doFizzBuzz [n] (map (comp peek' fizzbuzz) (gen n)))
ã«ã¼ãã¨ãããå帰ã¨ãããã®ããããã¯ããããããªãã£ãã®ã§mapé¢æ°ä½¿ãã¾ãã
ã¨ã®äºã§ãã
追è¨
ã³ã¡ã³ãæ¬ã§ããã´ã¼ããã«ææãããã¾ããã®ã§ä¿®æ£(ããã£ãææã¯ã³ã¡ã³ãæ¬è¦ã¦ãã ãã)ã
(ns cljtest.core) (defn peek' [x] (do (println x) x)) (defn gen [n] (range 1 (inc n))) (defn fizzbuzz [x] (if (zero? (mod x 15)) "FizzBuzz" (if (zero? (mod x 3)) "Fizz" (if (zero? (mod x 5)) "Buzz" x)))) (defn do-fizzBuzz [n] (doseq [x (gen n)] (peek' (fizzbuzz x))))
doseqãããåãããªããã§ããã©ãªã¹ãå å 表è¨ã¿ãããªã®ã¨ãªããããã®é¢æ°ãå¼æ°ã«ã¨ã£ã¦ãé¢æ°ãç¹°ãè¿ãå®è¡ãããã¯ãï¼ã£ã½ãã§ããå¯ä½ç¨ã®ããé¢æ°ä½¿ãæã«ä½¿ããã¤ã¨ã®ãã¨ã
æ´ã«è¿½è¨
(ns cljtest.core) (defn gen [n] (range 1 (inc n))) (defn fizzbuzz [x] (if (zero? (mod x 15)) "FizzBuzz" (if (zero? (mod x 3)) "Fizz" (if (zero? (mod x 5)) "Buzz" x)))) (defn do-fizzbuzz [n] (doseq [x (gen n)] (println (fizzbuzz x))))
peek'é¢æ°ãããªãã¨ã®ãã¨ã§ã
ã¾ã¨ã
Clojureå®å ¨ã«ãã¹ã¿ã¼ãã