fp::nestvals -- repeated
composition returning intermediate values
Introductionfp::nestvals(f,n) returns a function which
applies the function f n-fold repeatedly to
ist argument and returns the intermediate results.
Call(s)fp::nestvals(f, n)
Parametersf |
- | function |
n |
- | nonnegative integer |
ReturnsA function.
Related
Functions
Detailsfp::nestvals returns a function which applies the
function f 0- to n-fold repeated to its
arguments and returns these n+1 values as a list.fp::nestvals returns the function
x -> [x, f(x), f(f(x)),... f(f(...f(x)...))]
[ _fnest(f,i) $i=0..n
], but more efficient.
Example
1Apply f 3 times nested to
x:
>> fp::nestvals(f, 3)(x)
[x, f(x), f(f(x)), f(f(f(x)))]
Example
2Apply cos 4
times nested to 1.0 and return the result and intermediate
values:
>> fp::nestvals(cos, 4)(1.0)
[1.0, 0.5403023059, 0.8575532159, 0.6542897905, 0.7934803588]