clojure

If I load a file with (ns my-namespace) in it, why doesn't it switch my current namepace?

有些话、适合烂在心里 提交于 2019-12-23 10:01:25
问题 I have a file like this (ns boston.core) If I (load "boston/core") from the REPL, however, my *ns* doesn't change to boston but remains user . Why is this? 回答1: This is because load just loads the specified file (into the boston.core namespace, as specified at the top of the file). It doesn't do anything to the current namespace in the REPL. If you also want to switch namespace in the REPL to use whatever has just been loaded you need to do something like: (load "boston/core") (ns boston.core

Clojure range-case macro

天涯浪子 提交于 2019-12-23 09:57:35
问题 In the book "The Scheme Programming Language, 4th Edition", by R. Kent Dybvig, on page 86, the author has written a define-syntax (Scheme macro) for a case statement that accepts ranges for its conditions. I thought I would try this in Clojure. Here is the result. How can I improve this? I use :ii , :ie , :ei , and :ee for the range operators, indicating inclusive-inclusive, inclusive-exclusive, exclusive-inclusive, and exclusive-exclusive, respectively. Is there a better choice? I chose to

Clojure map-longest

二次信任 提交于 2019-12-23 09:47:02
问题 I am trying to write a Clojure utility function called map-longest (alternate name suggestion appreciated). This function will have the following "signature": (map-longest fun missing-value-seq c1 & colls) and will behave similarly to map , except than it will continue processing the supplied collections until the longest is exhausted. For collections shorter than the longest, when it runs out of values, it will take them from the missing-values-seq . It should be lazy, but obviously cannot

Am I using atom? wrong or there is something else…?

我怕爱的太早我们不能终老 提交于 2019-12-23 09:38:40
问题 Basically... => (atom? 5) CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1) => (atom? /a) RuntimeException Invalid token: /a clojure.lang.Util.runtimeException (Util.java:156) RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:156) => (atom? "hello world") CompilerException java.lang.RuntimeException: Unable to resolve symbol: atom? in this context, compiling:(NO_SOURCE_PATH:1) So does

In Clojure how can I read a public member variables of an instance of a Java class?

旧时模样 提交于 2019-12-23 09:37:11
问题 In Clojure how can I read a public member variables of an instance of a Java class? I want something like: (. instance publicMemberName) I also tried: instance/publicMemberName but this only works with static methods 回答1: In Java, the class java.awt.Point has public fields x and y . See the javadocs here http://download.oracle.com/javase/6/docs/api/java/awt/Point.html. In Clojure the dot macro works for fields and methods. This worked for me: user=> (let [p (new java.awt.Point 2 4)] (.x p)) 2

What is the performance of `count` on a Clojure set?

℡╲_俬逩灬. 提交于 2019-12-23 09:33:50
问题 So, I read that the count operation is O(1) for a Clojure vectors, lists and maps. (count [1 2 3]) ;=> 3 But is it also O(1) for a Clojure set? I imagine it probably is, but I'm not really sure how to find out. I had a quick read of http://clojure.org/data_structures#Data%20Structures-Sets, but couldn't see the info there. 回答1: It is O(1) You can verify this by observing that clojure.lang.PersistentSet maintains a _count field in the Java source code: https://github.com/clojure/clojure/blob

How to automatically reload Clojure code?

。_饼干妹妹 提交于 2019-12-23 09:26:12
问题 How to automatically reload Clojure code? I have watched the presentation. And in there they use some hot swap Clojure technology that reloads code whenever changes are made into source files. I can run the code, but I can not observe the effect of auto reload. How is it possible to reload the code? the source code. 回答1: There are many ways to reload code, depending on the situation: Emacs/CIDER shortcuts to reload a file. Probably Rich is doing something similar. Also see this: How to reload

How to hide TieredCompilation warning?

末鹿安然 提交于 2019-12-23 09:25:57
问题 I'm using Linux Mint and OpenJDK. java -version shows this: java version "1.7.0_79" OpenJDK Runtime Environment (IcedTea 2.5.6) (7u79-2.5.6-0ubuntu1.14.04.1) OpenJDK Client VM (build 24.79-b02, mixed mode, sharing) Whenever I run a Java-based app like lein , I get this warning: OpenJDK Client VM warning: TieredCompilation is disabled in this release. It's irrelevant to my interests, so I'd rather not see it in the command output. How can I hide or disable it? 回答1: The problem is in Leiningen.

Why this code does not throw StackOverflow exception

若如初见. 提交于 2019-12-23 09:19:53
问题 In clojure v1.6.0, this code just runs forever and consumes 100% of one core: (defn average [x y] (/ (+ x y) 2)) (defn improve [guess x] (average guess (/ x guess))) (defn sqrt-iter [guess x] (sqrt-iter (improve guess x) x)) (sqrt-iter 1 4) I'd expect it to throw a StackOverflowError immediately, but it doesn't. Any explanation why it happens? 回答1: Because 1 is a long. The code starts computing extremely long rationals, and slows to a crawl after a few iterations. If you run it with 1.0 and 4

Why this code does not throw StackOverflow exception

半世苍凉 提交于 2019-12-23 09:19:07
问题 In clojure v1.6.0, this code just runs forever and consumes 100% of one core: (defn average [x y] (/ (+ x y) 2)) (defn improve [guess x] (average guess (/ x guess))) (defn sqrt-iter [guess x] (sqrt-iter (improve guess x) x)) (sqrt-iter 1 4) I'd expect it to throw a StackOverflowError immediately, but it doesn't. Any explanation why it happens? 回答1: Because 1 is a long. The code starts computing extremely long rationals, and slows to a crawl after a few iterations. If you run it with 1.0 and 4