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

@ -0,0 +1,6 @@
DECLARE associative ASSOC STRING
associative("abc") = "first three"
associative("xyz") = "last three"
PRINT associative("xyz")

View file

@ -1,15 +1,12 @@
#define system.
#define system'collections.
import system'collections.
// --- Program ---
#symbol program =
program =
[
// 1. Create
#var aMap := Dictionary new.
aMap@"key" := "foox".
aMap@"key" := "foo".
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".
].

View file

@ -0,0 +1,35 @@
// empty map
var emptyMap = new HashMap<String, Integer>()
// map initialization
var map = {"Scott"->50, "Carson"->40, "Luca"->30, "Kyle"->38}
// map key/value assignment
map["Scott"] = 51
// get a value
var x = map["Scott"]
// remove an entry
map.remove("Scott")
// loop and maps
for(entry in map.entrySet()) {
print("Key: ${entry.Key}, Value: ${entry.Value}")
}
// functional iteration
map.eachKey(\ k ->print(map[k]))
map.eachValue(\ v ->print(v))
map.eachKeyAndValue(\ k, v -> print("Key: ${v}, Value: ${v}"))
var filtered = map.filterByValues(\ v ->v < 50)
// any object can be treated as an associative array
class Person {
var name: String
var age: int
}
// access properties on Person dynamically via associative array syntax
var scott = new Person()
scott["name"] = "Scott"
scott["age"] = 29

View file

@ -1 +0,0 @@
'foo' in assoc // true

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,23 @@
; empty associative array has two names
#empty
#()
; creating the new empty associative array
(define empty-map #empty)
; creating associative array with values
(define my-map (list->ff '(
(1 . 100)
(2 . 200)
(7 . 777))))
; add new key-value pair to the existing associative array
(define my-new-map (put my-map 'the-key 'the-value))
; print our arrays
(print empty-map)
; ==> #()
(print my-map)
; ==> #((1 . 100) (2 . 200) (7 . 777))
(print my-new-map)
; ==> #((1 . 100) (2 . 200) (7 . 777) (the-key . the-value))

View file

@ -0,0 +1,5 @@
map = .directory~new
map["foo"] = 5
map["bar"] = 10
map["baz"] = 15
map["foo"] = 6

View file

@ -0,0 +1,2 @@
item = map["foo"] -- => 6
item = map["invalid"] -- => .nil

View file

@ -0,0 +1,3 @@
loop key over map
say key
end

View file

@ -0,0 +1,3 @@
loop value over map~allItems
say value
end

View file

@ -0,0 +1,5 @@
s = map~supplier
loop while s~available
say s~index "=>" s~item
s~next
end

View file

@ -0,0 +1,24 @@
* Iterate over key-value pairs of a map
map = new('map 1:one 2:two 3:three')
visit(domain(map),'expr to evaluate for each member')
visit(range(map),'expr to evaluate for each member')
next
this = next(map) :f(done)
out(show(this) ':' show(get(map,this)) :next)
done
loop(d = domain(map)
next
out('next domain entry',next(d)) :s(next)
done
loop(d = range(map)
next
out('next domain entry',next(d)) :s(next)
done

View file

@ -0,0 +1,18 @@
mata
a=asarray_create()
// Add entries
asarray(a,"one",1)
asarray(a,"two",2)
// Check existence of key
asarray_contains(a,"two")
// Get a vector of all keys
asarray_keys(a)
// Number of entries
asarray_elements(a)
// End Mata session
end

View file

@ -0,0 +1,5 @@
zkl: Dictionary("one",1, "two",2, "three",3)
D(two:2,three:3,one:1)
zkl: T("one",1, "two",2, "three",3).toDictionary()
D(two:2,three:3,one:1)