clojure

Not getting integer overflow in Clojure?

≯℡__Kan透↙ 提交于 2019-12-23 08:57:54
问题 I am running Clojure 1.3.0 with La Clojure in IntelliJ IDEA while reading The Joy Of Clojure , and on section 4.1.3 (page 64), the authors demonstrate integer overflow with the following code: (+ Integer/MAX_VALUE Integer/MAX_VALUE) ;=> java.lang.ArithmeticException: integer overflow However, when I try it out on the REPL, I get instead user=> (+ Integer/MAX_VALUE Integer/MAX_VALUE) 4294967294 user=> Integer/MAX_VALUE 2147483647 What is happening here? Why are my integers being added

counting only truthy values in a collection [duplicate]

非 Y 不嫁゛ 提交于 2019-12-23 08:36:08
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: how to return only truthy values as the result of a map operation I have a collection which has falsy and truthy values. I would like to only count the truthy values, is there a way to do that ? (count (1 2 3 nil nil)) => 5 回答1: If you want to keep the truthy values just need to use the identity function: (count (filter identity '(1 2 3 nil nil false true))) 回答2: I would recommend doing this with reduce as

counting only truthy values in a collection [duplicate]

孤街醉人 提交于 2019-12-23 08:36:06
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: how to return only truthy values as the result of a map operation I have a collection which has falsy and truthy values. I would like to only count the truthy values, is there a way to do that ? (count (1 2 3 nil nil)) => 5 回答1: If you want to keep the truthy values just need to use the identity function: (count (filter identity '(1 2 3 nil nil false true))) 回答2: I would recommend doing this with reduce as

(Another) Stack overflow on loop-recur in Clojure

眉间皱痕 提交于 2019-12-23 08:28:25
问题 Similar questions: One, Two, Three. I am thoroughly flummoxed here. I'm using the loop-recur form, I'm using doall, and still I get a stack overflow for large loops. My Clojure version is 1.5.1. Context: I'm training a neural net to mimic XOR. The function xor is the feed-forward function, taking weights and input and returning the result; the function b-xor is the back-propagation function that returns updated weights given the results of the last call to xor . The following loop runs just

(Another) Stack overflow on loop-recur in Clojure

匆匆过客 提交于 2019-12-23 08:28:03
问题 Similar questions: One, Two, Three. I am thoroughly flummoxed here. I'm using the loop-recur form, I'm using doall, and still I get a stack overflow for large loops. My Clojure version is 1.5.1. Context: I'm training a neural net to mimic XOR. The function xor is the feed-forward function, taking weights and input and returning the result; the function b-xor is the back-propagation function that returns updated weights given the results of the last call to xor . The following loop runs just

Clojure Parallel Mapping and Infinite Sequences

馋奶兔 提交于 2019-12-23 08:05:59
问题 Let's say I define the sequence of all natural numbers in the following way: (def naturals (iterate inc 0)) I also define a function mapping the naturals to nil that takes a while to compute like so: (defn hard-comp [_] (Thread/sleep 500)) Note the computation time to evaulate the following s-expressions as measured by clojure.core/time . (dorun (map hard-comp (range 30))) ; 15010.367496 msecs (dorun (pmap hard-comp (range 30))) ; 537.044554 msecs (dorun (map hard-comp (doall (take 30

Security implications of Clojure keyword creation from user data?

不打扰是莪最后的温柔 提交于 2019-12-23 07:57:26
问题 Suppose that I take a user-supplied string, userstring, and call (keyword userstring) on it. Are there any security concerns about doing this? And if so, what would be the best way to mitigate them? 回答1: Per http://clojure.org/reader, there are rules for which characters are valid in symbols and keywords. (For now, alphanumeric characters and * , + , ! , - , _ , and ? .) You should never create a symbol containing any other characters. However, right now, these rules are completely unenforced

How to dispatch a multimethod on the type of an array

我的未来我决定 提交于 2019-12-23 07:55:48
问题 I'm working on a multimethod that needs to update a hash for a bunch of different things in a sequence. Looked fairly straitforward until I tried to enter the 'type of an array of X'. (defmulti update-hash #(class %2)) (type (byte 1)) => java.lang.Byte (defmethod update-hash java.lang.Byte [md byte] (. md update byte)) (type (into-array [ (byte 1)])) => [Ljava.lang.Byte; (defmethod update-hash < WHAT GOES HERE > [md byte] 回答1: Either of these should work: (defmethod update-hash (Class/forName

How does Clojure's laziness interact with calls to Java/impure code?

拥有回忆 提交于 2019-12-23 07:52:35
问题 We stumbled upon an issue in our code today, and couldn't answer this Clojure question: Does Clojure evaluate impure code (or calls to Java code) strictly or lazily? It seems that side-effects + lazy sequences can lead to strange behavior. Here's what we know that led to the question: Clojure has lazy sequences: user=> (take 5 (range)) ; (range) returns an infinite list (0 1 2 3 4) And Clojure has side-effects and impure functions: user=> (def value (println 5)) 5 ; 5 is printed out to screen

In clojure, how to write a function that applies several string replacements?

Deadly 提交于 2019-12-23 07:46:11
问题 I would like to write a function replace-several that receives a string and a set of replacements and apply all the replacements (where the replacements see the result of the previous replacements). I thought about the following interface: (replace-several "abc" #"a" "c" #"b" "l" #"c" "j"); should return "jlj" Two questions: Is it the most idiomatic interface in clojure? How to implement this function? Remark: To make a single replacement, there is replace available in clojure.string. 回答1: