spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories
  Home » CLOJURE » Page 1
Implementing break in Clojure
You don't really need a break statement, because looping works very differently in clojure than it does in, say, java. For instance, the following: user=> (loop [[x & t] [0 1 2 3 4 5 6 7 8 9]] #_=> (println "x=" x) #_=> (if (= x 5) #_=> x #_=> (recur t))) x= 0 x= 1 x= 2 x= 3 x= 4 x= 5 5 user=> is roughly equivalent to if(x == 5) break; in java. You ha

Categories : Clojure

Clojure, transform collection to map
We can use map to construct a hash-map for each index of the collections. When provided with more than two arguments, map moves through all the collections in parallel. user> (let [dates '("Aujourd'hui" "Demain" "25.11" "26.11" "27.11" "28.11" "29.11") mins '("2 °C" "2 °C" "1 °C" "0 °C" "-3 °C" "-4 °C" "0 °C") maxes '("8 °C" "6 °C" "4 °C" "2 °C" "1 °C" "1

Categories : Clojure

How to check if Clojure value supports metadata?
user> (defn meta-available? [x] (instance? clojure.lang.IMeta x)) #'user/meta-available? user> (meta-available? 1) ;=> false user> (meta-available? "abc") ;=> false user> (meta-available? [1 2 3]) ;=> true user> (meta-available? {:a 1}) ;=> true

Categories : Clojure

Thread through functions unless not null
user> (defn keep-looking [value & sources] (loop [s sources] (if-let [ret ((first s) value)] ret (recur (rest sources))))) #'user/keep-looking user> (keep-looking :something {:not-something 1} {:something 3}) ;=> 3

Categories : Clojure

Clojure: 'folding' a sequence (partitioning, as it turned out)
(->> '(1 2 3 4 5 6 7 8 9) (partition 3) (map vec)) Take the original list and then partition it by 3 and finally map each partition to a vector. I think using the ->> macro makes it read nicer.

Categories : Clojure

lein repl stopped working
Try increasing the timeout as explained here: lein REPL server launch timed out If that doesnt work, I would suggest connecting with a jconsole or doing a thread dump to see where is the process waiting for.

Categories : Clojure

lexical/dynamic scope in Clojure - scope when redefining a var
Okay. in Clojure, all defs are global scope. All. Including the defs here inside your function some-function body. Consequently when executing some-function, the last two forms create a new global definition of non-dynamic-var and then call out to the function using it having modified the global root binding of the var non-dynamic-var.

Categories : Clojure

How to write touch-all (touch all reachable entities from an entity) in Datomic?
The answer to your question is immutability. entity is bound to the first touched entity passed by parameter, but that value doesn't change when the recursion touches other entities. If you want a single value containing a tree of touched entities you should do something like: (defn touch-all "Touches `entity and all other reachable Entities of `entity" [e] (if (instance? datomic.query.En

Categories : Clojure

write large data structures as EDN to disk in clojure
You can control the number of items in a collection that are printed via *print-length* Consider using spit instead of manually opening the writer and pr-str instead of manually binding to *out*. (binding [*print-length* false] (spit "data.clj" (pr-str large-data-structure)) Edit from comment: (with-open [w (clojure.java.io/writer "data.clj")] (binding [*print-length* false

Categories : Clojure

How to integrate libraries (clojars) into Lightable plugins
Yes currently seems that Cljs native or Cljx for that matter, doesn't resonate well with the LightTable UI. Trying https://github.com/swannodette/om/wiki/Basic-Tutorial you'll find that it doesn't take much. You can create a LT keybinding to open browser tab, navigate to project folder index.html and make another for reloading (as autoreload is a work in progress I think). Then another tab with t

Categories : Clojure

Couldn't start riemann.bin?
The error seems to state that port 5555 is already in use. Perhaps a previously (aborted) start left a process that still has a handle on the port ? Do a netstat -ao (Windows) or netstat -ap (Linux) to see if there's already a listening port 5555, and which process has a handle on it, then kill that process.

Categories : Clojure

When and how many times can a clojure file be evaluated with leiningen?
Leiningen knows nothing about the structure of your project in terms of how namespaces relate to each-other. Consequently, when compiling a project, lein simply boots a JVM and forces each namespace to be loaded one at a time. This means that, as you noticed, namespaces will be reloaded causing the double evaluation behaviour you are noticing. In comparison, (clojure.core/compile) simply loads th

Categories : Clojure

How to generate random password with the fixed length in Clojure?
(defn rand-string [characters n] (->> (fn [] (rand-nth characters)) repeatedly (take n) (apply str))) It's not deterministic as-is because rand-nth has side-effects, but it should be enough for you to get started with.

Categories : Clojure

Clojure-koans run error:
Have you run lein deps in the project? From the documentation (https://github.com/functional-koans/clojure-koans): The only things you'll need to run the Clojure Koans are: JRE 1.5 or higher clojure-1.5.1.jar You can use Leiningen to automatically install the Clojure jar in the right place. Leiningen will also get you a couple more jarfiles, including JLine, which allows you som

Categories : Clojure

Which version of Java should I use for Clojure (performance)?
Newer versions of Java generally are faster than old versions. I would expect Java 8 to be at least the same, if not a little faster for Clojure code. Java 8 is faster than Java 6 and Java 7 in this benchmark: http://www.optaplanner.org/blog/2014/03/20/HowMuchFasterIsJava8.html

Categories : Clojure

How do I configure sshd_config
As of Nov 23 2014, the package is not available on clojars for whatever reason. Note: Pallet will check the md5 hash after the first write and will error if you try lifting again after manually making local changes. This can be worked around with remote-file with :content and :overwrite-changes true instead of using ssh-crate. See https://github.com/pallet/pallet/blob/develop/src/pallet/actions.

Categories : Clojure

Rust vs. Clojure speed comparasion, any improvement for the Clojure code?
If you look at the source of +, you will see that (reduce +) and (apply +) are identical for higher argument counts. (apply +) is optimized for the 1 or 2 argument versions though. (range) is going to be much faster than (iterate inc 0) for most cases. partial is slower than a simple anonymous function, and should be reserved for cases where you don't know how many more args will be supplied. S

Categories : Clojure

Clojure Transients Example - No significant speedup
Your results are consistent with my experience with transients. I've used them quite a bit and I typically see a 2x performance improvement. I tried this on Ubuntu 12.04, OpenJDK 1.7 with Clojure 1.6.0 and 1.7.0-alpha3. I get 2x performance with transients, slightly less than the 3x I get on OSX with the 1.8 Oracle jvm. Also the above page is from the time of Clojure 1.2, and the performance of

Categories : Clojure

Array-map example in clojure
You can use assoc to add values to a map. assoc takes 3 args. The first arg is the map that you want to add to, 2nd arg is a key, and the third is a value. The function returns the old map with the key-value pair added. Example: (assoc {} 1 {:name "abc"}) returns {1 {:name "abc"}}

Categories : Clojure

Dealing with database reads in Clojure
One thing you could do here would be a dependency-injection-like approach of supplying the (potentially) side-effectful function as an optional parameter, e.g.: (defn transform-users [users & {:keys [ids->profiles] :or {ids->profiles #(db/read :profiles %)}] (let [ids (map :id users) profiles (ids->profiles ids)] (profiles->something profiles))) This

Categories : Clojure

Why does a clojure future block the main thread?
Your question is really twofold: Why does lein not return? lein hangs because the thread pool which backs Clojure futures does not use daemon threads so you have to explicitly shut it down. If you change your code to the following, it should work: (def f (future 42)) (defn -main [& args] (println @f) (shutdown-agents)) Futures block the main thread The line (println @f) can pote

Categories : Clojure

Application configuration file
Clojure Cookbook has a section exactly on that: https://github.com/clojure-cookbook/clojure-cookbook/blob/master/04_local-io/4-15_edn-config.asciidoc.

Categories : Clojure

Clojure: sequences, mapping and the "current number/index" issue
map takes 1 or more arguments (map file-handler (range) files) there is also a shortcut for this (map-indexed file-handler files) This will require updating file-handler to take 2 arguments, or making a wrapper function that takes the index and the file and calls file-handler on the file.

Categories : Clojure


Recently Add
How to add days to current date in clojure
Implementing break in Clojure
Clojure, transform collection to map
How to check if Clojure value supports metadata?
Thread through functions unless not null
Clojure: 'folding' a sequence (partitioning, as it turned out)
lein repl stopped working
lexical/dynamic scope in Clojure - scope when redefining a var
How to write touch-all (touch all reachable entities from an entity) in Datomic?
write large data structures as EDN to disk in clojure
How to integrate libraries (clojars) into Lightable plugins
Couldn't start riemann.bin?
When and how many times can a clojure file be evaluated with leiningen?
How to generate random password with the fixed length in Clojure?
Clojure-koans run error:
Which version of Java should I use for Clojure (performance)?
How do I configure sshd_config
Rust vs. Clojure speed comparasion, any improvement for the Clojure code?
Clojure Transients Example - No significant speedup
Array-map example in clojure
Dealing with database reads in Clojure
Why does a clojure future block the main thread?
Application configuration file
Clojure: sequences, mapping and the "current number/index" issue
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.