What’s the one-level sequence flattening function in Clojure?
My general first choice is apply concat. Also, don’t overlook (for [subcoll coll, item subcoll] item) — depending on the broader context, this may result in clearer code.
My general first choice is apply concat. Also, don’t overlook (for [subcoll coll, item subcoll] item) — depending on the broader context, this may result in clearer code.
If it’s just a matter of wanting to capture the string programmatically, you can bind *out* to something else before using time. user=> (def x (with-out-str (time (+ 2 2)))) #’user/x user=> x “\”Elapsed time: 0.119 msecs\”\n” If you want more control over the format, then you can create your own version of time using … Read more
The first concern – yes the core operations are side effects. However channels don’t have the problems normally associated with mutable references as they don’t represent a “place” – channels are opaque, you cannot inspect them, in fact you can’t even query whether a channel is closed or not beyond reading nil. The second concern … Read more
The error’s meaning shouldn’t be too hard to sort out: a number is being used where a function is expected. Parenthesis in Clojure are not a grouping construct, they are used primarily to invoke function calls. If you change (salary) to salary you will return the number rather than attempting to call it as a … Read more
I’ll go from high-level down to your particular problem: How Clojure (or LISPs) Generally Work REPLs, or Read-Eval-Print Loops are the core of how LISPs are designed: The reader converts a stream of characters into data structures (called Reader Forms). The evaluator takes collection of reader forms and evaluates them. The printer emits the results … Read more
Clojure is always compiled. The Clojure compiler produces Java byte code, which is typically then JIT-compiled to native code by the JVM. The thing that can be confusing is the dynamic and interactive nature of Clojure that means you can invoke the compiler at run-time if you want to. This is all part of the … Read more
How about this? (apply str (repeat 3 “str”)) Or just (repeat 3 “str”) if you want a sequence instead of a string.
And another one: (let [m {:a 1 :b 2 :c 1}] (select-keys m (for [[k v] m :when (= v 1)] k)))
After Clojure 1.6 you can use some?: (some? :foo) => true (some? nil) => false This is useful, eg, as a predicate: (filter some? [1 nil 2]) => (1 2)
A lazy-seq call just executes the body once the first time it is accessed, then caches and returns the same result whenever it is called again in the future. If you want to use this to build long (or even infinite) sequences, then you need to recursively nest other lazy-seq calls in the returned sequence. … Read more