clojure

Generating a symbol from a string and applying it as a function

*爱你&永不变心* 提交于 2021-02-05 10:10:51
问题 I'm just learning clojure, and I'm hitting a wall. I'm trying to read an arithmetic expression as an infix string and process it in Clojure. e.g. "1 + 2" -> (+ 1 2) I read in the "+" and turn it into a symbol like this: (def plus (symbol "clojure.core" "+")) Which appears to work properly, but when I call it, I don't get what I'm expecting: user=> plus + user=> (plus 1 1) 1 user=> (plus 1 2) 2 user=> (plus 1 2 3) ArityException Wrong number of args (3) passed to: Symbol clojure.lang.AFn

Generating a symbol from a string and applying it as a function

前提是你 提交于 2021-02-05 10:08:05
问题 I'm just learning clojure, and I'm hitting a wall. I'm trying to read an arithmetic expression as an infix string and process it in Clojure. e.g. "1 + 2" -> (+ 1 2) I read in the "+" and turn it into a symbol like this: (def plus (symbol "clojure.core" "+")) Which appears to work properly, but when I call it, I don't get what I'm expecting: user=> plus + user=> (plus 1 1) 1 user=> (plus 1 2) 2 user=> (plus 1 2 3) ArityException Wrong number of args (3) passed to: Symbol clojure.lang.AFn

Generating a symbol from a string and applying it as a function

∥☆過路亽.° 提交于 2021-02-05 10:05:33
问题 I'm just learning clojure, and I'm hitting a wall. I'm trying to read an arithmetic expression as an infix string and process it in Clojure. e.g. "1 + 2" -> (+ 1 2) I read in the "+" and turn it into a symbol like this: (def plus (symbol "clojure.core" "+")) Which appears to work properly, but when I call it, I don't get what I'm expecting: user=> plus + user=> (plus 1 1) 1 user=> (plus 1 2) 2 user=> (plus 1 2 3) ArityException Wrong number of args (3) passed to: Symbol clojure.lang.AFn

clojure recursion conj a list

梦想的初衷 提交于 2021-02-05 09:28:23
问题 ((fn foo [x] (when (> x 0) (conj (foo (dec x)) x))) 5) For this code, the result is [5 4 3 2 1] Why isn't is [1,2,3,4,5]? I see we do conf from result of recursive foo call with a value. For I thought it should be 1 2 3 4 5? Need help to understand this. Thanks. 回答1: From the documentation of conj : clojure.core/conj ([coll x] [coll x & xs]) conj[oin]. Returns a new collection with the xs 'added'. (conj nil item) returns (item). The 'addition' may happen at different 'places' depending on the

In clojure, how to write the identity function using the anonymous function literal?

社会主义新天地 提交于 2021-02-04 22:16:30
问题 How could one write the identity function in clojure using anonymous function literal (#())? The following code doesn't work: (#(%) 5) It raises an exception because it is converted to: ((fn[x] (x)) 5) The problem in that when using #() , the function body is enveloped with parentheses. Any idea, how to elegantly overcome this? 回答1: Well, first of all, there is the identity function. But you can use #(do %) if you insist. 来源: https://stackoverflow.com/questions/9148896/in-clojure-how-to-write

In clojure, how to write the identity function using the anonymous function literal?

孤街醉人 提交于 2021-02-04 22:15:53
问题 How could one write the identity function in clojure using anonymous function literal (#())? The following code doesn't work: (#(%) 5) It raises an exception because it is converted to: ((fn[x] (x)) 5) The problem in that when using #() , the function body is enveloped with parentheses. Any idea, how to elegantly overcome this? 回答1: Well, first of all, there is the identity function. But you can use #(do %) if you insist. 来源: https://stackoverflow.com/questions/9148896/in-clojure-how-to-write

In clojure, how to write the identity function using the anonymous function literal?

心已入冬 提交于 2021-02-04 22:13:48
问题 How could one write the identity function in clojure using anonymous function literal (#())? The following code doesn't work: (#(%) 5) It raises an exception because it is converted to: ((fn[x] (x)) 5) The problem in that when using #() , the function body is enveloped with parentheses. Any idea, how to elegantly overcome this? 回答1: Well, first of all, there is the identity function. But you can use #(do %) if you insist. 来源: https://stackoverflow.com/questions/9148896/in-clojure-how-to-write

Is there a Way to Return the Last Element of a Set in Clojure?

感情迁移 提交于 2021-01-29 14:34:45
问题 user-> (last '(First (Second (Last)))) -> (Second (Last)) I would typically assume this ^ would return just (Last). Why is this? And is there a way to return just (Last) or Last. What about also for: (defn function [input] ;assume input = '(First (Second (Last))) (last input) ) 回答1: Strictly speaking, (First (Second (Last))) is not a Set, but a List. A Set is a collection of elements with no duplicates and ordering of the elements in not required. If you look closely, it's a list of 2

Error when trying to run Java related commands on MacOS

一笑奈何 提交于 2021-01-29 12:29:50
问题 For example, if I do java -version I get: Error: dl failure on line 542 Error: failed /Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home/lib/server/libjvm.dylib, because dlopen(/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home/lib/server/libjvm.dylib, 10): no suitable image found. Did find: /Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home/lib/server/libjvm.dylib: code signature invalid for '/Library/Java/JavaVirtualMachines/jdk-14.jdk/Contents/Home/lib/server/libjvm

How to print EDN output in JSON format using Cheshire custom encoder

女生的网名这么多〃 提交于 2021-01-29 10:22:45
问题 I am newbie with Clojure and I am trying to print EDN output to valid JSON format using Cheshire custom encoder for classes defined in java. My EDN file: {:xyz #XyzBuilder "testString"} Clojure code: (defn getXyz [str] (.getXyz (XyzBuilder.) str) ) (defn custom-readers [] {'xyz/builder getXyz} ) (add-encoder com.java.sample.Xyz (fn [c jsonGenerator] (.writeString jsonGenerator (str c)))) (edn/read-string {:readers (custom-readers)} (slurp filename) ) This generates below output: {"xyz":"Xyz