March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,16 +1,9 @@
import std.stdio, std.math, std.typetuple, std.functional;
enum sin = (in real x) => std.math.sin(x),
asin = (in real x) => std.math.asin(x),
cos = (in real x) => std.math.cos(x),
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) {
import std.stdio, std.math, std.typetuple, std.functional;
alias dir = TypeTuple!(sin, cos, x => x ^^ 3);
alias inv = TypeTuple!(asin, acos, cbrt);
// foreach (f, g; staticZip!(dir, inv))
foreach (immutable i, f; dir)
writefln("%6.3f", compose!(f, inv[i])(0.5));
}
}

View file

@ -1,18 +1,18 @@
T delegate(S) compose(T, U, S)(in T function(in U) f,
in U function(in S) g) {
return s => f(g(s));
}
void main() {
import std.stdio, std.math, std.range;
immutable sin = (in real x) => sin(x),
asin = (in real x) => asin(x),
cos = (in real x) => cos(x),
acos = (in real x) => acos(x),
cube = (in real x) => x ^^ 3,
cbrt = (in real x) => cbrt(x);
static T delegate(S) compose(T, U, S)(in T function(in U) f,
in U function(in S) g) {
return s => f(g(s));
}
foreach (f, g; zip([sin, cos, cube], [asin, acos, cbrt]))
immutable sin = (in real x) pure nothrow => x.sin,
asin = (in real x) pure nothrow => x.asin,
cos = (in real x) pure nothrow => x.cos,
acos = (in real x) pure nothrow => x.acos,
cube = (in real x) pure nothrow => x ^^ 3,
cbrt = (in real x) /*pure*/ nothrow => x.cbrt;
foreach (f, g; [sin, cos, cube].zip([asin, acos, cbrt]))
writefln("%6.3f", compose(f, g)(0.5));
}

View file

@ -1,13 +1,11 @@
cube(x) { return x * x * x; }
inv_cube(x) { return Math.pow(x, 1/3); }
compo(f1, f2) {
return func(x) { return f1(f2(x));};
}
main() {
var funcs = [Math.sin, Math.exp, cube];
var invs = [Math.asin, Math.log, inv_cube];
for (int i = 0; i < 3; i++)
print(compo(funcs[i], invs[i])(1));
import 'dart:math' as Math;
cube(x) => x*x*x;
cuberoot(x) => Math.pow(x, 1/3);
compose(f,g) => ((x)=>f(g(x)));
main(){
var functions = [Math.sin, Math.exp, cube];
var inverses = [Math.asin, Math.log, cuberoot];
for (int i = 0; i < 3; i++){
print(compose(functions[i], inverses[i])(0.5));
}
}

View file

@ -1,9 +1,9 @@
// Functions as values of a variable
var cube = function(x) {
var cube = function (x) {
return Math.pow(x, 3);
};
var cuberoot = function(x) {
return Math.pow(x, 1/3);
var cuberoot = function (x) {
return Math.pow(x, 1 / 3);
};
// Higher order function

View file

@ -0,0 +1,17 @@
// Functions as values of a variable
var cube = x => Math.pow(x, 3);
var cuberoot = x => Math.pow(x, 1 / 3);
// Higher order function
var compose = (f, g) => (x => f(g(x)));
// Storing functions in a array
var fun = [ Math.sin, Math.cos, cube ];
var inv = [ Math.asin, Math.acos, cuberoot ];
for (var i = 0; i < 3; i++) {
// Applying the composition to 0.5
console.log(compose(inv[i], fun[i])(0.5));
}

View file

@ -1,6 +1,6 @@
sub infix:<> (&#56404;, &#56403;) { -> \x { #56404; #56403; x } }
sub infix:<> (&𝑔, &𝑓) { -> \x { 𝑔 𝑓 x } }
my \#56372; = &sin, &cos, { $_ ** <3/1> }
my \#56373; = &asin, &acos, { $_ ** <1/3> }
my \𝐴 = &sin, &cos, { $_ ** <3/1> }
my \𝐵 = &asin, &acos, { $_ ** <1/3> }
say .(.5) for #56372; Z⚬ #56373;
say .(.5) for 𝐴 Z 𝐵