digitalmars.D.bugs - [Issue 5510] New: std.functional.iterate
- d-bugmail puremagic.com (58/58) Jan 31 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5510
- d-bugmail puremagic.com (12/12) Feb 02 2011 http://d.puremagic.com/issues/show_bug.cgi?id=5510
http://d.puremagic.com/issues/show_bug.cgi?id=5510 Summary: std.functional.iterate Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: enhancement Priority: P2 Component: Phobos AssignedTo: nobody puremagic.com ReportedBy: bearophile_hugs eml.cc I suggest to add to the std.functional module a higher order function similar to this one, that's useful in some situations: /// iterate(f, x, 3) == f(f(f(x))) auto iterate(F, T)(F f, T z, int n) in { assert(n >= 0); } body { foreach (_; 0 .. n) z = f(z); return z; } import std.stdio: writeln; import std.math: sin; void main() { writeln(iterate((double x){return sin(x);}, 0.2, 4)); } Alternative implementation: import std.functional; // iterate(f, x, 3) == f(f(f(x))) auto iterate(alias F, T)(T z, int n) in { assert(n >= 0); } body { alias unaryFun!F fun; foreach (_; 0 .. n) z = fun(z); return z; } import std.stdio: writeln; import std.math: sin; void main() { writeln(iterate!((double x){return sin(x);})(0.2, 4)); writeln(iterate!((x){return sin(x);})(0.2, 4)); writeln(iterate!sin(0.2, 4)); } See also: http://reference.wolfram.com/mathematica/ref/Nest.html http://reference.wolfram.com/mathematica/ref/NestList.html This also suggests: - The alternative name "nest()" for this higher order function; - A second function a nestAll() that returns a lazy iterable of all the intermediate results. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 31 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5510 bearophile_hugs eml.cc changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED Resolution| |WONTFIX Closed, according to Andrei: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=128495 -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 02 2011