Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
1
Task/Apply-a-callback-to-an-array/0DESCRIPTION
Normal file
1
Task/Apply-a-callback-to-an-array/0DESCRIPTION
Normal file
|
|
@ -0,0 +1 @@
|
|||
In this task, the goal is to take a combined set of elements and apply a function to each element.
|
||||
4
Task/Apply-a-callback-to-an-array/1META.yaml
Normal file
4
Task/Apply-a-callback-to-an-array/1META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Iteration
|
||||
note: Basic language learning
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(defun sq-each (xs)
|
||||
(if (endp xs)
|
||||
nil
|
||||
(cons (* (first xs) (first xs))
|
||||
(sq-each (rest xs)))))
|
||||
|
|
@ -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)
|
||||
)
|
||||
|
|
@ -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
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
void
|
||||
map(list l, void (*fp) (object))
|
||||
{
|
||||
integer i;
|
||||
|
||||
i = 0;
|
||||
while (i < l_length(l)) {
|
||||
fp(l_query(l, i));
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
out(object o)
|
||||
{
|
||||
o_integer(o);
|
||||
o_byte(10);
|
||||
}
|
||||
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
list l;
|
||||
|
||||
l_append(l, 0);
|
||||
l_append(l, 1);
|
||||
l_append(l, 2);
|
||||
l_append(l, 3);
|
||||
|
||||
map(l, out);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
map("callback", "3,4,5")
|
||||
|
||||
callback(array){
|
||||
Loop, Parse, array, `,
|
||||
MsgBox % (2 * A_LoopField)
|
||||
}
|
||||
|
||||
map(callback, array){
|
||||
%callback%(array)
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
;; apply a named function, inc
|
||||
(map inc [1 2 3 4])
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
;; apply a function
|
||||
(map (fn [x] (* x x)) [1 2 3 4])
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
;; shortcut syntax for a function
|
||||
(map #(* % %) [1 2 3 4])
|
||||
|
|
@ -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]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
1> L = [1,2,3].
|
||||
[1,2,3]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
2> lists:foreach(fun(X) -> io:format("~w ",[X]) end, L).
|
||||
1 2 3 ok
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
3> lists:map(fun(X) -> X + 1 end, L).
|
||||
[2,3,4]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
4> lists:foldl(fun(X, Sum) -> X + Sum end, 0, L).
|
||||
6
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: map ( addr n fn -- )
|
||||
-rot cells bounds do i @ over execute i ! cell +loop ;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
create data 1 , 2 , 3 , 4 , 5 ,
|
||||
data 5 ' 1+ map \ adds one to each element of data
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
for _, i := range []int{1, 2, 3, 4, 5} {
|
||||
fmt.Println(i * i)
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
}))
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
let square x = x*x
|
||||
let values = [1..10]
|
||||
map square values
|
||||
|
|
@ -0,0 +1 @@
|
|||
[square x | x <- values]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
let printSquares = mapM_ (print.square)
|
||||
printSquares values
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
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
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
interface IntToVoid {
|
||||
void run(int x);
|
||||
}
|
||||
|
||||
for (int z : myIntArray) {
|
||||
new IntToVoid() {
|
||||
public void run(int x) {
|
||||
System.out.println(x);
|
||||
}
|
||||
}.run(z);
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
interface IntToInt {
|
||||
int run(int x);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function map(a, func) {
|
||||
for (var i in a)
|
||||
a[i] = func(a[i]);
|
||||
}
|
||||
|
||||
var a = [1, 2, 3, 4, 5];
|
||||
map(a, function(v) { return v * v; });
|
||||
|
|
@ -0,0 +1 @@
|
|||
var a = (1).to(10).collect(Math.pow.curry(undefined,2));
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
function cube(num) {
|
||||
return Math.pow(num, 3);
|
||||
}
|
||||
|
||||
var numbers = [1, 2, 3, 4, 5];
|
||||
|
||||
// get results of calling cube on every element
|
||||
var cubes1 = numbers.map(cube);
|
||||
|
||||
// display each result in a separate dialog
|
||||
cubes1.forEach(alert);
|
||||
|
||||
// array comprehension
|
||||
var cubes2 = [cube(n) for each (n in numbers)];
|
||||
var cubes3 = [n * n * n for each (n in numbers)];
|
||||
|
|
@ -0,0 +1 @@
|
|||
Functional.map('x*x*x', [1,2,3,4,5])
|
||||
|
|
@ -0,0 +1 @@
|
|||
myArray = {1, 2, 3, 4, 5}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
map = function(f, data)
|
||||
local result = {}
|
||||
for k,v in ipairs(data) do
|
||||
result[k] = f(v)
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
myFunc = function(x) return x*x end
|
||||
|
||||
print(unpack( map(myFunc, myArray) ))
|
||||
--> 1 4 9 16 25
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
function cube($n)
|
||||
{
|
||||
return($n * $n * $n);
|
||||
}
|
||||
|
||||
$a = array(1, 2, 3, 4, 5);
|
||||
$b = array_map("cube", $a);
|
||||
print_r($b);
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
# create array
|
||||
my @a = (1, 2, 3, 4, 5);
|
||||
|
||||
# create callback function
|
||||
sub mycallback {
|
||||
return 2 * shift;
|
||||
}
|
||||
|
||||
# use array indexing
|
||||
for (my $i = 0; $i < scalar @a; $i++) {
|
||||
print "mycallback($a[$i]) = ", mycallback($a[$i]), "\n";
|
||||
}
|
||||
|
||||
# using foreach
|
||||
foreach my $x (@a) {
|
||||
print "mycallback($x) = ", mycallback($x), "\n";
|
||||
}
|
||||
|
||||
# using map (useful for transforming an array)
|
||||
my @b = map mycallback($_), @a; # @b is now (2, 4, 6, 8, 10)
|
||||
|
||||
# and the same using an anonymous function
|
||||
my @c = map { $_ * 2 } @a; # @c is now (2, 4, 6, 8, 10)
|
||||
|
||||
# use a callback stored in a variable
|
||||
my $func = \&mycallback;
|
||||
my @d = map $func->($_), @a; # @d is now (2, 4, 6, 8, 10)
|
||||
|
||||
# filter an array
|
||||
my @e = grep { $_ % 2 == 0 } @a; # @e is now (2, 4)
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
: (mapc println (1 2 3 4 5)) # Print numbers
|
||||
1
|
||||
2
|
||||
3
|
||||
4
|
||||
5
|
||||
-> 5
|
||||
|
||||
: (mapcar '((N) (* N N)) (1 2 3 4 5)) # Calculate squares
|
||||
-> (1 4 9 16 25)
|
||||
|
||||
: (mapcar ** (1 2 3 4 5) (2 .)) # Same, using a circular list
|
||||
-> (1 4 9 16 25)
|
||||
|
||||
: (mapcar if '(T NIL T NIL) '(1 2 3 4) '(5 6 7 8)) # Conditional function
|
||||
-> (1 6 3 8)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
?- assert((fun(X, Y) :- Y is 2 * X)).
|
||||
true.
|
||||
|
||||
?- maplist(fun, [1,2,3,4,5], L).
|
||||
L = [2,4,6,8,10].
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
def square(n):
|
||||
return n * n
|
||||
|
||||
numbers = [1, 3, 5, 7]
|
||||
|
||||
squares1 = [square(n) for n in numbers] # list comprehension
|
||||
|
||||
squares2a = map(square, numbers) # functional form
|
||||
|
||||
squares2b = map(lambda x: x*x, numbers) # functional form with `lambda`
|
||||
|
||||
squares3 = [n * n for n in numbers] # no need for a function,
|
||||
# anonymous or otherwise
|
||||
|
||||
isquares1 = (n * n for n in numbers) # iterator, lazy
|
||||
|
||||
import itertools
|
||||
isquares2 = itertools.imap(square, numbers) # iterator, lazy
|
||||
|
|
@ -0,0 +1 @@
|
|||
print " ".join(str(n * n) for n in range(10))
|
||||
|
|
@ -0,0 +1 @@
|
|||
print " ".join(map(str, map(square, range(10))))
|
||||
|
|
@ -0,0 +1 @@
|
|||
0 1 4 9 16 25 36 49 64 81
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
cube <- function(x) x*x*x
|
||||
elements <- 1:5
|
||||
cubes <- cube(elements)
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
cubes <- numeric(5)
|
||||
for(i in seq_along(cubes))
|
||||
{
|
||||
cubes[i] <- cube(elements[i])
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
elements2 <- list(1,2,3,4,5)
|
||||
cubes <- sapply(elements2, cube)
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/*REXX program to apply a callback to a stemmed (REXX) array. */
|
||||
a.=; b.=
|
||||
a.0= 0
|
||||
a.1= 1
|
||||
a.2= 2
|
||||
a.3= 3
|
||||
a.4= 4
|
||||
a.5= 5
|
||||
a.6= 6
|
||||
a.7= 7
|
||||
a.8= 8
|
||||
a.9= 9
|
||||
a.10=10
|
||||
|
||||
call listab 'before'
|
||||
call bangit 'a','b' /*factorialize the A array, store results in B */
|
||||
call listab ' after'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────BANGIT subroutine───────────────────*/
|
||||
bangit: do i=0
|
||||
_=value(arg(1)'.'i); if _=='' then return
|
||||
call value arg(2)'.'i,fact(_)
|
||||
end /*i*/
|
||||
/*──────────────────────────────────FACT subroutine─────────────────────*/
|
||||
fact: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !
|
||||
/*──────────────────────────────────LISTAB subroutine───────────────────*/
|
||||
listab: do j=0 while a.j\==''; say arg(1) 'a.'j"="a.j; end /*j*/
|
||||
say; do k=0 while b.k\==''; say arg(1) 'b.'k"="b.k; end /*k*/
|
||||
return
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#lang racket
|
||||
|
||||
;; using the `for/vector` comprehension form
|
||||
(for/vector ([i #(1 2 3 4 5)]) (sqr i))
|
||||
|
||||
;; the usual functional `map`
|
||||
(vector-map sqr #(1 2 3 4 5))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for i in [1,2,3,4,5] do
|
||||
puts i**2
|
||||
end
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1,2,3,4,5].each{ |i| puts i**2 }
|
||||
|
|
@ -0,0 +1 @@
|
|||
[1,2,3,4,5].map{ |i| i**2 }
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
class MAIN is
|
||||
do_something(i:INT):INT is
|
||||
return i * i;
|
||||
end;
|
||||
|
||||
main is
|
||||
a:ARRAY{INT} := |1, 2, 3, 4, 5|;
|
||||
-- we use an anonymous closure to apply our do_something "callback"
|
||||
a.map(bind(do_something(_)));
|
||||
loop #OUT + a.elt! + "\n"; end;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
val l = List(1,2,3,4)
|
||||
l.foreach {i => println(i)}
|
||||
|
|
@ -0,0 +1 @@
|
|||
l.foreach(println(_))
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
val a = Array(1,2,3,4)
|
||||
a.foreach {i => println(i)}
|
||||
a.foreach(println(_)) '' // same as previous line''
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
def doSomething(in: int) = {println("Doing something with "+in)}
|
||||
l.foreach(doSomething)
|
||||
|
|
@ -0,0 +1 @@
|
|||
for(val i <- a) println(i)
|
||||
|
|
@ -0,0 +1 @@
|
|||
val squares = l.map{i => i * i} ''//squares is'' List(1,4,9,16)
|
||||
|
|
@ -0,0 +1 @@
|
|||
val squares = for (val i <- l) yield i * i
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(define (square n) (* n n))
|
||||
(define x #(1 2 3 4 5))
|
||||
(map square (vector->list x))
|
||||
|
|
@ -0,0 +1 @@
|
|||
(map (lambda (n) (* n n)) '(1 2 3 4 5))
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(define (map f L)
|
||||
(if (null? L)
|
||||
L
|
||||
(cons (f (car L)) (map f (cdr L)))))
|
||||
|
|
@ -0,0 +1 @@
|
|||
#( 1 2 3 4 5 ) collect: [:n | n * n].
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
foreach var $dat {
|
||||
myfunc $var
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
foreach name [array names dat] {
|
||||
myfunc $dat($name)
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
proc map {f list} {
|
||||
set res {}
|
||||
foreach e $list {lappend res [$f $e]}
|
||||
return $res
|
||||
}
|
||||
proc square x {expr {$x*$x}}
|
||||
|
||||
% map square {1 2 3 4 5}
|
||||
1 4 9 16 25
|
||||
Loading…
Add table
Add a link
Reference in a new issue