September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -8,5 +8,5 @@ r_put(r, "B", "associative"); # a string value
if (r_first(r, s)) {
do {
o_form("key ~, value ~ (~)\n", s, r[s], r_type(r, s));
} while (r_greater(r, s, s));
} while (rsk_greater(r, s, s));
}

View file

@ -0,0 +1,10 @@
DECLARE associative ASSOC STRING
associative("abc") = "first three"
associative("mn") = "middle two"
associative("xyz") = "last three"
LOOKUP associative TO keys$ SIZE amount
FOR i = 0 TO amount - 1
PRINT keys$[i], ":", associative(keys$[i])
NEXT

View file

@ -1,20 +1,18 @@
#define system.
#define system'collections.
#define system'routines.
#define extensions.
import system'collections.
import system'routines.
import extensions.
// --- Program ---
#symbol program =
program =
[
// 1. Create
#var aMap := Dictionary new.
aMap@"key" := "foox".
aMap@"key2":= "foo2".
aMap@"key3":= "foo3".
aMap@"key4":= "foo4".
var aMap := Dictionary new.
aMap["key"] := "foox".
aMap["key"] := "foo".
aMap["key2"]:= "foo2".
aMap["key3"]:= "foo3".
aMap["key4"]:= "foo4".
// Enumerate
aMap run &each: aKeyValue
[ console writeLine:(aKeyValue key):" : ":aKeyValue ].
aMap forEach
(:aKeyValue)[ console printLine(aKeyValue key," : ",aKeyValue) ].
].

View file

@ -0,0 +1,16 @@
Public Sub Main()
Dim cList As Collection = ["2": "quick", "4": "fox", "1": "The", "9": "dog", "7": "the", "5": "jumped", "3": "brown", "6": "over", "8": "lazy"]
Dim siCount As Short
Dim sTemp As String
For Each sTemp In cList
Print cList.key & "=" & sTemp;;
Next
Print
For siCount = 1 To cList.Count
Print cList[Str(siCount)];;
Next
End

View file

@ -0,0 +1,35 @@
package main
import (
"os"
"text/template"
)
func main() {
m := map[string]int{
"hello": 13,
"world": 31,
"!": 71,
}
// iterating over key-value pairs:
template.Must(template.New("").Parse(`
{{- range $k, $v := . -}}
key = {{$k}}, value = {{$v}}
{{end -}}
`)).Execute(os.Stdout, m)
// iterating over keys:
template.Must(template.New("").Parse(`
{{- range $k, $v := . -}}
key = {{$k}}
{{end -}}
`)).Execute(os.Stdout, m)
// iterating over values:
template.Must(template.New("").Parse(`
{{- range . -}}
value = {{.}}
{{end -}}
`)).Execute(os.Stdout, m)
}

View file

@ -1,10 +1,13 @@
import qualified Data.Map as M
myMap :: M.Map String Int
myMap = M.fromList [("hello", 13), ("world", 31), ("!", 71)]
main = do -- pairs
print $ M.toList myMap
-- keys
print $ M.keys myMap
-- values
print $ M.elems myMap
main :: IO ()
main =
(putStrLn . unlines) $
[ show . M.toList -- Pairs
, show . M.keys -- Keys
, show . M.elems -- Values
] <*>
pure myMap

View file

@ -1,21 +1,21 @@
Map<String, Integer> myDict = new HashMap<String, Integer>();
myDict.put("hello", 1);
myDict.put("world", 2);
myDict.put("!", 3);
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);
// iterating over key-value pairs:
for (Map.Entry<String, Integer> e : myDict.entrySet()) {
for (Map.Entry<String, Integer> e : map.entrySet()) {
String key = e.getKey();
Integer value = e.getValue();
System.out.println("key = " + key + ", value = " + value);
}
// iterating over keys:
for (String key : myDict.keySet()) {
for (String key : map.keySet()) {
System.out.println("key = " + key);
}
// iterating over values:
for (Integer value : myDict.values()) {
for (Integer value : map.values()) {
System.out.println("value = " + value);
}

View file

@ -1,15 +1,15 @@
Map<String, Integer> myDict = new HashMap<>();
myDict.put("hello", 1);
myDict.put("world", 2);
myDict.put("!", 3);
Map<String, Integer> map = new HashMap<>();
map.put("hello", 1);
map.put("world", 2);
map.put("!", 3);
// iterating over key-value pairs:
myDict.forEach((k, v) -> {
map.forEach((k, v) -> {
System.out.printf("key = %s, value = %s%n", k, v);
});
// iterating over keys:
myDict.keySet().forEach(k -> System.out.printf("key = %s%n", k));
map.keySet().forEach(k -> System.out.printf("key = %s%n", k));
// iterating over values:
myDict.values().forEach(v -> System.out.printf("value = %s%n", v));
map.values().forEach(v -> System.out.printf("value = %s%n", v));

View file

@ -1,15 +0,0 @@
var myhash = {}; // a new, empty object
myhash["hello"] = 3;
myhash.world = 6; // obj.name is equivalent to obj["name"] for certain values of name
myhash["!"] = 9;
var output = '', // initialise as string
key;
for (key in myhash) {
if (myhash.hasOwnProperty(key)) {
output += "key is: " + key;
output += " => ";
output += "value is: " + myhash[key]; // cannot use myhash.key, that would be myhash["key"]
output += "\n";
}
}

View file

@ -1,13 +0,0 @@
var myhash = {}; // a new, empty object
myhash["hello"] = 3;
myhash.world = 6; // obj.name is equivalent to obj["name"] for certain values of name
myhash["!"] = 9;
var output = '', // initialise as string
val;
for (val in myhash) {
if (myhash.hasOwnProperty(val)) {
output += "myhash['" + val + "'] is: " + myhash[val];
output += "\n";
}
}

View file

@ -0,0 +1,31 @@
def mydict: {"hello":13, "world": 31, "!": 71};
# Iterating over the keys
mydict | keys[]
# "!"
# "hello"
# "world"
# Iterating over the values:
mydict[]
# 13
# 31
# 71
# Generating a stream of {"key": key, "value": value} objects:
mydict | to_entries[]
# {"key":"hello","value":13}
# {"key":"world","value":31}
# {"key":"!","value":71}
# Generating a stream of [key,value] arrays:
mydict | . as $o | keys[] | [., $o[.]]
#["!",71]
#["hello",13]
#["world",31]
# Generating a stream of [key,value] arrays, without sorting (jq > 1.4 required)
mydict | . as $o | keys_unsorted[] | [., $o[.]]
# ["hello",13]
# ["world",31]
# ["!",71]

View file

@ -2,7 +2,7 @@ fun main(a: Array<String>) {
val map = mapOf("hello" to 1, "world" to 2, "!" to 3)
with(map) {
forEach { println("key = ${it.key}, value = ${it.value}") }
entries.forEach { println("key = ${it.key}, value = ${it.value}") }
keys.forEach { println("key = $it") }
values.forEach { println("value = $it") }
}

View file

@ -1,6 +1,6 @@
import tables
var t: TTable[int,string] = initTable[int,string]()
var t: Table[int,string] = initTable[int,string]()
t[1] = "one"
t[2] = "two"

View file

@ -1,23 +0,0 @@
import tables
var t: TTable[int,string] = initTable[int,string]()
t[1] = "one"
t[2] = "two"
t[3] = "three"
t.add(4,"four")
echo "t has " & $t.len & " elements"
echo "has t key 4? " & $t.hasKey(4)
echo "has t key 5? " & $t.hasKey(5)
#iterate keys
echo "key iteration:"
for k in t.keys:
echo "at[" & $k & "]=" & t[k]
#itetate pairs
echo "pair iteration:"
for k,v in t.pairs:
echo "at[" & $k & "]=" & v

View file

@ -0,0 +1,58 @@
;;; create sample associative array
(define aa (list->ff '(
(hello . 1)
(world . 2)
(! . 3))))
(print aa)
; ==> #((! . 3) (hello . 1) (world . 2))
;;; simplest iteration over all associative array (using ff-iter, lazy iterator)
(let loop ((kv (ff-iter aa)))
(cond
((null? kv) #true)
((pair? kv)
(print (car kv))
(loop (cdr kv)))
(else (loop (force kv)))))
; ==> (! . 3)
; ==> (hello . 1)
; ==> (world . 2)
;;; iteration with returning value (using ff-fold)
(print
"folding result: "
(ff-fold
(lambda (result key value)
(print "key: " key ", value: " value)
(+ result 1))
0
aa))
; ==> key: !, value: 3
; ==> key: hello, value: 1
; ==> key: world, value: 2
; ==> folding result: 3
;;; same but right fold (using ff-foldr)
(print
"rfolding result: "
(ff-foldr
(lambda (result key value)
(print "key: " key ", value: " value)
(+ result 1))
0
aa))
; ==> key: world, value: 2
; ==> key: hello, value: 1
; ==> key: !, value: 3
; ==> rfolding result: 3
;;; at least create new array from existing (let's multiply every value by value)
(define bb (ff-map aa
(lambda (key value)
(* value value))))
(print bb)
; ==> #((! . 9) (hello . 1) (world . 4))

View file

@ -0,0 +1,21 @@
d = .directory~new
d["hello"] = 1
d["world"] = 2
d["!"] = 3
-- iterating over keys:
loop key over d
say "key =" key
end
-- iterating over values:
loop value over d~allitems
say "value =" value
end
-- iterating over key-value pairs:
s = d~supplier
loop while s~available
say "key =" s~index", value =" s~item
s~next
end

View file

@ -0,0 +1,13 @@
mata
// Create an associative array
a=asarray_create()
asarray(a,"one",1)
asarray(a,"two",2)
// Loop over entries
loc=asarray_first(a)
do {
printf("%s %f\n",asarray_key(a,loc),asarray_contents(a,loc))
loc=asarray_next(a,loc)
} while(loc!=NULL)
end

View file

@ -0,0 +1,5 @@
var d=Dictionary("A","alpha","D","delta", "B","beta", "C", "gamma");
d.keys.pump(Console.print,fcn(k){String(k,",")})
d.values.apply("toUpper").println();
d.makeReadOnly(); // can only iterate over k,v pairs if read only
foreach k,v in (d){print(k,":",v,"; ")}