langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,2 @@
|
|||
> (map (fn (x) (* x x)) '(1 2 3 4))
|
||||
(1 4 9 16)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
each (* [first, first] ) 1 2 3 4
|
||||
=1 4 9 16
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var arr = [1,2,3,4]
|
||||
arr.map proc(some: var int) = echo(some, " squared = ", some*some)
|
||||
|
|
@ -0,0 +1 @@
|
|||
Array.map
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let square x = x * x;;
|
||||
let values = Array.init 10 ((+) 1);;
|
||||
Array.map square values;;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let values = [1;2;3;4;5;6;7;8;9;10];;
|
||||
List.map square values;;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Array.iter (fun x -> Printf.printf "%d" x) [|1; 2; 3; 4; 5|];;
|
||||
|
|
@ -0,0 +1 @@
|
|||
List.iter (fun x -> Printf.printf "%d" x) [1; 2; 3; 4; 5];;
|
||||
|
|
@ -0,0 +1 @@
|
|||
Array.iter (Printf.printf "%d") [|1; 2; 3; 4; 5|];;
|
||||
|
|
@ -0,0 +1 @@
|
|||
List.iter (Printf.printf "%d") [1; 2; 3; 4; 5];;
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
use Structure;
|
||||
|
||||
bundle Default {
|
||||
class Test {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
Run();
|
||||
}
|
||||
|
||||
function : native : Run() ~ Nil {
|
||||
values := IntVector->New([1, 2, 3, 4, 5]);
|
||||
squares := values->Apply(Square(Int) ~ Int);
|
||||
each(i : squares) {
|
||||
squares->Get(i)->PrintLine();
|
||||
};
|
||||
}
|
||||
|
||||
function : Square(value : Int) ~ Int {
|
||||
return value * value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function e = f(x, y)
|
||||
e = x^2 + exp(-1/(y+1));
|
||||
endfunction
|
||||
|
||||
% f([2,3], [1,4]) gives and error, but
|
||||
arrayfun(@f, [2, 3], [1,4])
|
||||
% works
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
#include <order/interpreter.h>
|
||||
|
||||
ORDER_PP( 8tuple_map(8fn(8X, 8times(8X, 8X)), 8tuple(1, 2, 3, 4, 5)) )
|
||||
// -> (1,4,9,16,25)
|
||||
|
||||
ORDER_PP( 8seq_map(8fn(8X, 8times(8X, 8X)), 8seq(1, 2, 3, 4, 5)) )
|
||||
// -> (1)(4)(9)(16)(25)
|
||||
|
||||
ORDER_PP( 8seq_for_each(8fn(8X, 8print(8X 8comma)), 8seq(1, 2, 3, 4, 5)) )
|
||||
// prints 1,2,3,4,5, and returns 8nil
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
declare
|
||||
fun{Square A}
|
||||
A*A
|
||||
end
|
||||
|
||||
Lst = [1 2 3 4 5]
|
||||
|
||||
%% apply a PROCEDURE to every element
|
||||
{ForAll Lst Show}
|
||||
|
||||
%% apply a FUNCTION to every element
|
||||
Result = {Map Lst Square}
|
||||
{Show Result}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
callback(n)=n+n;
|
||||
apply(callback, [1,2,3,4,5])
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
-- Let's create a generic class with one method to be used as an interface:
|
||||
create or replace
|
||||
TYPE callback AS OBJECT (
|
||||
-- A class needs at least one member even though we don't use it
|
||||
-- There's no generic OBJECT type, so let's call it NUMBER
|
||||
dummy NUMBER,
|
||||
-- Here's our function, and since PL/SQL doesn't have generics,
|
||||
-- let's use type NUMBER for our params
|
||||
MEMBER FUNCTION exec(n number) RETURN number
|
||||
) NOT FINAL not instantiable;
|
||||
/
|
||||
|
||||
-- Now let's inherit from that, defining a class with one method. We'll have ours square a number.
|
||||
-- We can pass this class into any function that takes type callback:
|
||||
CREATE OR REPLACE TYPE CB_SQUARE under callback (
|
||||
OVERRIDING MEMBER FUNCTION exec(n NUMBER) RETURN NUMBER
|
||||
)
|
||||
/
|
||||
CREATE OR REPLACE
|
||||
TYPE BODY CB_SQUARE AS
|
||||
OVERRIDING MEMBER FUNCTION exec(n NUMBER) RETURN NUMBER IS
|
||||
BEGIN
|
||||
RETURN n * n;
|
||||
END exec;
|
||||
END;
|
||||
/
|
||||
|
||||
-- And a package to hold our test
|
||||
CREATE OR REPLACE
|
||||
PACKAGE PKG_CALLBACK AS
|
||||
myCallback cb_square;
|
||||
TYPE intTable IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
|
||||
ints intTable;
|
||||
i PLS_INTEGER;
|
||||
|
||||
procedure test_callback;
|
||||
END PKG_CALLBACK;
|
||||
/
|
||||
|
||||
CREATE OR REPLACE PACKAGE BODY PKG_CALLBACK AS
|
||||
-- Our generic mapping function that takes a "method" and a collection
|
||||
-- Note that it takes the generic callback type
|
||||
-- that doesn't know anything about squaring
|
||||
procedure do_callback(myCallback IN callback, ints IN OUT intTable) IS
|
||||
i PLS_INTEGER;
|
||||
myInt NUMBER;
|
||||
begin
|
||||
for i in 1 .. ints.count loop
|
||||
myInt := ints(i);
|
||||
-- PL/SQL call's the child's method
|
||||
ints(i) := myCallback.exec(myInt);
|
||||
END LOOP;
|
||||
end do_callback;
|
||||
|
||||
procedure test_callback IS
|
||||
BEGIN
|
||||
myCallback := cb_square(null);
|
||||
FOR i IN 1..5 LOOP
|
||||
ints(i) := i;
|
||||
END LOOP;
|
||||
|
||||
do_callback(myCallback, ints);
|
||||
|
||||
i := ints.FIRST;
|
||||
WHILE i IS NOT NULL LOOP
|
||||
DBMS_OUTPUT.put_line(ints(i));
|
||||
i := ints.next(i);
|
||||
END LOOP;
|
||||
END test_callback;
|
||||
END PKG_CALLBACK;
|
||||
/
|
||||
|
||||
BEGIN
|
||||
PKG_CALLBACK.TEST_CALLBACK();
|
||||
END;
|
||||
/
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
my $function = { 2 * $^x + 3 };
|
||||
my @array = 1 .. 5;
|
||||
|
||||
# via map function
|
||||
.say for map $function, @array;
|
||||
|
||||
# via map method
|
||||
.say for @array.map($function);
|
||||
|
||||
# via for loop
|
||||
for @array {
|
||||
say $function($_);
|
||||
}
|
||||
|
||||
# via the "hyper" metaoperator and method indirection
|
||||
say @array».$function;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
int cube(int n)
|
||||
{
|
||||
return n*n*n;
|
||||
}
|
||||
|
||||
array(int) a = ({ 1,2,3,4,5 });
|
||||
array(int) b = cube(a[*]); // automap operator
|
||||
array(int) c = map(a, cube); // conventional map function
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
;;; Define a procedure
|
||||
define proc(x);
|
||||
printf(x*x, '%p,');
|
||||
enddefine;
|
||||
|
||||
;;; Create array
|
||||
lvars ar = { 1 2 3 4 5};
|
||||
|
||||
;;; Apply procedure to array
|
||||
appdata(ar, proc);
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1 2 3 4 5] { dup mul = } forall
|
||||
|
|
@ -0,0 +1 @@
|
|||
[ [1 2 3 4 5] { dup mul } forall ]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1 2 3 4 5] {dup *} map
|
||||
|
|
@ -0,0 +1 @@
|
|||
1..5 | ForEach-Object { $_ * $_ }
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
function map ([array] $a, [scriptblock] $s) {
|
||||
$a | ForEach-Object $s
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
Procedure Cube(Array param.i(1))
|
||||
Protected n.i
|
||||
For n = 0 To ArraySize(param())
|
||||
Debug Str(param(n)) + "^3 = " + Str(param(n) * param(n) * param(n))
|
||||
Next
|
||||
EndProcedure
|
||||
|
||||
Dim AnArray.i(4)
|
||||
|
||||
For n = 0 To ArraySize(AnArray())
|
||||
AnArray(n) = Random(99)
|
||||
Next
|
||||
|
||||
Cube(AnArray())
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
REBOL [
|
||||
Title: "Array Callback"
|
||||
Date: 2010-01-04
|
||||
Author: oofoe
|
||||
URL: http://rosettacode.org/wiki/Apply_a_callback_to_an_Array
|
||||
]
|
||||
|
||||
map: func [
|
||||
"Apply a function across an array."
|
||||
f [native! function!] "Function to apply to each element of array."
|
||||
a [block!] "Array to process."
|
||||
/local x
|
||||
][x: copy [] forall a [append x do [f a/1]] x]
|
||||
|
||||
square: func [x][x * x]
|
||||
|
||||
; Tests:
|
||||
|
||||
assert: func [code][print [either do code [" ok"]["FAIL"] mold code]]
|
||||
|
||||
print "Simple loop, modify in place:"
|
||||
assert [[1 100 81] = (a: [1 10 9] forall a [a/1: square a/1] a)]
|
||||
|
||||
print [crlf "Functional style with 'map':"]
|
||||
assert [[4 16 36] = map :square [2 4 6]]
|
||||
|
||||
print [crlf "Applying native function with 'map':"]
|
||||
assert [[2 4 6] = map :square-root [4 16 36]]
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
>> x = rand(2,4)
|
||||
0.707213207 0.275298961 0.396757763 0.232312312
|
||||
0.215619868 0.207078017 0.565700032 0.666090571
|
||||
>> sin(x)
|
||||
0.649717845 0.271834652 0.386430003 0.230228332
|
||||
0.213952984 0.205601224 0.536006923 0.617916954
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
x = rand(2,4);
|
||||
y = zeros(2,4);
|
||||
for (i in 1:2)
|
||||
{
|
||||
for (j in 1:4)
|
||||
{
|
||||
y[i;j] = sin( x[i;j] );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
x = <<>>;
|
||||
for (i in 1:9)
|
||||
{
|
||||
x.[i] = rand();
|
||||
}
|
||||
|
||||
y = <<>>;
|
||||
for (i in members(x))
|
||||
{
|
||||
y.[i] = sin( x.[i] );
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
# To print the squared elements
|
||||
[1 2 3 4 5] each dup * print
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
# To obtain a new array
|
||||
group [1 2 3 4 5] each
|
||||
dup *
|
||||
list
|
||||
|
|
@ -0,0 +1 @@
|
|||
[ 1 2 3 4 5 ] ^array'fromQuote [ 10 * ] ^array'map ^array'display
|
||||
|
|
@ -0,0 +1 @@
|
|||
[ "Hello" "World" "Foo" ] ^array'fromQuote [ "%s " puts ] ^array'apply
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function apply(list, ageless to_apply)
|
||||
(comprehend(x; list) (to_apply(x)));
|
||||
|
||||
function square(x) (x*x);
|
||||
|
||||
iterate(x; apply([0...9], square))
|
||||
x!;
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
include "short.salm";
|
||||
|
||||
fun apply(list, ageless to_apply)
|
||||
(comp(x; list) (to_apply(x)));
|
||||
|
||||
fun square(x) (x*x);
|
||||
|
||||
iter(x; apply([0...9], square))
|
||||
x!;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function apply(list, to_apply)
|
||||
(comprehend(x; list) (to_apply(x)));
|
||||
|
||||
function square(x) (x*x);
|
||||
|
||||
iterate(x; apply([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], square))
|
||||
x!;
|
||||
|
|
@ -0,0 +1 @@
|
|||
#( 1 2 3 4 5 ) collect: [| :n | n * n].
|
||||
|
|
@ -0,0 +1 @@
|
|||
map f l
|
||||
|
|
@ -0,0 +1 @@
|
|||
map (fn x=>x+1) [1,2,3];; (* [2,3,4] *)
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3].squared; // returns [1, 4, 9]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1, 2, 3].collect({ arg x; x*x });
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
var square = {
|
||||
arg x;
|
||||
x*x;
|
||||
};
|
||||
var map = {
|
||||
arg fn, xs;
|
||||
all {: fn.value(x), x <- xs };
|
||||
};
|
||||
map.value(square,[1,2,3]);
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
© For no return value
|
||||
Define foreach(fe_cname,fe_list) = Prgm
|
||||
Local fe_i
|
||||
For fe_i,1,dim(fe_list)
|
||||
#fe_cname(fe_list[fe_i])
|
||||
EndFor
|
||||
EndPrgm
|
||||
|
||||
© For a list of results
|
||||
Define map(map_cnam,map_list) = seq(#map_cnam(map_list[map_i]),map_i,1,dim(map_list))
|
||||
|
||||
Define callback(elem) = Prgm
|
||||
Disp elem
|
||||
EndPrgm
|
||||
|
||||
foreach("callback", {1,2,3,4,5})
|
||||
Disp map("√", {1,2,3,4,5})
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var a = [1, 2, 3, 4, 5];
|
||||
a.map(function(v) { return v * v; })
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
var a = [1, 2, 3, 4, 5];
|
||||
a.map( :v: v*v );
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
@(do
|
||||
(defun mapvec (vec fun)
|
||||
(each ((i (range 0 (- (length vec) 1))))
|
||||
[fun [vec i]]))
|
||||
|
||||
(mapvec #(1 2 3 4 5 6 7 8 9 10)
|
||||
(lambda (x) (format t "~a\n" x))))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
( array count function -- )
|
||||
{
|
||||
value| array fn |
|
||||
[ i array ] is I
|
||||
[ to fn swap to array 0 swap [ I array.get :stack fn invoke I array.put ] countedLoop ]
|
||||
} is map-array
|
||||
|
||||
( Build an array )
|
||||
5 cells is-array a
|
||||
10 0 a array.put
|
||||
11 1 a array.put
|
||||
12 2 a array.put
|
||||
13 3 a array.put
|
||||
14 4 a array.put
|
||||
|
||||
( Add 1 to each item in the array )
|
||||
a 5 [ 1 + ] map-array
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
function map(%array,%arrayCount,%function)
|
||||
{
|
||||
for(%i=0;%i<%arrayCount;%i++)
|
||||
{
|
||||
eval("%a = "@%array@"["@%i@"];");
|
||||
eval(""@%function@"("@%a@");");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
$array[0] = "Hello.";
|
||||
$array[1] = "Hi.";
|
||||
$array[2] = "How are you?";
|
||||
|
|
@ -0,0 +1 @@
|
|||
map("$array",3,"echo");
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
=> Hello.
|
||||
|
||||
=> Hi.
|
||||
|
||||
=> How are you?
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
map() {
|
||||
map_command=$1
|
||||
shift
|
||||
for i do "$map_command" "$i"; done
|
||||
}
|
||||
list=1:2:3
|
||||
(IFS=:; map echo $list)
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
map() {
|
||||
typeset command=$1
|
||||
shift
|
||||
for i do "$command" "$i"; done
|
||||
}
|
||||
set -A ary 1 2 3
|
||||
map print "${ary[@]}"
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
map(){for i ($*[2,-1]) $1 $i}
|
||||
a=(1 2 3)
|
||||
map print $a
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
#import nat
|
||||
|
||||
#cast %nL
|
||||
|
||||
demo = successor* <325,32,67,1,3,7,315>
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1 2 3 4] [dup *] map
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
class callback
|
||||
dim sRule
|
||||
|
||||
public property let rule( x )
|
||||
sRule = x
|
||||
end property
|
||||
|
||||
public default function applyTo(a)
|
||||
dim p1
|
||||
for i = lbound( a ) to ubound( a )
|
||||
p1 = a( i )
|
||||
a( i ) = eval( sRule )
|
||||
next
|
||||
applyTo = a
|
||||
end function
|
||||
end class
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
dim a1
|
||||
dim cb
|
||||
set cb = new callback
|
||||
|
||||
cb.rule = "ucase(p1)"
|
||||
a1 = split("my dog has fleas", " " )
|
||||
cb.applyTo a1
|
||||
wscript.echo join( a1, " " )
|
||||
|
||||
cb.rule = "p1 ^ p1"
|
||||
a1 = array(1,2,3,4,5,6,7,8,9,10)
|
||||
cb.applyto a1
|
||||
wscript.echo join( a1, ", " )
|
||||
|
|
@ -0,0 +1 @@
|
|||
A.map(F)
|
||||
|
|
@ -0,0 +1 @@
|
|||
A.map(F, x, y)
|
||||
Loading…
Add table
Add a link
Reference in a new issue