September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,35 +1,21 @@
void
map(list l, void (*fp) (object))
map(list l, void (*fp)(object))
{
integer i;
i = 0;
while (i < l_length(l)) {
fp(l[i]);
i += 1;
}
l_ucall(l, fp, 0);
}
void
out(object o)
{
o_integer(o);
o_byte(10);
o_(o, "\n");
}
integer
main(void)
{
list l;
l_append(l, 0);
l_append(l, 1);
l_append(l, 2);
l_append(l, 3);
map(l, out);
map(l_effect(0, 1, 2, 3), out);
return 0;
}

View file

@ -1,9 +0,0 @@
((main
{ [val 1 1 2 3 5 8 13 21]
{double !} eachar
collect !
{%d ' ' . <<} each})
(double { 2 * })
(collect { -1 take }))

View file

@ -1,34 +1,11 @@
using System;
int[] intArray = { 1, 2, 3, 4, 5 };
// Simplest method: LINQ, functional
int[] squares1 = intArray.Select(x => x * x).ToArray();
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
// Slightly fancier: LINQ, query expression
int[] squares2 = (from x in intArray
select x * x).ToArray();
[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);
}
}
// 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

@ -1,2 +1,34 @@
int[] intArray = { 1, 2, 3, 4, 5 };
Array.ForEach(intArray, i => Console.WriteLine(i * i));
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

@ -1,10 +1,6 @@
#define system.
#define system'routines.
import system'routines.
#symbol PrintSecondPower =
(:n) [ console writeLine:(n * n) ].
#symbol program =
program =
[
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) run &each:PrintSecondPower.
(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) forEach(:n) [ console writeLine(n * n) ].
].

View file

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

View file

@ -1,2 +1 @@
let printSquares = mapM_ (print.square)
printSquares values
[1 .. 10] >>= pure . (^ 2)

View file

@ -1,4 +1 @@
import Data.Array.IArray
let square x = x*x
let values = array (1,10) [(i,i)|i <- [1..10]] :: Array Int Int
amap square values
(^ 2) <$> [1..10]

View file

@ -0,0 +1,2 @@
let printSquares = mapM_ (print.square)
printSquares values

View file

@ -0,0 +1,10 @@
import Data.Array (Array, listArray)
square :: Int -> Int
square x = x * x
values :: Array Int Int
values = listArray (1, 10) [1 .. 10]
main :: IO ()
main = print $ fmap square values

View file

@ -1,11 +1,45 @@
interface IntToVoid {
void run(int x);
}
public class ArrayCallback7 {
for (int z : myIntArray) {
new IntToVoid() {
public void run(int x) {
System.out.println(x);
interface IntConsumer {
void run(int x);
}
interface IntToInt {
int run(int x);
}
static void forEach(int[] arr, IntConsumer consumer) {
for (int i : arr) {
consumer.run(i);
}
}.run(z);
}
static void update(int[] arr, IntToInt mapper) {
for (int i = 0; i < arr.length; i++) {
arr[i] = mapper.run(arr[i]);
}
}
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
forEach(numbers, new IntConsumer() {
public void run(int x) {
System.out.println(x);
}
});
update(numbers, new IntToInt() {
@Override
public int run(int x) {
return x * x;
}
});
forEach(numbers, new IntConsumer() {
public void run(int x) {
System.out.println(x);
}
});
}
}

View file

@ -1,13 +1,17 @@
interface IntToInt {
int run(int x);
}
import java.util.Arrays;
int[] result = new int[myIntArray.length];
for (int i = 0; i < myIntArray.length; i++) {
result[i] =
new IntToInt() {
public int run(int x) {
return x * x;
}
}.run(myIntArray[i]);
public class ArrayCallback {
public static void main(String[] args) {
int[] myIntArray = {1, 2, 3, 4, 5};
int sum = Arrays.stream(myIntArray)
.map(x -> {
int cube = x * x * x;
System.out.println(cube);
return cube;
})
.reduce(0, (left, right) -> left + right); // <-- could substitute .sum() for .reduce(...) here.
System.out.println("sum: " + sum);
}
}

View file

@ -1 +0,0 @@
Functional.map('x*x*x', [1,2,3,4,5])

View file

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

View file

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

View file

@ -1,5 +1,5 @@
fun main(args: Array<String>) {
val array = arrayOf(1,2,3,4,5,6,7,8,9,10) // build
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

View file

@ -1,2 +1,2 @@
var arr = [1,2,3,4]
arr.map proc(some: var int) = echo(some, " squared = ", some*some)
var arr = @[1,2,3,4]
arr.apply proc(some: var int) = echo(some, " squared = ", some*some)

View file

@ -1,2 +0,0 @@
var arr = [1,2,3,4]
arr.map proc(some: var int) = echo(some, " squared = ", some*some)

View file

@ -0,0 +1,5 @@
(for-each
(lambda (element)
(display element))
'(1 2 3 4 5))
; ==> 12345

View file

@ -0,0 +1,24 @@
start = .array~of("Rick", "Mike", "David", "Mark")
new = map(start, .routines~reversit)
call map new, .routines~sayit
-- a function to perform an iterated callback over an array
-- using the provided function. Returns an array containing
-- each function result
::routine map
use strict arg array, function
resultArray = .array~new(array~items)
do item over array
resultArray~append(function~call(item))
end
return resultArray
::routine reversit
use arg string
return string~reverse
::routine sayit
use arg string
say string
return .true -- called as a function, so a result is required

View file

@ -1 +1 @@
func callback(i) { say i**2 };
func callback(i) { say i**2 }

View file

@ -1 +1 @@
[1,2,3,4].each(callback);
[1,2,3,4].each(callback)

View file

@ -1 +1 @@
[1,2,3,4].each{|i| say i**2 };
[1,2,3,4].each{|i| say i**2 }

View file

@ -1 +1 @@
[1,2,3,4,5].map{|i| i**2 };
[1,2,3,4,5].map{|i| i**2 }

View file

@ -0,0 +1,22 @@
BEGIN
! APPLIES A CALLBACK FUNCTION TO AN ARRAY ;
PROCEDURE APPLY(ARR, FUN);
REAL ARRAY ARR;
PROCEDURE FUN IS REAL PROCEDURE FUN(X); REAL X;;
BEGIN
INTEGER I;
FOR I := LOWERBOUND(ARR, 1) STEP 1 UNTIL UPPERBOUND(ARR, 1) DO
ARR(I) := FUN(ARR(I));
END APPLY;
! CALLBACK ;
REAL PROCEDURE SQUARE(X); REAL X; SQUARE := X * X;
REAL ARRAY A(1:5);
INTEGER I;
FOR I := 1 STEP 1 UNTIL 5 DO A(I) := I;
APPLY(A, SQUARE);
FOR I := 1 STEP 1 UNTIL 5 DO OUTFIX(A(I), 2, 8); OUTIMAGE;
END.

View file

@ -1 +1 @@
[1, 2, 3].collect({ | x | x * x })
[1, 2, 3].collect { |x| x * x }

View file

@ -0,0 +1 @@
L(1,2,3,4,5).apply('+(5))