September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,22 +1,4 @@
-- Compose (right to left) a list of ordinary 2nd class handlers (of arbitrary length)
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on lambda(x)
script
on lambda(a, f)
mReturn(f)'s lambda(a)
end lambda
end script
foldr(result, x, fs)
end lambda
end script
end compose
-- TEST
-- FUNCTIONS TO COMPOSE -------------------------------------------------------
on root(x)
x ^ 0.5
@ -30,17 +12,30 @@ on half(x)
x / 2
end half
-- TEST -----------------------------------------------------------------------
on run
tell compose([half, succ, root]) to lambda(5)
compose([half, succ, root])'s |λ|(5)
--> 1.61803398875
end run
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on |λ|(x)
script
on |λ|(a, f)
mReturn(f)'s |λ|(a)
end |λ|
end script
-- GENERIC FUNCTIONS
foldr(result, x, fs)
end |λ|
end script
end compose
-- foldr :: (a -> b -> a) -> a -> [b] -> a
on foldr(f, startValue, xs)
@ -48,7 +43,7 @@ on foldr(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from lng to 1 by -1
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -61,7 +56,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,14 @@
import extensions.
func1 extension op
{
compose(BaseFunction1 f)
= (:x)(self eval(f eval:x)).
}
program =
[
var fg := (:x)(x + 1) compose(:x)(x * x).
console printLine(fg eval(3)).
].

View file

@ -1,5 +1,5 @@
-module(fn).
-export([compose/1, multicompose/2]).
-export([compose/2, multicompose/1]).
compose(F,G) -> fun(X) -> F(G(X)) end.

View file

@ -1 +0,0 @@
def compose = { f, g -> { x -> f(g(x)) } }

View file

@ -1,15 +0,0 @@
def sq = { it * it }
def plus1 = { it + 1 }
def minus1 = { it - 1 }
def plus1sqd = compose(sq,plus1)
def sqminus1 = compose(minus1,sq)
def identity = compose(plus1,minus1)
def plus1sqdminus1 = compose(minus1,compose(sq,plus1))
def identity2 = compose(Math.&sin,Math.&asin)
println "(x+1)**2 = (0+1)**2 = " + plus1sqd(0)
println "x**2-1 = 20**2-1 = " + sqminus1(20)
println "(x+1)-1 = (12+1)-1 = " + identity(12)
println "(x+1)**2-1 = (3+1)**2-1 = " + plus1sqdminus1(3)
println "sin(asin(x)) = sin(asin(1)) = " + identity2(1)

View file

@ -0,0 +1,3 @@
(defn compose [f g]
(fn [x]
(f (g x))))

View file

@ -1,20 +1,16 @@
(() => {
'use strict';
// compose :: [(a -> a)] -> (a -> a)
let compose = fs => x => fs.reduceRight((a, f) => f(a), x);
// TEST a composition of 3 functions (right to left)
let sqrt = Math.sqrt,
succ = x => x + 1,
half = x => x / 2;
return compose([half, succ, sqrt])(5);
// --> 1.618033988749895

View file

@ -0,0 +1,19 @@
(() => {
'use strict';
// compose :: (a -> b) -> (b -> c) -> (a -> c)
const compose = (f, g) => x => g(f(x));
// TEST
const
sqrt = Math.sqrt,
succ = x => x + 1,
half = x => x / 2;
const
succSqrt = compose(sqrt, succ),
halfSuccSqrt = compose(succSqrt, half);
return halfSuccSqrt(5);
})();

View file

@ -1 +1 @@
compose: {x@y@z}
compose:{'[x;y]}

View file

@ -1,3 +1 @@
sin_asin: compose[_sin;_asin]
sin_asin 0.5
0.5
compose:{x[y[z]]}

View file

@ -0,0 +1,3 @@
sin_asin:compose[sin;asin] // or compose . (sin;asin)
sin_asin 0.5
0.5

View file

@ -0,0 +1,12 @@
// version 1.0.6
fun f(x: Int): Int = x * x
fun g(x: Int): Int = x + 2
fun compose(f: (Int) -> Int, g: (Int) -> Int): (Int) -> Int = { f(g(it)) }
fun main(args: Array<String>) {
val x = 10
println(compose(::f, ::g)(x))
}

View file

@ -1,119 +0,0 @@
#include <Foundation/Foundation.h>
// the protocol of objects that can behave "like function"
@protocol FunctionCapsule <NSObject>
-(id)computeWith: (id)x;
@end
// a commodity for "encapsulating" double f(double)
typedef double (*func_t)(double);
@interface FunctionCaps : NSObject <FunctionCapsule>
{
func_t function;
}
+(id)capsuleFor: (func_t)f;
-(id)initWithFunc: (func_t)f;
@end
@implementation FunctionCaps
-(id)initWithFunc: (func_t)f
{
if ((self = [self init])) {
function = f;
}
return self;
}
+(id)capsuleFor: (func_t)f
{
return [[[self alloc] initWithFunc: f] autorelease];
}
-(id)computeWith: (id)x
{
return [NSNumber numberWithDouble: function([x doubleValue])];
}
@end
// the "functions" composer
@interface FunctionComposer : NSObject <FunctionCapsule>
{
id<FunctionCapsule> funcA;
id<FunctionCapsule> funcB;
}
+(id) createCompositeFunctionWith: (id<FunctionCapsule>)A and: (id<FunctionCapsule>)B;
-(id) initComposing: (id<FunctionCapsule>)A with: (id<FunctionCapsule>)B;
@end
@implementation FunctionComposer
+(id) createCompositeFunctionWith: (id<FunctionCapsule>)A and: (id<FunctionCapsule>)B
{
return [[[self alloc] initComposing: A with: B] autorelease];
}
-(id) init
{
[self release];
@throw [NSException exceptionWithName:NSInternalInconsistencyException
reason:@"FunctionComposer: init with initComposing!"
userInfo:nil];
return nil;
}
-(id) initComposing: (id<FunctionCapsule>)A with: (id<FunctionCapsule>)B
{
if ((self = [super init])) {
if ( [A respondsToSelector: @selector(computeWith:)] &&
[B respondsToSelector: @selector(computeWith:)] ) {
funcA = [A retain]; funcB = [B retain];
return self;
}
NSLog(@"FunctionComposer: cannot compose functions not responding to protocol FunctionCapsule!");
[self release];
}
return nil;
}
-(id)computeWith: (id)x
{
return [funcA computeWith: [funcB computeWith: x]];
}
-(void) dealloc
{
[funcA release];
[funcB release];
[super dealloc];
}
@end
// functions outside...
double my_f(double x)
{
return x+1.0;
}
double my_g(double x)
{
return x*x;
}
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id<FunctionCapsule> funcf = [FunctionCaps capsuleFor: my_f];
id<FunctionCapsule> funcg = [FunctionCaps capsuleFor: my_g];
id<FunctionCapsule> composed = [FunctionComposer
createCompositeFunctionWith: funcf and: funcg];
printf("g(2.0) = %lf\n", [[funcg computeWith: [NSNumber numberWithDouble: 2.0]] doubleValue]);
printf("f(2.0) = %lf\n", [[funcf computeWith: [NSNumber numberWithDouble: 2.0]] doubleValue]);
printf("f(g(2.0)) = %lf\n", [[composed computeWith: [NSNumber numberWithDouble: 2.0]] doubleValue]);
[pool release];
return 0;
}

View file

@ -1,42 +0,0 @@
#include <Foundation/Foundation.h>
typedef id (^Function)(id);
// a commodity for "encapsulating" double f(double)
typedef double (*func_t)(double);
Function encapsulate(func_t f) {
return [[^(id x) { return [NSNumber numberWithDouble: f([x doubleValue])]; } copy] autorelease];
}
Function compose(Function a, Function b) {
return [[^(id x) { return a(b(x)); } copy] autorelease];
}
// functions outside...
double my_f(double x)
{
return x+1.0;
}
double my_g(double x)
{
return x*x;
}
int main()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Function f = encapsulate(my_f);
Function g = encapsulate(my_g);
Function composed = compose(f, g);
printf("g(2.0) = %lf\n", [g([NSNumber numberWithDouble: 2.0]) doubleValue]);
printf("f(2.0) = %lf\n", [f([NSNumber numberWithDouble: 2.0]) doubleValue]);
printf("f(g(2.0)) = %lf\n", [composed([NSNumber numberWithDouble: 2.0]) doubleValue]);
[pool release];
return 0;
}

View file

@ -1,3 +0,0 @@
sub infix:<> (&f, &g --> Block) {
-> |args { f g |args }
}

View file

@ -1,3 +0,0 @@
sub triple($n) { 3 * $n }
my &f = &triple &prefix:<-> { $^n + 2 };
say &f(5); # Prints "-21".

View file

@ -1,3 +1,3 @@
sub triple($n) { 3 * $n }
my &f = &triple &prefix:<-> { $^n + 2 };
my &f = &triple &[-] { $^n + 2 };
say &f(5); # Prints "-21".

View file

@ -1,7 +1,12 @@
/compose { % f g -> { g f }
[ 3 1 roll exch
% procedures are not executed when encountered directly
% insert an 'exec' after procedures, but not after operators
1 index type /operatortype ne { /exec cvx exch } if
dup type /operatortype ne { /exec cvx } if
] cvx
} def
/square { dup mul } def
/plus1 { 1 add } def
/sqPlus1{ square plus1 } def
% if the task really demands we make a function called "compose", well
/compose { def } def % so now we can say:
/sqPlus1 { square plus1 } compose
/sqPlus1 /square load /plus1 load compose def

View file

@ -1,3 +1,3 @@
compose: procedure; parse arg f,g,x; interpret 'return' f"(" g'(' x "))"
compose: procedure; parse arg f,g,x; interpret 'return' f"(" g'(' x "))"
exit /*control never gets here, but this was added just in case.*/
exit /*control should never gets here, but this was added just in case.*/

View file

@ -0,0 +1,6 @@
fn compose<'a,F,G,T,U,V>(f: F, g: G) -> Box<Fn(T) -> V + 'a>
where F: Fn(U) -> V + 'a,
G: Fn(T) -> U + 'a,
{
Box::new(move |x| f(g(x)))
}

View file

@ -0,0 +1,7 @@
#![feature(conservative_impl_trait)]
fn compose<'a,F,G,T,U,V>(f: F, g: G) -> impl Fn(T) -> V + 'a
where F: Fn(U) -> V + 'a,
G: Fn(T) -> U + 'a,
{
move |x| f(g(x))
}

View file

@ -1,5 +1,6 @@
func compose(f, g) {
func(x) { f(g(x)) };
};
var fg = compose(func(x){Math.sin(x)}, func(x){Math.cos(x)});
say fg(0.5); # => 0.7691963548410084218525147580510688880995
func(x) { f(g(x)) }
}
var fg = compose(func(x){ sin(x) }, func(x){ cos(x) })
say fg(0.5) # => 0.76919635484100842185251475805107

View file

@ -0,0 +1 @@
Utils.Helpers.fcomp('+(1),'*(2))(5) //-->11

View file

@ -0,0 +1,2 @@
fcn fcomp(f,g,h,etc){
{ fcn(x,hgf){ T(x).pump(Void,hgf.xplode()) }.fp1(vm.arglist.reverse()); }