clojure

What is the difference between the functions seq?, sequential? and coll?

不想你离开。 提交于 2019-12-30 05:45:07
问题 What is the difference between the functions seq? sequential? and coll? I found some information scattered throughout the internet, but I think it would be better to centralize that information here. 回答1: seq? is a predicate that returns true if it's argument implements ISeq interface, which is to say it provides the methods first , rest , cons . See http://clojure.org/sequences. (seq? [1 2]) false (seq? (seq [1 2])) true sequential? is a predicate that returns true if it's argument

Flattening a map by join the keys

北战南征 提交于 2019-12-30 04:10:08
问题 Given a nested map with only keyword keys such as {:foo {:bar 1 :baz [2 3] :qux {:quux 4}} :corge 5} , how can I implement flatten-map so that (flatten-map {:foo {:bar 1 :baz [2 3] :qux {:quux 4}} :corge 5} "-") produces something like {:foo-bar 1 :foo-baz [2 3] :foo-qux-quux 4 :corge 5} . My best attempt is: (defn flatten-map ([form separator] (flatten-map form separator nil)) ([form separator prefix] (if (map? form) (into {} (map (fn [[k v]] [(keyword (str prefix (name k))) (flatten-map v

How do nested dosync calls behave?

大城市里の小女人 提交于 2019-12-30 04:01:13
问题 What happens when you create nested dosync calls? Will sub-transactions be completed in the parent scope? Are these sub-transactions reversible if the parent transaction fails? 回答1: If you mean syntactic nesting, then the answer is it depends on whether the inner dosync will run on the same thread as the outer one . In Clojure, whenever a dosync block is entered, a new transaction is started if one hasn't been running already on this thread . This means that while execution stays on a single

Passing state as parameter to a ring handler?

余生颓废 提交于 2019-12-30 04:01:07
问题 How does one inject state into ring handlers most conveniently (without using global vars)? Here is an example: (defroutes main-routes (GET "/api/fu" [] (rest-of-the-app the-state))) (def app (-> (handler/api main-routes))) I would like to get the-state into the compojure handler for main-routes . The state might be something like a map created with: (defn create-app-state [] {:db (connect-to-db) :log (create-log)}) In a non ring application I would create the state in a main function and

What is the idiomatic way to swap two elements in a vector

烈酒焚心 提交于 2019-12-30 03:01:06
问题 Is there a better or more concise way to do the following? (defn swap [v i1 i2] "swap two positions in a vector" (let [e1 (v i1) e2 (v i2)] (-> (assoc v i1 e2) (assoc i2 e1)))) 回答1: I can't think of a particularly elegant solution, either. Here's how I'd write it though: (defn swap [v i1 i2] (asso­c v i2 (v i1) i1 (v i2)))­ 来源: https://stackoverflow.com/questions/5979538/what-is-the-idiomatic-way-to-swap-two-elements-in-a-vector

Android Adverse To Dynamic Languages

不想你离开。 提交于 2019-12-29 18:36:19
问题 I believe I read at some point that due to Android running on the Dalvik VM, that dynamic languages for the JVM (Clojure, Jython, JRuby etc.) would be hard pressed to obtain good performance on Dalvik (and hence on Android). If I recall correctly, the reasoning was that under the hood, in order to achieve the dynamic typing, there was quite a bit of fiddling done with the java bytecode and that the bytecode->dalvik translation wouldn't pick this up easily. So should I avoid a dynamic JVM

How to change Clojure or Lein's Clojure default version?

╄→гoц情女王★ 提交于 2019-12-29 09:01:26
问题 I'm not sure why but when I enter the REPL through $ clojure or $ lein repl , >(clojure-version) says '1.2.1'. I want it to say '1.5'. Version 1.5 works fine in my projects managed by lein. I just want to tell lein to always use 1.5 by default, instead of 1.2. 回答1: Currently this isn't supported outside a project, but it's being worked on: https://github.com/technomancy/leiningen/issues/966 A workaround for now is just to specify it in your project.clj Further recommendation: upgrade to

Do-while loop in Clojure?

淺唱寂寞╮ 提交于 2019-12-29 08:38:05
问题 So I want to first execute a bunch of code, and then ask the user if he wants to do that again. I thought the most convenient way to do this would be a do-while loop like in C++, and since I couldn't seem to find any do-while functions in Clojure, I wrote the following: (defmacro do-while "Executes body before testing for truth expression" [test & body] `(do (do ~@body) (while ~test ~@body))) Would there be a better (as in more idiomatic Clojure-ish) way of writing this macro, or perhaps a

How to make a record from a sequence of values

て烟熏妆下的殇ゞ 提交于 2019-12-29 08:29:05
问题 I have a simple record definition, for example (defrecord User [name email place]) What is the best way to make a record having it's values in a sequence (def my-values ["John" "john@example.com" "Dreamland"]) I hoped for something like (apply User. my-values) but that won't work. I ended up doing: (defn make-user [v] (User. (nth v 0) (nth v 1) (nth v 2))) But I'm sensing there is some better way for achieving this... 回答1: Warning: works only for literal sequables! (see Mihał's comment) Try

clojure classpath problem for (require) function?

北慕城南 提交于 2019-12-29 08:03:28
问题 CLASSPATH has the "/Users/smcho/Desktop/clojure" as one of its path, and this directory has the file hello.clj. Running clojure, and running (require 'hello) give this error message. java.io.FileNotFoundException: Could not locate hello__init.class or hello.clj on classpath: (NO_SOURCE_FILE:0) When I change directory to "/Users/.../clojure", and run the same (require 'hello), there's no problem. The . is on the CLASSPATH. Running java -cp /Users/smcho/bin/jar/clojure.jar:/Users/smcho/Desktop