clojure

less verbose ways of spec'ing records and maps?

半世苍凉 提交于 2019-12-21 23:10:04
问题 Is there a less verbose way of making a spec for a map or a record than the way it is presented in the official spec guide? (defrecord Person [first-name last-name email phone]) (s/def ::first-name string?) (s/def ::last-name string?) (s/def ::email ::email-type) (s/def ::person (s/keys :req-un [::first-name ::last-name ::email] :opt-un [::phone])) ideally it would be nice if I could just write something like (defrecord Person [first-name last-name email phone]) (s/def ::person (s/keys :req

Full outer join two sequences of maps in Clojure

纵饮孤独 提交于 2019-12-21 22:53:36
问题 Given: (def seq1 ({:id 1 :val 10} {:id 2 :val 20})) (def seq2 ({:id 1 :val 12} {:id 3 :val 30})) Within each sequence, the :id value is guaranteed to be unique in that sequence, though not necessarily ordered. How can these two sequences of maps be joined by the :id key, so that the result is as shown? {1 [{:id 1 :val 10} {:id 1 :val 12}], 2 [{:id 2 :val 20} nil ], 3 [nil {:id 3 :val 30}]} The ultimate result is a map of pairs. This is similar to a full outer join, where not only the

Enumerate namespaces and dynamically load them in ClojureScript

感情迁移 提交于 2019-12-21 18:02:42
问题 This may actually be a bit of an XY-problem, so I'll try to explain what the goal is first. I'm building a ClojureScript application which is composed of a set of Reagent components. It provides a user interface where you can dynamically add or remove UI elements. These UI elements (components) have a certain type. For example a Markdown component is-a Text component. Whenever the user is presented with the option to add Text we list all the components that match the type+descendants (in this

How to append to a nested list in a Clojure atom?

匆匆过客 提交于 2019-12-21 17:44:13
问题 I want to append a value to a list in a Clojure atom: (def thing (atom {:queue '()})) I know when it's not an atom, I can do this: (concat '(1 2) '(3)) How can I translate that into a swap! command? Note: I asked a similar question involving maps: Using swap to MERGE (append to) a nested map in a Clojure atom? 回答1: user=> (def thing (atom {:queue '()})) #'user/thing user=> (swap! thing update-in [:queue] concat (list 1)) {:queue (1)} user=> (swap! thing update-in [:queue] concat (list 2)) {

Is there a Java or Ruby library for generating MOBI ebook documents? [closed]

邮差的信 提交于 2019-12-21 17:37:40
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . There is a Python library for this in the Calibre project, but I would like to find a library that I can use from Clojure or Ruby. Any ideas? 回答1: Check out the KindleGen tool available from Amazon here. It's a command line tool used to build eBooks that can be sold through Amazon's Kindle platform. You can call

Why does TCO require support from the VM?

孤街浪徒 提交于 2019-12-21 17:05:22
问题 Some VMs, most notably the JVM, are said to not support TCO. As a result, language like Clojure require the user to use loop recur instead. However, I can rewrite self-tail calls to use a loop. For example, here's a tail-call factorial: def factorial(x, accum): if x == 1: return accum else: return factorial(x - 1, accum * x) Here's a loop equivalent: def factorial(x, accum): while True: if x == 1: return accum else: x = x - 1 accum = accum * x This could be done by a compiler (and I've

Clojure doseq generates huge code?

China☆狼群 提交于 2019-12-21 13:19:10
问题 I've recently been playing with clojure and reached a problem that I'm not sure how to handle. I have a doseq with 7 parameters and it expands to a huge block, almost passing the maximum class size. Why does doseq expand to such a huge block of clojure code? Example: (def q '(doseq [p0 (nth (:params operator) 0 (quote (nil))) p1 (nth (:params operator) 1 (quote (nil))) p2 (nth (:params operator) 2 (quote (nil))) p3 (nth (:params operator) 3 (quote (nil))) p4 (nth (:params operator) 4 (quote

Clojure doseq generates huge code?

柔情痞子 提交于 2019-12-21 13:19:00
问题 I've recently been playing with clojure and reached a problem that I'm not sure how to handle. I have a doseq with 7 parameters and it expands to a huge block, almost passing the maximum class size. Why does doseq expand to such a huge block of clojure code? Example: (def q '(doseq [p0 (nth (:params operator) 0 (quote (nil))) p1 (nth (:params operator) 1 (quote (nil))) p2 (nth (:params operator) 2 (quote (nil))) p3 (nth (:params operator) 3 (quote (nil))) p4 (nth (:params operator) 4 (quote

Recursive map query using specter

拟墨画扇 提交于 2019-12-21 13:04:21
问题 Is there a simple way in specter to collect all the structure satisfying a predicate ? (./pull '[com.rpl/specter "1.0.0"]) (use 'com.rpl.specter) (def data {:items [{:name "Washing machine" :subparts [{:name "Ballast" :weight 1} {:name "Hull" :weight 2}]}]}) (reduce + (select [(walker :weight) :weight] data)) ;=> 3 (select [(walker :name) :name] data) ;=> ["Washing machine"] How can we get all the value for :name, including ["Ballast" "Hull"] ? 回答1: Here's one way, using recursive-path and

Recursive map query using specter

送分小仙女□ 提交于 2019-12-21 13:04:08
问题 Is there a simple way in specter to collect all the structure satisfying a predicate ? (./pull '[com.rpl/specter "1.0.0"]) (use 'com.rpl.specter) (def data {:items [{:name "Washing machine" :subparts [{:name "Ballast" :weight 1} {:name "Hull" :weight 2}]}]}) (reduce + (select [(walker :weight) :weight] data)) ;=> 3 (select [(walker :name) :name] data) ;=> ["Washing machine"] How can we get all the value for :name, including ["Ballast" "Hull"] ? 回答1: Here's one way, using recursive-path and