langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,14 @@
/* NetRexx */
options replace format comments java crossref savelog symbols
surname = 'Unknown' -- default value
surname['Fred'] = 'Bloggs'
surname['Davy'] = 'Jones'
try = 'Fred'
say surname[try] surname['Bert']
-- extract the keys
loop fn over surname
say fn.right(10) ':' surname[fn]
end fn

View file

@ -0,0 +1,15 @@
#!/usr/bin/env ocaml
let map = [| ('A', 1); ('B', 2); ('C', 3) |] ;;
(* iterate over pairs *)
Array.iter (fun (k,v) -> Printf.printf "key: %c - value: %d\n" k v) map ;;
(* iterate over keys *)
Array.iter (fun (k,_) -> Printf.printf "key: %c\n" k) map ;;
(* iterate over values *)
Array.iter (fun (_,v) -> Printf.printf "value: %d\n" v) map ;;
(* in functional programming it is often more useful to fold over the elements *)
Array.fold_left (fun acc (k,v) -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) "Elements:\n" map ;;

View file

@ -0,0 +1,10 @@
let map = Hashtbl.create 42;;
Hashtbl.add map 'A' 1;;
Hashtbl.add map 'B' 2;;
Hashtbl.add map 'C' 3;;
(* iterate over pairs *)
Hashtbl.iter (fun k v -> Printf.printf "key: %c - value: %d\n" k v) map ;;
(* in functional programming it is often more useful to fold over the elements *)
Hashtbl.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;

View file

@ -0,0 +1,11 @@
module CharMap = Map.Make (Char);;
let map = CharMap.empty;;
let map = CharMap.add 'A' 1 map;;
let map = CharMap.add 'B' 2 map;;
let map = CharMap.add 'C' 3 map;;
(* iterate over pairs *)
CharMap.iter (fun k v -> Printf.printf "key: %c - value: %d\n" k v) map ;;
(* in functional programming it is often more useful to fold over the elements *)
CharMap.fold (fun k v acc -> acc ^ Printf.sprintf "key: %c - value: %d\n" k v) map "Elements:\n" ;;

View file

@ -0,0 +1,25 @@
class Iteration {
function : Main(args : String[]) ~ Nil {
assoc_array := Collection.StringMap->New();
assoc_array->Insert("Hello", IntHolder->New(1));
assoc_array->Insert("World", IntHolder->New(2));
assoc_array->Insert("!", IntHolder->New(3));
keys := assoc_array->GetKeys();
values := assoc_array->GetValues();
each(i : keys) {
key := keys->Get(i)->As(String);
value := assoc_array->Find(key)->As(IntHolder)->Get();
"key={$key}, value={$value}"->PrintLine();
};
"-------------"->PrintLine();
each(i : keys) {
key := keys->Get(i)->As(String);
value := values->Get(i)->As(IntHolder)->Get();
"key={$key}, value={$value}"->PrintLine();
};
}
}

View file

@ -0,0 +1,14 @@
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:71], @"!", nil];
// iterating over keys:
for (id key in myDict) {
NSLog(@"key = %@", key);
}
// iterating over values:
for (id value in [myDict objectEnumerator]) {
NSLog(@"value = %@", value);
}

View file

@ -0,0 +1,18 @@
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:71], @"!", nil];
// iterating over keys:
NSEnumerator *enm = [myDict keyEnumerator];
id key;
while ((key = [enm nextObject])) {
NSLog(@"key = %@", key);
}
// iterating over values:
enm = [myDict objectEnumerator];
id value;
while ((value = [enm nextObject])) {
NSLog(@"value = %@", value);
}

View file

@ -0,0 +1,9 @@
NSDictionary *myDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:13], @"hello",
[NSNumber numberWithInt:31], @"world",
[NSNumber numberWithInt:71], @"!", nil];
// iterating over keys and values:
[myDict enumerateKeysAndObjectsUsingBlock: ^(id key, id value, BOOL *stop) {
NSLog(@"key = %@, value = %@", key, value);
}];

View file

@ -0,0 +1,6 @@
declare
MyMap = unit('hello':13 'world':31 '!':71)
in
{ForAll {Record.toListInd MyMap} Show} %% pairs
{ForAll {Record.arity MyMap} Show} %% keys
{ForAll {Record.toList MyMap} Show} %% values

View file

@ -0,0 +1,11 @@
my %pairs = hello => 13, world => 31, '!' => 71;
for %pairs.kv -> $k, $v {
say "(k,v) = ($k, $v)";
}
{ say "$^a => $^b" } for %pairs.kv;
say "key = $_" for %pairs.keys;
say "value = $_" for %pairs.values;

View file

@ -0,0 +1,20 @@
mapping(string:string) m = ([ "A":"a", "B":"b", "C":"c" ]);
foreach(m; string key; string value)
{
write(key+value);
}
Result: BbAaCc
// only keys
foreach(m; string key;)
{
write(key);
}
Result: BAC
// only values
foreach(m;; string value)
{
write(value);
}
Result: bac

View file

@ -0,0 +1,6 @@
% over keys and values
<</a 1 /b 2 /c 3>> {= =} forall
% just keys
<</a 1 /b 2 /c 3>> {= } forall
% just values
<</a 1 /b 2 /c 3>> {pop =} forall

View file

@ -0,0 +1 @@
$h = @{ 'a' = 1; 'b' = 2; 'c' = 3 }

View file

@ -0,0 +1 @@
$h.GetEnumerator() | ForEach-Object { Write-Host Key: $_.Name, Value: $_.Value }

View file

@ -0,0 +1,3 @@
foreach ($e in $h.GetEnumerator()) {
Write-Host Key: $e.Name, Value: $e.Value
}

View file

@ -0,0 +1,5 @@
$h.Keys | ForEach-Object { Write-Host Key: $_ }
foreach ($k in $h.Keys) {
Write-Host Key: $k
}

View file

@ -0,0 +1,5 @@
$h.Values | ForEach-Object { Write-Host Value: $_ }
foreach ($v in $h.Values) {
Write-Host Value: $v
}

View file

@ -0,0 +1,8 @@
NewMap dict.s()
dict("de") = "German"
dict("en") = "English"
dict("fr") = "French"
ForEach dict()
Debug MapKey(dict()) + ":" + dict()
Next

View file

@ -0,0 +1,22 @@
x = <<>>; // create an empty list
x.hello = 1;
x.world = 2;
x.["!"] = 3;
// to iterate over identifiers of a list one needs to use the function ''members''
// the identifiers are returned as a lexicographically ordered string row-vector
// here ["!", "hello", "world"]
for(i in members(x))
{ printf("%s %g\n", i, x.[i]); }
// occasionally one needs to check if there exists member of a list
y = members(x); // y contains ["!", "hello", "world"]
clear(x.["!"]); // remove member with identifier "!" from the list "x"
for(i in y)
{ printf("%s %g\n", i, x.[i]); } // this produces error because x.["!"] does not exist
for(i in y)
{
if (exist(x.[i]))
{ printf("%s %g\n", i, x.[i]); } // we print a member of the list "x" only if it exists
}

View file

@ -0,0 +1,16 @@
* # Create sample table
t = table()
t<'cat'> = 'meow'
t<'dog'> = 'woof'
t<'pig'> = 'oink'
* # Convert table to key/value array
a = convert(t,'array')
* # Iterate pairs
ploop i = i + 1; output = a<i,1> ' -> ' a<i,2> :s(ploop)
* # Iterate keys
kloop j = j + 1; output = a<j,1> :s(kloop)
* # Iterate vals
vloop k = k + 1; output = a<k,2> :s(vloop)
end

View file

@ -0,0 +1,29 @@
$ include "seed7_05.s7i";
const type: dictType is hash [string] integer;
var dictType: myDict is dictType.value;
const proc: main is func
local
var string: stri is "";
var integer: number is 0;
begin
myDict @:= ["hello"] 1;
myDict @:= ["world"] 2;
myDict @:= ["!"] 3;
# iterating over key-value pairs:
for number key stri range myDict do
writeln("key = " <& number <& ", value = " <& stri);
end for;
# iterating over keys:
for key stri range myDict do
writeln("key = " <& stri);
end for;
# iterating over values:
for number range myDict do
writeln("value = " <& number);
end for;
end func;

View file

@ -0,0 +1,12 @@
define: #pairs -> ({'hello' -> 1. 'world' -> 2. '!' -> 3. 'another!' -> 3} as: Dictionary).
pairs keysAndValuesDo: [| :key :value |
inform: '(k, v) = (' ; key printString ; ', ' ; value printString ; ')'
].
pairs keysDo: [| :key |
inform: '(k, v) = (' ; key printString ; ', ' ; (pairs at: key) printString ; ')'
].
pairs do: [| :value |
inform: 'value = ' ; value printString
].

View file

@ -0,0 +1,6 @@
@(do (defvar *h* (make-hash nil nil nil))
(each ((k '(a b c))
(v '(1 2 3)))
(set [*h* k nil] v))
(dohash (k v *h*)
(format t "~a -> ~a\n" k v))))

View file

@ -0,0 +1,12 @@
a=([key1]=value1 [key2]=value2)
# just keys
printf '%s\n' "${!a[@]}"
# just values
printf '%s\n' "${a[@]}"
# keys and values
for key in "${!a[@]}"; do
printf '%s => %s\n' "$key" "${a[$key]}"
done

View file

@ -0,0 +1,11 @@
typeset -A a
a=(key1 value1 key2 value2)
# just keys
print -l -- ${(k)a}
# just values
print -l -- ${(v)a}
# keys and values
printf '%s => %s\n' ${(kv)a}

View file

@ -0,0 +1,29 @@
using Gee;
void main(){
// declare HashMap
var map = new HashMap<string, double?>();
// set 3 entries
map["pi"] = 3.14;
map["e"] = 2.72;
map["golden"] = 1.62;
// iterate over (key,value) pair
foreach (var elem in map.entries){
string name = elem.key;
double num = elem.value;
stdout.printf("%s,%f\n", name, num);
}
// iterate over keys
foreach (string key in map.keys){
stdout.printf("%s\n", key);
}
// iterate over values
foreach (double num in map.values){
stdout.printf("%f\n", num);
}
}

View file

@ -0,0 +1,20 @@
include c:\cxpl\stdlib;
char Dict(10,10);
int Entries;
proc AddEntry(Letter, Greek); \Insert entry into associative array
char Letter, Greek;
[Dict(Entries,0):= Letter;
StrCopy(Greek, @Dict(Entries,1));
Entries:= Entries+1; \(limit checks ignored for simplicity)
];
int I;
[Entries:= 0;
AddEntry(^A, "alpha");
AddEntry(^D, "delta");
AddEntry(^B, "beta");
AddEntry(^C, "gamma");
for I:= 0 to Entries-1 do
[ChOut(0, Dict(I,0)); ChOut(0, ^ ); Text(0, @Dict(I,1)); CrLf(0)];
]