clojure

Clojure core.async, CPU hangs after timeout. Anyway to properly kill macro thread produced by (go..) block?

时间秒杀一切 提交于 2019-12-22 04:53:38
问题 Based on core.async walk through example, I created below similar code to handle some CPU intensive jobs using multiple channels with a timeout of 10 seconds. However after the main thread returns, the CPU usage remains around 700% (8 CPUs machine). I have to manually run nrepl-close in emacs to shut down the Java process. Is there any proper way to kill macro thread produced by (go..) block ? I tried close! each chan, but it doesn't work. I want to make sure CPU usage back to 0 by Java

How to serve the stream pdf with ring

╄→尐↘猪︶ㄣ 提交于 2019-12-22 04:49:14
问题 I'm trying to serve a clj-http generated document directly via ring/compojure. I thought ring.util/piped-output-stream would work, but it seems I'm not understanding something here... This: (defn laminat-pdf-t [natno] (piped-input-stream (fn [output-stream]) (pdf [ {:title (str "Omanimali-Kuscheltierpass" natno) :orientation :landscape :size :a6 :author "Omanimali - Stefanie Tuschen" :register-system-fonts true } ;; [:svg {} (clojure.java.io/file ;; (str "/einbuergern/" natno "/svg" ))] [

Can one safely ignore the difference between a macro and a built-in?

主宰稳场 提交于 2019-12-22 04:49:12
问题 I'm starting out with Clojure, which is also my first lisp. There's obviously a lot to take in, and in an attempt to lessen the cognitive load, I try to find the parts which I can safely ignore (for now). Can one safely treat forms with macros and forms with built-ins the same, or are there pitfalls that will spring up later? In other words, will I ever run into a situation where I need to know that (defn f1 []) expands to (def f1 (.withMeta (clojure.core/fn f1 ([])) (.meta (var f1)))) 回答1:

How can I dynamically look up a static class member in Clojure?

 ̄綄美尐妖づ 提交于 2019-12-22 04:33:15
问题 In Clojure I can look up a static member of a Java class (e.g. a field holding a constant) like this: ClassName/CONSTANT_FIELD How can I access the member when I only know it's name at runtime? An example would be looping over a sequence of field names and getting all the field values. I would like to do something like this (this code is not working, of course): (let [c "CONSTANT_FIELD"] ClassName/c) What's the best way to do that? 回答1: You can use Java's reflection API. (let [c "CONSTANT

Clojure - dispatch on return type? (As expressive as Haskell Typeclasses)

∥☆過路亽.° 提交于 2019-12-22 04:26:19
问题 This is a question about the expressiveness of Clojure vs other languages such as Haskell. The broader issue is solutions to the Expression Problem This question reached the conclusion that in general Clojure protocols (and multimethods) were less expressive than Haskell typeclasses because protocols dispatched on first argument, and Haskell typeclasses could dispatch on return type. (Now I think this reasoning is really interesting, and have no interest in starting a language war. I'm just

Race conditions and clojure Atoms

删除回忆录丶 提交于 2019-12-22 04:25:15
问题 Hi guys: The Documentation for the clojure "atom" states that - "Changes to atoms are always free of race conditions." However- a race condition is defined not just in terms of a change , but rather, in context of parallel logical operations in different threads. I am wondering - what is the significance of the guarantee that "Changes to atoms are always free of race conditions" ? In java, we have atomic primitives, which support certain thread-safe operations which are specific (for example,

How to split a number in Clojure?

感情迁移 提交于 2019-12-22 04:20:11
问题 I am looking for a nice method to split a number with n digits in Clojure I have these two verbose inefficient methods: (->> (str 942) seq (map str) (map read-string)) => (9 4 2) and... (defn digits [n] ;YUK!! (cons (str (mod n 10)) (lazy-seq (positive-numbers (quot n 10))))) (map read-string (reverse (take 5 (digits 10012)))) => (1 0 0 1 2) Is there a more concise method for doing this type sort of operation? 回答1: A concise version of your first method is (defn digits [n] (->> n str (map

Validation of Numeric Arguments in Clojure

纵饮孤独 提交于 2019-12-22 04:17:20
问题 I have a clojure function: (defn f [arg1 arg2] ...) I would like to test if arg1 and arg2 are numeric (only numeric types should pass - not numerically formatted strings). There are, of course, a whole bunch of ways to do this, but I'd like to do it as idiomatically as possible. Suggestions? Edit: I know about :pre . Any comment on whether or not that is an appropriate/necessary way to handle this would be appreciated. 回答1: Pre-conditions can do that: (defn test [arg1 arg2] {:pre [(number?

StackOverFlow while counting digits

人盡茶涼 提交于 2019-12-22 04:10:14
问题 I am trying to count the number of digits in a number in Clojure as follows: I get a StackOverflowError even for 2 digit numbers (defn num-digits [n] (if (= 0 n) 0 (inc (num-digits (/ n 10))))) (println (num-digits 93)) But if I replace / with unchecked-divide then it works for at least 93. But neither of the techniques works for: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 First

What is the difference between fn and fn*?

二次信任 提交于 2019-12-22 04:01:47
问题 In Clojure, what is the difference between fn and fn*? I see fn* when I syntax quote a function created with the # macro. For example, in the REPL: user=> `#(inc %) (fn* [p1__342__343__auto__] (clojure.core/inc p1__342__343__auto__)) Is this simply used for debugging purposes to identify that the function was created by # rather than fn directly? 回答1: According to this post on google groups fn* is a primitive form to create functions and fn is a macro built on top of it to implement higher