Anonymous user
First-class functions: Difference between revisions
Updated both D entries
(Updated both D entries) |
|||
Line 578:
=={{header|D}}==
===Using Standard Compose===
<lang d>void main() {▼
We need wrappers because the standard functions have different signatures (eg pure/nothrow).▼
asin = (in real x) => std.math.asin(x),▼
cos = (in real x) => std.math.cos(x),▼
foreach (immutable i, f; dir)
acos = (in real x) => std.math.acos(x),▼
cube = (in real x) => x ^^ 3,▼
cbrt = (in real x) => std.math.cbrt(x);▼
▲void main() {
▲ alias TypeTuple!(sin, cos, cube) dir;
▲ alias TypeTuple!(asin, acos, cbrt) inv;
▲ foreach (i, f; dir) {
writefln("%6.3f", compose!(f, inv[i])(0.5));
}▼
}</lang>
{{out}}
Line 599 ⟶ 591:
0.500
0.500</pre>
===Defining Compose===▼
▲===Defining Compose===
void main() {▼
▲
▲<lang d>void main() {
import std.stdio, std.math, std.range;
return
▲ }
immutable sin
▲ cube = (in real x) pure nothrow => x ^^ 3,
foreach (f, g;
writefln("%6.3f", compose(f, g)(0.5));
}</lang>
|