langs a-z
This commit is contained in:
parent
db842d013d
commit
d066446780
11389 changed files with 98361 additions and 1020 deletions
|
|
@ -0,0 +1,16 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
using Nemerle.Collections;
|
||||
using Nemerle.Collections.NCollectionsExtensions;
|
||||
|
||||
module AssocArray
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def list1 = ["apples", "oranges", "bananas", "kumquats"];
|
||||
def list2 = [13, 34, 12];
|
||||
def inventory = Hashtable(ZipLazy(list1, list2));
|
||||
foreach (item in inventory)
|
||||
WriteLine("{0}: {1}", item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
/* NetRexx program ****************************************************
|
||||
* 04.11.2012 Walter Pachl derived from REXX
|
||||
**********************************************************************/
|
||||
options replace format comments java crossref savelog symbols nobinary
|
||||
values='triangle quadrilateral pentagon hexagon heptagon octagon' -
|
||||
'nonagon decagon dodecagon'
|
||||
keys ='three four five six seven eight nine ten twelve'
|
||||
kcopy=keys
|
||||
k='' /* initialize the arrays */
|
||||
v=''
|
||||
value='unknown'
|
||||
Loop i=1 By 1 While kcopy>'' /* initialize the two arrays */
|
||||
Parse kcopy ki kcopy; k[i]=ki
|
||||
Parse values vi values; v[i]=vi
|
||||
End
|
||||
Loop j=1 To i-1
|
||||
value[k[j]]=v[j]
|
||||
End
|
||||
Say 'Enter one of these words:'
|
||||
Say ' 'keys
|
||||
Parse Ask z
|
||||
Say z '->' value[z]
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
vals = [ 'zero', 'one', 'two', 'three', 'four', 'five' ]
|
||||
keys = [ 'k0', 'k1', 'k2', 'k3', 'k4', 'k5' ]
|
||||
hash1 = Rexx
|
||||
hash2 = Map
|
||||
|
||||
hash1 = HashMap()
|
||||
hash2 = ''
|
||||
makeHash(hash1, keys, vals) -- using a Map object (overloaded method)
|
||||
makeHash(hash2, keys, vals) -- using a Rexx object (overloaded method)
|
||||
|
||||
return
|
||||
|
||||
-- Using a Java collection object
|
||||
method makeHash(hash = Map, keys = Rexx[], vals = Rexx[]) static
|
||||
loop k_ = 0 to keys.length - 1
|
||||
hash.put(keys[k_], vals[k_])
|
||||
end k_
|
||||
|
||||
key = Rexx
|
||||
loop key over hash.keySet()
|
||||
say key.right(8)':' hash.get(key)
|
||||
end key
|
||||
say
|
||||
|
||||
return
|
||||
|
||||
-- For good measure a version using the default Rexx object as a hash (associative array)
|
||||
method makeHash(hash = Rexx, keys = Rexx[], vals = Rexx[]) static
|
||||
loop k_ = 0 to keys.length - 1
|
||||
hash[keys[k_]] = vals[k_]
|
||||
end k_
|
||||
|
||||
loop key over hash
|
||||
say key.right(8)':' hash[key]
|
||||
end key
|
||||
say
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
let keys = [ "foo"; "bar"; "baz" ]
|
||||
and vals = [ 16384; 32768; 65536 ]
|
||||
and hash = Hashtbl.create 0;;
|
||||
|
||||
List.iter2 (Hashtbl.add hash) keys vals;;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let keys = [| "foo"; "bar"; "baz" |]
|
||||
and vals = [| 16384; 32768; 65536 |]
|
||||
and hash = Hashtbl.create 0;;
|
||||
|
||||
for i = 0 to Array.length keys - 1 do
|
||||
Hashtbl.add hash keys.(i) vals.(i)
|
||||
done;;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
module StringMap = Map.Make (String);;
|
||||
|
||||
let keys = [ "foo"; "bar"; "baz" ]
|
||||
and vals = [ 16384; 32768; 65536 ]
|
||||
and map = StringMap.empty;;
|
||||
|
||||
let map = List.fold_right2 StringMap.add keys vals map;;
|
||||
14
Task/Hash-from-two-arrays/Objeck/hash-from-two-arrays.objeck
Normal file
14
Task/Hash-from-two-arrays/Objeck/hash-from-two-arrays.objeck
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
use Structure;
|
||||
|
||||
bundle Default {
|
||||
class HashOfTwo {
|
||||
function : Main(args : System.String[]) ~ Nil {
|
||||
keys := ["1", "2", "3"];
|
||||
vals := ["a", "b", "c"];
|
||||
hash := StringHash->New();
|
||||
each(i : vals) {
|
||||
hash->Insert(keys[i], vals[i]->As(Base));
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
NSArray *keys = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
|
||||
NSArray *values = [NSArray arrayWithObjects:[NSNumber numberWithInt:1],
|
||||
[NSNumber numberWithInt:2],
|
||||
[NSNumber numberWithInt:3], nil];
|
||||
NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];
|
||||
10
Task/Hash-from-two-arrays/Oz/hash-from-two-arrays.oz
Normal file
10
Task/Hash-from-two-arrays/Oz/hash-from-two-arrays.oz
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
declare
|
||||
fun {ZipRecord Keys Values}
|
||||
{List.toRecord unit {List.zip Keys Values MakePair}}
|
||||
end
|
||||
|
||||
fun {MakePair A B}
|
||||
A#B
|
||||
end
|
||||
in
|
||||
{Show {ZipRecord [a b c] [1 2 3]}}
|
||||
18
Task/Hash-from-two-arrays/Pascal/hash-from-two-arrays.pascal
Normal file
18
Task/Hash-from-two-arrays/Pascal/hash-from-two-arrays.pascal
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
program HashFromTwoArrays (Output);
|
||||
|
||||
uses
|
||||
contnrs;
|
||||
|
||||
var
|
||||
keys: array[1..3] of string = ('a', 'b', 'c');
|
||||
values: array[1..3] of integer = ( 1, 2, 3 );
|
||||
hash: TFPDataHashTable;
|
||||
i: integer;
|
||||
|
||||
begin
|
||||
hash := TFPDataHashTable.Create;
|
||||
for i := low(keys) to high(keys) do
|
||||
hash.add(keys[i], @values[i]);
|
||||
writeln ('Length of hash table: ', hash.Count);
|
||||
hash.Destroy;
|
||||
end.
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
my @keys = <a b c d e>;
|
||||
my @vals = ^5;
|
||||
my %hash = @keys Z @vals;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my @v = <a b c d e>;
|
||||
my %hash = @v Z @v.keys;
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my %hash;
|
||||
%hash{@keys} = @vals;
|
||||
|
|
@ -0,0 +1 @@
|
|||
{ <a b c d e> Z=> ^5 }
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
vars keys = { 1 a b c};
|
||||
vars vals = { 2 3 valb valc};
|
||||
vars i;
|
||||
;;; Create hash table
|
||||
vars ht = newmapping([], 500, 0, true);
|
||||
;;; Loop over input arrays (vectors)
|
||||
for i from 1 to length(keys) do
|
||||
vals(i) -> ht(keys(i));
|
||||
endfor;
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
% push our arrays
|
||||
[/a /b /c /d /e] [1 2 3 4 5]
|
||||
% create a dict with it
|
||||
{aload pop} dip let currentdict end
|
||||
% show that we have created the hash
|
||||
{= =} forall
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
function create_hash ([array] $keys, [array] $values) {
|
||||
$h = @{}
|
||||
if ($keys.Length -ne $values.Length) {
|
||||
Write-Error -Message "Array lengths do not match" `
|
||||
-Category InvalidData `
|
||||
-TargetObject $values
|
||||
} else {
|
||||
for ($i = 0; $i -lt $keys.Length; $i++) {
|
||||
$h[$keys[$i]] = $values[$i]
|
||||
}
|
||||
}
|
||||
return $h
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
Dim keys.s(3)
|
||||
Dim vals.s(3)
|
||||
NewMap Hash.s()
|
||||
|
||||
keys(0)="a" : keys(1)="b" : keys(2)="c" : keys(3)="d"
|
||||
vals(0)="1" : vals(1)="2" : vals(2)="3" : vals(3)="4"
|
||||
For n = 0 To 3
|
||||
Hash(keys(n))= vals(n)
|
||||
Next
|
||||
ForEach Hash()
|
||||
Debug Hash()
|
||||
Next
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[ 'a' 'b' 'c' ] as $keys [ 1 2 3 ] as $vals
|
||||
$keys $vals combine as $hash
|
||||
16
Task/Hash-from-two-arrays/SNOBOL4/hash-from-two-arrays.sno
Normal file
16
Task/Hash-from-two-arrays/SNOBOL4/hash-from-two-arrays.sno
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
* # Fill arrays
|
||||
keys = array(5); vals = array(5)
|
||||
ks = 'ABCDE'; vs = '12345'
|
||||
kloop i = i + 1; ks len(1) . keys<i> = :s(kloop)
|
||||
vloop j = j + 1; vs len(1) . vals<j> = :s(vloop)
|
||||
|
||||
* # Create hash
|
||||
hash = table(5)
|
||||
hloop k = k + 1; hash<keys<k>> = vals<k> :s(hloop)
|
||||
|
||||
* # Test and display
|
||||
ts = 'ABCDE'
|
||||
tloop ts len(1) . ch = :f(out)
|
||||
str = str ch ':' hash<ch> ' ' :(tloop)
|
||||
out output = str
|
||||
end
|
||||
15
Task/Hash-from-two-arrays/Seed7/hash-from-two-arrays.seed7
Normal file
15
Task/Hash-from-two-arrays/Seed7/hash-from-two-arrays.seed7
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const type: numericHash is hash [string] integer;
|
||||
var numericHash: myHash is numericHash.value;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var array string: keyList is [] ("one", "two", "three");
|
||||
var array integer: valueList is [] (1, 2, 3);
|
||||
var integer: number is 0;
|
||||
begin
|
||||
for number range 1 to length(keyList) do
|
||||
myHash @:= [keyList[number]] valueList[number];
|
||||
end for;
|
||||
end func;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
structure StringMap = BinaryMapFn (struct
|
||||
type ord_key = string
|
||||
val compare = String.compare
|
||||
end);
|
||||
|
||||
val keys = [ "foo", "bar", "baz" ]
|
||||
and vals = [ 16384, 32768, 65536 ]
|
||||
and myMap = StringMap.empty;
|
||||
|
||||
val myMap = foldl StringMap.insert' myMap (ListPair.zipEq (keys, vals));
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
exception NotFound;
|
||||
|
||||
val keys = [ "foo", "bar", "baz" ]
|
||||
and vals = [ 16384, 32768, 65536 ]
|
||||
and hash = HashTable.mkTable (HashString.hashString, op=) (42, NotFound);
|
||||
|
||||
ListPair.appEq (HashTable.insert hash) (keys, vals);
|
||||
10
Task/Hash-from-two-arrays/TXR/hash-from-two-arrays.txr
Normal file
10
Task/Hash-from-two-arrays/TXR/hash-from-two-arrays.txr
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
@(do (defun hash-from-two (vec1 vec2 equal-based-p)
|
||||
(let ((table (make-hash nil nil equal-based-p)))
|
||||
(for ((i 0)) ((< i (length vec1)) table) ((inc i))
|
||||
(set (gethash table (vecref vec1 i) 0)
|
||||
(vecref vec2 i))))))
|
||||
@(bind hash @(hash-from-two #(a b c) #(1 2 3) nil))
|
||||
@(bind (keys vals) @(let (k v)
|
||||
(dohash (key val hash (list k v))
|
||||
(push key k)
|
||||
(push val v))))
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
cat <<VAL >p.values
|
||||
apple
|
||||
boy
|
||||
cow
|
||||
dog
|
||||
elephant
|
||||
VAL
|
||||
|
||||
cat <<KEYS >p.keys
|
||||
a
|
||||
b
|
||||
c
|
||||
d
|
||||
e
|
||||
KEYS
|
||||
|
||||
paste -d\ <(cat p.values | sort) <(cat p.keys | sort)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
keys = <'foo','bar','baz'>
|
||||
values = <12354,145430,76748>
|
||||
|
||||
hash_function = keys-$values
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
#cast %nL
|
||||
|
||||
test = hash_function* <'bar','baz','foo','bar'>
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Set dict = CreateObject("Scripting.Dictionary")
|
||||
os = Array("Windows", "Linux", "MacOS")
|
||||
owner = Array("Microsoft", "Linus Torvalds", "Apple")
|
||||
For n = 0 To 2
|
||||
dict.Add os(n), owner(n)
|
||||
Next
|
||||
MsgBox dict.Item("Linux")
|
||||
MsgBox dict.Item("MacOS")
|
||||
MsgBox dict.Item("Windows")
|
||||
15
Task/Hash-from-two-arrays/Vala/hash-from-two-arrays.vala
Normal file
15
Task/Hash-from-two-arrays/Vala/hash-from-two-arrays.vala
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
using Gee;
|
||||
|
||||
void main(){
|
||||
// mostly copied from C# example
|
||||
var hashmap = new HashMap<string, string>();
|
||||
|
||||
string[] arg_keys = {"foo", "bar", "val"};
|
||||
string[] arg_values = {"little", "miss", "muffet"};
|
||||
|
||||
if (arg_keys.length == arg_values.length ){
|
||||
for (int i = 0; i < arg_keys.length; i++){
|
||||
hashmap[arg_keys[i]] = arg_values[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
Dim dict As New Collection
|
||||
os = Array("Windows", "Linux", "MacOS")
|
||||
owner = Array("Microsoft", "Linus Torvalds", "Apple")
|
||||
For n = 0 To 2
|
||||
dict.Add owner(n), os(n)
|
||||
Next
|
||||
Debug.Print dict.Item("Linux")
|
||||
Debug.Print dict.Item("MacOS")
|
||||
Debug.Print dict.Item("Windows")
|
||||
Loading…
Add table
Add a link
Reference in a new issue