clojure

Differences in whether realization of a lazy sequence inside of a lazy sequence occurs

霸气de小男生 提交于 2019-12-22 09:48:23
问题 I wondered: What happens when you embed an expression that forces realization of a lazy sequence inside of an outer lazy sequence that's not realized? Answer: It seems to depend on how you create the outer lazy sequence. If the outer sequence comes from map , the inner sequence is realized, and if the outer sequence comes for iterate , it's not. Well, I'm pretty sure that that is not the right way to describe what happens below--I'm pretty sure that I'm not understanding something. Can

How can I provide a nice, easy-to-use abstraction for tracking “dirty” elements of a list?

不想你离开。 提交于 2019-12-22 09:01:34
问题 For "fun", and to learn functional programming, I'm developing a program in Clojure that does algorithmic composition using ideas from this theory of music called "Westergaardian Theory". It generates lines of music (where a line is just a single staff consisting of a sequence of notes, each with pitches and durations). It basically works like this: Start with a line consisting of three notes (the specifics of how these are chosen are not important). Randomly perform one of several

How to memoize a function that uses core.async and non-blocking channel read?

百般思念 提交于 2019-12-22 08:57:36
问题 I'd like to use memoize for a function that uses core.async and <! e.g (defn foo [x] (go (<! (timeout 2000)) (* 2 x))) (In the real-life, it could be useful in order to cache the results of server calls) I was able to achieve that by writing a core.async version of memoize (almost the same code as memoize): (defn memoize-async [f] (let [mem (atom {})] (fn [& args] (go (if-let [e (find @mem args)] (val e) (let [ret (<! (apply f args))]; this line differs from memoize [ret (apply f args)] (swap

ClojureScript map lookup slow

无人久伴 提交于 2019-12-22 08:54:09
问题 I have a simple map: (def my-map {[1 2 3] 1 [1 2 4] 5 [3 4 2] 3 [4 5 3] 3 [5 2 5] 6 [9 2 1] 5 [8 3 1] 6}) that I use for performing lookups. This performs rather poorly, however: (time (doseq [x (range 500)] (my-map [1 2 8]))) "Elapsed time: 170 msecs" On the same machine, Clojure can do 500,000 in about 236 msecs, or about 700x faster. While it's not unexpected for Clojure to be faster than ClojureScript, I'm confused why ClojureScript would be so much slower. Any ideas about how I could

sequence of a rolling average in Clojure

前提是你 提交于 2019-12-22 08:35:18
问题 I'm looking for an elegant way to generate a sequence of the rolling average of a sequence of numbers. Hopefully something more elegant than using lazy-seq 回答1: Without any consideration of efficiency: (defn average [lst] (/ (reduce + lst) (count lst))) (defn moving-average [window lst] (map average (partition window 1 lst))) user> (moving-average 5 '(1 2 3 4 5 6 7 8)) (3 4 5 6) If you need it to be fast, there are some fairly obvious improvements to be made! But it will get less elegant. 回答2

How can I deploy a Leiningen template to Clojars?

大兔子大兔子 提交于 2019-12-22 08:07:41
问题 I have created a Leiningen project on my local machine which I then turn into a template by doing: lein create-template webdb : Then I install the template: cd webdb lein install : which allows me to create projects based on the template locally: lein new webdb anewproject : Everything works fine up to here. However if I try to deploy the template to clojars using: cd webdb lein deploy clojars : then whenever I try to use the clojars profile to create a template I get an error: lein new org

From Static Typing to Dynamic Typing

依然范特西╮ 提交于 2019-12-22 06:48:30
问题 I have always worked on statically typed languages (C/C++, Java). I have been playing with Clojure and I really like it. One thing I am worried about is: say that I have a windows that takes 3 modules as arguments and along the way the requirements change and I need to pass another module to the function. I just change the function and the compiler complains everywhere I used it. But in Clojure it won't complain until the function is called. I can just do a regex search and replace but it

How to setup the classpath when running the jar made from 'lein uberjar'?

旧巷老猫 提交于 2019-12-22 06:44:41
问题 I have a hello.clj as follows. (ns hello) (defn hi [] (println "HI")) Normally, I can use this function from main.clj as follows. The hello.clj is in the same directory that contains main.clj. And the classpath includes . (current path). (use 'hello) (hi) How can I use this hello.clj for the 'lein uberjar'? I used 'lein new myproject; lein deps' to get the following structure. . |-- README |-- classes | `-- myproject |-- lib | |-- clojure-1.2.0-beta1.jar | |-- clojure-contrib-1.2.0-beta1.jar

Why can't Leiningen always use my :gen-class properly?

送分小仙女□ 提交于 2019-12-22 06:41:40
问题 Let's say I create a new Leiningen project ( lein new app example ) and add some code in example/src/example/core.clj that makes use of :gen-class : (ns example.core (:gen-class :extends javafx.application.Application)) (defn -start [this stage] (.show stage)) (defn -main [& args] (javafx.application.Application/launch example.core args)) If I then create a JAR ( lein uberjar ) and run it, everything works fine. However, if I instead try to run my app directly ( lein run ), I get a

Embedding arbitrary objects in Clojure code

亡梦爱人 提交于 2019-12-22 06:38:28
问题 I want to embed a Java object (in this case a BufferedImage) in Clojure code that can be eval d later. Creating the code works fine: (defn f [image] `(.getRGB ~image 0 0)) => #'user/f (f some-buffered-image) => (.getRGB #<BufferedImage BufferedImage@5527f4f9: type = 2 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=ff000000 IntegerInterleavedRaster: width = 256 height = 256 #Bands = 4 xOff = 0 yOff = 0 dataOffset[0] 0> 0 0) However you get an exception when trying to eval it: (eval