clojure

Clojure: How to Preserve Variadic Args Between Function Calls

[亡魂溺海] 提交于 2019-12-14 03:29:34
问题 I have two variadic functions. One of them passes its arguments to the other. The problem is that the varargs are becoming a list on the second call. How do I keep them varargs? => (defn foo [x & ys] (println x ys)) => (defn bar [x & ys] (foo (clojure.string/upper-case x) ys)) => (foo "hi") hi nil => (bar "hi") HI (nil) In the real function, foo passes its args to a variadic java function, so the varargs really need to stay varargs. How do I do this? 回答1: From http://clojuredocs.org/clojure

How to share code between separate clojurescripts in the same project

浪子不回头ぞ 提交于 2019-12-14 03:10:03
问题 I have a project that compiles 2 scripts used in 2 pages of the app: :cljsbuild {:builds [{:source-path "src-cljs/search", :compiler {:output-to "resources/public/cljs/search.js" }} {:source-path "src-cljs/view", :compiler { :output-to "resources/public/cljs/view.js"}} ] I have code common for both scripts. How to share this code? The only way I have found is through a separate Clojure project - I execute 'lein install' on it and it is available to my ClojuresScript code as any other third

Output “System.out.println” into Emacs Cider's REPL (or other buffer)

半城伤御伤魂 提交于 2019-12-14 02:57:42
问题 I am using a Java library in Clojure . It does output many things directly to the console using System.out.println . I am using the latest Cider with Emacs. I am starting Cider with cider-jack-in . However, I can see these outputs. I thought they would be copied to the nrepl-server buffer, but it is not. I also tried to run (alter-var-root #'*out* (constantly *out*)) in the REPL, but no success. What am I missing in how this should be done, if it can be done at all. 回答1: You need to replace

Equivalent to clojure.contrib's show?

限于喜欢 提交于 2019-12-14 00:19:47
问题 There used to be this useful utility called show in clojure.contrib. Now, that it's deprecated, is there an equivalent to it? Thanks! 回答1: De-constructing show to be more "simple", making available distinct pieces of re-usable functionality, was discussed by Stuart Halloway in a talk he give on clojure simplicity. The resulting code makes use of clojure.reflect/reflect and clojure.pprint/print-table and standard clojure filter : (require 'clojure.reflect) (require 'clojure.pprint) (->>

Editing programs “while they are running”? How?

冷暖自知 提交于 2019-12-14 00:17:24
问题 This question is a corollary to: Editing programs “while they are running”? Why? I'm only recently being exposed to the world of Clojure and am fascinated by a few examples I've seen of "live coding". The question linked above discusses the "why." My question is: How is this live coding technique possible? Is it a characteristic of the clojure language which makes it possible? Or is it just a pattern that they applied which could be applied to any language? I've got a background in python and

Clojure: pre post functions

一笑奈何 提交于 2019-12-14 00:16:54
问题 Context I'm aware of http://blog.fogus.me/2009/12/21/clojures-pre-and-post/ What I want is not exactly pre/post conditions. I want to have pre/post functions that are executed exactly once. I don't see any documentation promising me this feature about the pre/post conditions (i.e. that they're not executed multiple times.) Question For a Clojure function, is there anyway to tag it with pre/post functions that are executed exactly once, the pre function when the function is called the post

Adding the elements of a Vector without using `reduce` or `apply`

雨燕双飞 提交于 2019-12-13 21:38:12
问题 So I am trying to re-implement the reduce method, so it can add a couple of numbers that normally can be done using reduce , like: (reduce + [1 2 3]) ;; 6 (newRd + [1 2 3]) ;; 6 So I thought maybe it can be done using a recursive function that adds the first element of the vector every time it is called and do it again for the rest of the vector. Something like this: (defn newRd [list] (let [sum 0] (if (not= 0 (count list)) (+ sum (first list)) (newRd (rest list)) ) ) ) I think I am not doing

Simple java interop from clojure

半腔热情 提交于 2019-12-13 20:08:50
问题 I am trying to get a simple JDatePicker to work from clojure. Following these code snipets but with the intent of getting it to work from clojure through java interop. I can't even get started. This is the java code: UtilDateModel model = new UtilDateModel(); JDatePanelImpl datePanel = new JDatePanelImpl(model); JDatePickerImpl datePicker = new JDatePickerImpl(datePanel); frame.add(datePicker); I can't even get passed the first part in clojure user=> (import org.jdatepicker.JDatePicker) org

Does the Clojure compiler check if records and types implement protocols?

爱⌒轻易说出口 提交于 2019-12-13 19:36:41
问题 Is the Clojure compiler meant to check if a record or type that says it instantiates a protocol actually implements the methods listed in it? I'm trying this out now and so far, it doesn't seem to. 回答1: A record can implement a protocol without implementing any of its methods: (defprotocol Structure (weight [this]) (balanced? [this])) (defrecord Mobile [] Structure ) ... is accepted. If you try to use a non-existent method: (balanced? (Mobile.)) ;java.lang.AbstractMethodError: user.Mobile

Automatic TCO in Clojure

偶尔善良 提交于 2019-12-13 18:34:13
问题 Is there a way to define a function in Clojure that is automatically tail-call-optimized? e.g. (defrecur fact [x] (if (= x 1) 1 (* x (fact (dec x))))) would be translated internally to something like: (defn fact [x] (loop [n x f 1] (if (= n 1) f (recur (dec n) (* f n))))) Can you tell me if something like this already exists? 回答1: The short answer is "No". The slightly longer answer is that Clojure is deliberately designed to require explicit indication where Tail Call Optimisation is desired