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, 2014 at 11:14 PM, Andy L <core.as...@gmail.com> wrote:

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

Reply via email to