Learn you a Clojure for Great Groovy!
æè¿è©±é¡ã®ã¿ã¤ãã«ã¯ããã®ããèªãã§ããéä¸ï¼9ç« ã«å
¥ã£ã)ã
ãã®è©±ã§ã¯ãªãããã®ã¨ã³ããªã¯ Clojure 㧠Ninety-Nine Prolog Problems ããã£ã¦ããã¨ãã«æãã¤ãããã®ã
Ninety-Nine Prolog Problems ã Lists ã®ã¨ããã ããç¾å¨ Groovy, Haskell, Scala, Prolog, Clojure ã§è§£ããã*1
Lists ã®ååã®ã¯ã©ã¤ããã¯ã¹ã¯ encode-direct 辺ãã ã Groovy ã«è¼ã¹ã¦ Clojure ã¯ç°¡æ½ã«æ¸ãã¦ãã¾ãã
;; 1.13 (defn encode-direct [s] (for [[n c] (map (juxt count first) (partition-by identity s))] (if (= 1 n) c [n c]) )) (assert (= [[4 \a] \b [2 \c] [2 \a] \d [4 \e]] (encode-direct "aaaabccaadeeee") ))
ãã㯠Haskell ã® group é¢æ°ã«ããããã®ã Clojure ã«åå¨ããããã ã
Prelude> :m + Data.List Prelude Data.List> group "aaaabccaadeeee" ["aaaa","b","cc","aa","d","eeee"]
ã¤ã¾ã 1.09 ã® pack ã Clojure ã§ã¯
- åèï¼partition-by - clojure.core | ClojureDocs - Community-Powered Clojure Documentation and Examples
;; 1.09 (defn pack [s] (partition-by identity s)) (assert (= [[\a \a \a \a] [\b] [\c \c] [\a \a] [\d] [\e \e \e \e]] (pack "aaaabccaadeeee") ))
ã§ãGroovy ã§ã¯ *2
// 1.09 def pack(list) { if (!list) return [] list = list.reverse() list.tail().inject([[list.head()]]){ acc, v -> acc.head()[0] == v ? [[v,*acc.head()],*acc.tail()] : [[v],*acc] } } assert ["aaaa","b","cc","aa","d","eeee"] == pack("aaaabccaadeeee".toList())*.join()
Groovy ã«ã Collection#split ã¨ããåãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®ã¡ã½ãããåå¨ãããæ¯ãèããéãã
Groovy ã® split ã¯ãå¼æ°ã®ã¯ãã¼ã¸ã£ã§ true 㨠false ã«åé¡ãã¦ããã®ã«å¯¾ã
Clojure ã® partition-by ã¯ãå¼æ°ã®ã¯ãã¼ã¸ã£ã§å¤æããçµæã1ã¤åã®ã°ã«ã¼ãã¨åããã©ããã§åé¡ãã¦ããã
ããã¯ä¾¿å©ã ããã¤ããªã partition-by ãå®è£ ããã¨ããã ã Clojure ã¯ãªã¼ãã³ãªã®ã§è²¸ãã¦ããããã¨ã«ããã*3
@Grab(group='org.clojure', module='clojure', version='1.4.0') @Grab(group='org.apache.commons', module='commons-lang3', version='3.1') import clojure.lang.Compiler import clojure.lang.Symbol import clojure.lang.RT import static org.apache.commons.lang3.StringUtils.removeEnd import static org.apache.commons.lang3.StringUtils.splitByCharacterTypeCamelCase def methodMissing(String name, args) { def names = splitByCharacterTypeCamelCase(name)*.toLowerCase() if (names[0] == "is") { names = names.tail() names[-1] = names[-1] + "?" } return Compiler.eval(Symbol.create(names.join("-"))).invoke(*args) } def propertyMissing(String name) { // Why? // Caused by: java.lang.RuntimeException: Unable to resolve symbol: out in this context if (name == "out") return System.out return Compiler.eval(Symbol.create(name)) } def pack(s) { partitionBy(identity, s) } def encode1(s) { map(juxt(count, first), pack(s)) } def encode2(list) { pack(list).collect{ [it.size(),it.head()] } } assert ["aaaa", "b", "cc", "aa", "d", "eeee"] == pack("aaaabccaadeeee")*.join() assert [[4,'a'], [1,'b'], [2,'c'], [2,'a'], [1,'d'], [4,'e']] == encode1("aaaabccaadeeee") assert [[4,'a'], [1,'b'], [2,'c'], [2,'a'], [1,'d'], [4,'e']] == encode2("aaaabccaadeeee") assert isZero(0) // Why? // groovy.lang.MissingPropertyException: No such property: lang for class: clojure // assert 'a' == clojure.lang.RT.first("abc") assert 'a' == RT.first("abc")
encode1 㯠Groovy ã§ã encode2 ã®ããã«æ¸ããã®ã§ã¡ãªãããããããã§ã¯ãªããä»ã®é¢æ°ãåãããã¾ããã¨ãããã¨ã§æ¸ãã¦ã¿ãã
propertyMissing ã®æ¯ãèããããåãã£ã¦ããªãã®ã§ Why? ã2ç®æçºçãã¦ããã
clojure.lang.RT ã®ã¡ã½ãããªãç´æ¥å¼ã³åºããã¨ãå¯è½ãªããã ã
Symbol ãæ¯åçæã㦠eval ãã¦ãããããã¯ãã£ãã·ã¥ã«ä¿æãã¦ããããããããªãã
æ¹åç¹ã¯ããã¨æãã Clojure ã®é¢æ°ã¯ Groovy ããç°¡åã«å¼ã³åºããããã¨ãããã¨ãè¨ãããã£ãã