-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet.ff
36 lines (31 loc) · 1.13 KB
/
Set.ff
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
newtype Set[T](map: Map[T, Unit])
empty[T](): Set[T] {
Set(Map.empty())
}
extend self[T: Order]: Set[T] {
add(value: T): Set[T] {Set(self.map.add(value, Unit))}
addAll(that: Set[T]): Set[T] {Set(self.map.addAll(that.map))}
remove(value: T): Set[T] {Set(self.map.remove(value))}
removeAll(that: Set[T]): Set[T] {Set(self.map.removeAll(that.map))}
contains(value: T): Bool {self.map.contains(value)}
size(): Int {self.map.size()}
toList(): List[T] {self.map.toList().map {_.first}}
toArray(): Array[T] {self.map.toArray().map {_.first}}
toStream(cycle: Bool = False): Stream[T] {self.map.toStream(cycle).map {_.first }}
each(body: T => Unit): Unit {self.map.each {| k, _ => body(k)}}
}
instance Set[A: Equal: Order]: Equal {
equals(x: Set[A], y: Set[A]): Bool {
Equal.equals(x.toList(), y.toList())
}
}
instance Set[A: Order]: Order {
compare(x: Set[A], y: Set[A]): Ordering {
Ordering.compare(x.toList(), y.toList())
}
}
instance Set[A: Show: Order]: Show {
show(x: Set[A]): String {
Show.show(x.toList()) + ".toSet()"
}
}