This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
commit 776bba907c
3887 changed files with 59896 additions and 7282 deletions

View file

@ -1,11 +1,21 @@
var cube:Function = function(x) {
return Math.pow(x, 3);
};
var cuberoot:Function = function(x) {
return Math.pow(x, 1/3);
};
function compose(f:Function, g:Function):Function {
return function(x:Number) {return f(g(x));};
}
var functions:Array = [Math.cos, Math.tan, function(x:Number){return x*x;}];
var inverse:Array = [Math.acos, Math.atan, function(x:Number){return Math.sqrt(x);}];
var functions:Array = [Math.cos, Math.tan, cube];
var inverse:Array = [Math.acos, Math.atan, cuberoot];
function test() {
for (var i:uint = 0; i < functions.length; i++) {
trace(compose(functions[i], inverse[i])(0.5));
// Applying the composition to 0.5
trace(compose(functions[i], inverse[i])(0.5));
}
}
test();

View file

@ -0,0 +1,22 @@
compose f g:
labda:
f g
negate:
- 0
set :A [ @++ compose @negate @-- ]
set :B [ @-- compose @++ @negate ]
test n:
for i range 0 -- len A:
if /= n call compose get-from B i get-from A i n:
return false
true
test to-num input print\ "Enter a number: "
if:
print "f^-1(f(x)) = x"
else:
print "Something went wrong."

View file

@ -0,0 +1,16 @@
-module( first_class_functions ).
-export( [task/0] ).
task() ->
As = [fun math:sin/1, fun math:cos/1, fun cube/1],
Bs = [fun math:asin/1, fun math:acos/1, fun square_inverse/1],
[io:fwrite( "Value: 1.5 Result: ~p~n", [functional_composition([A, B], 1.5)]) || {A, B} <- lists:zip(As, Bs)].
functional_composition( Funs, X ) -> lists:foldl( fun(F, Acc) -> F(Acc) end, X, Funs ).
square( X ) -> math:pow( X, 2 ).
square_inverse( X ) -> math:sqrt( X ).

View file

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

View file

@ -0,0 +1,10 @@
#lang racket
(define (compose f g) (λ (x) (f (g x))))
(define (cube x) (expt x 3))
(define (cube-root x) (expt x (/ 1 3)))
(define funlist (list sin cos cube))
(define ifunlist (list asin acos cube-root))
(for ([f funlist] [i ifunlist])
(displayln ((compose i f) 0.5)))

View file

@ -0,0 +1,3 @@
0.5
0.4999999999999999
0.5