Skip to content

Set

Canonical source: protos/lib/collections/Set.protos at locked revision e99b712ecf658a527b1fe82c8beba2c1286d8dc9.

// 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 represented as ordinary Maps keyed by their members (ordinary `hash` plus `==`
//! key equality).
/// Answers a fresh Set containing `elements`.
call: (...elements) => {
result: Map()
elements.each((element) => {
result[element] = true
})
result
}
/// Answers whether `element` is a member of `set`.
contains: (set, element) => {
set.containsKey(element)
}
/// Answers the number of members of `set`.
size: (set) => {
set.size()
}
/// Adds `element` to `set` in place and answers `set`.
add: (set, element) => {
set[element] = true
set
}
/// Removes `element` from `set` in place and answers `set`.
remove: (set, element) => {
set.remove(element)
set
}
/// Calls `block(element)` for each member of `set` and answers `set`.
each: (set, block) => {
set.each((element, marker) => {
block(element)
})
set
}
/// Answers a fresh Set of the members of `left` or `right`.
union: (left, right) => {
result: Map()
left.each((element, marker) => {
result[element] = true
})
right.each((element, marker) => {
result[element] = true
})
result
}
/// Answers a fresh Set of the members of `left` that are also in `right`.
intersection: (left, right) => {
result: Map()
left.each((element, marker) => {
right.containsKey(element).ifTrue(() => {
result[element] = true
})
})
result
}
/// Answers a fresh Set of the members of `left` that are not in `right`.
difference: (left, right) => {
result: Map()
left.each((element, marker) => {
right.containsKey(element).ifFalse(() => {
result[element] = true
})
})
result
}
/// Answers whether `left` and `right` have exactly the same members.
sameMembers: (left, right) => {
(left.size() == right.size()).ifFalse(() => {
^false
})
left.each((element, marker) => {
right.containsKey(element).ifFalse(() => {
^false
})
})
true
}
/// Answers whether every member of `left` is a member of `right`.
isSubset: (left, right) => {
left.each((element, marker) => {
right.containsKey(element).ifFalse(() => {
^false
})
})
true
}
/// Answers whether every member of `right` is a member of `left`.
isSuperset: (left, right) => {
right.each((element, marker) => {
left.containsKey(element).ifFalse(() => {
^false
})
})
true
}
/// Answers whether `left` and `right` have no member in common.
isDisjoint: (left, right) => {
left.each((element, marker) => {
right.containsKey(element).ifTrue(() => {
^false
})
})
true
}

← collections family · Standard-library source