Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,4 @@
|
|||
/Apply g to exactly one argument
|
||||
compose1: {f: x; g: y; {f[g[x]]}}
|
||||
/Extra: apply to multiple arguments
|
||||
compose: {f: x; g: y; {f[g apply args]}}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
;; By decreasing order of performance
|
||||
;; 1) user definition : lambda and closure
|
||||
|
||||
(define (ucompose f g ) (lambda (x) ( f ( g x))))
|
||||
(ucompose sin cos)
|
||||
→ (🔒 λ (_x) (f (g _x)))
|
||||
|
||||
;; 2) built-in compose : lambda
|
||||
|
||||
(compose sin cos)
|
||||
→ (λ (_#:g1002) (#apply-compose (#list #cos #sin) _#:g1002))
|
||||
|
||||
;; 3) compiled composition
|
||||
|
||||
(define (sincos x) (sin (cos x)))
|
||||
sincos → (λ (_x) (⭕️ #sin (#cos _x)))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
((ucompose sin cos) 3) → -0.8360218615377305
|
||||
((compose sin cos) 3) → -0.8360218615377305
|
||||
(sincos 3) → -0.8360218615377305
|
||||
7
Task/Function-composition/FunL/function-composition.funl
Normal file
7
Task/Function-composition/FunL/function-composition.funl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
import math.{sin, asin}
|
||||
|
||||
def compose( f, g ) = x -> f( g(x) )
|
||||
|
||||
sin_asin = compose( sin, asin )
|
||||
|
||||
println( sin_asin(0.5) )
|
||||
16
Task/Function-composition/LFE/function-composition-1.lfe
Normal file
16
Task/Function-composition/LFE/function-composition-1.lfe
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
(defun compose (f g)
|
||||
(lambda (x)
|
||||
(funcall f
|
||||
(funcall g x))))
|
||||
|
||||
(defun compose (funcs)
|
||||
(lists:foldl #'compose/2
|
||||
(lambda (x) x)
|
||||
funcs))
|
||||
|
||||
(defun check ()
|
||||
(let* ((sin-asin (compose #'math:sin/1 #'math:asin/1))
|
||||
(expected (math:sin (math:asin 0.5)))
|
||||
(compose-result (funcall sin-asin 0.5)))
|
||||
(io:format '"Expected answer: ~p~n" (list expected))
|
||||
(io:format '"Answer with compose: ~p~n" (list compose-result))))
|
||||
13
Task/Function-composition/LFE/function-composition-2.lfe
Normal file
13
Task/Function-composition/LFE/function-composition-2.lfe
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
> (funcall (compose #'math:sin/1 #'math:asin/1)
|
||||
0.5)
|
||||
0.49999999999999994
|
||||
> (funcall (compose `(,#'math:sin/1
|
||||
,#'math:asin/1
|
||||
,(lambda (x) (+ x 1))))
|
||||
0.5)
|
||||
1.5
|
||||
> (check)
|
||||
Expected answer: 0.49999999999999994
|
||||
Answer with compose: 0.49999999999999994
|
||||
ok
|
||||
>
|
||||
10
Task/Function-composition/Lingo/function-composition-1.lingo
Normal file
10
Task/Function-composition/Lingo/function-composition-1.lingo
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
-- in some movie script
|
||||
----------------------------------------
|
||||
-- Composes 2 call-functions, returns a new call-function
|
||||
-- @param {symbol|instance} f
|
||||
-- @param {symbol|instance} g
|
||||
-- @return {instance}
|
||||
----------------------------------------
|
||||
on compose (f, g)
|
||||
return script("Composer").new(f, g)
|
||||
end
|
||||
34
Task/Function-composition/Lingo/function-composition-2.lingo
Normal file
34
Task/Function-composition/Lingo/function-composition-2.lingo
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
-- parent script "Composer"
|
||||
|
||||
property _f
|
||||
property _g
|
||||
|
||||
----------------------------------------
|
||||
-- @constructor
|
||||
-- @param {symbol|instance} f
|
||||
-- @param {symbol|instance} g
|
||||
----------------------------------------
|
||||
on new (me, f, g)
|
||||
me._f = f
|
||||
me._g = g
|
||||
return me
|
||||
end
|
||||
|
||||
on call (me)
|
||||
if ilk(me._g)=#instance then
|
||||
cmd = "_movie.call(#call,me._g,VOID"
|
||||
else
|
||||
cmd = "_movie.call(me._g,_movie"
|
||||
end if
|
||||
a = [] -- local args list
|
||||
repeat with i = 1 to the paramCount-2
|
||||
a[i] = param(i+2)
|
||||
put ",a["&i&"]" after cmd
|
||||
end repeat
|
||||
put ")" after cmd
|
||||
if ilk(me._f)=#instance then
|
||||
return _movie.call(#call, me._f, VOID, value(cmd))
|
||||
else
|
||||
return _movie.call(me._f, _movie, value(cmd))
|
||||
end if
|
||||
end
|
||||
16
Task/Function-composition/Lingo/function-composition-3.lingo
Normal file
16
Task/Function-composition/Lingo/function-composition-3.lingo
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
-- compose new function based on built-in function 'sin' and user-defined function 'asin'
|
||||
f1 = compose(#asin, #sin)
|
||||
put call(f1, _movie, 0.5)
|
||||
-- 0.5000
|
||||
|
||||
-- compose new function based on previously composed function 'f1' and user-defined function 'double'
|
||||
f2 = compose(#double, f1)
|
||||
put call(f2, _movie, 0.5)
|
||||
-- 1.0000
|
||||
|
||||
-- compose new function based on 2 composed functions
|
||||
f1 = compose(#asin, #sin)
|
||||
f2 = compose(#double, #triple)
|
||||
f3 = compose(f2, f1)
|
||||
put call(f3, _movie, 0.5)
|
||||
-- 3.0000
|
||||
14
Task/Function-composition/Lingo/function-composition-4.lingo
Normal file
14
Task/Function-composition/Lingo/function-composition-4.lingo
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
-- in some movie script
|
||||
on asin (x)
|
||||
res = atan(sqrt(x*x/(1-x*x)))
|
||||
if x<0 then res = -res
|
||||
return res
|
||||
end
|
||||
|
||||
on double (x)
|
||||
return x*2
|
||||
end
|
||||
|
||||
on triple (x)
|
||||
return x*3
|
||||
end
|
||||
9
Task/Function-composition/Nim/function-composition.nim
Normal file
9
Task/Function-composition/Nim/function-composition.nim
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
import future
|
||||
|
||||
proc compose[A,B,C](f: A -> B, g: B -> C): A -> C = (x: A) => f(g(x))
|
||||
|
||||
proc plustwo(x: int): int = x + 2
|
||||
proc minustwo(x: int): int = x - 2
|
||||
|
||||
var plusminustwo = compose(plustwo, minustwo)
|
||||
echo plusminustwo(10)
|
||||
|
|
@ -0,0 +1 @@
|
|||
g f
|
||||
|
|
@ -0,0 +1 @@
|
|||
: compose(f, g) #[ g perform f perform ] ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
1.2 compose(#asin, #sin) perform
|
||||
[ 1, 2, 3, 4, 5 ] compose(#[ map(#sqrt) ], #[ filter(#isEven) ]) perform
|
||||
25
Task/Function-composition/Phix/function-composition.phix
Normal file
25
Task/Function-composition/Phix/function-composition.phix
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
sequence ctable = {}
|
||||
|
||||
function compose(integer f, integer g)
|
||||
ctable = append(ctable,{f,g})
|
||||
return length(ctable)
|
||||
end function
|
||||
|
||||
function call_composite(integer f, atom x)
|
||||
integer g
|
||||
{f,g} = ctable[f]
|
||||
return call_func(f,{call_func(g,{x})})
|
||||
end function
|
||||
|
||||
function plus1(atom x)
|
||||
return x+1
|
||||
end function
|
||||
|
||||
function halve(atom x)
|
||||
return x/2
|
||||
end function
|
||||
|
||||
constant m = compose(routine_id("halve"),routine_id("plus1"))
|
||||
|
||||
?call_composite(m,1) -- displays 1
|
||||
?call_composite(m,4) -- displays 2.5
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
func compose<A,B,C>(f: (B) -> C, g: (A) -> B) -> (A) -> C {
|
||||
return { f(g($0)) }
|
||||
}
|
||||
|
||||
let sin_asin = compose(sin, asin)
|
||||
println(sin_asin(0.5))
|
||||
|
|
@ -0,0 +1 @@
|
|||
! @[f g] x ; f(g(x))
|
||||
|
|
@ -0,0 +1 @@
|
|||
! ^(f g) x ; f(g(x))
|
||||
|
|
@ -0,0 +1 @@
|
|||
@var compose &[f g] &x !f!g x
|
||||
2
Task/Function-composition/jq/function-composition.jq
Normal file
2
Task/Function-composition/jq/function-composition.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# apply g first and then f
|
||||
def compose(f; g): g | f;
|
||||
Loading…
Add table
Add a link
Reference in a new issue