clojure

How can I call a java static method in clojure?

倾然丶 夕夏残阳落幕 提交于 2020-01-09 09:58:52
问题 I wish to call class on the String class. How can I access this static method? 回答1: You can call a static method using (ClassName/methodName arguments) . However class is not a static method, it's a java keyword and you don't need it in clojure. To get the Class object associated with the String class, just use String . 回答2: An example is worth 100 words: (. String (valueOf 1)) 回答3: Class doesn't have a "class" method, nor a "class" member. The symbol String is mapped to the class java.lang

inconsistency between repl and test runner

十年热恋 提交于 2020-01-07 06:58:28
问题 I'm having some issues with testing a clojure macro. When I put the code through the repl, it behaves as expected, but when I try to expect this behavior in a test, I'm getting back nil instead. I have a feeling it has to do with how the test runner handles macroexpansion, but I'm not sure what exactly is going on. Any advice/alternative ways to test this code is appreciated. Here is a simplified example of the macro I'm trying to test (defmacro macro-with-some-validation [-name- & forms]

Declare dummy (unused) parameters for Clojure anonymous function

て烟熏妆下的殇ゞ 提交于 2020-01-07 02:37:05
问题 As also explained in the answers of this question, Clojure determines the number of parameters of an anonymous function (defined through #() ), by the maximal parameter index referenced in the body (e.g. if the maximal referenced parameter is %4 , then that anonymous function has 4 parameters). Question: is there some syntax to tell the Clojure compiler that an anonymous function expects one parameter, even not referencing that parameter? Or, in this case, the only "clean way"* is to use the

How to use tail recursion correctly?

大城市里の小女人 提交于 2020-01-06 18:57:43
问题 I am trying to rewrite this piece of code from https://github.com/lspector/gp/blob/master/src/gp/evolvefn_zip.clj to use recur: (defn random-code [depth] (if (or (zero? depth) (zero? (rand-int 2))) (random-terminal) (let [f (random-function)] (cons f (repeatedly (get function-table f) #(random-code (dec depth))))))) The problem is, I have absolutely no idea how to do that. The only thing I can think of is something like this: (defn random-code [depth] (loop [d depth t 0 c []] (if (or (zero?

Clojure compilation and running with cygwin

一笑奈何 提交于 2020-01-06 15:01:32
问题 compiling clojure files generate .class files in classes directory . Since classes direct is child to current directory so for running a java program which is calling clojure will be javac -cp classes CalculateSum.java . How to run ?? java CalculateSum isn't possible since class files are in child directory classes . java -cp classes CalculateSum isn't working. 回答1: Compile by javac >dir /d /b chapter_2 >dir /b chapter_2 CalculateSum.java >javac chapter_2/CalculateSum.java >dir /b chapter_2

Insertion sort in clojure throws StackOverFlow error

核能气质少年 提交于 2020-01-06 13:58:05
问题 (defn insert [s k] (let [spl (split-with #(< % k) s)] (concat (first spl) (list k) (last spl)))) (defn insert-sort [s] (reduce (fn [s k] (insert s k)) '() s)) (insert-sort (reverse (range 5000))) throws a stack over flow error. What am I doing wrong here? 回答1: Same issue as with Recursive function causing a stack overflow. Concat builds up a bunch of nested lazy sequences like (concat (concat (concat ...))) without doing any actual work , and then when you force the first element all the

Can I know how many digits a number is without count it?

我们两清 提交于 2020-01-06 12:41:08
问题 When I want to know how many digits of a big number, I'd do this: user> (count (str (factorial-bigint-loop 32))) 36 Is there a better way? Can I represent the number in scientific notation? 回答1: If the number is strictly positive, something like: (+ 1 (floor (log10 n))) should do the trick 来源: https://stackoverflow.com/questions/27229987/can-i-know-how-many-digits-a-number-is-without-count-it

get key-chains of a tree in Clojure [duplicate]

一曲冷凌霜 提交于 2020-01-06 11:48:55
问题 This question already has answers here : How can I get the nested keys of a map in clojure? (10 answers) Closed 5 years ago . I wanted to get key-chains of a tree, from every root to every leave. for example input tree: {"b" {:term true}, "a" {"y" {:term true}, "x" {:term true}}} I was expecting output: (("b" :term) ("a" "x" :term) ("a" "y" :term)) or ["b" "ax" "ay"] I've figured out a tail-recursion, it worked fine: (defn down-strings [trie] "this one works. 80msecs for address.txt, not bad

Destructuring argument in Clojure

感情迁移 提交于 2020-01-06 08:44:30
问题 Newbie enjoying Clojure a lot here. So, I have an HTTP route: (POST "/login" request (post-login request)) here, "request" is a map with a lot of http things inside. And the "post-login" function: (defn post-login ;; Login into the app [{{email "email" password "password"} :form-params session :session :as req}] (let [user (modusers/get-user-by-email-and-password email password)] ;; If authenticated (if-not (nil? (:user user)) (do (assoc (response/found "/") :session (assoc session :identity

ClojureScript zipmap tricks me or what?

徘徊边缘 提交于 2020-01-06 06:19:50
问题 I use Clojurescript to develop webbrowser-games. (Actually a friend of mine teaches me, we started only a few weeks ago). I wanted to generate a map in which keys are vectors and values are numbers. For e.g.: {[0 0] 0, [0 1] 1, [0 2] 2, ...}. I used this formula: (defn oxo [x y] (zipmap (map vec (combi/cartesian-product (range 0 x) (range 0 y))) (range (* x y)))) (where combi/ refers to clojure.math.combinatorics). When it generates the map, key-value pairs are ok, but they are in a random