-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex01_set.go
More file actions
36 lines (31 loc) · 1006 Bytes
/
ex01_set.go
File metadata and controls
36 lines (31 loc) · 1006 Bytes
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
package generics
// Context: Generic Data Structures (Set)
// You need a mathematical Set data structure that guarantees uniqueness.
// You need it to work for `int`, `string`, and `uuid.UUID`.
//
// Why this matters: Historically, Go developers either copy-pasted 3 versions
// of the Set, or used `interface{}` which lost type safety and required casting.
// Generics allow exactly ONE implementation that is 100% type-safe.
//
// Requirements:
// 1. Refactor `Set` to use a type parameter `T`.
// 2. Determine the correct constraint for `T`. (Hint: To be a map key, it must
// be `comparable`).
// 3. Update `NewSet`, `Add`, and `Contains`.
// BUG: This set is hardcoded to strings.
// TODO: Make it Generic: `Set[T constraint]`
type Set struct {
items map[string]struct{}
}
func NewSet() *Set {
return &Set{
items: make(map[string]struct{}),
}
}
func (s *Set) Add(item string) {
s.items[item] = struct{}{}
}
func (s *Set) Contains(item string) bool {
_, ok := s.items[item]
return ok
}