2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,2 +1,3 @@
|
|||
In this task, the goal is to take a combined set of elements
|
||||
and apply a function to each element.
|
||||
;Task:
|
||||
Take a combined set of elements and apply a function to each element.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,11 @@
|
|||
on callback for arg
|
||||
-- Returns a string like "arc has 3 letters"
|
||||
arg & " has " & (count arg) & " letters"
|
||||
end callback
|
||||
|
||||
set alist to {"arc", "be", "circle"}
|
||||
repeat with aref in alist
|
||||
-- Passes a reference to some item in alist
|
||||
-- to callback, then speaks the return value.
|
||||
say (callback for aref)
|
||||
end repeat
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
on run
|
||||
|
||||
set xs to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
||||
|
||||
{map(square, xs), ¬
|
||||
filter(isEven, xs), ¬
|
||||
foldl(sum, 0, xs)}
|
||||
|
||||
--> {{1, 4, 9, 16, 25, 36, 49, 64, 81, 100}, {2, 4, 6, 8, 10}, 55}
|
||||
|
||||
end run
|
||||
|
||||
-- square :: Num -> Num -> Num
|
||||
on square(x)
|
||||
x * x
|
||||
end square
|
||||
|
||||
-- sum :: Num -> Num -> Num
|
||||
on sum(a, b)
|
||||
a + b
|
||||
end sum
|
||||
|
||||
-- isEven :: Int -> Bool
|
||||
on isEven(n)
|
||||
n mod 2 = 0
|
||||
end isEven
|
||||
|
||||
|
||||
-- GENERIC HIGHER ORDER FUNCTIONS
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- foldl :: (a -> b -> a) -> a -> [b] -> a
|
||||
on foldl(f, startValue, xs)
|
||||
tell mReturn(f)
|
||||
set v to startValue
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to lambda(v, item i of xs, i, xs)
|
||||
end repeat
|
||||
return v
|
||||
end tell
|
||||
end foldl
|
||||
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if lambda(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -1,11 +0,0 @@
|
|||
on callback for arg
|
||||
-- Returns a string like "arc has 3 letters"
|
||||
arg & " has " & (count arg) & " letters"
|
||||
end callback
|
||||
|
||||
set alist to {"arc", "be", "circle"}
|
||||
repeat with aref in alist
|
||||
-- Passes a reference to some item in alist
|
||||
-- to callback, then speaks the return value.
|
||||
say (callback for aref)
|
||||
end repeat
|
||||
|
|
@ -1,6 +1 @@
|
|||
((main
|
||||
{ (1 1 2 3 5 8 13 21) dup
|
||||
{double !} each
|
||||
{%d ' ' . <<} each})
|
||||
|
||||
(double { dup 2 * set }))
|
||||
sq { dup * } <
|
||||
|
|
|
|||
|
|
@ -1,9 +1 @@
|
|||
((main
|
||||
{ (1 1 2 3 5 8 13 21)
|
||||
{double !} each
|
||||
collect !
|
||||
{%d ' ' . <<} each})
|
||||
|
||||
(double { 2 * })
|
||||
|
||||
(collect { -1 take }))
|
||||
( 0 1 1 2 3 5 8 13 21 34 ) { sq ! } over ! lsnum !
|
||||
|
|
|
|||
|
|
@ -0,0 +1 @@
|
|||
Enum.map([1, 2, 3], fn(n) -> n * 2 end)
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
fun main(args: Array<String>) {
|
||||
val array = arrayOf(1,2,3,4,5,6,7,8,9,10) // build
|
||||
val function = { i: Int -> i * i } // function to apply
|
||||
val list = array.map { function(it) } // process each item
|
||||
println(list) // print results
|
||||
}
|
||||
|
|
@ -0,0 +1,88 @@
|
|||
MODULE ApplyCallBack;
|
||||
IMPORT
|
||||
Out := NPCT:Console;
|
||||
|
||||
TYPE
|
||||
Fun = PROCEDURE (x: LONGINT): LONGINT;
|
||||
Ptr2Ary = POINTER TO ARRAY OF LONGINT;
|
||||
|
||||
VAR
|
||||
a: ARRAY 5 OF LONGINT;
|
||||
x: ARRAY 3 OF LONGINT;
|
||||
r: Ptr2Ary;
|
||||
|
||||
PROCEDURE Min(x,y: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
IF x <= y THEN RETURN x ELSE RETURN y END;
|
||||
END Min;
|
||||
|
||||
PROCEDURE Init(VAR a: ARRAY OF LONGINT);
|
||||
BEGIN
|
||||
a[0] := 0;
|
||||
a[1] := 1;
|
||||
a[2] := 2;
|
||||
a[3] := 3;
|
||||
a[4] := 4;
|
||||
END Init;
|
||||
|
||||
PROCEDURE Fun1(x: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x * 2
|
||||
END Fun1;
|
||||
|
||||
PROCEDURE Fun2(x: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x DIV 2;
|
||||
END Fun2;
|
||||
|
||||
PROCEDURE Fun3(x: LONGINT): LONGINT;
|
||||
BEGIN
|
||||
RETURN x + 3;
|
||||
END Fun3;
|
||||
|
||||
PROCEDURE Map(F: Fun; VAR x: ARRAY OF LONGINT);
|
||||
VAR
|
||||
i: LONGINT;
|
||||
BEGIN
|
||||
FOR i := 0 TO LEN(x) - 1 DO
|
||||
x[i] := F(x[i])
|
||||
END
|
||||
END Map;
|
||||
|
||||
PROCEDURE Map2(F: Fun; a: ARRAY OF LONGINT; VAR r: ARRAY OF LONGINT);
|
||||
VAR
|
||||
i,l: LONGINT;
|
||||
BEGIN
|
||||
l := Min(LEN(a),LEN(x));
|
||||
FOR i := 0 TO l - 1 DO
|
||||
r[i] := F(a[i])
|
||||
END
|
||||
END Map2;
|
||||
|
||||
PROCEDURE Map3(F: Fun; a: ARRAY OF LONGINT): Ptr2Ary;
|
||||
VAR
|
||||
r: Ptr2Ary;
|
||||
i: LONGINT;
|
||||
BEGIN
|
||||
NEW(r,LEN(a));
|
||||
FOR i := 0 TO LEN(a) - 1 DO
|
||||
r[i] := F(a[i]);
|
||||
END;
|
||||
RETURN r
|
||||
END Map3;
|
||||
|
||||
PROCEDURE Show(a: ARRAY OF LONGINT);
|
||||
VAR
|
||||
i: LONGINT;
|
||||
BEGIN
|
||||
FOR i := 0 TO LEN(a) - 1 DO
|
||||
Out.Int(a[i],4)
|
||||
END;
|
||||
Out.Ln
|
||||
END Show;
|
||||
|
||||
BEGIN
|
||||
Init(a);Map(Fun1,a);Show(a);
|
||||
Init(a);Map2(Fun2,a,x);Show(x);
|
||||
Init(a);r := Map3(Fun3,a);Show(r^);
|
||||
END ApplyCallBack.
|
||||
|
|
@ -0,0 +1 @@
|
|||
call(callback, [1,2,3,4,5])
|
||||
|
|
@ -1 +1 @@
|
|||
[1, 2, 3].squared; // returns [1, 4, 9]
|
||||
[1, 2, 3].squared // returns [1, 4, 9]
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[1, 2, 3].collect({ arg x; x*x });
|
||||
[1, 2, 3].collect({ | x | x * x })
|
||||
|
|
|
|||
|
|
@ -1,9 +1,5 @@
|
|||
var square = {
|
||||
arg x;
|
||||
x*x;
|
||||
};
|
||||
var map = {
|
||||
arg fn, xs;
|
||||
var square = { |x| x * x };
|
||||
var map = { |fn, xs|
|
||||
all {: fn.value(x), x <- xs };
|
||||
};
|
||||
map.value(square,[1,2,3]);
|
||||
map.value(square, [1, 2, 3]);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,10 @@
|
|||
10 LET a$="x+x"
|
||||
20 LET b$="x*x"
|
||||
30 LET c$="x+x^2"
|
||||
40 LET f$=c$: REM Assign a$, b$ or c$
|
||||
150 FOR i=1 TO 5
|
||||
160 READ x
|
||||
170 PRINT x;" = ";VAL f$
|
||||
180 NEXT i
|
||||
190 STOP
|
||||
200 DATA 2,5,6,10,100
|
||||
Loading…
Add table
Add a link
Reference in a new issue