ncomp would be faster if it's implemented a la exponentiation by squaring.

On Mon, Nov 16, 2015 at 12:55 PM, Ben Wolfson <wolf...@gmail.com> wrote:

> If you rearrange the arguments of your proposed three-argument iterate:
>
> (defn iterate
>   ...
>   ([f n x]
>    (if (zero? n)
>      x
>      (recur f (f x) (dec n))))
>
> it supports partial application of the multiply-composed function to
> different arguments more easily, which suggests redefinition as:
>
> (defn ncomp [f n]
>   (loop [g identity n n]
>     (if (zero? n)
>        g
>        (recur (comp g f) (dec n)))))
>
> This seems to come closer to matching what's desired from the
> three-argument iterate without confusingly (as you note) overloading
> "iterate": you want f applied n times to x and the stream of intermediate
> values isn't really of interest.
>
> On Mon, Nov 16, 2015 at 12:37 PM, Jason Felice <jason.m.fel...@gmail.com>
> wrote:
>
>>
>> I *frequently* see:
>>
>> (nth (iterate foo bar) n)
>>
>> And:
>>
>> (->> (iterate foo bar)
>>    (drop n)
>>    first)
>>
>> I've also coded this in `avi` after being surprised no such thing exists:
>>
>> (defn n-times
>>   [thing n a-fn]
>>   (reduce
>>     (fn [thing n]
>>       (a-fn thing))
>>     thing
>>     (range n)))
>>
>> (which is kind of a bad implementation, now that I think of it):
>>
>> Would it be useful to file a ticket for a new arity for iterate?:
>>
>> (defn iterate
>>   ...
>>   ([f x n]
>>    (if (zero? n)
>>      x
>>      (recur f (f x) (dec n))))
>>
>> There's a little bit of weirdness - this returns a single value, while
>> the other arity returns a lazy sequence.  However, I think it's the correct
>> name (and the original arity might  have better been named "iterations" for
>> symmetry with "reduce" and "reductions").  But ah well.
>>
>> Thoughts?
>>
>> --
>> 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]
>
>


-- 
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