Category talk:Wren-set: Difference between revisions
m
→Source code: Now uses Wren S/H lexer.
m (→Source code: Fixed syntax highlighting.) |
m (→Source code: Now uses Wren S/H lexer.) |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1:
===Source code===
<syntaxhighlight lang=
/* Set represents an unordered collection of unique objects. It is implemented as a Map
Line 104:
for (k in _m.keys) m[k] = 1
return m
}
// Copies the elements of the current instance to a Bag, with a value of 1, and
// returns the Bag.
toBag {
var b = Bag.new()
for (k in _m.keys) b.add(k, 1)
return b
}
Line 235 ⟶ 243:
isEmpty { _m.count == 0 }
//
distinct { _m.keys }▼
values { _m.values }
// Adds an element 'e' to the current instance. If the bag already contains 'e'
// its value is incremented.
add(e) {
Line 344 ⟶ 358:
// Returns whether or not the current instance contains no elements of a sequence.
containsNone(seq) { !containsAny(seq) }
▲ // Returns a sequence of all distinct elements in the current instance.
▲ distinct { _m.keys }
// Copies the elements of the current instance to a List and returns the List.
// An element with value 'v' is repeated 'v' times.
var l = []
for (k in _m.keys) {
Line 366 ⟶ 377:
return m
}
// Copies the distinct elements of the current instance to a Set and
// returns the set
toSet { Set.new(distinct) }
// Copies the elements of the current instance to a new Bag object.
Line 499 ⟶ 514:
// as the elements of another Bag and vice versa.
!=(other) { !(this == other) }
// Returns whether or not the distinct elements of this instance are the same
// as the distinct elements of another Bag and vice versa.
sameDistinct(other) {
if (other.type != Bag) Fiber.abort("Argument must be a Bag.")
if (_m.count != other.count) return false
for (k in _m.keys) {
if (!other.contains(k)) return false
}
return true
}
// Returns whether or not all elements of this instance are distinct.
allDistinct { _m.values.all { |v| v == 1 } }
// Returns the string representation of the current instance enclosed in angle brackets
|