Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
category:
- Iteration
from: http://rosettacode.org/wiki/Apply_a_callback_to_an_array
note: Basic language learning

View file

@ -0,0 +1,4 @@
;Task:
Take a combined set of elements and apply a function to each element.
<br><br>

View file

@ -0,0 +1,3 @@
V array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
V arrsq = array.map(i -> i * i)
print(arrsq)

View file

@ -0,0 +1,54 @@
define SRC_LO $00
define SRC_HI $01
define DEST_LO $02
define DEST_HI $03
define temp $04 ;temp storage used by foo
;some prep work since easy6502 doesn't allow you to define arbitrary bytes before runtime.
SET_TABLE:
TXA
STA $1000,X
INX
BNE SET_TABLE
;stores the identity table at memory address $1000-$10FF
CLEAR_TABLE:
LDA #0
STA $1200,X
INX
BNE CLEAR_TABLE
;fills the range $1200-$12FF with zeroes.
LDA #$10
STA SRC_HI
LDA #$00
STA SRC_LO
;store memory address $1000 in zero page
LDA #$12
STA DEST_HI
LDA #$00
STA DEST_LO
;store memory address $1200 in zero page
loop:
LDA (SRC_LO),y ;load accumulator from memory address $1000+y
JSR foo ;multiplies accumulator by 3.
STA (DEST_LO),y ;store accumulator in memory address $1200+y
INY
CPY #$56 ;alternatively you can store a size variable and check that here instead.
BCC loop
BRK
foo:
STA temp
ASL ;double accumulator
CLC
ADC temp ;2a + a = 3a
RTS

View file

@ -0,0 +1,18 @@
LEA MyArray,A0
MOVE.W #(MyArray_End-MyArray)-1,D7 ;Len(MyArray)-1
MOVEQ #0,D0 ;sanitize D0-D2 to ensure nothing from any previous work will affect our math.
MOVEQ #0,D1
MOVEQ #0,D2
loop:
MOVE.B (A0),D0
MOVE.B D0,D1
MOVE.B D0,D2
MULU D1,D2
MOVE.B D2,(A0)+
dbra d7,loop
jmp * ;halt the CPU
MyArray:
DC.B 1,2,3,4,5,6,7,8,9,10
MyArray_End:

View file

@ -0,0 +1,3 @@
[ 1 , 2, 3 ]
' n:sqr
a:map

View file

@ -0,0 +1,8 @@
(defun apply-to-each (xs)
(if (endp xs)
nil
(cons (fn-to-apply (first xs))
(sq-each (rest xs)))))
(defun fn-to-apply (x)
(* x x))

View file

@ -0,0 +1,17 @@
PROC call back proc = (INT location, INT value)VOID:
(
printf(($"array["g"] = "gl$, location, value))
);
PROC map = (REF[]INT array, PROC (INT,INT)VOID call back)VOID:
(
FOR i FROM LWB array TO UPB array DO
call back(i, array[i])
OD
);
main:
(
[4]INT array := ( 1, 4, 9, 16 );
map(array, call back proc)
)

View file

@ -0,0 +1,12 @@
begin
procedure printSquare ( integer value x ) ; writeon( i_w := 1, s_w := 0, " ", x * x );
% applys f to each element of a from lb to ub (inclusive) %
procedure applyI ( procedure f; integer array a ( * ); integer value lb, ub ) ;
for i := lb until ub do f( a( i ) );
% test applyI %
begin
integer array a ( 1 :: 3 );
a( 1 ) := 1; a( 2 ) := 2; a( 3 ) := 3;
applyI( printSquare, a, 1, 3 )
end
end.

View file

@ -0,0 +1,10 @@
- 1 2 3
¯1 ¯2 ¯3
2 * 1 2 3 4
2 4 8 16
2 × 4
2 4 6 8
3 * 3 3 9
3 9 27
81 243 729
2187 6561 19683

View file

@ -0,0 +1,6 @@
$ awk 'func psqr(x){print x,x*x}BEGIN{split("1 2 3 4 5",a);for(i in a)psqr(a[i])}'
4 16
5 25
1 1
2 4
3 9

View file

@ -0,0 +1,19 @@
package
{
public class ArrayCallback
{
public function main():void
{
var nums:Array = new Array(1, 2, 3);
nums.map(function(n:Number, index:int, arr:Array):void { trace(n * n * n); });
// You can also pass a function reference
nums.map(cube);
}
private function cube(n:Number, index:int, arr:Array):void
{
trace(n * n * n);
}
}
}

View file

@ -0,0 +1,37 @@
with Ada.Text_Io;
with Ada.Integer_text_IO;
procedure Call_Back_Example is
-- Purpose: Apply a callback to an array
-- Output: Prints the squares of an integer array to the console
-- Define the callback procedure
procedure Display(Location : Positive; Value : Integer) is
begin
Ada.Text_Io.Put("array(");
Ada.Integer_Text_Io.Put(Item => Location, Width => 1);
Ada.Text_Io.Put(") = ");
Ada.Integer_Text_Io.Put(Item => Value * Value, Width => 1);
Ada.Text_Io.New_Line;
end Display;
-- Define an access type matching the signature of the callback procedure
type Call_Back_Access is access procedure(L : Positive; V : Integer);
-- Define an unconstrained array type
type Value_Array is array(Positive range <>) of Integer;
-- Define the procedure performing the callback
procedure Map(Values : Value_Array; Worker : Call_Back_Access) is
begin
for I in Values'range loop
Worker(I, Values(I));
end loop;
end Map;
-- Define and initialize the actual array
Sample : Value_Array := (5,4,3,2,1);
begin
Map(Sample, Display'access);
end Call_Back_Example;

View file

@ -0,0 +1,19 @@
void
map(list l, void (*fp)(object))
{
l.ucall(fp, 0);
}
void
out(object o)
{
o_(o, "\n");
}
integer
main(void)
{
list(0, 1, 2, 3).map(out);
return 0;
}

View file

@ -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

View file

@ -0,0 +1,78 @@
on run
set xs to {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{map(square, xs), ¬
filter(even, xs), ¬
foldl(add, 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
-- add :: Num -> Num -> Num
on add(a, b)
a + b
end add
-- even :: Int -> Bool
on even(x)
0 = x mod 2
end even
-- GENERIC HIGHER ORDER FUNCTIONS
-- 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 |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- 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 |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if class of f is script then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- 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 |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map

View file

@ -0,0 +1,3 @@
arr: [1 2 3 4 5]
print map arr => [2*&]

View file

@ -0,0 +1,10 @@
map("callback", "3,4,5")
callback(array){
Loop, Parse, array, `,
MsgBox % (2 * A_LoopField)
}
map(callback, array){
%callback%(array)
}

View file

@ -0,0 +1,16 @@
DIM a(4)
a() = 1, 2, 3, 4, 5
PROCmap(a(), FNsqrt())
FOR i = 0 TO 4
PRINT a(i)
NEXT
END
DEF FNsqrt(n) = SQR(n)
DEF PROCmap(array(), RETURN func%)
LOCAL I%
FOR I% = 0 TO DIM(array(),1)
array(I%) = FN(^func%)(array(I%))
NEXT
ENDPROC

View file

@ -0,0 +1 @@
sq { dup * } <

View file

@ -0,0 +1 @@
( 0 1 1 2 3 5 8 13 21 34 ) { sq ! } over ! lsnum !

View file

@ -0,0 +1,29 @@
( ( callbackFunction1
= location value
. !arg:(?location,?value)
& out$(str$(array[ !location "] = " !!value))
)
& ( callbackFunction2
= location value
. !arg:(?location,?value)
& !!value^2:?!value
)
& ( mapar
= arr len callback i
. !arg:(?arr,?len,?callback)
& 0:?i
& whl
' ( !i:<!len
& !callback$(!i,!i$!arr)
& 1+!i:?i
)
)
& tbl$(array,4)
& 1:?(0$array)
& 2:?(1$array)
& 3:?(2$array)
& 4:?(3$array)
& mapar$(array,4,callbackFunction1)
& mapar$(array,4,callbackFunction2)
& mapar$(array,4,callbackFunction1)
);

View file

@ -0,0 +1,4 @@
#Print out each element in array
[:a :b :c :d :e].each { element |
p element
}

View file

@ -0,0 +1 @@
[:a :b :c :d :e].each ->p

View file

@ -0,0 +1,16 @@
#include <iostream> //cout for printing
#include <algorithm> //for_each defined here
//create the function (print the square)
void print_square(int i) {
std::cout << i*i << " ";
}
int main() {
//create the array
int ary[]={1,2,3,4,5};
//stl for_each
std::for_each(ary,ary+5,print_square);
return 0;
}
//prints 1 4 9 16 25

View file

@ -0,0 +1,22 @@
#include <iostream> // cout for printing
#include <algorithm> // for_each defined here
#include <vector> // stl vector class
// create the function (print the square)
void print_square(int i) {
std::cout << i*i << " ";
}
int main() {
// create the array
std::vector<int> ary;
ary.push_back(1);
ary.push_back(2);
ary.push_back(3);
ary.push_back(4);
ary.push_back(5);
// stl for_each
std::for_each(ary.begin(),ary.end(),print_square);
return 0;
}
//prints 1 4 9 16 25

View file

@ -0,0 +1,24 @@
#include <iostream> // cout for printing
#include <algorithm> // for_each defined here
#include <vector> // stl vector class
#include <functional> // bind and ptr_fun
// create a binary function (print any two arguments together)
template<class type1,class type2>
void print_juxtaposed(type1 x, type2 y) {
std::cout << x << y;
}
int main() {
// create the array
std::vector<int> ary;
ary.push_back(1);
ary.push_back(2);
ary.push_back(3);
ary.push_back(4);
ary.push_back(5);
// stl for_each, using binder and adaptable unary function
std::for_each(ary.begin(),ary.end(),std::bind2nd(std::ptr_fun(print_juxtaposed<int,std::string>),"x "));
return 0;
}
//prints 1x 2x 3x 4x 5x

View file

@ -0,0 +1,6 @@
using namespace std;
using namespace boost::lambda;
vector<int> ary(10);
int i = 0;
for_each(ary.begin(), ary.end(), _1 = ++var(i)); // init array
transform(ary.begin(), ary.end(), ostream_iterator<int>(cout, " "), _1 * _1); // square and output

View file

@ -0,0 +1,15 @@
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
int main() {
std::vector<int> intVec(10);
std::iota(std::begin(intVec), std::end(intVec), 1 ); // Fill the vector
std::transform(std::begin(intVec) , std::end(intVec), std::begin(intVec),
[](int i) { return i * i ; } ); // Transform it with closures
std::copy(std::begin(intVec), end(intVec) ,
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
return 0;
}

View file

@ -0,0 +1,11 @@
int[] intArray = { 1, 2, 3, 4, 5 };
// Simplest method: LINQ, functional
int[] squares1 = intArray.Select(x => x * x).ToArray();
// Slightly fancier: LINQ, query expression
int[] squares2 = (from x in intArray
select x * x).ToArray();
// Or, if you only want to call a function on each element, just use foreach
foreach (var i in intArray)
Console.WriteLine(i * i);

View file

@ -0,0 +1,34 @@
using System;
static class Program
{
// Purpose: Apply a callback (or anonymous method) to an Array
// Output: Prints the squares of an int array to the console.
// Compiler: Visual Studio 2005
// Framework: .net 2
[STAThread]
public static void Main()
{
int[] intArray = { 1, 2, 3, 4, 5 };
// Using a callback,
Console.WriteLine("Printing squares using a callback:");
Array.ForEach<int>(intArray, PrintSquare);
// or using an anonymous method:
Console.WriteLine("Printing squares using an anonymous method:");
Array.ForEach<int>
(
intArray,
delegate(int value)
{
Console.WriteLine(value * value);
});
}
public static void PrintSquare(int value)
{
Console.WriteLine(value * value);
}
}

View file

@ -0,0 +1,16 @@
#ifndef CALLBACK_H
#define CALLBACK_H
/*
* By declaring the function in a separate file, we allow
* it to be used by other source files.
*
* It also stops ICC from complaining.
*
* If you don't want to use it outside of callback.c, this
* file can be removed, provided the static keyword is prepended
* to the definition.
*/
void map(int* array, int len, void(*callback)(int,int));
#endif

View file

@ -0,0 +1,27 @@
#include <stdio.h>
#include "callback.h"
/*
* We don't need this function outside of this file, so
* we declare it static.
*/
static void callbackFunction(int location, int value)
{
printf("array[%d] = %d\n", location, value);
}
void map(int* array, int len, void(*callback)(int,int))
{
int i;
for(i = 0; i < len; i++)
{
callback(i, array[i]);
}
}
int main()
{
int array[] = { 1, 2, 3, 4 };
map(array, 4, callbackFunction);
return 0;
}

View file

@ -0,0 +1,34 @@
% This procedure will call a given procedure with each element
% of the given array. Thanks to CLU's type parameterization,
% it will work for any type of element.
apply_to_all = proc [T: type] (a: array[T], f: proctype(int,T))
for i: int in array[T]$indexes(a) do
f(i, a[i])
end
end apply_to_all
% Callbacks for both string and int
show_int = proc (i, val: int)
po: stream := stream$primary_output()
stream$putl(po, "array[" || int$unparse(i) || "] = " || int$unparse(val));
end show_int
show_string = proc (i: int, val: string)
po: stream := stream$primary_output()
stream$putl(po, "array[" || int$unparse(i) || "] = " || val);
end show_string
% Here's how to use them
start_up = proc ()
po: stream := stream$primary_output()
ints: array[int] := array[int]$[2, 3, 5, 7, 11]
strings: array[string] := array[string]$
["enemy", "lasagna", "robust", "below", "wax"]
stream$putl(po, "Ints: ")
apply_to_all[int](ints, show_int)
stream$putl(po, "\nStrings: ")
apply_to_all[string](strings, show_string)
end start_up

View file

@ -0,0 +1,23 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. Map.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Table-Size CONSTANT 30.
LOCAL-STORAGE SECTION.
01 I USAGE UNSIGNED-INT.
LINKAGE SECTION.
01 Table-Param.
03 Table-Values USAGE COMP-2 OCCURS Table-Size TIMES.
01 Func-Id PIC X(30).
PROCEDURE DIVISION USING Table-Param Func-Id.
PERFORM VARYING I FROM 1 BY 1 UNTIL Table-Size < I
CALL Func-Id USING BY REFERENCE Table-Values (I)
END-PERFORM
GOBACK
.

View file

@ -0,0 +1,4 @@
square x = x * x
values :: {#Int}
values = {x \\ x <- [1 .. 10]}

View file

@ -0,0 +1 @@
mapArray f array = {f x \\ x <-: array}

View file

@ -0,0 +1,2 @@
Start :: {#Int}
Start = mapArray square values

View file

@ -0,0 +1 @@
[1 2 3 4] * 2 + 1 -> print

View file

@ -0,0 +1 @@
[1 2 3 4] -> * n: n * 2 + 1 -> print

View file

@ -0,0 +1,4 @@
[1 2 3 4]
-> * fn n:
n * 2 + 1
-> print

View file

@ -0,0 +1,4 @@
fn double-plus-one n:
n * 2 + 1
[1 2 3 4] -> * double-plus-one -> print

View file

@ -0,0 +1,2 @@
;; apply a named function, inc
(map inc [1 2 3 4])

View file

@ -0,0 +1,2 @@
;; apply a function
(map (fn [x] (* x x)) [1 2 3 4])

View file

@ -0,0 +1,2 @@
;; shortcut syntax for a function
(map #(* % %) [1 2 3 4])

View file

@ -0,0 +1,4 @@
map = (arr, f) -> (f(e) for e in arr)
arr = [1, 2, 3, 4, 5]
f = (x) -> x * x
console.log map arr, f # prints [1, 4, 9, 16, 25]

View file

@ -0,0 +1 @@
(map nil #'print #(1 2 3 4 5))

View file

@ -0,0 +1,2 @@
(defun square (x) (* x x))
(map 'vector #'square #(1 2 3 4 5))

View file

@ -0,0 +1,2 @@
(defvar *a* (vector 1 2 3))
(map-into *a* #'1+ *a*)

View file

@ -0,0 +1,54 @@
MODULE Callback;
IMPORT StdLog;
TYPE
Callback = PROCEDURE (x: INTEGER;OUT doubled: INTEGER);
Callback2 = PROCEDURE (x: INTEGER): INTEGER;
PROCEDURE Apply(proc: Callback; VAR x: ARRAY OF INTEGER);
VAR
i: INTEGER;
BEGIN
FOR i := 0 TO LEN(x) - 1 DO;
proc(x[i],x[i]);
END
END Apply;
PROCEDURE Apply2(func: Callback2; VAR x: ARRAY OF INTEGER);
VAR
i: INTEGER;
BEGIN
FOR i := 0 TO LEN(x) - 1 DO;
x[i] := func(x[i]);
END
END Apply2;
PROCEDURE Double(x: INTEGER; OUT y: INTEGER);
BEGIN
y := x * x;
END Double;
PROCEDURE Double2(x: INTEGER): INTEGER;
BEGIN
RETURN x * x
END Double2;
PROCEDURE Do*;
VAR
i: INTEGER;
ary: ARRAY 10 OF INTEGER;
BEGIN
FOR i := 0 TO LEN(ary) - 1 DO ary[i] := i END;
Apply(Double,ary);
FOR i := 0 TO LEN(ary) - 1 DO
StdLog.Int(ary[i]);StdLog.Ln
END;
StdLog.Ln;
Apply2(Double2,ary);
FOR i := 0 TO LEN(ary) - 1 DO
StdLog.Int(ary[i]);StdLog.Ln
END
END Do;
END Callback.

View file

@ -0,0 +1,7 @@
values = [1, 2, 3]
new_values = values.map do |number|
number * 2
end
puts new_values #=> [2, 4, 6]

View file

@ -0,0 +1,12 @@
values = [1, 2, 3]
def double(number)
number * 2
end
# the `->double(Int32)` syntax creates a proc from a function/method. argument types must be specified.
# the `&proc` syntax passes a proc as a block.
# combining the two passes a function/method as a block
new_values = values.map &->double(Int32)
puts new_values #=> [2, 4, 6]

View file

@ -0,0 +1,7 @@
import std.stdio, std.algorithm;
void main() {
auto items = [1, 2, 3, 4, 5];
auto m = items.map!(x => x + 5)();
writeln(m);
}

View file

@ -0,0 +1,17 @@
// Declare the callback function
procedure callback(const AInt:Integer);
begin
WriteLn(AInt);
end;
const
// Declare a static array
myArray:Array[0..4] of Integer=(1,4,6,8,7);
var
// Declare interator variable
i:Integer;
begin
// Iterate the array and apply callback
for i:=0 to length(myArray)-1 do
callback(myArray[i]);
end.

View file

@ -0,0 +1,12 @@
func Array.Select(pred) {
let ys = []
for x in this when pred(x) {
ys.Add(x)
}
return ys
}
var arr = [1, 2, 3, 4, 5]
var squares = arr.Select(x => x * x)
print(squares)

View file

@ -0,0 +1,4 @@
def array := [1,2,3,4,5]
def square(value) {
return value * value
}

View file

@ -0,0 +1,4 @@
def callback(index, value) {
println(`Item $index is $value.`)
}
array.iterate(callback)

View file

@ -0,0 +1,8 @@
def map(func, collection) {
def output := [].diverge()
for item in collection {
output.push(func(item))
}
return output.snapshot()
}
println(map(square, array))

View file

@ -0,0 +1,20 @@
delegate callback( i int ) returns( int ) end
program ApplyCallbackToArray
function main()
values int[] = [ 1, 2, 3, 4, 5 ];
func callback = square;
for ( i int to values.getSize() )
values[ i ] = func( values[ i ] );
end
for ( i int to values.getSize() )
SysLib.writeStdout( values[ i ] );
end
end
function square( i int ) returns( int )
return( i * i );
end
end

View file

@ -0,0 +1,24 @@
PROGRAM CALLBACK
!
! for rosettacode.org
!
DIM A[5]
FUNCTION CBACK(X)
CBACK=2*X-1
END FUNCTION
PROCEDURE PROCMAP(ZETA,DUMMY(X)->OUTP)
OUTP=DUMMY(ZETA)
END PROCEDURE
BEGIN
A[1]=1 A[2]=2 A[3]=3 A[4]=4 A[5]=5
FOR I%=1 TO 5 DO
PROCMAP(A[I%],CBACK(X)->OUTP)
PRINT(OUTP;)
END FOR
PRINT
END PROGRAM

View file

@ -0,0 +1,11 @@
(vector-map sqrt #(0 4 16 49))
→ #( 0 2 4 7)
;; or
(map exp #(0 1 2))
→ #( 1 2.718281828459045 7.38905609893065)
;; or
(for/vector ([elem #(2 3 4)] [i (in-naturals)]) (printf "v[%d] = %a" i elem) (* elem elem))
v[0] = 2
v[1] = 3
v[2] = 4
→ #( 4 9 16)

View file

@ -0,0 +1,32 @@
square = fn (N) {
N * N
}
# list comprehension
squares1 = fn (Numbers) {
[square(N) for N in Numbers]
}
# functional form
squares2a = fn (Numbers) {
lists.map(fn square:1, Numbers)
}
# functional form with lambda
squares2b = fn (Numbers) {
lists.map(fn (N) { N * N }, Numbers)
}
# no need for a function
squares3 = fn (Numbers) {
[N * N for N in Numbers]
}
@public
run = fn () {
Numbers = [1, 3, 5, 7]
io.format("squares1 : ~p~n", [squares1(Numbers)])
io.format("squares2a: ~p~n", [squares2a(Numbers)])
io.format("squares2b: ~p~n", [squares2b(Numbers)])
io.format("squares3 : ~p~n", [squares3(Numbers)])
}

View file

@ -0,0 +1,8 @@
import system'routines;
PrintSecondPower(n){ console.writeLine(n * n) }
public program()
{
new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}.forEach:PrintSecondPower
}

View file

@ -0,0 +1,2 @@
Enum.map([1, 2, 3], fn(n) -> n * 2 end)
Enum.map [1, 2, 3], &(&1 * 2)

View file

@ -0,0 +1,2 @@
1> L = [1,2,3].
[1,2,3]

View file

@ -0,0 +1,2 @@
2> lists:foreach(fun(X) -> io:format("~w ",[X]) end, L).
1 2 3 ok

View file

@ -0,0 +1,2 @@
3> lists:map(fun(X) -> X + 1 end, L).
[2,3,4]

View file

@ -0,0 +1,2 @@
4> lists:foldl(fun(X, Sum) -> X + Sum end, 0, L).
6

View file

@ -0,0 +1,18 @@
function apply_to_all(sequence s, integer f)
-- apply a function to all elements of a sequence
sequence result
result = {}
for i = 1 to length(s) do
-- we can call add1() here although it comes later in the program
result = append(result, call_func(f, {s[i]}))
end for
return result
end function
function add1(atom x)
return x + 1
end function
-- add1() is visible here, so we can ask for its routine id
? apply_to_all({1, 2, 3}, routine_id("add1"))
-- displays {2,3,4}

View file

@ -0,0 +1,2 @@
let evenp x = x % 2 = 0
let result = Array.map evenp [| 1; 2; 3; 4; 5; 6 |]

View file

@ -0,0 +1 @@
let result = Array.map (fun x -> x * x) [|1; 2; 3; 4; 5|]

View file

@ -0,0 +1 @@
Array.iter (fun x -> printfn "%d" x) [|1; 2; 3; 4; 5|]

View file

@ -0,0 +1,17 @@
#APPTYPE CONSOLE
FOREACH DIM e IN MyMap(Add42, {1, 2, 3})
PRINT e, " ";
NEXT
PAUSE
FUNCTION MyMap(f, a)
DIM ret[]
FOREACH DIM e IN a
ret[] = f(e)
NEXT
RETURN ret
END FUNCTION
FUNCTION Add42(n): RETURN n + 42: END FUNCTION

View file

@ -0,0 +1,17 @@
#APPTYPE CONSOLE
DIM languages[] = {{"English", {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}}, _
{"French", {"un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix"}}}
MAP(SpeakALanguage, languages)
PAUSE
SUB NameANumber(lang, nb, number)
PRINT "The number ", nb, " is called ", STRENC(number), " in ", lang
END SUB
SUB SpeakALanguage(lang)
MAP(NameANumber, lang[0], 1 TO 10, lang[1])
PRINT LPAD("", 40, "-")
END SUB

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,10 @@
(= map (fn (f lst)
(let res (cons nil nil))
(let tail res)
(while lst
(setcdr tail (cons (f (car lst)) nil))
(= lst (cdr lst))
(= tail (cdr tail)))
(cdr res)))
(print (map (fn (x) (* x x)) '(1 2 3 4 5 6 7 8 9 10)))

View file

@ -0,0 +1,2 @@
: map ( addr n fn -- )
-rot cells bounds do i @ over execute i ! cell +loop ;

View file

@ -0,0 +1,2 @@
create data 1 , 2 , 3 , 4 , 5 ,
data 5 ' 1+ map \ adds one to each element of data

View file

@ -0,0 +1,9 @@
module arrCallback
contains
elemental function cube( x )
implicit none
real :: cube
real, intent(in) :: x
cube = x * x * x
end function cube
end module arrCallback

View file

@ -0,0 +1,17 @@
program testAC
use arrCallback
implicit none
integer :: i, j
real, dimension(3,4) :: b, &
a = reshape( (/ ((10 * i + j, i = 1, 3), j = 1, 4) /), (/ 3,4 /) )
do i = 1, 3
write(*,*) a(i,:)
end do
b = cube( a ) ! Applies CUBE to every member of a,
! and stores each result in the equivalent element of b
do i = 1, 3
write(*,*) b(i,:)
end do
end program testAC

View file

@ -0,0 +1,14 @@
program test
C
C-- Declare array:
integer a(5)
C
C-- Fill it with Data
data a /45,22,67,87,98/
C
C-- Do something with all elements (in this case: print their squares)
do i=1,5
print *,a(i)*a(i)
end do
C
end

View file

@ -0,0 +1,19 @@
' FB 1.05.0 Win64
Sub PrintEx(n As Integer)
Print n, n * n, n * n * n
End Sub
Sub Proc(a() As Integer, callback As Sub(n As Integer))
For i As Integer = LBound(a) To UBound(a)
callback(i)
Next
End Sub
Dim a(1 To 10) As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Print " n", "n^2", "n^3"
Print " -", "---", "---"
Proc(a(), @PrintEx)
Print
Print "Press any key to quit the program"
Sleep

View file

@ -0,0 +1,3 @@
f = {|x| x^2} // Anonymous function to square input
a = [1,2,3,5,7]
println[map[f, a]]

View file

@ -0,0 +1,3 @@
[1, 2, 3].foreach( println )
[1, 2, 3].foreach( a -> println(2a) )

View file

@ -0,0 +1 @@
map (\x->x+1) [1,2,3] -- [2,3,4]

View file

@ -0,0 +1 @@
map (+1) [1,2,3] -- [2,3,4]

View file

@ -0,0 +1,20 @@
include "NSLog.incl"
void local fn Callback( n as NSInteger )
NSLog( @"Square root of %ld = %f", n, sqr(n) )
end fn
void local fn DoIt
NSUInteger i, count
CFArrayRef array = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10]
count = len(array)
for i = 0 to count -1
fn Callback( fn NumberIntegerValue( array[i] ) )
next
end fn
fn DoIt
HandleEvents

View file

@ -0,0 +1,15 @@
include "NSLog.incl"
void local fn Callback( array as CFArrayRef, obj as CFTypeRef )
long value = intVal(obj)
NSLog( @"Square root of %ld = %f", value, sqr(value) )
end fn
void local fn DoIt
CFArrayRef array = @[@1, @2, @3, @4, @5, @6, @7, @8, @9, @10]
ArrayEnumerateObjects( array, @fn Callback, NULL )
end fn
fn DoIt
HandleEvents

View file

@ -0,0 +1,21 @@
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
List(b, n -> n*n);
# [ 1, 4, 9, 16 ]
# Apply and don't return anything (only side effects)
Perform(b, Display);
1
2
3
4
b;
# [ 1 .. 4 ]

View file

@ -0,0 +1,9 @@
package main
import "fmt"
func main() {
for _, i := range []int{1, 2, 3, 4, 5} {
fmt.Println(i * i)
}
}

View file

@ -0,0 +1,31 @@
package main
import "fmt"
type intSlice []int
func (s intSlice) each(f func(int)) {
for _, i := range s {
f(i)
}
}
func (s intSlice) Map(f func(int) int) intSlice {
r := make(intSlice, len(s))
for j, i := range s {
r[j] = f(i)
}
return r
}
func main() {
s := intSlice{1, 2, 3, 4, 5}
s.each(func(i int) {
fmt.Println(i * i)
})
fmt.Println(s.Map(func(i int) int {
return i * i
}))
}

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,6 @@
# applies add2 (adds 2) to each element
add2 = {
return add(@1, 2)
}
l = {1, 2, 3, 4, 5, 6, 7}
puts each(add2, flat(@l))

View file

@ -0,0 +1,3 @@
let square x = x*x
let values = [1..10]
map square values

View file

@ -0,0 +1 @@
[square x | x <- values]

Some files were not shown because too many files have changed in this diff Show more