www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Need help with calling a list of functions

reply Luigi <lewissk gmail.com> writes:
I need to call a function that can create a function from an 
array of functions and calls them in reverse order.  I am 
learning D any help would be


import std.stdio;
import std.algorithm;
import std.array : array;
import std.range;

auto comp(T)(T function(T) [] list) pure {
     auto backwards = retro(funs);
     return <<HEre is where I think I need some help in calling 
the list of functions with a delegate and fold or reduce>>;
}

void main()
{
     auto fun = comp([(real x)=>a/3.0,(real x)=>x*x,(real 
x)=>x+1.0]);
     writeln(fun(2.0));                // should print 3
}
Nov 03 2018
next sibling parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 4 November 2018 at 01:17:01 UTC, Luigi wrote:
 I need to call a function that can create a function from an 
 array of functions and calls them in reverse order.  I am 
 learning D any help would be


 import std.stdio;
 import std.algorithm;
 import std.array : array;
 import std.range;

 auto comp(T)(T function(T) [] list) pure {
     auto backwards = retro(funs);
     return <<HEre is where I think I need some help in calling 
 the list of functions with a delegate and fold or reduce>>;
 }

 void main()
 {
     auto fun = comp([(real x)=>a/3.0,(real x)=>x*x,(real 
 x)=>x+1.0]);
     writeln(fun(2.0));                // should print 3
 }
Use recursion: T delegate(T) comp(T)(T function(T) [] list) pure { if (list.length == 1) return (T arg) => list[0](arg); else return (T arg) => list[0](comp(list[1 .. $])(arg)); }
Nov 03 2018
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 11/03/2018 06:17 PM, Luigi wrote:
 I need to call a function that can create a function from an array of 
 functions and calls them in reverse order.  I am learning D any help 
 would be
 
 
 import std.stdio;
 import std.algorithm;
 import std.array : array;
 import std.range;
 
 auto comp(T)(T function(T) [] list) pure {
      auto backwards = retro(funs);
      return <<HEre is where I think I need some help in calling the list 
 of functions with a delegate and fold or reduce>>;
 }
 
 void main()
 {
      auto fun = comp([(real x)=>a/3.0,(real x)=>x*x,(real x)=>x+1.0]);
      writeln(fun(2.0));                // should print 3
 }
 
Here is one that uses a loop: import std.stdio; import std.range : front; import std.traits : ReturnType; auto comp(Funcs...)(Funcs funcs) pure { alias R = ReturnType!(typeof([funcs].front)); auto impl(R x) { foreach_reverse (func; funcs) { x = func(x); } return x; } return (double x) => impl(x); } void main() { auto fun = comp((real x)=>x/3.0,(real x)=>x*x,(real x)=>x+1.0); assert(fun(2.0) == 3); } I used a variadic template parameter instead of an array parameter. Ali
Nov 03 2018
prev sibling parent Sebastiaan Koppe <mail skoppe.eu> writes:
On Sunday, 4 November 2018 at 01:17:01 UTC, Luigi wrote:
 I need to call a function that can create a function from an 
 array of functions and calls them in reverse order.  I am 
 learning D any help would be
That sounds a lot like std.functional.compose
Nov 04 2018