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,6 @@
|
|||
: pass-me
|
||||
"I was passed\n" . ;
|
||||
: passer
|
||||
w:exec ;
|
||||
\ pass 'pass-me' to 'passer'
|
||||
' pass-me passer
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
twice:{x[x[y]]}
|
||||
echo twice "Hello!"
|
||||
51
Task/Higher-order-functions/ECL/higher-order-functions.ecl
Normal file
51
Task/Higher-order-functions/ECL/higher-order-functions.ecl
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
//a Function prototype:
|
||||
INTEGER actionPrototype(INTEGER v1, INTEGER v2) := 0;
|
||||
|
||||
INTEGER aveValues(INTEGER v1, INTEGER v2) := (v1 + v2) DIV 2;
|
||||
INTEGER addValues(INTEGER v1, INTEGER v2) := v1 + v2;
|
||||
INTEGER multiValues(INTEGER v1, INTEGER v2) := v1 * v2;
|
||||
|
||||
//a Function prototype using a function prototype:
|
||||
INTEGER applyPrototype(INTEGER v1, actionPrototype actionFunc) := 0;
|
||||
|
||||
//using the Function prototype and a default value:
|
||||
INTEGER applyValue2(INTEGER v1,
|
||||
actionPrototype actionFunc = aveValues) :=
|
||||
actionFunc(v1, v1+1)*2;
|
||||
|
||||
//Defining the Function parameter inline, witha default value:
|
||||
INTEGER applyValue4(INTEGER v1,
|
||||
INTEGER actionFunc(INTEGER v1,INTEGER v2) = aveValues)
|
||||
:= actionFunc(v1, v1+1)*4;
|
||||
INTEGER doApplyValue(INTEGER v1,
|
||||
INTEGER actionFunc(INTEGER v1, INTEGER v2))
|
||||
:= applyValue2(v1+1, actionFunc);
|
||||
|
||||
//producing simple results:
|
||||
OUTPUT(applyValue2(1)); // 2
|
||||
OUTPUT(applyValue2(2)); // 4
|
||||
OUTPUT(applyValue2(1, addValues)); // 6
|
||||
OUTPUT(applyValue2(2, addValues)); // 10
|
||||
OUTPUT(applyValue2(1, multiValues)); // 4
|
||||
OUTPUT(applyValue2(2, multiValues)); // 12
|
||||
OUTPUT(doApplyValue(1, multiValues)); // 12
|
||||
OUTPUT(doApplyValue(2, multiValues)); // 24
|
||||
|
||||
|
||||
|
||||
//A definition taking function parameters which themselves
|
||||
//have parameters that are functions...
|
||||
|
||||
STRING doMany(INTEGER v1,
|
||||
INTEGER firstAction(INTEGER v1,
|
||||
INTEGER actionFunc(INTEGER v1,INTEGER v2)),
|
||||
INTEGER secondAction(INTEGER v1,
|
||||
INTEGER actionFunc(INTEGER v1,INTEGER v2)),
|
||||
INTEGER actionFunc(INTEGER v1,INTEGER v2))
|
||||
:= (STRING)firstAction(v1, actionFunc) + ':' + (STRING)secondaction(v1, actionFunc);
|
||||
|
||||
OUTPUT(doMany(1, applyValue2, applyValue4, addValues));
|
||||
// produces "6:12"
|
||||
|
||||
OUTPUT(doMany(2, applyValue4, applyValue2,multiValues));
|
||||
// produces "24:12"
|
||||
13
Task/Higher-order-functions/ERRE/higher-order-functions.erre
Normal file
13
Task/Higher-order-functions/ERRE/higher-order-functions.erre
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
PROGRAM FUNC_PASS
|
||||
|
||||
FUNCTION ONE(X,Y)
|
||||
ONE=(X+Y)^2
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION TWO(X,Y)
|
||||
TWO=ONE(X,Y)+1
|
||||
END FUNCTION
|
||||
|
||||
BEGIN
|
||||
PRINT(TWO(10,11))
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function square(n As Integer) As Integer
|
||||
Return n * n
|
||||
End Function
|
||||
|
||||
Function cube(n As Integer) As Integer
|
||||
Return n * n * n
|
||||
End Function
|
||||
|
||||
Sub doCalcs(from As Integer, upTo As Integer, title As String, func As Function(As Integer) As Integer)
|
||||
Print title; " -> ";
|
||||
For i As Integer = from To upTo
|
||||
Print Using "#####"; func(i);
|
||||
Next
|
||||
Print
|
||||
End Sub
|
||||
|
||||
doCalcs 1, 10, "Squares", @square
|
||||
doCalcs 1, 10, "Cubes ", @cube
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
include "ConsoleWindow"
|
||||
|
||||
dim as pointer functionOneAddress
|
||||
|
||||
def fn FunctionOne( x as long, y as long ) as long = (x + y) ^ 2
|
||||
functionOneAddress = @fn FunctionOne
|
||||
|
||||
def fn FunctionTwo( x as long, y as long ) using functionOneAddress
|
||||
|
||||
print fn FunctionTwo( 12, 12 )
|
||||
26
Task/Higher-order-functions/Lily/higher-order-functions.lily
Normal file
26
Task/Higher-order-functions/Lily/higher-order-functions.lily
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
define square(x: Integer): Integer
|
||||
{
|
||||
return x * x
|
||||
}
|
||||
|
||||
var l = [1, 2, 3] # Inferred type: List[Integer].
|
||||
|
||||
# Transform using a user-defined function.
|
||||
print(l.map(square)) # [1, 4, 9]
|
||||
|
||||
# Using a built-in method this time.
|
||||
print(l.map(Integer.to_s)) # ["1", "2", "3"]
|
||||
|
||||
# Using a lambda (with the type of 'x' properly inferred).
|
||||
print(l.map{|x| (x + 1).to_s()}) # ["2", "3", "4"]
|
||||
|
||||
# In reverse order using F#-styled pipes.
|
||||
Boolean.to_i |> [true, false].map |> print
|
||||
|
||||
define apply[A, B](value: A, f: Function(A => B)): B
|
||||
{
|
||||
return f(value)
|
||||
}
|
||||
|
||||
# Calling user-defined transformation.
|
||||
print(apply("123", String.parse_i)) # Some(123)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
-- in some movie script
|
||||
----------------------------------------
|
||||
-- Runs provided function on all elements of the provided list, returns results as new list
|
||||
-- @param {list} tList
|
||||
-- @param {symbol} cbFunc
|
||||
-- @param {object} [cbObj=_movie]
|
||||
-- @return {list}
|
||||
----------------------------------------
|
||||
on map (tList, cbFunc, cbObj)
|
||||
if voidP(cbObj) then cbObj = _movie
|
||||
res = []
|
||||
cnt = tList.count
|
||||
repeat with i = 1 to cnt
|
||||
res[i] = call(cbFunc, cbObj, tList[i])
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
l = [1, 2, 3]
|
||||
|
||||
-- passes the built-in function 'sin' (which is a method of the _movie object) as argument to map
|
||||
res = map(l, #sin)
|
||||
|
||||
put res
|
||||
-- [0.8415, 0.9093, 0.1411]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function lambda_true(x: 'a)(y: 'a): 'a = x;;
|
||||
function lambda_false(x: 'a)(y: 'a): 'a = y;;
|
||||
function lambda_if(c:'a -> 'a -> 'a )(t: 'a)(f: 'a): 'a = c(t)(f);;
|
||||
|
||||
print( lambda_if(lambda_true)("condition was true")("condition was false") );;
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
func g(a: int, b: int, f: func(int,int): int): int
|
||||
{
|
||||
return f(a, b);
|
||||
}
|
||||
|
||||
import morfa.base;
|
||||
|
||||
func main(): void
|
||||
{
|
||||
println("Add: ", g(2, 3, func(a: int, b: int) { return a + b; }));
|
||||
println("Multiply: ", g(2, 3, func(a: int, b: int) { return a * b; }));
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
proc first(fn: proc): auto =
|
||||
return fn()
|
||||
|
||||
proc second(): string =
|
||||
return "second"
|
||||
|
||||
echo first(second)
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3, 4, 5 ] map(#1+)
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
procedure use(integer fi, integer a, integer b)
|
||||
print(1,call_func(fi,{a,b}))
|
||||
end procedure
|
||||
|
||||
function add(integer a, integer b)
|
||||
return a + b
|
||||
end function
|
||||
|
||||
use(routine_id("add"),23,45)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
func first(f) {
|
||||
return f();
|
||||
}
|
||||
|
||||
func second {
|
||||
return "second";
|
||||
}
|
||||
|
||||
say first(second); # => "second"
|
||||
say first(func { "third" }); # => "third"
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
function call_me(func, arg) {
|
||||
return func(arg);
|
||||
}
|
||||
|
||||
let answer = call_me(function(x) { return 6 * x; }, 7);
|
||||
print(answer);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
func func1(f: String->String) -> String { return f("a string") }
|
||||
func func2(s: String) -> String { return "func2 called with " + s }
|
||||
println(func1(func2)) // prints "func2 called with a string"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
func func3<T>(f: (Int,Int)->T) -> T { return f(1, 2) }
|
||||
println(func3 {(x, y) in x + y}) // prints "3"
|
||||
10
Task/Higher-order-functions/Ursa/higher-order-functions.ursa
Normal file
10
Task/Higher-order-functions/Ursa/higher-order-functions.ursa
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
def first (function f)
|
||||
return (f)
|
||||
end
|
||||
|
||||
def second ()
|
||||
return "second"
|
||||
end
|
||||
|
||||
out (first second) endl console
|
||||
# "second" is output to the console
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
def foo( filter ):
|
||||
("world" | filter) as $str
|
||||
| "hello \($str)" ;
|
||||
|
||||
# blue is defined here as a filter that adds blue to its input:
|
||||
def blue: "blue \(.)";
|
||||
|
||||
foo( blue ) # prints "hello blue world"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
def g(f; x; y): [x,y] | f;
|
||||
|
||||
g(add; 2; 3) # => 5
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
def is_even:
|
||||
if floor == . then (. % 2) == 0
|
||||
else error("is_even expects its input to be an integer")
|
||||
end;
|
||||
26
Task/Higher-order-functions/jq/higher-order-functions-4.jq
Normal file
26
Task/Higher-order-functions/jq/higher-order-functions-4.jq
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# Are all integers between 1 and 5 even?
|
||||
# For this example, we will use all/2 even
|
||||
# though it requires a release of jq after jq 1.4;
|
||||
# we do so to highlight the fact that all/2
|
||||
# terminates the generator once the condition is satisfied:
|
||||
all( range(1;6); is_even )
|
||||
false
|
||||
|
||||
# Display the even integers in the given range:
|
||||
range(1;6) | select(is_even)
|
||||
2
|
||||
4
|
||||
|
||||
# Evaluate is_even for each integer in an array
|
||||
[range(1;6)] | map(is_even)
|
||||
[false, true, false, true, false]
|
||||
|
||||
# Note that in jq, there is actually no need to call
|
||||
# a higher-order function in cases like this.
|
||||
# For example one can simply write:
|
||||
range(1;6) | is_even
|
||||
false
|
||||
true
|
||||
false
|
||||
true
|
||||
false
|
||||
Loading…
Add table
Add a link
Reference in a new issue