This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -3,13 +3,13 @@
#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 ;
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,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,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,22 @@
numbers = [1, 3, 5, 7]
square1 = [square(n) for n in numbers] # list comprehension
squares2a = map(square, numbers) # functional form
squares2b = map(x -> x*x, numbers) # functional form with `lambda`
#There is also extended block form for the map function
squares2c = map(numbers) do x
sum = 0
for i = 1:x
sum += x^2 #trivial, but you get the point
end
return sum
end
squares3 = [n * n for n in numbers] # no need for a function,
squares4 = numbers .* numbers #element-wise operation
squares4a = numbers .^ 2 #includes .+, .-, ./, comparison, and bitwise operations as well

View file

@ -1,7 +1,7 @@
#lang racket
;; using the `for/vector` comprehension form
;; using the `for/vector' comprehension form
(for/vector ([i #(1 2 3 4 5)]) (sqr i))
;; the usual functional `map`
;; the usual functional `map'
(vector-map sqr #(1 2 3 4 5))