-
Notifications
You must be signed in to change notification settings - Fork 11
/
collections.nim
66 lines (52 loc) · 1.08 KB
/
collections.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Array
# Length is known at compile time
var a = [1,2,3,4,5,6,7,8,9]
var b: array[128, int]
b[9] = 10
b[0..8] = a
var c: array['a'..'d', float] = [1.0, 1.1, 1.2, 1.3]
c['b'] = 10000
# Seq
# Variable length sequences
var d = @[1,2,3,5,6,7,8,9]
d.add(10)
d.add([11,12,13,14])
d[0] = 0
var e: seq[float] = @[]
e.add(15.5)
var f = newSeq[string]()
f.add("foo")
f.add("bar")
# Tuples
# Fixed length, named
var g = (13, 13, 14)
g[0] = 12
var h: tuple[key: string, val: int] = ("foo", 100)
# A sequence of key-val tuples:
var i = {"foo": 12, "bar": 13}
# Set
# Bit vector of ordinals
var j: set[char]
j.incl('X')
var k = {'a'..'z', '0'..'9'}
j = j + k
# Tables
# Hash tables (there are also ordered hash tables and counting hash tables)
import tables
var l = initTable[string, int]()
l["foo"] = 12
l["bar"] = 13
var m = {"foo": 12, "bar": 13}.toTable
m["baz"] = 14
# Sets
# Hash sets (also ordered hash sets)
import sets
var n = initHashSet[string]()
n.incl("foo")
var o = ["foo", "bar", "baz"].toHashSet
o.incl("foobar")
# Queues
import deques
var p = initDeque[int]()
p.addLast(12)
p.addLast(13)