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 <ajobers...@gmail.com>
wrote:

> 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 you want and
> the result used as if it was a normal function.
>
>
> Andy
>
> On Fri, Nov 14, 2014 at 7:16 AM, Henrik Lundahl <henrik.lund...@gmail.com>
> wrote:
>
>> 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 <gary.verhae...@gmail.com
>> > wrote:
>>
>>> 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 clojure@googlegroups.com
>>> Note that posts from new members are moderated - please be patient with
>>> your first post.
>>> To unsubscribe from this group, send email to
>>> clojure+unsubscr...@googlegroups.com
>>> For more options, visit this group at
>>> http://groups.google.com/group/clojure?hl=en
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "Clojure" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to clojure+unsubscr...@googlegroups.com.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>  --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to clojure@googlegroups.com
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> clojure+unsubscr...@googlegroups.com
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>  --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to