clojure

What are the reasons that protocols and multimethods in Clojure are less powerful for polymorphism than typeclasses in Haskell?

◇◆丶佛笑我妖孽 提交于 2019-12-29 04:30:18
问题 More broadly this question is about various approaches to the expression problem. The idea is that your program is a combination of a datatype and operations over it. We want to be able to add new cases without recompiling the old classes. Now Haskell has some really awesome solutions to the expression problem with the TypeClass. In particular - we can do: class Eq a where (==) :: a -> a -> Bool (/=) :: a -> a -> Bool member :: (Eq a) => a -> [a] -> Bool member y [] = False member y (x:xs) =

Clojure Jython interop

﹥>﹥吖頭↗ 提交于 2019-12-29 03:30:07
问题 I was wondering if anyone has tried somehow calling Jython functions from within Clojure, and how you went about doing this if so. I have not used Jython, but I would imagine the Jython interpreter can be invoked in the same way as any other java code, and Python programs can be run within it. However I wonder if it would be possible to somehow call individual python functions from Clojure. Like I said, I have not tried this yet, so it might actually be straightforward and obvious. I'm just

How do you evaluate a string as a clojure expression?

房东的猫 提交于 2019-12-28 16:30:43
问题 How would I get something similar to the following?: (evaluate-text "(+ 1 2)") ; resolves to 3 回答1: (load-string "(+ 1 2)") 回答2: user> (eval (read-string "(+ 1 2)")) 3 You probably shouldn't ever need to do this. Macros and fns make this kind of thing unnecessary 99% of the time. This is quite brittle, and can be unsafe if these strings are coming from user input, and so on. 回答3: How similar does it have to be? Clojure's eval works on lists, so: (eval (list + 1 2)) #=> 3 来源: https:/

Clojure base64 encoding

北城以北 提交于 2019-12-28 13:45:10
问题 I want something as simple as "string" -> base64. With the older base64.encode-str it was easy (and sounded "more clojure", but the newer clojure.data.codec.base64 requires input and output streams and seems an ugly wrapper around Java way of doing things. So, what is the way, having a string, to get a base64 encoded array? Thanks 回答1: You can use encode function and pass array of bytes: (encode (.getBytes "Hello world!")) 回答2: Four years later, but I think this is worth mentioning if you're

How do you compute the difference between successive elements of a list of unknown size, functionally?

你。 提交于 2019-12-28 05:49:05
问题 In a programming language that is purely functional (like Haskell) or where you are only using it in a functional way (eg clojure); suppose you have a list/seq/enumerable (of unknown size) of integers and you want to produce a new list/seq/enumerable that contains the differences between successive items, how would you do it? What I did previously in C# was to fold over the list and keep a state object as the aggregating value which recorded the 'previous' item so that you could do a diff on

Wrong number of args (0) passed to: PersistentVector on loop/recur function

时光毁灭记忆、已成空白 提交于 2019-12-28 04:31:49
问题 Trying to define a factors function that will return a vector of all the factors of a number using loop/recur. ;; `prime?` borrowed from https://swizec.com/blog/comparing-clojure-and-node-js-for-speed/swizec/1593 (defn prime? [n] (if (even? n) false (let [root (num (int (Math/sqrt n)))] (loop [i 3] (if (> i root) true (if (zero? (mod n i)) false (recur (+ i 2)))))))) (defn factors [x] ( (loop [n x i 2 acc []] (if (prime? n) (conj acc n) (if (zero? (mod n i)) (recur (/ n i) 2 (conj acc i))

How can I display the definition of a function in Clojure at the REPL?

强颜欢笑 提交于 2019-12-28 03:46:06
问题 I'm looking for the ability to have the REPL print the current definition of a function. Is there any way to do this? For example, given: (defn foo [] (if true "true")) I'd like to say something like (print-definition foo) and get something along the lines of (foo [] (if true "true")) printed. 回答1: An alternative to source (which should be available via clojure.repl/source when starting a REPL, as of 1.2.0 . If you're working with 1.1.0 or lower, source is in clojure.contrib.repl-utils .),

Does Clojure have an equivalent to Python's if __name__==“__main__”? [duplicate]

断了今生、忘了曾经 提交于 2019-12-25 18:07:53
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What is the clojure equivalent of the Python idiom “if name == 'main'”? I would use -main , but it only runs in compiled mode, not interpreted mode. I would use (if (.isAbsolute (java.io.File. *file*)) (main *command-line-args*)) , but that runs during any (load) ing of scripts. 回答1: Duplicate of What is the clojure equivalent of the Python idiom "if __name__ == '__main__'"?. I guess I'm supposed to "vote to

Does Clojure have an equivalent to Python's if __name__==“__main__”? [duplicate]

北城余情 提交于 2019-12-25 18:07:20
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: What is the clojure equivalent of the Python idiom “if name == 'main'”? I would use -main , but it only runs in compiled mode, not interpreted mode. I would use (if (.isAbsolute (java.io.File. *file*)) (main *command-line-args*)) , but that runs during any (load) ing of scripts. 回答1: Duplicate of What is the clojure equivalent of the Python idiom "if __name__ == '__main__'"?. I guess I'm supposed to "vote to

how to extract data in nested list/vector clojure

烈酒焚心 提交于 2019-12-25 17:48:12
问题 I have parse xml and get the following result (({:tag :Column, :attrs {:Name "VENDOR_KEY", :Type "Int", :NotNull "Yes"}, :content nil} {:tag :Column, :attrs {:Name "RETAILER_KEY", :Type "Int", :NotNull "Yes"}, :content nil} {:tag :Column, :attrs {:Name "ITEM_KEY", :Type "Int", :NotNull "Yes"}, :content nil}) ({:tag :Column, :attrs {:Name "Store_Key", :Type "Int", :NotNull "Yes"}, :content nil})) then how to convert it to the following, basically I want to extract the value of key :attrs in