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 <[email protected]>
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 <[email protected]>
> 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 [email protected]
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>> 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 [email protected].
>> 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 [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> 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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.