Category talk:Wren-set: Difference between revisions

m
→‎Source code: Now uses Wren S/H lexer.
(→‎Source code: Added a Bag class plus couple other minor changes.)
m (→‎Source code: Now uses Wren S/H lexer.)
 
(7 intermediate revisions by the same user not shown)
Line 1:
===Source code===
 
<langsyntaxhighlight ecmascriptlang="wren">/* Module "set.wren" */
 
/* 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 }
 
// AddsReturns ana elementsequence 'e'of toall distinct elements in the current instance. If the set already contains 'e'
distinct { _m.keys }
 
// Returns a sequence of all distinct elementsvalues in the current instance.
values { _m.values }
 
// Adds an element 'e' to the current instance. If the bag already contains 'e'
// its value is incremented.
add(e) {
Line 282 ⟶ 296:
}
 
// Merges all the elements of another Bag or Set object into the current instance
// increasing values where necessary. A specialized version of 'addAll'.
merge(other) {
if (other.type != Bag && other.type != Set) {
Fiber.abort("Argument must be a Bag or a Set.")
}
for (e in other.distinct) _m[e] = _m.containsKey(e) ? _m[e] + other[e] : other[e]
}
 
// Removes an element 'e' from the current instance, if it exists and whether distinct
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.
toListtoFlatList {
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 374 ⟶ 389:
}
 
// Copies all elements of this instance and another Bag or Set to a new Bag object.
union(other) {
if (other.type != Bag && other.type != Set) {
Fiber.abort("Argument must be a Bag or a Set.")
}
var b = Bag.new()
Line 385 ⟶ 400:
}
 
// Copies all elements which this instance and another Bag or Set have in common
// to a new Bag object.
intersect(other) {
if (other.type != Bag && other.type != Set) {
Fiber.abort("Argument must be a Bag or a Set.")
}
var b = Bag.new()
Line 403 ⟶ 418:
}
 
// Copies all elements of this instance which are not elements of another Bag nor Set
// to a new Bag object.
except(other) {
if (other.type != Bag && other.type != Set) {
Fiber.abort("Argument must be a Bag or a Set.")
}
var b = Bag.new()
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
Line 511 ⟶ 540:
 
iteratorValue(iter) { _m.iteratorValue(iter) }
}</syntaxhighlight>
}
 
// Type alias for class in case of a name clash with other modules.
var Set_Set = Set
var Set_Bag = Bag</lang>
9,482

edits