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,3 @@
|
|||
module rosetta "1.0.0" {
|
||||
import ceylon.numeric "1.2.1";
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import ceylon.numeric.float {
|
||||
|
||||
sin, exp, asin, log
|
||||
}
|
||||
|
||||
shared void run() {
|
||||
|
||||
function cube(Float x) => x ^ 3;
|
||||
function cubeRoot(Float x) => x ^ (1.0 / 3.0);
|
||||
|
||||
value functions = {sin, exp, cube};
|
||||
value inverses = {asin, log, cubeRoot};
|
||||
|
||||
for([func, inv] in zipPairs(functions, inverses)) {
|
||||
print(compose(func, inv)(0.5));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
;; adapted from Racket
|
||||
;; (compose f g h ... ) is a built-in defined as :
|
||||
;; (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])
|
||||
(writeln ((compose i f) 0.5)))
|
||||
→
|
||||
0.5
|
||||
0.4999999999999999
|
||||
0.5
|
||||
30
Task/First-class-functions/Lasso/first-class-functions.lasso
Normal file
30
Task/First-class-functions/Lasso/first-class-functions.lasso
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#!/usr/bin/lasso9
|
||||
|
||||
define cube(x::decimal) => {
|
||||
return #x -> pow(3.0)
|
||||
}
|
||||
|
||||
define cuberoot(x::decimal) => {
|
||||
return #x -> pow(1.0/3.0)
|
||||
}
|
||||
|
||||
define compose(f, g, v) => {
|
||||
return {
|
||||
return #f -> detach -> invoke(#g -> detach -> invoke(#1))
|
||||
} -> detach -> invoke(#v)
|
||||
}
|
||||
|
||||
|
||||
local(functions = array({return #1 -> sin}, {return #1 -> cos}, {return cube(#1)}))
|
||||
local(inverse = array({return #1 -> asin}, {return #1 -> acos}, {return cuberoot(#1)}))
|
||||
|
||||
loop(3)
|
||||
stdoutnl(
|
||||
compose(
|
||||
#functions -> get(loop_count),
|
||||
#inverse -> get(loop_count),
|
||||
0.5
|
||||
)
|
||||
)
|
||||
|
||||
/loop
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
-- sin, cos and sqrt are built-in, square, asin and acos are user-defined
|
||||
A = [#sin, #cos, #square]
|
||||
B = [#asin, #acos, #sqrt]
|
||||
|
||||
testValue = 0.5
|
||||
|
||||
repeat with i = 1 to 3
|
||||
-- for implementation details of compose() see https://www.rosettacode.org/wiki/Function_composition#Lingo
|
||||
f = compose(A[i], B[i])
|
||||
res = call(f, _movie, testValue)
|
||||
put res = testValue
|
||||
end repeat
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
on square (x)
|
||||
return x*x
|
||||
end
|
||||
|
||||
on asin (x)
|
||||
res = atan(sqrt(x*x/(1-x*x)))
|
||||
if x<0 then res = -res
|
||||
return res
|
||||
end
|
||||
|
||||
on acos (x)
|
||||
return PI/2 - asin(x)
|
||||
end
|
||||
27
Task/First-class-functions/Nim/first-class-functions.nim
Normal file
27
Task/First-class-functions/Nim/first-class-functions.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
from math import nil
|
||||
|
||||
proc cube(x: float64) : float64 {.procvar.} =
|
||||
math.pow(x, 3)
|
||||
|
||||
proc cuberoot(x: float64) : float64 {.procvar.} =
|
||||
math.pow(x, 1/3)
|
||||
|
||||
proc compose[A](f: proc(x: A): A, g: proc(x: A): A) : (proc(x: A): A) =
|
||||
proc c(x: A): A {.closure.} =
|
||||
f(g(x))
|
||||
return c
|
||||
|
||||
proc sin(x: float64) : float64 {.procvar.} =
|
||||
math.sin(x)
|
||||
proc asin(x: float64) : float64 {.procvar.}=
|
||||
math.arcsin(x)
|
||||
proc cos(x: float64) : float64 {.procvar.} =
|
||||
math.cos(x)
|
||||
proc acos(x: float64) : float64 {.procvar.} =
|
||||
math.arccos(x)
|
||||
|
||||
var fun = @[sin, cos, cube]
|
||||
var inv = @[asin, acos, cuberoot]
|
||||
|
||||
for i in 0..2:
|
||||
echo $compose(inv[i], fun[i])(0.5)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
: compose(f, g) #[ g perform f perform ] ;
|
||||
|
||||
[ #cos, #sin, #[ 3 pow ] ] [ #acos, #asin, #[ 3 inv powf ] ] zipWith(#compose)
|
||||
map(#[ 0.5 swap perform ]) conform(#[ 0.5 == ]) println
|
||||
25
Task/First-class-functions/Phix/first-class-functions.phix
Normal file
25
Task/First-class-functions/Phix/first-class-functions.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
|
||||
15
Task/First-class-functions/Sidef/first-class-functions.sidef
Normal file
15
Task/First-class-functions/Sidef/first-class-functions.sidef
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
func compose(f,g) {
|
||||
func (*args) {
|
||||
f(g(args...));
|
||||
}
|
||||
}
|
||||
|
||||
var cube = func(a) { a.pow(3) };
|
||||
var croot = func(a) { a.root(3) };
|
||||
|
||||
var flist1 = [Math.method(:sin), Math.method(:cos), cube];
|
||||
var flist2 = [Math.method(:asin), Math.method(:acos), croot];
|
||||
|
||||
flist1.range.each { |i|
|
||||
say compose(flist1[i], flist2[i])(0.5);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import Darwin
|
||||
func compose<A,B,C>(f: (B) -> C, g: (A) -> B) -> (A) -> C {
|
||||
return { f(g($0)) }
|
||||
}
|
||||
let funclist = [ { (x: Double) in sin(x) }, { (x: Double) in cos(x) }, { (x: Double) in pow(x, 3) } ]
|
||||
let funclisti = [ { (x: Double) in asin(x) }, { (x: Double) in acos(x) }, { (x: Double) in cbrt(x) } ]
|
||||
println(map(zip(funclist, funclisti)) { f, inversef in compose(f, inversef)(0.5) })
|
||||
Loading…
Add table
Add a link
Reference in a new issue