This commit is contained in:
Ingy döt Net 2013-04-10 16:57:12 -07:00
parent 518da4a923
commit 764da6cbbb
6144 changed files with 83610 additions and 11 deletions

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( intVec.begin( ) , intVec.end( ) , 1 ) ;//fill the vector
std::transform( intVec.begin( ) , intVec.end( ) , intVec.begin( ) ,
[ ] ( int i ) { return i * i ; } ) ; //transform it with closures
std::copy( intVec.begin( ) , intVec.end( ) ,
std::ostream_iterator<int> ( std::cout , " " ) ) ;
std::cout << std::endl ;
return 0 ;
}

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

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 @@
(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,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,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,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,11 @@
#define std'patterns'*.
#define std'routines'*.
#define std'dictionary'*.
#symbol PrintSquarePower: aNumber
= 'program'output << aNumber * aNumber << "%n".
#symbol Program =
[
Scan::(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) run:(#symbol PrintSquarePower).
].

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}