This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,2 @@
{square * . [id, id]}
& square: <1,2,3,4,5>

View file

@ -0,0 +1 @@
{ 1 2 3 4 } [ sq . ] each

View file

@ -0,0 +1 @@
{ 1 2 3 4 } [ sq ] map

View file

@ -0,0 +1,9 @@
class Main
{
public static Void main ()
{
[1,2,3,4,5].each |Int i| { echo (i) }
Int[] result := [1,2,3,4,5].map |Int i->Int| { return i * i }
echo (result)
}
}

View file

@ -0,0 +1,17 @@
a := [1 .. 4];
b := ShallowCopy(a);
# Apply and replace values
Apply(a, n -> n*n);
a;
# [ 1, 4, 9, 16 ]
# Apply and don't change values
Perform(b, Display);
1
2
3
4
b;
# [ 1 .. 4 ]

View file

@ -0,0 +1 @@
[1,2,3,4].each { println it }

View file

@ -0,0 +1 @@
[1,2,3,4].collect { it * it }

View file

@ -0,0 +1 @@
b = a^3

View file

@ -0,0 +1,9 @@
procedure main()
local lst
lst := [10, 20, 30, 40]
every callback(write,!lst)
end
procedure callback(p,arg)
return p(" -> ", arg)
end

View file

@ -0,0 +1 @@
list(1,2,3,4,5) map(squared)

View file

@ -0,0 +1,5 @@
callback =: *:
array =: 1 2 3 4 5
callback"_1 array
1 4 9 16 25

View file

@ -0,0 +1 @@
[1 2 3 4 5] [dup *] map.

View file

@ -0,0 +1,8 @@
a = [1,2,3,4,5]
julia> map(x -> x*2,a)
5-element Int32 Array:
2
4
6
8
10

View file

@ -0,0 +1,7 @@
julia> a .* 2
5-element Int32 Array:
2
4
6
8
10

View file

@ -0,0 +1,3 @@
: square(*) dup * ;
[1 2 3 4 5] square . "\n" .
[1 2 3 4 5] 'square apply . "\n" .

View file

@ -0,0 +1,14 @@
+ a : ARRAY(INTEGER);
+ b : {INTEGER;};
a := ARRAY(INTEGER).create 1 to 3;
1.to 3 do { i : INTEGER;
a.put i to i;
};
b := { arg : INTEGER;
(arg * arg).print;
'\n'.print;
};
a.foreach b;

View file

@ -0,0 +1,6 @@
to square :x
output :x * :x
end
show map "square [1 2 3 4 5] ; [1 4 9 16 25]
show map [? * ?] [1 2 3 4 5] ; [1 4 9 16 25]
foreach [1 2 3 4 5] [print square ?] ; 1 4 9 16 25, one per line

View file

@ -0,0 +1,9 @@
define(`foreach', `pushdef(`$1')_foreach($@)popdef(`$1')')dnl
define(`_arg1', `$1')dnl
define(`_foreach', `ifelse(`$2', `()', `',
`define(`$1', _arg1$2)$3`'$0(`$1', (shift$2), `$3')')')dnl
dnl
define(`apply',`foreach(`x',$1,`$2(x)')')dnl
dnl
define(`z',`eval(`$1*2') ')dnl
apply(`(1,2,3)',`z')

View file

@ -0,0 +1,11 @@
> map( sqrt, [ 1.1, 3.2, 5.7 ] );
[1.048808848, 1.788854382, 2.387467277]
> map( x -> x + 1, { 1, 3, 5 } );
{2, 4, 6}
> sqrt~( [ 1.1, 3.2, 5.7 ] );
[1.048808848, 1.788854382, 2.387467277]
> (x -> x + 1)~( { 1, 3, 5 } );
{2, 4, 6}

View file

@ -0,0 +1,14 @@
> a := Array( [ 1.1, 3.2, 5.7 ] );
a := [1.1, 3.2, 5.7]
> sqrt~( a );
[1.048808848, 1.788854382, 2.387467277]
> a;
[1.1, 3.2, 5.7]
> map( sqrt, a );
[1.048808848, 1.788854382, 2.387467277]
> a;
[1.1, 3.2, 5.7]

View file

@ -0,0 +1,5 @@
> map[inplace]( sqrt, a );
[1.048808848, 1.788854382, 2.387467277]
> a;
[1.048808848, 1.788854382, 2.387467277]

View file

@ -0,0 +1,2 @@
> map( `+`, [ 1, 2, 3 ], 3 );
[4, 5, 6]

View file

@ -0,0 +1,5 @@
> map2( `-`, 5, [ 1, 2, 3 ] );
[4, 3, 2]
> map[2]( `/`, 5, [ 1, 2, 3 ] );
[5, 5/2, 5/3]

View file

@ -0,0 +1,7 @@
(#*#)& /@ {1, 2, 3, 4}
Map[Function[#*#], {1, 2, 3, 4}]
Map[((#*#)&,{1,2,3,4}]
Map[Function[w,w*w],{1,2,3,4}]

View file

@ -0,0 +1,8 @@
/* for lists or sets */
map(sin, [1, 2, 3, 4]);
map(sin, {1, 2, 3, 4});
/* for matrices */
matrixmap(sin, matrix([1, 2], [2, 4]));

View file

@ -0,0 +1,26 @@
MODULE Callback EXPORTS Main;
IMPORT IO, Fmt;
TYPE CallBack = PROCEDURE (a: CARDINAL; b: INTEGER);
Values = REF ARRAY OF INTEGER;
VAR sample := ARRAY [1..5] OF INTEGER {5, 4, 3, 2, 1};
callback := Display;
PROCEDURE Display(loc: CARDINAL; val: INTEGER) =
BEGIN
IO.Put("array[" & Fmt.Int(loc) & "] = " & Fmt.Int(val * val) & "\n");
END Display;
PROCEDURE Map(VAR values: ARRAY OF INTEGER; size: CARDINAL; worker: CallBack) =
VAR lvalues := NEW(Values, size);
BEGIN
FOR i := FIRST(lvalues^) TO LAST(lvalues^) DO
worker(i, values[i]);
END;
END Map;
BEGIN
Map(sample, NUMBER(sample), callback);
END Callback.