clojure

What is the clojure equivalent to google guice?

╄→尐↘猪︶ㄣ 提交于 2019-12-21 07:11:01
问题 I came across google guice and could not really understand it and what it did, although there seems to be alot of hype around it. I was hoping to get a clojurian perspective of the library and why it is needed/not needed in clojure applications and if there was anything similar built into the language. 回答1: Because of Java's OO and type system, dynamically switching between different underlying implementations (for test (mocking) purposes for instance) can be difficult to manage. Libraries

In clojure, how to merge several maps combining mappings with same key into a list?

南笙酒味 提交于 2019-12-21 06:58:21
问题 In Clojure, I would like to combine several maps into a single map where mappings with same key are combined into a list. For example: {:humor :happy} {:humor :sad} {:humor :happy} {:weather :sunny} should lead to: {:weather :sunny, :humor (:happy :sad :happy)} I thought about: (merge-with (comp flatten list) data) But it is not efficient because flatten has O(n) complexity. Then I came up with: (defn agg[x y] (if (coll? x) (cons y x) (list y x))) (merge-with agg data) But it feels not

= and == in Clojure

烈酒焚心 提交于 2019-12-21 06:47:21
问题 On REPL, if I define (def fits (map vector (take 10 (iterate inc 0)))) and then call (== [2] (nth fits 2)) I get false. But (= [2] (nth fits 2)) returns true. Is this expected ? I tried (class [2]) and (class (nth fits 2) and both return Persistent Vector. 回答1: == is for comparing numbers. If either of its arguments is not a number, it will always return false: (== :a :a) ; => false As you can see by saying (clojure.contrib.repl-utils/source ==) at the REPL (with repl-utils require 'd, of

What does “^:static” do in Clojure?

风流意气都作罢 提交于 2019-12-21 06:47:15
问题 I've seen the ^:static metadata on quite a few function in the Clojure core.clj source code, e.g. in the definition of seq? : (def ^{:arglists '([x]) :doc "Return true if x implements ISeq" :added "1.0" :static true} seq? (fn ^:static seq? [x] (instance? clojure.lang.ISeq x))) What precisely does this metadata do, and why it it used so frequently throughout core.clj ? 回答1: In the development of Clojure 1.3 Rich wanted to add the ability for functions to return types other than Object. This

How to run clojure from matlab

荒凉一梦 提交于 2019-12-21 05:40:40
问题 How can I run a clojure script from matlab? I tried following: run matlab with jdk 1.7 and then call java MATLAB_JAVA=/usr/lib/jvm/java-7-oracle/jre matlab in the matlab, set classpath and use clojure compiler javaaddpath([pwd '/lib/clojure-1.5.1.jar']) import clojure.lang.RT Here I got error: Error using import Import argument 'clojure.lang.RT' cannot be found or cannot be imported. When I writing java class that runs clojure, everything working from console, but whould not run from matlab.

How to wrap a string in an input-stream?

拜拜、爱过 提交于 2019-12-21 05:31:47
问题 How can I wrap a string in an input-stream in such a way that I can test the function bellow? (defn parse-body [body] (cheshire/parse-stream (clojure.java.io/reader body) true)) (deftest test-parse-body (testing "read body" (let [body "{\"age\": 28}"] ;; must wrap string (is (= (parse-body body) {:age 28})) ))) 回答1: It is straightforward to construct an InputStream from a String using host interop, by converting to a byte-array first: (defn string->stream ([s] (string->stream s "UTF-8")) ([s

clojure: What's the use of the #'/(var …) form?

ぐ巨炮叔叔 提交于 2019-12-21 04:39:28
问题 In the compojure library in the core namespace, I see the following form: (defn- compile-route "Compile a route in the form (method path & body) into a function." [method route bindings body] `(#'if-method ~method (#'if-route ~(prepare-route route) (fn [request#] (let-request [~bindings request#] (render (do ~@body) request#)))))) and (defmacro GET "Generate a GET route." [path args & body] (compile-route :get path args body)) Further up in the file, the if-method and if-route functions are

Consuming WSDL in Clojure

≡放荡痞女 提交于 2019-12-21 04:38:09
问题 I need to consume a WSDL web service and the Java client-side code I've seen so far looks bloated and complicated. I was wondering whether a cleaner solution might exist in Clojure so that I may perhaps implement that part in Clojure and expose a simpler API to the Java code. 回答1: cd your_project_dir/src wsimport -p some.import.ns http://.../service?wsdl It would create ./some.import.ns/*.class . So you can just use them in your clojure project (ns your.ns ... (:import [some.import.ns some_WS

How do I add permissions on an AWS SQS Queue?

六眼飞鱼酱① 提交于 2019-12-21 04:26:09
问题 With the following code, I can add a permission using my AWS account number but the queue does not receive any messages from SNS. AddPermissionRequest addPermissionRequest = new AddPermissionRequest(); addPermissionRequest.ActionName.Add("SendMessage"); addPermissionRequest.ActionName.Add("ReceiveMessage"); addPermissionRequest.QueueUrl = queueUrl; addPermissionRequest.Label = General.IpAddressAWSFriendly; addPermissionRequest.AWSAccountId.Add(AWS_ACCOUNT_ID); sqs.AddPermission

Define my own reader macro in clojure

大憨熊 提交于 2019-12-21 04:14:50
问题 I would like to define my own reader macro in clojure: (read-string "ßfoo") => (some_func :foo) Is it possible? 回答1: It is possible to create tagged literals by having a reader map in data_readers.clj at the top of your classpath. This must be in data_readers.clj at the top of your classpath (usually src directory). {ß reader-demo.core/read-fn} This goes into reader-demo.core (defn some-func [arg] (str "some-func invoked with " arg)) (defn read-fn [arg] (-> arg keyword some-func)) Invoking #ß