September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
5
Task/Function-composition/Agda/function-composition.agda
Normal file
5
Task/Function-composition/Agda/function-composition.agda
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
compose : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c}
|
||||
→ (B → C)
|
||||
→ (A → B)
|
||||
→ A → C
|
||||
compose f g x = f (g x)
|
||||
26
Task/Function-composition/Aime/function-composition.aime
Normal file
26
Task/Function-composition/Aime/function-composition.aime
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
compose_i(,,)
|
||||
{
|
||||
($0)(($1)($2));
|
||||
}
|
||||
|
||||
compose(,)
|
||||
{
|
||||
compose_i.apply($0, $1);
|
||||
}
|
||||
|
||||
double(real a)
|
||||
{
|
||||
2 * a;
|
||||
}
|
||||
|
||||
square(real a)
|
||||
{
|
||||
a * a;
|
||||
}
|
||||
|
||||
main(void)
|
||||
{
|
||||
o_(compose(square, double)(40), "\n");
|
||||
|
||||
0;
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
>(defun compose (f g) (lambda (x) (funcall f (funcall g x))))
|
||||
COMPOSE
|
||||
>(let ((sin-asin (compose #'sin #'asin))))
|
||||
>(let ((sin-asin (compose #'sin #'asin)))
|
||||
(funcall sin-asin 0.5))
|
||||
0.5
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
(defun compose (f g)
|
||||
(eval `(lambda (x) (funcall ',f (funcall ',g x))))
|
||||
(eval `(lambda (x) (funcall ',f (funcall ',g x)))))
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
import extensions.
|
||||
import extensions;
|
||||
|
||||
func1 extension op
|
||||
extension op : Func1
|
||||
{
|
||||
compose(BaseFunction1 f)
|
||||
= (:x)(self(f(x))).
|
||||
compose(Func1 f)
|
||||
= (x => self(f(x)));
|
||||
}
|
||||
|
||||
program =
|
||||
[
|
||||
var fg := (:x)(x + 1) compose(:x)(x * x).
|
||||
public program()
|
||||
{
|
||||
var fg := (x => x + 1).compose:(x => x * x);
|
||||
|
||||
console printLine(fg(3)).
|
||||
].
|
||||
console.printLine(fg(3))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
composeList :: [a -> a] -> a -> a
|
||||
composeList = flip (foldr id)
|
||||
|
||||
|
||||
main :: IO ()
|
||||
main = print $ composeList [(/ 2), succ, sqrt] 5
|
||||
|
|
@ -2,16 +2,19 @@
|
|||
'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,
|
||||
const compose = (...fs) =>
|
||||
x => fs.reduceRight(
|
||||
(a, f) => f(a),
|
||||
x
|
||||
);
|
||||
|
||||
// Test a composition of 3 functions (right to left)
|
||||
const
|
||||
sqrt = Math.sqrt,
|
||||
succ = x => x + 1,
|
||||
|
||||
half = x => x / 2;
|
||||
|
||||
return compose([half, succ, sqrt])(5);
|
||||
return compose(half, succ, sqrt)(5);
|
||||
|
||||
// --> 1.618033988749895
|
||||
})();
|
||||
|
|
|
|||
36
Task/Function-composition/Python/function-composition-3.py
Normal file
36
Task/Function-composition/Python/function-composition-3.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
from math import (acos, cos, asin, sin)
|
||||
|
||||
|
||||
# compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
def compose(g, f):
|
||||
'''Right to left function composition.'''
|
||||
return lambda x: g(f(x))
|
||||
|
||||
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''Test'''
|
||||
|
||||
print(list(map(
|
||||
lambda f: f(0.5),
|
||||
zipWith(compose)(
|
||||
[sin, cos, lambda x: x ** 3.0]
|
||||
)([asin, acos, lambda x: x ** (1 / 3.0)])
|
||||
)))
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
|
||||
# zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
def zipWith(f):
|
||||
'''A list constructed by zipping with a
|
||||
custom function, rather than with the
|
||||
default tuple constructor.'''
|
||||
return lambda xs: lambda ys: (
|
||||
map(f, xs, ys)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
43
Task/Function-composition/Python/function-composition-4.py
Normal file
43
Task/Function-composition/Python/function-composition-4.py
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
from functools import reduce
|
||||
from numbers import Number
|
||||
import math
|
||||
|
||||
|
||||
def main():
|
||||
'''Test'''
|
||||
|
||||
f = composeList([
|
||||
lambda x: x / 2,
|
||||
succ,
|
||||
math.sqrt
|
||||
])
|
||||
|
||||
print(
|
||||
f(5)
|
||||
)
|
||||
|
||||
|
||||
# GENERIC FUNCTIONS ---------------------------------------
|
||||
|
||||
|
||||
# composeList :: [(a -> a)] -> (a -> a)
|
||||
def composeList(fs):
|
||||
'''Composition, from right to left,
|
||||
of a series of functions.'''
|
||||
return lambda x: reduce(
|
||||
lambda a, f: f(a),
|
||||
fs[::-1],
|
||||
x
|
||||
)
|
||||
|
||||
|
||||
# succ :: Enum a => a -> a
|
||||
def succ(x):
|
||||
'''The successor of a value. For numeric types, (1 +).'''
|
||||
return 1 + x if isinstance(x, Number) else (
|
||||
chr(1 + ord(x))
|
||||
)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
|
@ -3,3 +3,12 @@
|
|||
;; or:
|
||||
|
||||
(define ((compose f g) x) (f (g x)))
|
||||
|
||||
;; or to compose an arbitrary list of 1 argument functions:
|
||||
|
||||
(define-syntax compose
|
||||
(lambda (x)
|
||||
(syntax-case x ()
|
||||
((_) #'(lambda (y) y))
|
||||
((_ f) #'f)
|
||||
((_ f g h ...) #'(lambda (y) (f ((compose g h ...) y)))))))
|
||||
|
|
|
|||
|
|
@ -0,0 +1,14 @@
|
|||
function compose<T, U, V> (fn1: (input: T) => U, fn2: (input: U) => V){
|
||||
return function(value: T) {
|
||||
return fn2(fn1(value))
|
||||
}
|
||||
}
|
||||
|
||||
function size (s: string): number { return s.length; }
|
||||
|
||||
function isEven(x: number): boolean { return x % 2 === 0; }
|
||||
|
||||
const evenSize = compose(size, isEven);
|
||||
|
||||
console.log(evenSize("ABCD")) // true
|
||||
console.log(evenSize("ABC")) // false
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
10 DEF FN f(x)=SQR x
|
||||
20 DEF FN g(x)=ABS x
|
||||
30 DEF FN c(x)=FN f(FN g(x))
|
||||
40 PRINT FN c(-4)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
10 DEF FN f(x)=SQR x
|
||||
20 DEF FN g(x)=ABS x
|
||||
30 DEF FN c(a$,b$,x)=VAL ("FN "+a$+"(FN "+b$+"(x))")
|
||||
40 PRINT FN c("f","g",-4)
|
||||
50 PRINT FN c("g","f",-4)
|
||||
Loading…
Add table
Add a link
Reference in a new issue