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,12 @@
std::map<std::string, int> myDict;
myDict["hello"] = 1;
myDict["world"] = 2;
myDict["!"] = 3;
// iterating over key-value pairs:
for (std::map<std::string, int>::iterator it = myDict.begin(); it != myDict.end(); it++) {
// the thing pointed to by the iterator is a pair<std::string, int>
std::string key = it->first;
int value = it->second;
std::cout << "key = " << key << ", value = " << value << std::endl;
}

View file

@ -0,0 +1,21 @@
#include <map>
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main()
{
map<string, int> myDict;
myDict["hello"] = 1;
myDict["world"] = 2;
myDict["!"] = 3;
for_each(myDict.begin(), myDict.end(),
[](const pair<string,int>& p)
{
cout << "key = " << p.first << ", value = " << p.second << endl;
});
return 0;
}

View file

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
namespace AssocArrays
{
class Program
{
static void Main(string[] args)
{
Dictionary<string,int> assocArray = new Dictionary<string,int>();
assocArray["Hello"] = 1;
assocArray.Add("World", 2);
assocArray["!"] = 3;
foreach (KeyValuePair<string, int> kvp in assocArray)
{
Console.WriteLine(kvp.Key + " : " + kvp.Value);
}
foreach (string key in assocArray.Keys)
{
Console.WriteLine(key);
}
foreach (int val in assocArray.Values)
{
Console.WriteLine(val.ToString());
}
}
}
}

View file

@ -0,0 +1,8 @@
;; iterate using dolist, destructure manually
(dolist (pair alist)
(destructuring-bind (key . value) pair
(format t "~&Key: ~a, Value: ~a." key value)))
;; iterate and destructure with loop
(loop for (key . value) in alist
do (format t "~&Key: ~a, Value: ~a." key value))

View file

@ -0,0 +1,2 @@
(loop for (key value) on plist :by 'cddr
do (format t "~&Key: ~a, Value: ~a." key value))

View file

@ -0,0 +1,3 @@
(maphash (lambda (key value)
(format t "~&Key: ~a, Value: ~a." key value))
hash-table)

View file

@ -0,0 +1,2 @@
(loop for key being each hash-key of hash-table using (hash-value value)
do (format t "~&Key: ~a, Value: ~a." key value))

View file

@ -0,0 +1,6 @@
(with-hash-table-iterator (next-entry hash-table)
(loop
(multiple-value-bind (nextp key value) (next-entry)
(if (not nextp)
(return)
(format t "~&Key: ~a, Value: ~a." key value)))))

View file

@ -0,0 +1,40 @@
import std.stdio: writeln;
void main() {
// the associative array
auto aa = ["alice":2, "bob":97, "charlie":45];
// how to iterate key/value pairs:
foreach (key, value; aa)
writeln("1) Got key ", key, " with value ", value);
writeln();
// how to iterate the keys:
foreach (key, _; aa)
writeln("2) Got key ", key);
writeln();
// how to iterate the values:
foreach (value; aa)
writeln("3) Got value ", value);
writeln();
// how to extract the values, lazy:
foreach (value; aa.byValue())
writeln("4) Got value ", value);
writeln();
// how to extract the keys, lazy:
foreach (key; aa.byKey())
writeln("5) Got key ", key);
writeln();
// how to extract all the keys:
foreach (key; aa.keys)
writeln("6) Got key ", key);
writeln();
// how to extract all the values:
foreach (value; aa.values)
writeln("7) Got value ", value);
}

View file

@ -0,0 +1,3 @@
m = { 'def' => 1, 'abc' => 2 }
for( kv in m ) io.writeln( kv );
for( k in m.keys(); v in m.values() ) io.writeln( k, v )

View file

@ -0,0 +1,29 @@
program AssociativeArrayIteration;
{$APPTYPE CONSOLE}
uses SysUtils, Generics.Collections;
var
i: Integer;
s: string;
lDictionary: TDictionary<string, Integer>;
lPair: TPair<string, Integer>;
begin
lDictionary := TDictionary<string, Integer>.Create;
try
lDictionary.Add('foo', 5);
lDictionary.Add('bar', 10);
lDictionary.Add('baz', 15);
lDictionary.AddOrSetValue('foo', 6);
for lPair in lDictionary do
Writeln(Format('Pair: %s = %d', [lPair.Key, lPair.Value]));
for s in lDictionary.Keys do
Writeln('Key: ' + s);
for i in lDictionary.Values do
Writeln('Value: ', i);
finally
lDictionary.Free;
end;
end.

View file

@ -0,0 +1,21 @@
def map := [
"a" => 1,
"b" => 2,
"c" => 3,
]
for key => value in map {
println(`$key $value`)
}
for value in map { # ignore keys
println(`. $value`)
}
for key => _ in map { # ignore values
println(`$key .`)
}
for key in map.domain() { # iterate over the set whose values are the keys
println(`$key .`)
}

View file

@ -0,0 +1,26 @@
#define std'dictionary'*.
#define std'routines'*.
#define std'collections'*.
#define std'patterns'*.
// --- Program ---
#symbol Program =
[
// 1. Create
#var aMap := Dictionary.
aMap
append &dictionary_key:"key" &content:"foox"
append &dictionary_key:"key" &content:"foo"
append &dictionary_key:"key2" &content:"foo2"
append &dictionary_key:"key3" &content:"foo3"
append &dictionary_key:"key4" &content:"foo4".
(aMap enumerator)~foreach run: anItem =
[
'program'Output << anItem dictionary_key << " : " << anItem content << "%n".
].
// only values
Scan::aMap run: aValue = ('program'Output << avalue << "%n").
].