This repository has been archived by the owner on Apr 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
constructors.go
139 lines (118 loc) · 3.14 KB
/
constructors.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package c3
// NewList creates a new, empty List.
func NewList() List {
return newList()
}
// NewBag creates a new, empty Bag.
func NewBag() Bag {
return NewList()
}
// NewQueue creates a new, empty Queue.
func NewQueue() Queue {
return &queue{nil, nil, nil, 0, 0, 0}
}
// NewSet creates a new, empty Set.
func NewSet() Set {
return &set{0, make(map[interface{}]bool)}
}
// NewStack creates a new, empty Stack.
func NewStack() Stack {
return &stack{newList()}
}
// NewQuery provides a entry point to the c3 query api.
//
// Usage:
// list := c3.ListOf(1,2,3)
// q := NewQuery(list).
// Where(/* filter function here */).
// Select( /* selector function here */).
// ToList() /* collect the results */
func NewQuery(items Iterable) *Q {
return &Q{items}
}
// QueryOf provides a entry point to the c3 query api.
//
// Usage:
// q := c3.QueryOf(1,2,3).
// Where(/* filter function here */).
// Select( /* selector function here */).
// ToList() /* collect the results */
func QueryOf(items ...interface{}) *Q {
return NewQuery(IterableOf(items...))
}
// StackOf creates a new Stack with the given items.
func StackOf(items ...interface{}) Stack {
s := NewStack()
for item := range items {
s.Push(item)
}
return s
}
// SetOf creates a new Set containing the unique items.
func SetOf(items ...interface{}) Set {
set := NewSet()
for item := range items {
set.Add(item)
}
return set
}
// QueueOf creates a Queue with the given items.
func QueueOf(items ...interface{}) Queue {
q := NewQueue()
for item := range items {
q.Enqueue(item)
}
return q
}
// ListOf creates a List with the given items.
func ListOf(items ...interface{}) List {
l := len(items)
x := make([]interface{}, l)
copy(x, items)
return &list{0, x}
}
// MakeIterable converts the Generator function into an Iterable.
func MakeIterable(g Generator) Iterable {
return &generatorIterable{g}
}
// MakeIterator converts the Generate function into an Iterator.
func MakeIterator(g Generate) Iterator {
return &generateIterator{g, defaultElementValue}
}
// MakeGenerator converts the Iterable into a Generator function.
func MakeGenerator(items Iterable) Generator {
return func() Generate {
return MakeGenerate(items.Iterator())
}
}
// MakeGenerate converts the Iterator into a Generate function.
func MakeGenerate(i Iterator) Generate {
return func() (interface{}, bool) {
ok := i.MoveNext()
value := i.Value()
return value, ok
}
}
// IterableOf creates an Iterable with the given items.
func IterableOf(items ...interface{}) Iterable {
return ListOf(items...)
}
// IteratorOf creates an Iterator with the given items.
func IteratorOf(items ...interface{}) Iterator {
return ListOf(items...).Iterator()
}
// ReadOnlyBagOf creates a ReadOnlyBag with the given items.
func ReadOnlyBagOf(items ...interface{}) ReadOnlyBag {
return ListOf(items...)
}
// ReadOnlyListOf creates a ReadOnlyList with the given items.
func ReadOnlyListOf(items ...interface{}) ReadOnlyList {
return ListOf(items...)
}
// BagOf creates a Bag with the given items.
func BagOf(items ...interface{}) Bag {
return ListOf(items...)
}
func newList() *list {
return &list{0, make([]interface{}, 0, 4)}
}