Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-19 Thread Robert Levy
I like cyclefn too. I thought of something like that too, but my reason for preferring https://github.com/rplevy/funcycle/blob/master/src/fun/cycle.clj over it was only that if you aren't careful and you hang onto the produced function, you might get some unwanted non-determinism. On Mon, Nov 17,

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-17 Thread Andy L
Thanks for all the ideas. I like cyclefn the most, a bit of investment resulting in super clean output. Andy On Sat, Nov 15, 2014 at 9:35 AM, Ben Wolfson wrote: > or > > (defn enumerate [xs] (map vector (range) xs)) > > (defn altsum [n] (reduce (fn [acc [i f]] (f acc (inc i))) >

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-15 Thread Ben Wolfson
or (defn enumerate [xs] (map vector (range) xs)) (defn altsum [n] (reduce (fn [acc [i f]] (f acc (inc i))) 0 (take n (enumerate (cycle [+ -]) On Fri, Nov 14, 2014 at 3:41 PM, Andrew Oberstar wrote: > How about this? > > (defn cyclefn > [

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-14 Thread Andrew Oberstar
How about this? (defn cyclefn [& fs] (let [fcycle (cycle fs) rem-fs (atom fcycle)] (fn [& args] (let [f (first @rem-fs)] (swap! rem-fs rest) (apply f args) (reduce (cyclefn + -) (range 1 100)) cyclefn could be used to cycle through any set of functions y

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-14 Thread Henrik Lundahl
How about this? :-) (defn altsum [n] (/ (if (odd? n) (+ 1 n) (- n)) 2)) -- Henrik On Fri, Nov 14, 2014 at 1:48 PM, Gary Verhaegen wrote: > What about cheating a bit? > > (interleave > (iterate #(+ % 2) 1) > (iterate #(- % 2) -2)) > > Then take n, reduce +, or whatever else you might want

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-14 Thread Jonas
Is this cheating? (defn altsum [n] (int (* (if (even? (inc n)) 1 -1) (Math/floor (/ (inc n) 2) On Friday, November 14, 2014 3:31:38 AM UTC+2, Andy L wrote: > > Hi, > > All I was able to come up with was this > > (defn altsum[n] (reduce + (map * (range 1 (inc n)) (in

a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-14 Thread Gary Verhaegen
What about cheating a bit? (interleave (iterate #(+ % 2) 1) (iterate #(- % 2) -2)) Then take n, reduce +, or whatever else you might want to do with the series. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
Changed name: https://github.com/rplevy/funcycle/blob/master/src/fun/cycle.clj On Fri, Nov 14, 2014 at 12:49 AM, Robert Levy wrote: > This is pretty trivial, but it is maybe sometimes more natural to express > something as a cycling pattern of function applications over some data than > as one c

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
This is pretty trivial, but it is maybe sometimes more natural to express something as a cycling pattern of function applications over some data than as one constant function. The typical way of expressing those cases involves twisting it around so that the data is rotated/alternated instead of wh

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
I was just thinking about this some more. Here's a slightly less terrible idea: (defn rotate [coll] (rest (take (inc (count coll)) (cycle coll (defn reducycle [fns init coll] (let [fns-atom (atom fns) alt-op (fn [& args] (swap! fns-atom rotate)

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
sorry, make that (*reduce alt-op *(*range 1 10)*) On Thu, Nov 13, 2014 at 10:28 PM, Robert Levy wrote: > (let [op (atom +)] > (defn alt-op [a b] > ((swap! op #(if (= % +) - +)) a b))) > > (map alt-op (range 1 10)) > > On Thu, Nov 13, 2014 at 10:09 PM, Robert Levy wrote: > >> But for "app

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
(let [op (atom +)] (defn alt-op [a b] ((swap! op #(if (= % +) - +)) a b))) (map alt-op (range 1 10)) On Thu, Nov 13, 2014 at 10:09 PM, Robert Levy wrote: > But for "applyv" you could do this: > > (*reduce + *(*map *(*comp eval list*) (*cycle *[*+ -*]) (*range 1 10*))) > > On Thu, Nov 13,

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
But for "applyv" you could do this: (*reduce + *(*map *(*comp eval list*) (*cycle *[*+ -*]) (*range 1 10*))) On Thu, Nov 13, 2014 at 10:06 PM, Robert Levy wrote: > Is that any more elegant than Dave's (reduce + (map * (cycle [1 -1]) > (range 1 n))) though? I would say that's the best actually

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
Is that any more elegant than Dave's (reduce + (map * (cycle [1 -1]) (range 1 n))) though? I would say that's the best actually sensible answer proposed in this thread. On Thu, Nov 13, 2014 at 9:54 PM, Andy L wrote: > > > On Thu, Nov 13, 2014 at 7:23 PM, Robert Levy wrote: > >> You don't need

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Andy L
On Thu, Nov 13, 2014 at 7:23 PM, Robert Levy wrote: > You don't need this for numbers over 900 right? > > I see what you mean. But no, I just practice and try to capture patterns. So, going after your example I got following: (reduce + (map applyv (cycle [+ -]) (range 1 10))) where something li

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
You don't need this for numbers over 900 right? On Thu, Nov 13, 2014 at 9:19 PM, Robert Levy wrote: > (defmacro altsum [n] `(-> 0 ~@(map list (cycle [+ -]) (range 1 n > > On Thu, Nov 13, 2014 at 9:02 PM, Andy L wrote: > >> (reduce + (map * (mapcat (fn[_] [1 -1]) (repeat nil)) (range 1 n)))

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Robert Levy
(defmacro altsum [n] `(-> 0 ~@(map list (cycle [+ -]) (range 1 n On Thu, Nov 13, 2014 at 9:02 PM, Andy L wrote: > (reduce + (map * (mapcat (fn[_] [1 -1]) (repeat nil)) (range 1 n))) > > not the best pattern for this case, but possibly useful to generate > alternated values ... > > A. > > --

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Paul English
You could use (-1)^{i+1}, which would be a bit more mathy but it may not be efficient. (defn altsum [n] (->> (range 1 (inc n)) (map #(* % (Math/pow -1 (inc % (reduce +))) On Nov 13, 2014, at 6:31 PM, Andy L wrote: > Hi, > > All I was able to come up with was this > > (de

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Andy L
(reduce + (map * (mapcat (fn[_] [1 -1]) (repeat nil)) (range 1 n))) not the best pattern for this case, but possibly useful to generate alternated values ... A. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clo

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Andy L
On Thu, Nov 13, 2014 at 6:36 PM, Dave Ray wrote: > How about: > > (->> (map * (cycle [1 -1]) (range 1 n)) > (reduce +)) > > Thx - I did not know cycle before. I think this is it, although I prefer nesting over threading. This is another I was thinking about: -- You received this message b

Re: a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Dave Ray
How about: (->> (map * (cycle [1 -1]) (range 1 n)) (reduce +)) ? Dave On Thu, Nov 13, 2014 at 5:31 PM, Andy L wrote: > Hi, > > All I was able to come up with was this > > (defn altsum[n] (reduce + (map * (range 1 (inc n)) (interpose -1 (repeat > 1) > > ... works quite well, however

a nicer way to write 1 - 2 + 3 - 4 ... +/- n

2014-11-13 Thread Andy L
Hi, All I was able to come up with was this (defn altsum[n] (reduce + (map * (range 1 (inc n)) (interpose -1 (repeat 1) ... works quite well, however I was wondering if there is more idiomatic way to write that. Thanks, Andy -- You received this message because you are subscribed to the