pipe

Pipes functions in sequence. Offers the same functionality as compose, but with functions specified in reverse order. This may lead to more readable code in some situation because the order of execution is the same as lexical order.

alias pipe(fun...) = compose!(Reverse!(fun))

Parameters

fun

the call-able(s) or string(s) to compose into one function

Return Value

A new function f(x) that in turn returns fun[$-1](...fun[1](fun[0](x)))....

Examples

// Read an entire text file, split the resulting string in
// whitespace-separated tokens, and then convert each token into an
// integer
int[] a = pipe!(readText, split, map!(to!(int)))("file.txt");
import std.conv : to;
string foo(int a) { return to!(string)(a); }
int bar(string a) { return to!(int)(a) + 1; }
double baz(int a) { return a + 0.5; }
assert(compose!(baz, bar, foo)(1) == 2.5);
assert(pipe!(foo, bar, baz)(1) == 2.5);

assert(compose!(baz, `to!(int)(a) + 1`, foo)(1) == 2.5);
assert(compose!(baz, bar)("1"[]) == 2.5);

assert(compose!(baz, bar)("1") == 2.5);

assert(compose!(`a + 0.5`, `to!(int)(a) + 1`, foo)(1) == 2.5);

See Also

Meta