clojure

Using quote in Clojure

耗尽温柔 提交于 2020-01-02 00:50:16
问题 Quoting in clojure results in non-evaluation. ':a and :a return the same result. What is the difference between ':a and :a ? One is not evaluated and other evaluates to itself... but is this same as non-evaluation ? 回答1: ':a is shorthand for (quote :a) . (eval '(quote form)) returns form by definition. That is to say, if the Clojure function eval receives as its argument a list structure whose first element is the symbol quote , it returns the second element of said list structure without

Why do Clojure variable arity args get different types depending on use?

匆匆过客 提交于 2020-01-02 00:49:16
问题 In answering another question I came across something I didn't expect with Clojure's variable arity function args: user=> (defn wtf [& more] (println (type more)) :ok) #'user/wtf ;; 1) user=> (wtf 1 2 3 4) clojure.lang.ArraySeq :ok ;; 2) user=> (let [x (wtf 1 2 3 4)] x) clojure.lang.ArraySeq :ok ;; 3) user=> (def x (wtf 1 2 3 4)) clojure.lang.PersistentVector$ChunkedSeq #'user/x user=> x :ok Why is the type ArraySeq in 1) and 2), but PersistentVector$ChunkedSeq in 3)? 回答1: Short answer: It's

SICP, Continuation Passing Style and Clojure's trampoline

╄→гoц情女王★ 提交于 2020-01-01 19:19:09
问题 I am working with SICP and exercise 2.29-b gave me the opportunity to have fun with the Continuation Passing Style while traversing mobiles and branches. To make the story short, each mobile has left and right branch, which are composed by a length and either a numeric weight or another mobile. The question asks to find the total weight given a mobile. After the first mutually recursive solution, quite simple, I tried and successfully implemented a cps' one: (defn total-weight-cps [mobile]

Bundle native JNI shared libraries with Clojure libraries

天涯浪子 提交于 2020-01-01 12:06:11
问题 I am writing a library for clojure which involves native code. How can I bundle the shared library (aka native dependencies) when I deploy the clojure libraries to public repositories (like clojars)? Further Info: My project structure looks roughly like: src/ native/ - C code , C Object files and compiled shared libs java/ - Java stuff clojure/ - Clojure stuff I am currently using leineingen. I have tried doing: :jvm-opts [~(str "-Djava.library.path=src/native/:" (System/getenv "$LD_LIBRARY

Easiest way to manage my CLASSPATH?

和自甴很熟 提交于 2020-01-01 10:50:08
问题 I'm beginning to play with Clojure a bit and my Java experience is pretty limited. I'm coming from the dynamic world of Ruby and OO, so the functional side of things is very interesting! Anyway, as I discover libraries and various tools for use (and the tutorial files for the Pragmatic Clojure Book), everything typically calls for placing files in the CLASSPATH in order for Clojure to see the library for use. Is there such thing as good CLASSPATH practice? Would I ever want to only have a

Clojure: Implementing the comp function

99封情书 提交于 2020-01-01 10:48:08
问题 4Clojure Problem 58 is stated as: Write a function which allows you to create function compositions. The parameter list should take a variable number of functions, and create a function applies them from right-to-left. (= [3 2 1] ((__ rest reverse) [1 2 3 4])) (= 5 ((__ (partial + 3) second) [1 2 3 4])) (= true ((__ zero? #(mod % 8) +) 3 5 7 9)) (= "HELLO" ((__ #(.toUpperCase %) #(apply str %) take) 5 "hello world")) Here __ should be replaced by the solution. In this problem the function

Why is this Clojure program so slow? How to make it run fast?

久未见 提交于 2020-01-01 10:29:12
问题 Here it is clearly explained how to optimize a Clojure program dealing with primitive values: use type annotations and unchecked math, and it will run fast: (set! *unchecked-math* true) (defn add-up ^long [^long n] (loop [n n i 0 sum 0] (if (< n i) sum (recur n (inc i) (+ i sum))))) So, just out of curiosity, I've tried it in lein repl and, to my surprise, found this code running ~20 times slower that expected (Clojure 1.6.0 on Oracle JDK 1.8.0_11 x64): user=> (time (add-up 1e8)) "Elapsed

Why is this Clojure program so slow? How to make it run fast?

寵の児 提交于 2020-01-01 10:28:10
问题 Here it is clearly explained how to optimize a Clojure program dealing with primitive values: use type annotations and unchecked math, and it will run fast: (set! *unchecked-math* true) (defn add-up ^long [^long n] (loop [n n i 0 sum 0] (if (< n i) sum (recur n (inc i) (+ i sum))))) So, just out of curiosity, I've tried it in lein repl and, to my surprise, found this code running ~20 times slower that expected (Clojure 1.6.0 on Oracle JDK 1.8.0_11 x64): user=> (time (add-up 1e8)) "Elapsed

Big O of clojure library functions

时间秒杀一切 提交于 2020-01-01 10:01:42
问题 Can anyone point me to a resource that lists the Big-O complexity of basic clojure library functions such as conj, cons, etc.? I know that Big-O would vary depending on the type of the input, but still, is such a resource available? I feel uncomfortable coding something without having a rough idea of how quickly it'll run. 回答1: Here is a table composed by John Jacobsen and taken from this discussion: 回答2: Late to the party here, but I found the link in the comments of the first answer to be

Big O of clojure library functions

两盒软妹~` 提交于 2020-01-01 09:59:33
问题 Can anyone point me to a resource that lists the Big-O complexity of basic clojure library functions such as conj, cons, etc.? I know that Big-O would vary depending on the type of the input, but still, is such a resource available? I feel uncomfortable coding something without having a rough idea of how quickly it'll run. 回答1: Here is a table composed by John Jacobsen and taken from this discussion: 回答2: Late to the party here, but I found the link in the comments of the first answer to be