View source code
Display the source code in std/functional.d from which this
page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a
Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.
Requires a signed-in GitHub account. This works well for small changes.
If you'd like to make larger changes you may want to consider using
local clone.
Alias std.functional.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.
Parameters
Name | Description |
---|---|
fun | the call-able(s) or string (s) to compose into one function |
Returns
A new function f(x)
that in turn returns fun[0](fun[1](...(x)))...
.
Example
// 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");
See Also
Example
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; }
writeln(compose!(baz, bar, foo)(1)); // 2.5
writeln(pipe!(foo, bar, baz)(1)); // 2.5
writeln(compose!(baz, `to!(int)(a) + 1`, foo)(1)); // 2.5
writeln(compose!(baz, bar)("1"[])); // 2.5
writeln(compose!(baz, bar)("1")); // 2.5
writeln(compose!(`a + 0.5`, `to!(int)(a) + 1`, foo)(1)); // 2.5
}
/**
* <a href="https://en.wikipedia.org/wiki/Memoization">Memoizes</a> a function so as
* to avoid repeated computation. The memoization structure is a hash table keyed by a
* tuple of the function's arguments. There is a speed gain if the
* function is repeatedly called with the same arguments and is more
* expensive than a hash table lookup. For more information on memoization, refer to <a href="http://docs.google.com/viewer?url=http%3A%2F%2Fhop.perl.plover.com%2Fbook%2Fpdf%2F03CachingAndMemoization.pdf">this book chapter</a>.
Example:
double transmogrify(int a, string b) { ... expensive computation ... } alias fastTransmogrify = memoize!transmogrify; unittest { auto slow = transmogrify(2, "hello"); auto fast = fastTransmogrify(2, "hello"); assert(slow == fast);
Authors
License
Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.