Power set: Difference between revisions
Content added Content deleted
Line 810: | Line 810: | ||
import ( |
import ( |
||
"bytes" |
|||
"fmt" |
"fmt" |
||
"strconv" |
"strconv" |
||
Line 818: | Line 819: | ||
// element is an interface, allowing different kinds of elements to be |
// element is an interface, allowing different kinds of elements to be |
||
// implemented and stored in sets. |
// implemented and stored in sets. |
||
type |
type elem interface { |
||
// an element must be distinguishable from other elements to satisfy |
// an element must be distinguishable from other elements to satisfy |
||
// the mathematical definition of a set. a.eq(b) must give the same |
// the mathematical definition of a set. a.eq(b) must give the same |
||
// result as b.eq(a). |
// result as b.eq(a). |
||
Eq(element) bool |
|||
// String result is used only for printable output. Given a, b where |
// String result is used only for printable output. Given a, b where |
||
// a.eq(b), it is not required that a.String() == b.String(). |
// a.eq(b), it is not required that a.String() == b.String(). |
||
fmt.Stringer |
|||
} |
} |
||
// integer type satisfying element interface |
// integer type satisfying element interface |
||
type |
type Int int |
||
func (i |
func (i Int) Eq(e elem) bool { |
||
j, ok := e.(Int) |
|||
return ok && i == j |
|||
} |
|||
⚫ | |||
} |
} |
||
func (i |
func (i Int) String() string { |
||
return strconv.Itoa(int(i)) |
return strconv.Itoa(int(i)) |
||
} |
} |
||
// set |
// a set is a slice of elem's. methods are added to implement |
||
// |
// the element interface, to allow nesting. |
||
type set [] |
type set []elem |
||
// uniqueness of elements can be ensured by using add method |
// uniqueness of elements can be ensured by using add method |
||
func (s *set) |
func (s *set) add(e elem) { |
||
if !s. |
if !s.has(e) { |
||
*s = append(*s, e) |
*s = append(*s, e) |
||
} |
} |
||
} |
} |
||
func (s *set) |
func (s *set) has(e elem) bool { |
||
for _, ex := range *s { |
for _, ex := range *s { |
||
if e. |
if e.Eq(ex) { |
||
return true |
return true |
||
} |
} |
||
Line 862: | Line 861: | ||
} |
} |
||
// elem.Eq |
|||
// method to satify element interface |
|||
func (s set) |
func (s set) Eq(e elem) bool { |
||
t, ok := e.(set) |
t, ok := e.(set) |
||
if !ok { |
if !ok { |
||
Line 872: | Line 871: | ||
} |
} |
||
for _, se := range s { |
for _, se := range s { |
||
if !t. |
if !t.has(se) { |
||
return false |
return false |
||
} |
} |
||
Line 879: | Line 878: | ||
} |
} |
||
// elem.String |
|||
// method to satify element interface |
|||
func (s set) String() string { |
func (s set) String() string { |
||
var buf bytes.Buffer |
|||
buf.WriteRune('{') |
|||
for _, e := range s { |
for _, e := range s { |
||
if len(r) > 1 { |
if len(r) > 1 { |
||
buf.WriteRune(' ') |
|||
} |
} |
||
buf.WriteString(e.String()) |
|||
} |
} |
||
buf.WriteRune('}') |
|||
⚫ | |||
} |
} |
||
Line 906: | Line 907: | ||
func main() { |
func main() { |
||
var s set |
var s set |
||
for _, i := range [] |
for _, i := range []Int{1, 2, 2, 3, 4, 4, 4} { |
||
s. |
s.add(i) |
||
} |
} |
||
fmt.Println(s) |
fmt.Println(s) |