clojure

Why does a clojure future block the main thread?

北战南征 提交于 2020-01-24 10:56:49
问题 I have a trivial lein project where -main contains a future: (def f (future 42)) (defn -main [& args] (println @f)) When I run lein run it prints 42 but does not return. I don't understand why it does not return ? How do I get lein run to return ? 回答1: Your question is really twofold: Why does lein not return? lein hangs because the thread pool which backs Clojure futures does not use daemon threads so you have to explicitly shut it down. If you change your code to the following, it should

Why does a clojure future block the main thread?

我只是一个虾纸丫 提交于 2020-01-24 10:56:05
问题 I have a trivial lein project where -main contains a future: (def f (future 42)) (defn -main [& args] (println @f)) When I run lein run it prints 42 but does not return. I don't understand why it does not return ? How do I get lein run to return ? 回答1: Your question is really twofold: Why does lein not return? lein hangs because the thread pool which backs Clojure futures does not use daemon threads so you have to explicitly shut it down. If you change your code to the following, it should

Filter element nodes in XML with Clojure zippers

被刻印的时光 ゝ 提交于 2020-01-24 10:38:06
问题 How can you filter text nodes in XML with Clojure zippers? For example, you may have a pretty-printed XML document that interleaves element nodes with text nodes containing whitespace: (def doc "<?xml version=\"1.0\"?> <root> <a>1</a> <b>2</b> </root>") If you want to retrieve the content of the root 's children, you can do this: (require '[clojure.data.xml :as xml] '[clojure.zip :as zip] '[clojure.data.zip :as zf] '[clojure.data.zip.xml :as zip-xml]) (-> doc xml/parse-str zip/xml-zip (zip

In a Emacs + Slime + Clojure + Windows setup, failure to load clojure-auto

落爺英雄遲暮 提交于 2020-01-23 05:50:07
问题 I followed this very helpful guide on getting this development environment set up. When running the emacs.bat I get the following error in Emacs: File error: Cannot open load file, clojure-auto Unfortunitely I am completely new to both Clojure and Emacs, so any help in even figuring out where to begin looking would be helpful. Also, as a side note the last step in the guide is: Start up Emacs using the emacs.bat file. Then, just type "M-x slime", and the REPL will come up, and you're on your

In clojure, how can I evaluate the arguments to a macro from another macro?

会有一股神秘感。 提交于 2020-01-23 05:29:13
问题 I have two macros. The first one takes a symbol as the only parameter (because it's passed to def, which needs a symbol). The second function takes a list of symbols and should call the first with each symbol individually. (defmacro m1 [s] '(let [f# ... dynamic function definition ...] (def ~s f#)) The second macro should take a list of symbols and pass them to the first, but I can't get it to work. The best I could come up with was the following: (defmacro m2 [symbols] `(for [s# ~symbols]

In clojure, how can I evaluate the arguments to a macro from another macro?

偶尔善良 提交于 2020-01-23 05:28:33
问题 I have two macros. The first one takes a symbol as the only parameter (because it's passed to def, which needs a symbol). The second function takes a list of symbols and should call the first with each symbol individually. (defmacro m1 [s] '(let [f# ... dynamic function definition ...] (def ~s f#)) The second macro should take a list of symbols and pass them to the first, but I can't get it to work. The best I could come up with was the following: (defmacro m2 [symbols] `(for [s# ~symbols]

Inner-join in clojure

允我心安 提交于 2020-01-23 05:24:12
问题 Lets say I have (def test-left [{:name "Sean" :age 27} {:name "Ross" :age 27} {:name "Brian" :age 22}]) and (def test-right [{:owner "Sean" :item "Beer"} {:owner "Sean" :item "Pizza"} {:owner "Ross" :item "Computer"} {:owner "Matt" :item "Bike"}]) When I do a (clojure.set/join test-left test-right) I get a Cartesian product. 12 rows. What I want is Inner-join on :name of left = :owner of right so that I get only those result which have matching rows in the right side So I get [{:name "Sean"

Socket programming in Clojure

人盡茶涼 提交于 2020-01-23 02:52:26
问题 I'm a Clojure noob, so bear with me. I'm trying to write a simple program to send a command through a socket and get a response. My code is: (def IPaddress "10.71.18.81") (def port 1500) (def socket (new Socket IPaddress port)) (print (clojure.string/join ["\nConnected to HSM: " (. socket isConnected)])) (def in (DataInputStream. (BufferedInputStream. (. socket getInputStream)))) (def out (DataOutputStream. (BufferedOutputStream. (. socket getOutputStream)))) (def command "Some string") (.

Is it accurate to describe dispatch in Clojure using a Protocol as 'static'?

浪尽此生 提交于 2020-01-23 01:33:51
问题 Meikel Brandmeyer wrote a post on dispatch in Clojure with the URL title Static vs Dynamic. He writes: Protocols are not the only place where we have a trade-off of static vs. dynamic. There are several places where such a trade-off can be spotted. He provides the following example of static dispatch in a protocol: (defprotocol Flipable (flip [thing])) (defrecord Left [x]) (defrecord Right [x]) (extend-protocol Flipable Left (flip [this] (Right. (:x this))) Right (flip [this] (Left. (:x this)

Using 3rd party java libraries, like com.jcraft.jsch, with clojure

你离开我真会死。 提交于 2020-01-22 19:39:06
问题 I'm experimenting with clojure and am trying to get a feel for using 3rd party libraries. I've been able to download some source, bundle it into a jar file with leiningen, put it in my classpath and (use 'lib.etc) in my script. I've also played around with the objects in java.lang.*. I haven't had any success with 3rd party java, though. $ java -cp clojure.jar:clojure-contrib.jar:com.jcraft.jsch_0.1.31.jar clojure.main Clojure 1.1.0 user=> (require 'com.jcraft.jsch) java.io