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,17 @@
using System;
using System.Console;
using Nemerle.Collections;
module AssocArray
{
Main() : void
{
def hash1 = Hashtable([(1, "one"), (2, "two"), (3, "three")]);
def hash2 = Hashtable(3);
foreach (e in hash1)
hash2[e.Value] = e.Key;
WriteLine("Enter 1, 2, or 3:");
def entry = int.Parse(ReadLine());
WriteLine(hash1[entry]);
}
}

View file

@ -0,0 +1,12 @@
/* NetRexx */
options replace format comments java crossref savelog symbols
key0 = '0'
key1 = 'key0'
hash = '.' -- Initialize the associative array 'hash' to '.'
hash[key1] = 'value0' -- Set a specific key/value pair
say '<hash key="'key0'" value="'hash[key0]'" />' -- Display a value for a key that wasn't set
say '<hash key="'key1'" value="'hash[key1]'" />' -- Display a value for a key that was set

View file

@ -0,0 +1,3 @@
let hash = Hashtbl.create 0;;
List.iter (fun (key, value) -> Hashtbl.add hash key value)
["foo", 5; "bar", 10; "baz", 15];;

View file

@ -0,0 +1 @@
let bar = Hashtbl.find hash "bar";; (* bar = 10 *)

View file

@ -0,0 +1 @@
let quux = try Hashtbl.find hash "quux" with Not_found -> some_value;;

View file

@ -0,0 +1,12 @@
module String = struct
type t = string
let compare = Pervasives.compare
end
module StringMap = Map.Make(String);;
let map =
List.fold_left
(fun map (key, value) -> StringMap.add key value map)
StringMap.empty
["foo", 5; "bar", 10; "baz", 15]
;;

View file

@ -0,0 +1 @@
let bar = StringMap.find "bar" map;; (* bar = 10 *)

View file

@ -0,0 +1 @@
let quux = try StringMap.find "quux" map with Not_found -> some_value;;

View file

@ -0,0 +1,7 @@
let dict = ["foo", 5; "bar", 10; "baz", 15]
(* retrieve value *)
let bar_num = try List.assoc "bar" dict with Not_found -> 0;;
(* see if key exists *)
print_endline (if List.mem_assoc "foo" dict then "key found" else "key missing")

View file

@ -0,0 +1,10 @@
# create map
map := StringMap->New();
# insert
map->Insert("two", IntHolder->New(2)->As(Base));
map->Insert("thirteen", IntHolder->New(13)->As(Base));
map->Insert("five", IntHolder->New(5)->As(Base));
map->Insert("seven", IntHolder->New(7)->As(Base));
# find
map->Find("thirteen")->As(IntHolder)->GetValue()->PrintLine();
map->Find("seven")->As(IntHolder)->GetValue()->PrintLine();

View file

@ -0,0 +1,10 @@
# create map
map := StringHash->New();
# insert
map->Insert("two", IntHolder->New(2)->As(Base));
map->Insert("thirteen", IntHolder->New(13)->As(Base));
map->Insert("five", IntHolder->New(5)->As(Base));
map->Insert("seven", IntHolder->New(7)->As(Base));
# find
map->Find("thirteen")->As(IntHolder)->GetValue()->PrintLine();
map->Find("seven")->As(IntHolder)->GetValue()->PrintLine();

View file

@ -0,0 +1,5 @@
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"Joe Doe", @"name",
[NSNumber numberWithUnsignedInt:42], @"age",
[NSNull null], @"extra",
nil];

View file

@ -0,0 +1,5 @@
NSDictionary *dict = @{
@"name": @"Joe Doe",
@"age": @42,
@"extra": [NSNull null],
};

View file

@ -0,0 +1,3 @@
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"Joe Doe" forKey:@"name"];
[dict setObject:[NSNumber numberWithInt:42] forKey:@"age"];

View file

@ -0,0 +1,3 @@
NSString *name = [dict objectForKey:@"name"];
unsigned age = [dict objectForKey:@"age"] unsignedIntValue];
id missing = [dict objectForKey:@"missing"];

View file

@ -0,0 +1,52 @@
def n 200
Class AssociativeArray
'=====================
indexbase 1
string s[n]
sys max
method find(string k) as sys
sys i,e
e=max*2
for i=1 to e step 2
if k=s[i] then return i
next
end method
method dat(string k) as string
sys i=find(k)
if i then return s[i+1]
end method
method dat(string k, d) as sys
sys i=find(k)
if i=0 then
if max>=n
print "Array overflow" : return 0
end if
max+=1
i=max*2-1
s[i]=k
end if
s[i+1]=d
return i
end method
end class
'====
'TEST
'====
AssociativeArray A
'fill
A.s<={"shoes","LC1", "ships","LC2", "sealingwax","LC3", "cabbages","LC4", "kings","LC5"}
A.max=5
'access
print A.dat("ships") 'result LC2
A.dat("computers")="LC99" '
print A.dat("computers") 'result LC99

View file

@ -0,0 +1,9 @@
declare
Dict = {Dictionary.new}
in
Dict.foo := 5
Dict.bar := 10
Dict.baz := 15
Dict.foo := 20
{Inspect Dict}

View file

@ -0,0 +1,4 @@
declare
Rec = name(foo:5 bar:10 baz:20)
in
{Inspect Rec}

View file

@ -0,0 +1,14 @@
DECLARE
type assocArrayType is record (
myShape VARCHAR2(20),
mySize number,
isActive BOOLEAN
);
assocArray assocArrayType;
BEGIN
assocArray.myShape := 'circle';
dbms_output.put_line ('assocArray.myShape: ' || assocArray.myShape);
dbms_output.put_line ('assocArray.mySize: ' || assocArray.mySize);
END;
/

View file

@ -0,0 +1,2 @@
my %h1 = key1 => 'val1', 'key-2' => 2, three => -238.83, 4 => 'val3';
my %h2 = 'key1', 'val1', 'key-2', 2, 'three', -238.83, 4, 'val3';

View file

@ -0,0 +1,3 @@
my @a = 1..5;
my @b = 'a'..'e';
my %h = @a Z=> @b;

View file

@ -0,0 +1,4 @@
say %h1{'key1'};
say %h1<key1>;
%h1<key1> = 'val1';
%h1<key1 three> = 'val1', -238.83;

View file

@ -0,0 +1,2 @@
my $h = {key1 => 'val1', 'key-2' => 2, three => -238.83, 4 => 'val3'};
say $h<key1>;

View file

@ -0,0 +1,22 @@
;;; Create expandable hash table of initial size 50 and with default
;;; value 0 (default value is returned when the item is absent).
vars ht = newmapping([], 50, 0, true);
;;; Set value corresponding to string 'foo'
12 -> ht('foo');
;;; print it
ht('foo') =>
;;; Set value corresponding to vector {1 2 3}
17 -> ht({1 2 3});
;;; print it
ht({1 2 3}) =>
;;; Set value corresponding to number 42 to vector {0 1}
{0 1} -> ht(42);
;;; print it
ht(42) =>
;;; Iterate over keys printing keys and values.
appproperty(ht,
procedure (key, value);
printf(value, '%p\t');
printf(key, '%p\n');
endprocedure);

View file

@ -0,0 +1,2 @@
<</a 100 /b 200 /c 300>>
dup /a get =

View file

@ -0,0 +1 @@
$hashtable = @{}

View file

@ -0,0 +1,4 @@
$hashtable = @{
"key1" = "value 1"
"key2" = 5
}

View file

@ -0,0 +1,4 @@
$hashtable.foo = "bar"
$hashtable['bar'] = 42
$hashtable."a b" = 3.14 # keys can contain spaces, property-style access needs quotation marks, then
$hashtable[5] = 8 # keys don't need to be strings

View file

@ -0,0 +1,2 @@
$hashtable.key1 # value 1
$hashtable['key2'] # 5

View file

@ -0,0 +1,6 @@
$addressObj= "" | Select-Object nameStr,street1Str,street2Str,cityStr,stateStr,zipStr
$addressObj.nameStr = [string]"FirstName LastName"
$addressObj.street1Str= [string]"1 Main Street"
$addressObj.cityStr= [string]"Washington DC"
$addressObj.stateStr= [string]"DC"
$addressObj.zipStr= [string]"20009"

View file

@ -0,0 +1,3 @@
NewMap dict.s()
dict("country") = "Germany"
Debug dict("country")

View file

@ -0,0 +1,24 @@
x = <<>>; // create an empty list using strings as identifiers.
x.red = strtod("0xff0000"); // RLaB doesn't deal with hexadecimal numbers directly. Thus we
x.green = strtod("0x00ff00"); // convert it to real numbers using ''strtod'' function.
x.blue = strtod("0x0000ff");
// print content of a list
for (i in members(x))
{ printf("%8s %06x\n", i, int(x.[i])); } // we have to use ''int'' function to convert reals to integers so "%x" format works
// deleting a key/value
clear (x.red);
// we can also use numeric identifiers in the above example
xid = members(x); // this is a string array
for (i in 1:length(xid))
{ printf("%8s %06x\n", xid[i], int(x.[ xid[i] ])); }
// Finally, we can use numerical identifiers
// Note: ''members'' function orders the list identifiers lexicographically, in other words
// instead of, say, 1,2,3,4,5,6,7,8,9,10,11 ''members'' returns 1,10,11,2,3,4,5,6,7,8,9
x = <<>>; // create an empty list
for (i in 1:5)
{ x.[i] = i; } // assign to the element of list ''i'' the real value equal to i.

View file

@ -0,0 +1,13 @@
{ 'a' 1 'b' 2 'c' 3.14 'd' 'xyz' } as a_hash
a_hash print
hash (4 items)
a => 1
b => 2
c => 3.14
d => "xyz"
a_hash 'c' get # get key 'c'
6.28 a_hash 'c' set # set key 'c'
a_hash.'c' # get key 'c' shorthand
6.28 a_hash:'c' # set key 'c' shorthand

View file

@ -0,0 +1,8 @@
with hashTable'
hashTable constant table
table %{ first = 100 }%
table %{ second = "hello, world!" keepString %}
table @" first" putn
table @" second" puts

View file

@ -0,0 +1,9 @@
t = table()
t<"red"> = "#ff0000"
t<"green"> = "#00ff00"
t<"blue"> = "#0000ff"
output = t<"red">
output = t<"blue">
output = t<"green">
end

View file

@ -0,0 +1,45 @@
$ include "seed7_05.s7i";
# Define hash type
const type: myHashType is hash [string] integer;
# Define hash table
var myHashType: aHash is myHashType.value;
const proc: main is func
local
var string: stri is "";
var integer: number is 0;
begin
# Add elements
aHash @:= ["foo"] 42;
aHash @:= ["bar"] 100;
# Check presence of an element
if "foo" in aHash then
# Access an element
writeln(aHash["foo"]);
end if;
# Change an element
aHash @:= ["foo"] 7;
# Remove an element
excl(aHash, "foo");
# Loop over the hash values
for number range aHash do
writeln(number);
end for;
# Loop over the hash keys
for key stri range aHash do
writeln(stri);
end for;
# Loop over hash keys and values
for number key stri range aHash do
writeln("key: " <& stri <& ", value: " <& number);
end for;
end func;

View file

@ -0,0 +1 @@
Dictionary new*, 'MI' -> 'Michigan', 'MN' -> 'Minnesota'

View file

@ -0,0 +1,14 @@
needs asarray
( create an associative array )
1024 cells is-asarray foo
( store 100 as the "first" element in the array )
100 " first" foo asarray.put
( store 200 as the "second" element in the array )
200 " second" foo asarray.put
( obtain and print the values )
" first" foo asarray.get .
" second" foo asarray.get .

View file

@ -0,0 +1,44 @@
map='p.map'
function init() {
cat <<EOF > $map
apple a
boy b
cat c
dog d
elephant e
EOF
}
function put() {
k=$1; v=$2;
del $k
echo $v $k >> $map
}
function get() {
k=$1
for v in $(cat $map | grep "$k$"); do
echo $v
break
done
}
function del() {
k=$1
temp=$(mktemp)
mv $map $temp
cat $temp | grep -v "$k$" > $map
}
function dump() {
echo "-- Dump begin --"
cat $map
echo "-- Dump complete --"
}
init
get c
put c cow
get c
dump

View file

@ -0,0 +1,17 @@
using Gee;
void main(){
var map = new HashMap<string, int>(); // creates a HashMap with keys of type string, and values of type int
// two methods to set key,value pair
map["one"] = 1;
map["two"] = 2;
map.set("four", 4);
map.set("five", 5);
// two methods of getting key,value pair
stdout.printf("%d\n", map["one"]);
stdout.printf("%d\n", map.get("two"));
}

View file

@ -0,0 +1,27 @@
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)
];
func Lookup(Greek); \Given Greek name return English letter
char Greek;
int I;
[for I:= 0, Entries-1 do
if StrCmp(Greek, @Dict(I,1)) = 0 then return Dict(I,0);
return ^?;
];
[Entries:= 0;
AddEntry(^A, "alpha");
AddEntry(^D, "delta");
AddEntry(^B, "beta");
AddEntry(^C, "gamma");
ChOut(0, Lookup("beta")); CrLf(0);
ChOut(0, Lookup("omega")); CrLf(0);
]