-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin.go
More file actions
49 lines (46 loc) · 1.35 KB
/
join.go
File metadata and controls
49 lines (46 loc) · 1.35 KB
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
package set
// Join creates a set as the join of the sets a and b.
//
// If a and b are definitely finite the resulting set is also definitely finite.
// Otherwise the resulting set not definitely finite.
func Join(a Set, b Set) (Set, error) {
if a.DefinitelyFinite() && b.DefinitelyFinite() {
return defFiniteJoin(a, b)
}
return notDefFiniteJoin(a, b)
}
// Creates a new set as an join of a and b.
// Here is assumed that a and b are definitely finite.
func defFiniteJoin(a Set, b Set) (Set, error) {
// Create a definitely finite set
newSet := elementSet{make(map[interface{}]struct{})}
// Loop to add all elements from a and b explicit
for _, set := range []Set{a, b} {
list, err := set.List()
if err != nil {
return newSet, err
}
for _, element := range list {
newSet.elements[element] = struct{}{}
}
}
return newSet, nil
}
// Creates a new set as the join of a and b by using a function to define the set.
// Therefore the resulting set is not definitely finite.
func notDefFiniteJoin(a Set, b Set) (Set, error) {
newContains := func(x interface{}) (bool, error) {
// Loop to return true for all elements contains in a or b
for _, set := range []Set{a, b} {
yes, err := set.Contains(x)
if err != nil {
return false, err
}
if yes {
return true, nil
}
}
return false, nil
}
return functionSet{newContains}, nil
}