-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.go
67 lines (60 loc) · 1.16 KB
/
dict.go
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
/*
The immutable package implements an immutable dictionary container inspired by
on those found in Clojure.
*/
package immutable
type Value interface {
}
type Item struct {
key string
val Value
}
/*
Dict.
This struct implements the Dict interface via an internal itrie.
*/
type Dict struct {
t itrie
}
func (d Dict) Assoc(key string, val Value) Dict {
t, _ := assoc(d.t, key, val)
return Dict{t}
}
func (d Dict) Without(key string) Dict {
t, _ := without(d.t, key)
return Dict{t}
}
func (d Dict) Contains(key string) bool {
e := entryAt(d.t, key)
return e != nil
}
func (d Dict) ValueAt(key string) (Value, bool) {
e := entryAt(d.t, key)
if e != nil { return e.val(), true }
return nil, false
}
func (d Dict) Count() int {
if d.t != nil {
return d.t.count()
}
return 0
}
func (d Dict) Foreach(fn func(string, Value)) {
if d.t != nil {
d.t.foreach("", fn)
}
}
func (d Dict) Iter() chan Item {
ch := make(chan Item)
if d.t != nil {
emit := func(key string, val Value) { ch <- Item{key, val} }
helper := func(t itrie, emit func(string, Value)) {
t.foreach("", emit)
close(ch)
}
go helper(d.t, emit)
} else {
go close(ch)
}
return ch
}