clojure

Clojure and HBase: Iterate Lazily over a Scan

戏子无情 提交于 2020-01-03 03:36:04
问题 Lets say I want to print the output of an hbase table scan in clojure. (defmulti scan (fn [table & args] (map class args))) (defmethod scan [java.lang.String java.lang.String] [table start-key end-key] (let [scan (Scan. (Bytes/toBytes start-key) (Bytes/toBytes end-key))] (let [scanner (.getScanner table scan)] (doseq [result scanner] (prn (Bytes/toString (.getRow result)) (get-to-map result)))))) where get-to-map turns the result into a map. It could be run like this: (hbase.table/scan table

When using monger, do I need to supply connection each request?

六月ゝ 毕业季﹏ 提交于 2020-01-03 00:02:49
问题 In the documentation, the mongodb connection is established once, before being used without passing the connection to each command, is that the proper way to use monger, or should I pass the database connection to each call? 回答1: If you work with single database then it's best to set the connection once: (mg/connect! db-spec) But it's not a good idea when you have multiple databases. Monger have with-connection macro (see API docs) for this case: (mg/with-connection db-connection ...) You may

doseq over a simple lazy seq runs out of heap space

六月ゝ 毕业季﹏ 提交于 2020-01-02 17:16:54
问题 When stress-testing some Clojure code at work, I noticed it runs out of heap space when iterating over large data-sets. I eventually managed to trace the issues back to the combination of Clojure's doseq function, and implementation fo lazy sequences. This is the minimal code snippet that crashes Clojure by exhausting available heap space: (doseq [e (take 1000000000 (iterate inc 1))] (identity e)) The documentation for doseq clearly states that it doesn't retain the head of the lazy sequence,

doseq over a simple lazy seq runs out of heap space

感情迁移 提交于 2020-01-02 17:16:14
问题 When stress-testing some Clojure code at work, I noticed it runs out of heap space when iterating over large data-sets. I eventually managed to trace the issues back to the combination of Clojure's doseq function, and implementation fo lazy sequences. This is the minimal code snippet that crashes Clojure by exhausting available heap space: (doseq [e (take 1000000000 (iterate inc 1))] (identity e)) The documentation for doseq clearly states that it doesn't retain the head of the lazy sequence,

Simple Clojure XML edit

浪尽此生 提交于 2020-01-02 13:44:56
问题 Let's say I have a vector of maps [{:username "kbee" :firstname "Kay" :lastname "Bee"}, {:username "jcee" :firstname "Jay" :lastname "Cee"}] and i want to generate xml files for each map like the following <user> <username>kbee</username> <firstname>Kay</firstname> <lastname>Bee</lastname> </user> how do i use just the clojure core library to achieve this. (I looked at enlive and fleet but they seemed a little complicated for me.) ideally i'd like to do the following (map #(spit (str (

Simple Clojure XML edit

混江龙づ霸主 提交于 2020-01-02 13:41:29
问题 Let's say I have a vector of maps [{:username "kbee" :firstname "Kay" :lastname "Bee"}, {:username "jcee" :firstname "Jay" :lastname "Cee"}] and i want to generate xml files for each map like the following <user> <username>kbee</username> <firstname>Kay</firstname> <lastname>Bee</lastname> </user> how do i use just the clojure core library to achieve this. (I looked at enlive and fleet but they seemed a little complicated for me.) ideally i'd like to do the following (map #(spit (str (

More idiomatic way to calculate GS1 check digit in Clojure

只愿长相守 提交于 2020-01-02 07:32:42
问题 I am trying to calculate a GS1 check digit and have come up with the following code. The algorithm for calculating a check digit is: Reverse the barcode Drop the last digit (calculated check digit) Add the digits together with first, third, fifth, e.t.c. digit multiplied by 3 and even digits multiplied by 1. Subtract the sum from nearest equal or higher multiple of ten It sounds simple typed out but the solution I came up with seemed a bit inelegant. It does work but I want to know if there

How to open existing Clojure project in Eclipse?

ぐ巨炮叔叔 提交于 2020-01-02 07:09:54
问题 Ive tried to do it usual way File -> Import -> General -> Existing project (as for java projects) BUT eclipse dont see clojure projects and cannot open it :( do you have any idea why ? 回答1: Install Counterclockwise plugin in Eclipse (from eclipse marketplace). This brings clojure and leiningen support to eclipse. Add lein2-eclipse plugin to your project.clj : :plugins [[lein2-eclipse "2.0.0"]] Run following command inside the project directory : lein eclipse This generates necessary project

Saving+reading sorted maps to a file in Clojure

 ̄綄美尐妖づ 提交于 2020-01-02 06:33:12
问题 I'm saving a nested map of data to disk via spit . I want some of the maps inside my map to be sorted, and to stay sorted when I slurp the map back into my program. Sorted maps don't have a unique literal representation, so when I spit the map-of-maps onto disk, the sorted maps and the unsorted maps are represented the same, and #(read-string (slurp %)) ing the data makes every map the usual unsorted type. Here's a toy example illustrating the problem: (def sorted-thing (sorted-map :c 3 :e 5

How do I update a vector element of an atom in Clojure?

孤街醉人 提交于 2020-01-02 05:47:20
问题 I have a vector atom and I want to update an entry that is itself a map. (def vector-atom (atom [])) (swap! vector-atom conj { :id 1 :name "myname" }) How would I go about updating only this member? In the mindset of mutable Java land, I would do something like this: (defn find-by-id [id] (first (filter (fn [entry] (= (:id entry) id)) @vector-atom))) (defn update-entry [id new-entry] (let [curr-entry (find-by-id id) merged-entry (merge curr-entry new-entry)] ###set the curr-entry to merged