05-collections/06-sets-and-identity-sets.protos

Repository
guillermomolina/protos
Commit
379c6fa63685c39a5954cfbd577bfda3b4df8e75
Tree
8cd4f4afe29ae4c3866381f38c1858108e9799f6
Blob
682d438e52006daa270ed171cb8c699e54b7246a
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

// Sets and identity sets.
//
// std:collections/set answers ordinary Maps keyed by their members: membership
// uses `hash` and `==`. std:collections/identity_set answers IdentityMaps:
// membership uses `===`. Neither is a new Core value family; the module
// functions operate on those Map families.

Set: import("std:collections/set")
IdentitySet: import("std:collections/identity_set")

// Equality-based membership: 1 and 1.0 are equal, so they are one member.
numbers: Set(1, 2, 1.0)
print(Set.size(numbers))                        // 2
print(Set.contains(numbers, 1.0))               // true

// Identity-based membership: 1 and 1.0 are different identities.
identities: IdentitySet(1, 2, 1.0)
print(IdentitySet.size(identities))             // 3

// Two equal-looking objects are distinct identities.
first: { name: "Ada" }
second: { name: "Ada" }
people: IdentitySet(first)
print(IdentitySet.contains(people, first))      // true
print(IdentitySet.contains(people, second))     // false

// add and remove mutate the set in place and answer it.
print(Set.add(numbers, 3) === numbers)          // true
Set.remove(numbers, 2)
print(Set.contains(numbers, 2))                 // false
print(Error.handle(
    () => { Set.remove(numbers, 99); "removed" },
    (error) => { "removing an absent member is an Error" }
))

// Iteration follows insertion order.
Set.each(numbers, (member) => { print(member) })
// 1
// 3

// union, intersection and difference answer fresh sets.
left: Set("a", "b", "c")
right: Set("b", "c", "d")
union: Set.union(left, right)
print(union !== left)                           // true
print(Set.size(union))                          // 4
print(Set.size(Set.intersection(left, right)))  // 2
print(Set.contains(Set.difference(left, right), "a"))  // true

// Predicates compare memberships.
print(Set.isSubset(Set("b"), left))             // true
print(Set.isSuperset(left, Set("a", "c")))      // true
print(Set.isDisjoint(left, Set("x")))           // true
print(Set.sameMembers(Set(1, 2), Set(2.0, 1)))  // true
print(IdentitySet.sameMembers(IdentitySet(1), IdentitySet(1.0)))  // false