clojure

lazy-seq and stack overflow for infinite sequences

断了今生、忘了曾经 提交于 2019-12-13 18:24:25
问题 I am trying to show the importance of lazy-sequences or lazy-evaluation to the non-FP programmers. I have written this implementation of prime-generation to show the concept: (defn primes-gen [sieve] (if-not (empty? sieve) (let [prime (first sieve)] (cons prime (lazy-seq (primes-gen (filter (fn [x] (not= 0 (mod x prime))) (rest sieve)))))))) ;;;;; --------- TO SHOW ABOUT THE LAZY-THINGS ;; (take 400 (primes-gen (iterate inc 2))) ;; (take 400 (primes-gen (range 2 1000000000000N))) However, i

How do I get the argument in a macro from a map macro in Clojure?

久未见 提交于 2019-12-13 17:57:06
问题 I'm passing my macro to a map operation (which is also a macro). I'm having some trouble getting my values out. Here is an example: (def num-vec [1 2 3 4 5]) (defmacro describe-args [first-arg & remaining-args] `(println '~first-arg '~remaining-args)) (doall (map #(describe-args "my args " %) num-vec)) This returns: my args (p1__432#) my args (p1__432#) my args (p1__432#) my args (p1__432#) my args (p1__432#) My question is: How do I get the argument in a macro from a map macro in Clojure? (I

How to rewrite Ruby's `Array(x)` in Clojure?

瘦欲@ 提交于 2019-12-13 17:37:42
问题 In Ruby, there is Kernel#Array method, which acts like this: Array([1, 2, 3]) #=> [1, 2, 3] Array(1..5) #=> [1, 2, 3, 4, 5] Array(9000) #=> [9000] Array(nil) #=> [] In other words, it converts nil to empty array, non-collections to singleton arrays, and kinds of collection-like objects (i.e. objects which respond to #to_ary or #to_a ) to arrays. I want to have something similar to this in Clojure: (to-seq [1 2 3]) ;=> (1 2 3) (to-seq (range 1 5)) ;=> (1 2 3 4) (to-seq 9000) ;=> (9000) (to-seq

clojure gen-class returning own class

大城市里の小女人 提交于 2019-12-13 17:11:17
问题 I'm now making a class object with Clojure which has a method returning the object itself. Written with Java, the object that I'd like to make is like, class Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point copy() { return new Point(this.x, this.y); } } The current clojure code that I wrote is like, (ns myclass.Point :gen-class :prefix "point-" :init init :state state :constructors {[double double] []} :methods [[copy []

How to access a value using a space-separated key?

巧了我就是萌 提交于 2019-12-13 17:09:21
问题 When I have a space-separated key, how can I use it to extract a value without re-creating the key? I have a set of potential keys, column names actually, as the first sequence in data returned from clojure-csv: (This is formatted to avoid scrolling.) ["AGY/DIV " "STS" "GIC-ID " "LAST-NAME " "FIRST-NAME " "COVERAGE DESCRIPTION " "PREMIUM " "RUN-DATE" "BIL MO "] Then I create keys from this row and zipmap the keys with each subsequent row (sequence) of data: (defn create-map-keys "Takes a

Eval not working on unexpanded macro quote

跟風遠走 提交于 2019-12-13 17:03:51
问题 In common lisp I can do this: src-> (defmacro macro-hello () `"hello") (eval '(macro-hello)) no problem. In clojure: (defmacro macro-hello [] `"hello") (eval '(macro-hello)) gives me an error. Have I done something wrong? Clojure Error: Exception in thread "main" java.lang.Exception: Unable to resolve symbol: macro-hello in this context (NO_SOURCE_FILE:12) at clojure.lang.Compiler.analyze(Compiler.java:4340) at clojure.lang.Compiler.analyze(Compiler.java:4286) at clojure.lang.Compiler

Recreate a flattened tree

僤鯓⒐⒋嵵緔 提交于 2019-12-13 15:41:06
问题 I have a vector of maps, that I'd like to transform in a nested fashion. The data is structured as follows: (def data [{:id 1 :name "a" :parent 0} {:id 2 :name "b" :parent 0} {:id 3 :name "c" :parent 0} {:id 4 :name "a_1" :parent 1} {:id 5 :name "a_2" :parent 1} {:id 6 :name "b_1" :parent 2} {:id 7 :name "a_1_1" :parent 4}]) Each map has an :id , some other keys and values not important for this discussion, and :parent key, denoting if the elements belong to another element. If :parent is 0,

How do I install Clojure 1.3 with contribs on RHEL 6.1 / JDK7?

情到浓时终转凉″ 提交于 2019-12-13 15:07:58
问题 I've been struggling a bit trying to get this to work. Getting clojure 1.3 was a breeze, but now I've been trying to get contrib libraries to install and I get errors. Is there a guide on how to do this correctly? 回答1: The old clojure.contrib monolithic library is incompatible with clojure 1.3. See http://dev.clojure.org/display/design/Where+Did+Clojure.Contrib+Go As for how to install libraries correctly, either write your own pom.xml and use maven or use leiningen (much easier!) - https:/

Iteratively apply function to its result without generating a seq

依然范特西╮ 提交于 2019-12-13 14:23:10
问题 This is one of those "Is there a built-in/better/idiomatic/clever way to do this?" questions. I want a function--call it fn-pow --that will apply a function f to the result of applying f to an argument, then apply it to the result of applying it to its result, etc., n times. For example, (fn-pow inc 0 3) would be equivalent to (inc (inc (inc 0))) It's easy to do this with iterate : (defn fn-pow-0 [f x n] (nth (iterate f x) n)) but that creates and throws away an unnecessary lazy sequence. It

Where does Leiningen install the clojure libraries?

依然范特西╮ 提交于 2019-12-13 14:17:29
问题 Runnig lein for the first time, it installs clojure, but were to? It does not seem to be in /Library ... ~/Library ... ~/.lein ... Is there an established location for clojure.jar and the contribution jars on Unix/OS X## Heading ##? 回答1: Leiningen uses maven (or, to be more precise, the underlying libs which implement maven), which by default will install all dependencies under $HOME/.m2/ . 回答2: The default path is given by (.getPath (clojure.java.io/file (System/getProperty "user.home") ".m2