Re: How to work with variables that need to change

2014-04-15 Thread Gary Trakhman
One nitpick I noticed, reduce is eager, not lazy. On Tue, Apr 15, 2014 at 9:25 AM, Alex Vzorov wrote: > Hi, Cecil. > > The difference is that doseq is used to produce side-effects, which is > idiomatic, which is exactly what you did. When using doseq you can be > sure that it's body will evalua

Re: How to work with variables that need to change

2014-04-15 Thread Alex Vzorov
Hi, Cecil. The difference is that doseq is used to produce side-effects, which is idiomatic, which is exactly what you did. When using doseq you can be sure that it's body will evaluate, whereas with map, for, reduce you can't - they expect pure functions, and produce lazy sequences, and there

Re: How to work with variables that need to change

2014-04-12 Thread John Mastro
On Sat, Apr 12, 2014 at 5:13 AM, Cecil Westerhof wrote: > But it looks a ‘little’ cumbersome. Is there a better way to do this? Here's another way. Not the best way, but I offer it to introduce you to atoms [1] if you're not familiar yet. [1] http://clojure.org/atoms (def numbers '(4 6 8 10))

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 20:36 GMT+02:00 Fergal Byrne : > It's always best to see how best to do something, one day it'll make all > the difference. > I agree. Most people call it gold-plating. And of-course you have to be careful about that, but things like this are good to do is my opinion. > On Sat, Apr

Re: How to work with variables that need to change

2014-04-12 Thread Fergal Byrne
Cheers Cecil, It's always best to see how best to do something, one day it'll make all the difference. On Sat, Apr 12, 2014 at 7:03 PM, Cecil Westerhof wrote: > 2014-04-12 16:18 GMT+02:00 Fergal Byrne : > > That's fine, but note that creating new Calendar objects has an overhead, >> using Syste

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 16:18 GMT+02:00 Fergal Byrne : > That's fine, but note that creating new Calendar objects has an overhead, > using System.currentTimeMillis() is a static OS call which your now() > probably uses, as well as object creation. It probably won't be millisecond > sized, but you can measure y

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 18:19 GMT+02:00 Fergal Byrne : > > On Sat, Apr 12, 2014 at 4:55 PM, Cecil Westerhof > wrote: > >> (->> numbers >> (reduce timed-foo []) >> (map format-time) >> println) >> > > (->> numbers > (reduce timed-foo []) > (map format-time) > (map println)) > Tha

Re: How to work with variables that need to change

2014-04-12 Thread Fergal Byrne
On Sat, Apr 12, 2014 at 4:55 PM, Cecil Westerhof wrote: > (->> numbers > (reduce timed-foo []) > (map format-time) > println) > (->> numbers (reduce timed-foo []) (map format-time) (map println)) -- Fergal Byrne, Brenter IT Author, Real Machine Intelligence with

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 16:27 GMT+02:00 Fergal Byrne : > (defn timed-foo [times n] > (let [start (now)] > (foo n) > (conj times [n (- (now) start)]))) > > (defn format-time [[n t]] > (format "%2d threads took %7d milliseconds" n t)) > > (->> numbers > (reduce timed-foo []) > (map format-t

Re: How to work with variables that need to change

2014-04-12 Thread Niels van Klaveren
For storing timing results of expressions, I make use of an agent to store them in, and a modified time macro. (def timings (agent [])) (defmacro send-timing "Evaluates expr and prints the time it took. Returns the value of expr." {:added "1.0"} [agnt expr] `(let [start# (System/curren

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 16:18 GMT+02:00 Fergal Byrne : > That's fine, but note that creating new Calendar objects has an overhead, > using System.currentTimeMillis() is a static OS call which your now() > probably uses, as well as object creation. It probably won't be millisecond > sized, but you can measure y

Re: How to work with variables that need to change

2014-04-12 Thread Fergal Byrne
(defn timed-foo [times n] (let [start (now)] (foo n) (conj times [n (- (now) start)]))) (defn format-time [[n t]] (format "%2d threads took %7d milliseconds" n t)) (->> numbers (reduce timed-foo []) (map format-time) println) On Sat, Apr 12, 2014 at 3:15 PM, Cecil We

Re: How to work with variables that need to change

2014-04-12 Thread Fergal Byrne
Hi Cecil, That's fine, but note that creating new Calendar objects has an overhead, using System.currentTimeMillis() is a static OS call which your now() probably uses, as well as object creation. It probably won't be millisecond sized, but you can measure your (now) using criterium.bench to be su

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 15:49 GMT+02:00 Cecil Westerhof : > 2014-04-12 15:06 GMT+02:00 Fergal Byrne : > or to convert your code to something more functional (and including defns > for now and foo): > >> >> (defn now [] (. System currentTimeMillis)) >> > > I already defined it as: > (defn now [] > (ne

Re: How to work with variables that need to change

2014-04-12 Thread Cecil Westerhof
2014-04-12 15:06 GMT+02:00 Fergal Byrne : > For precise timing benchmarks, use criterium [1]. > For simple, gross timing, use > > (map #(time (foo %)) numbers) > That is not going to work, time prints the time instead of giving it back. or to convert your code to something more functional (and

Re: How to work with variables that need to change

2014-04-12 Thread Fergal Byrne
Hi Cecil, For precise timing benchmarks, use criterium [1]. For simple, gross timing, use (map #(time (foo %)) numbers) or to convert your code to something more functional (and including defns for now and foo): (defn now [] (. System currentTimeMillis)) (def numbers '(4 6 8 10)) (defn fo