First-class functions: Difference between revisions
Content deleted Content added
Keithpinson (talk | contribs) Added output example |
Javascript cleaned up using Scala as an inspiration |
||
Line 952:
=={{header|JavaScript}}==
<lang javascript>// Functions as values of a variable
var cube = function(x) {
return Math.pow(x, 3);
};
<lang javascript>var compose = function (f, g) { ▼
var cuberoot
}; ▼
};
// Higher order function
var inv = [Math.asin, Math.acos, function (x) { return Math.pow(x, 1/3); }];▼
return f(g(x));
};
// Storing functions in a array
▲(function () {
var fun = [Math.sin, Math.cos, cube];
for (var i = 0; i < 3; i++) {▼
var f = compose(inv[i], fn[i]);▼
</lang>▼
// Applying the composition to 0.5
▲}</lang>
Output:
<pre>0.5
0.4999999999999999
0.5</pre>
=={{header|Lua}}==
|