class Collection
The root class in the collection hierarchy.
A collection represents a group of objects, known as its elements.
max(closure)
Answers the element that is considered to be/have the maximum value.
The criteria is given by a closure that receives a single element
as input (one of the element). The closure must return a comparable
value (something that understands the >, >= messages).
If collection is empty, an ElementNotFound exception is thrown.
Example:
["a", "ab", "abc", "d" ].max({ e => e.length() })
=> Answers "abc"
[].max({ e => e.length() })
=> Throws error, list must not be empty
max()
Answers the element that represents the maximum value in the collection.
The criteria is by direct comparison of the elements (they must be sortable).
If collection is empty, an ElementNotFound exception is thrown.
Example:
[11, 1, 4, 8, 3, 15, 6].max() => Answers 15
[].max() => Throws error, list must not be empty
maxIfEmpty(toComparableClosure, emptyCaseClosure)
Answers the element that is considered to be/have the maximum value,
or applies a closure if the collection is empty.
The criteria is given by a closure that receives a single element
as input (one of the element). The closure must return a comparable
value (something that understands the >, >= messages).
The closure to execute when the collection is empty is given as a second
argument.
Example:
["a", "ab", "abc", "d" ].maxIfEmpty({ e => e.length() }, { "default" })
=> Answers "abc"
[].maxIfEmpty({ e => e.length() }, { "default" })
=> Answers "default"
maxIfEmpty(emptyCaseClosure)
Answers the element that is considered to be/have the maximum value,
or applies a closure if the collection is empty.
The criteria is by direct comparison of the elements.
The closure to execute when the collection is empty is given as a second
argument.
Example:
[11, 1, 4, 8, 3, 15, 6].maxIfEmpty({ 99 }) => Answers 15
[].maxIfEmpty({ 99 }) => Answers 99
min(closure)
Answers the element that is considered to be/have the minimum value.
The criteria is given by a closure that receives a single element
as input (one of the element). The closure must return a comparable
value (something that understands the <, <= messages).
Example:
["ab", "abc", "hello", "wollok world"].min({ e => e.length() })
=> Answers "ab"
[].min({ e => e.length() })
=> Throws error, list must not be empty
min()
Answers the element that represents the minimum value in the
non-empty collection.
The criteria is by direct comparison of the elements.
Example:
[11, 1, 4, 8, 3, 15, 6].min() => Answers 1
[].min() => Throws error, list must not be empty
minIfEmpty(toComparableClosure, emptyCaseClosure)
Answers the element that is considered to be/have the minimum value,
or applies a closure if the collection is empty.
The criteria is given by a closure that receives a single element
as input (one of the element). The closure must return a comparable
value (something that understands the >, >= messages).
The closure to execute when the collection is empty is given as a second
argument.
Example:
["ab", "abc", "hello", "wollok world"].minIfEmpty({ e => e.length() }, { "default" })
=> Answers "ab"
[].minIfEmpty({ e => e.length() }, { "default" })
=> Answers "default"
minIfEmpty(emptyCaseClosure)
Answers the element that is considered to be/have the minimum value,
or applies a closure if the collection is empty.
The criteria is by direct comparison of the elements.
The closure to execute when the collection is empty is given as a second
argument.
Example:
[11, 1, 4, 8, 3, 15, 6].minIfEmpty({ 99 }) => Answers 1
[].minIfEmpty({ 99 }) => Answers 99
uniqueElement()
Answers the unique element in the collection.
If collection is empty, an error is thrown.
If collection has more than one element, an error is thrown.
Example:
[1].uniqueElement() => Answers 1
[].uniqueElement() => Throws error, list must not be empty
[1, 2].uniqueElement() => Throws error, list must have one element
+(elements)
Concatenates this collection to all elements from the given
collection parameter giving a new collection
(no side effect)
Example:
[1, 2] + [3] => Answers [1, 2, 3]
[1, 2] + #{3} => supports concatenation between lists and sets, answers [1, 2, 3]
#{} + [] => Answers #{}
addAll(elements)
Adds all elements from the given collection parameter to self collection.
This is a side effect operation.
Example:
const list = []
list.addAll(#{2, 4}) => list == [2, 4], always pointing to a list
removeAll(elements)
Removes all elements of the given collection parameter from self collection.
This is a side effect operation.
Example:
const list = [1, 6, 5]
list.removeAll([6]) => list == [1, 5]
removeAllSuchThat(closure)
Removes those elements that meet a given condition.
This is a side effect operation.
Supports empty collections.
Example:
const list = [1, 6, 5]
list.removeAllSuchThat { e => e.even() } => list == [1, 5]
isEmpty()
Tells whether self collection has no elements
Example:
[1, 6, 5].isEmpty() => Answers false
[].isEmpty() => Answers true
forEach(closure)
Performs an operation on every element of self collection.
The logic to execute is passed as a closure that takes a single parameter.
Supports empty collections.
@returns nothing
Example:
plants.forEach { plant => plant.takeSomeWater() }
all(predicate)
Answers whether all the elements of self collection satisfy a given
condition. The condition is a closure argument that takes a single
element and answers a boolean value.
@returns true/false
Example:
plants.all({ plant => plant.hasFlowers() })
[1, 3, 5].all { number => number.odd() } => Answers true
[].all { number => number.odd() } => Answers true
any(predicate)
Tells whether at least one element of self collection satisfies a
given condition. The condition is a closure argument that takes a
single element and answers a boolean value.
@returns true/false
Example:
plants.any({ plant => plant.hasFlowers() })
[1, 2, 3].any { number => number.even() } ==> Answers true
[].any { number => number.even() } ==> Answers false
find(predicate)
Answers the element of self collection that satisfies a given condition.
If more than one element satisfies the condition then it depends
on the specific collection class which element will be returned.
@returns the element that complies the condition
@throws ElementNotFoundException if no element matched the given predicate
Example:
users.find { user => user.name() == "Cosme Fulanito" }
#{1, 4, 5}.find { number => number.even() } => Answers 4
#{1, 3}.find { number => number.even() } => Throws ElementNotFoundException
#{}.find { number => number.even() } => Throws ElementNotFoundException
findOrDefault(predicate, value)
Answers the element of self collection that satisfies a given condition,
or the given default otherwise, if no element matched the predicate.
If more than one element satisfies the condition then it depends on the specific
collection class which element will be returned.
@returns the element that complies the condition or the default value
Example:
users.findOrDefault({ user => user.name() == "Cosme Fulanito" }, homer)
[1, 3, 5].findOrDefault({ number => number.even() }, 0) => Answers 0
[].findOrDefault({ number => number.even() }, 0) => Answers 0
findOrElse(predicate, continuation)
Answers the element of self collection that satisfies a given condition,
or the the result of evaluating the given continuation.
If more than one element satisfies the condition then it depends on the
specific collection class which element will be returned.
@returns the element that complies the condition or the result
of evaluating the continuation
Example:
users.findOrElse({ user => user.name() == "Cosme Fulanito" }, { homer })
[1, 3, 5].findOrElse({ number => number.even() }, { 6.max(4) }) => Answers 6
[].findOrElse({ number => number.even() }, { false }) => Answers false
count(predicate)
Counts all elements of self collection that satisfies a given condition
The condition is a closure argument that takes a single element and
answers a number.
@returns an integer number
Example:
plants.count { plant => plant.hasFlowers() }
#{1, 2, 3, 4, 5}.count { number => number.odd() } => Answers 3
#{}.count { number => number.odd() } => Answers 0
occurrencesOf(element)
Counts the occurrences of a given element in self collection.
@returns an integer number
Example:
[1, 8, 4, 1].occurrencesOf(1) => Answers 2
[].occurrencesOf(2) => Answers 0
sum(closure)
Collects the sum of each value for all elements.
This is similar to call a map {} to transform each element into a
number object and then adding all those numbers.
The condition is a closure argument that takes a single element and
answers a boolean value.
@returns an integer
Example:
const totalNumberOfFlowers = plants.sum{ plant => plant.numberOfFlowers() }
[].sum{ employee => employee.salary() } => Answers 0
#{2, 4}.sum{ num => num * 2} => Answers 12
sum()
Sums all elements in the collection.
@returns a number
Example:
[1, 2, 3, 4, 5].sum() => Answers 15
[].sum() => Answers 0
average(closure)
Calculates the average of the transformation of each element into a numerical value
This is similar to call a map {} to transform each element into a
number and calculates the average value of the resulting list.
The condition is a closure argument that takes a single element and
returns a number
@returns a number
Example:
const averageNumberOfFlowers = plants.average{ plant => plant.numberOfFlowers() }
[].average { employee => employee.salary() } => throws an error
average()
Calculates the average of all elements in the collection.
@returns a number
Example:
[1, 2, 3, 4, 5].average() => Answers 3
[].average() => throws an error
map(closure)
Answers a new collection that contains the result of transforming
each of self collection's elements using a given closure.
The condition is a closure argument that takes a single element
and answers an object.
@returns another list
Example:
const ages = users.map({ user => user.age() })
[1, 2, 3].map { number => number.odd() } => Answers [true, false, true]
[].map { number => number.odd() } => Answers []
flatMap(closure)
Flattens a collection of collections: Map + flatten operation
@see map
@see flatten
Example:
object klaus {
method languages() = ["c", "cobol", "pascal"]
}
object fritz {
method languages() = ["java", "perl"]
}
[klaus, fritz].flatMap({ person => person.languages() })
=> Answers ["c", "cobol", "pascal", "java", "perl"]
filter(closure)
Answers a new collection that contains the elements that
meet a given condition. The condition is a closure argument that
takes a single element and answers a boolean.
@returns another collection (same type as self one)
Example:
const overageUsers = users.filter({ user => user.age() >= 18 })
#{1, 2, 3, 4, 5}.filter { number => number.even() } => Answers #{2, 4}
[1, 2, 3].filter { number => number.even() } => Answers [2]
#{}.filter { number => number.even() } => Answers #{}
contains(element)
Answers whether this collection contains the specified element.
Example:
[].contains(3) => Answers false
[1, 2, 3].contains(2) => Answers true
flatten()
Flattens a collection of collections
Example:
[ [1, 2], [3], [4, 0], [] ].flatten()
=> Answers [1, 2, 3, 4, 0]
printString() --override
Provides a (short) visual representation of this collection.
asList()
Converts a collection to a list.
No effect on Lists.
Examples
#{1, 2, 3}.asList() => Answers [1, 2, 3]
#{}.asList() => Answers []
[1, 2, 3].asList() => Answers [1, 2, 3]
[].asList() => Answers []
asSet()
Converts a collection to a set (removing duplicates if necessary).
No effect on Sets.
Examples:
[1, 2, 3].asSet() => Answers #{1, 2, 3}
[].asSet() => Answers #{}
[1, 2, 1, 1, 2].asSet() => Answers #{1, 2}
#{1, 2, 3}.asSet() => Answers #{1, 2, 3}
#{}.asSet() => Answers #{}
copy()
Answers a new collection of the same type and with the same content
as self. Supports empty collections.
@returns a new collection
Example:
const usersCopy = users.copy()
copyWithout(elementToRemove)
Answers a new collection without element that is passed by parameter.
If the element occurs more than once in the collection, all occurrences
will be removed.
@returns a new Collection
Example:
[1, 5, 9, 2, 4].copyWithout(9) => Answers [1, 5, 2, 4]
[1, 5, 9, 2, 9].copyWithout(9) => Answers [1, 5, 2]
copyWith(elementToAdd)
Answers a new collection with the added element which is received by parameter.
@returns a new Collection
Example:
[1, 5, 9, 2, 4].copyWith(9) => Answers [1, 5, 9, 2, 4, 9]
#{1, 5, 3}.copyWith(9) => Answers #{1, 5, 3, 9}
sortedBy(closure)
Answers a new List that contains the elements of self collection
sorted by a criteria given by a closure. The closure receives two objects
X and Y and answers a boolean, true if X should come before Y in the
resulting collection. Supports empty collections.
@returns a new List
Example:
const usersByAge = users.sortedBy({ a, b => a.age() < b.age() })
const studentsByNameDesc = students.sortedBy({ a, b => a.name() > b.name() })
[1, 5, 9, 2, 4].sortedBy { a, b => a < b } => Answers [1, 2, 4, 5, 9]
[1, 5, 9, 2, 4].sortedBy { a, b => a > b } => Answers [9, 5, 4, 2, 1]
[].sortedBy { a, b => a > b } => Answers []
newInstance()
Answers a new, empty collection of the same type as self.
@returns a new collection
Example:
const newCollection = users.newInstance()
anyOne()
@see subclasses implementations
add(element)
@see subclasses implementations
remove(element)
@see subclasses implementations
fold(element, closure)
@see subclasses implementations
size()
@see subclasses implementations
clear()
Removes all of the elements from this set. This is a side effect operation.
@see subclasses implementations
join(separator)
Answers the concatenated string representation of the elements in the given set.
You can pass an optional character as an element separator (default is ",")
Example:
["hola", "como", "estas"].join(" ") ==> Answers "hola como estas"
join()
Answers the concatenated string representation of the elements in the given set
with default element separator (",")
Example:
["hola", "como", "estas"].join() ==> Answers "hola,como,estas"
class Set (inherits Collection)
A collection that contains no duplicate elements.
It models the mathematical set abstraction.
A Set guarantees no order of elements.
Note: Great care must be exercised if mutable objects are used as set elements.
The behavior of a set is not specified if the value of an object is changed in
a manner that affects equals comparisons while the object is an element in the set.
A special case of this prohibition is that it is not permissible for a set to contain
itself as an element.
anyOne() --override
Answers any element of a non-empty collection
Examples
#{1, 2, 3}.anyOne() => Answers 1, for example
#{}.anyOne() => Throws error, set must not be empty
union(another)
Answers a new Set with the elements of both self and another collection.
Examples
#{1, 2}.union(#{5, 2}) => #{1, 2, 5}
#{}.union(#{3}) => #{3}
@returns a Set
intersection(another)
Answers a new Set with the elements of self that exist in another collection
Examples
#{1, 2}.intersection(#{5, 2}) => #{2}
#{}.intersection(#{3}) => #{}
@returns a Set
difference(another)
Answers a new Set with the elements of self that don't exist in another collection
Examples
#{1, 2}.difference(#{5, 2}) => #{1}
#{3}.difference(#{}) => #{3}
@returns a Set
fold(closure) --override
Reduce a collection to a certain value, beginning with a seed or initial value.
Examples
#{1, 9, 3, 8}.fold(0, {acum, each => acum + each})
=> Answers 21, the sum of all elements
#{}.fold(0, {acum, each => acum + each})
=> Answers 0, the seed.
var numbers = #{3, 2, 9, 1, 7}
numbers.fold(numbers.anyOne(), { acum, number => acum.max(number) })
=> Answers 9, the maximum of all elements
filter(closure) --override
Answers a new set with the elements meeting
a given condition. The condition is a closure argument that
takes a single element and answers a boolean.
Example:
#{1, 2, 3, 4, 5}.filter { number => number.even() } => Answers #{2, 4}
#{}.filter { number => number.even() } => Answers #{}
@see Collection#filter(closure)
max() --override
Answers the element that represents the maximum value in the collection.
The criteria is by direct comparison of the elements.
If set is empty, an ElementNotFound exception is thrown.
Example:
#{1, 9, 3, 15}.max() => Answers 15
#{}.max() => Throws error, set must not be empty
@see Collection#max()
findOrElse(predicate, continuation) --override
Tries to find an element in a collection (based on a closure) or
applies a continuation closure.
Examples:
#{1, 9, 3, 8}.findOrElse({ n => n.even() }, { 100 }) => Answers 8
#{1, 5, 3, 7}.findOrElse({ n => n.even() }, { 100 }) => Answers 100
add(element) --override
Adds the specified element to this set if it is not already present.
Example:
const set = #{}
set.add(3) => set = #{3}
set.add(2) => set = #{2, 3}
set.add(2) => set = #{2, 3}, second add produces no effect
remove(element) --override
Removes the specified element from this set if it is present.
Example:
const set = #{2, 3}
set.remove(3) => set = #{2}
set.remove(4) => set = #{2}, remove operation produces no effect
size() --override
Answers the number of elements in this set (its cardinality).
Example:
#{2, 3}.size() => Answers 2
#{}.size() => Answers 0
clear() --override
Removes all of the elements from this set. This is a side effect operation.
Example:
const set = #{2, 3}
set.clear() => set = #{}
join(separator) --override
Answers the concatenated string representation of the elements in the given set.
You can pass an optional character as an element separator (default is ",")
Examples:
#{1, 5, 3, 7}.join(":") => Answers "1:5:3:7"
#{"you","will","love","wollok"}.join(" ") => Answers "love will wollok you"
#{}.join(",") => Answers ""
join() --override
Answers the concatenated string representation of the elements in the given set
with default element separator (",")
Example:
#{"you","will","love","wollok"}.join() => Answers "love,will,wollok,you"
contains(other) --override
Answers whether this collection contains the specified element.
Example:
#{}.contains(3) => Answers false
#{1, 2, 3}.contains(2) => Answers true
#{1, 2, 3}.contains(4) => Answers false
@see List#contains(other)
==(other) --override
Two sets are equals if they have the same elements, no matter
the order.
Examples:
#{} == #{} => Answers true
#{1, 2} == #{2, 1} => Answers true
#{3, 2} == #{2, 1} => Answers false
class List (inherits Collection)
An ordered collection (also known as a sequence).
You iterate the list the same order elements are inserted.
The user can access elements by their integer index (position in the list).
A List can contain duplicate elements.
get(index)
Answers the element at the specified position in this non-empty list.
The first char value of the sequence is at index 0,
the next at index 1, and so on, as for array indexing.
Index must be a positive and integer value.
Examples:
[].get(0) => Throws error, list must not be empty
[1].get(-1) => Throws error, index must be 0 or positive
[1, 2, 3].get(3) => Throws error, index exceeds list size
[5, 2, 7].get(0) => Answers 5
newInstance() --override
Creates a new list
anyOne() --override
Answers any element of a non-empty collection.
Examples
#[1, 2, 3].anyOne() => Answers 3, for example
#[].anyOne() => Throws error, list must not be empty
first()
Answers first element of the non-empty list
@returns first element
Example:
[1, 2, 3, 4].first() => Answers 1
[].first() => Throws error, list must not be empty
head()
Synonym for first method
last()
Answers the last element of the non-empty list.
@returns last element
Examples:
[1, 2, 3, 4].last() => Answers 4
[].last() => Throws error, list must not be empty
subList(start)
Answers a view of the portion of this list between the specified start index
and the end of the list. Remember first element is position 0,
second is position 1, and so on.
If toIndex exceeds length of list, no error is thrown.
Example:
[1, 5, 3, 2, 7, 9].subList(2) => Answers [3, 2, 7, 9]
[1, 5, 3, 2, 7, 9].subList(4) => Answers [7, 9]
[].subList(1) => Answers []
subList(start, end)
Answers a view of the portion of this list between the specified fromIndex
and toIndex, both inclusive. Remember first element is position 0,
second is position 1, and so on.
If toIndex exceeds length of list, no error is thrown.
Example:
[1, 5, 3, 2, 7, 9].subList(2, 3) => Answers [3, 2]
[1, 5, 3, 2, 7, 9].subList(4, 6) => Answers [7, 9]
[].subList(1, 2) => Answers []
sortBy(closure)
Sorts elements of a list by a specific closure.
Order of elements is modified (produces effect).
Examples:
const list = [2, 9, 3]
list.sortBy { el1, el2 => el1 > el2 }
list.get(0) => Answers 9
@see List#sortedBy
take(n)
Takes first n elements of a list.
Examples:
[1,9,2,3].take(5) ==> Answers [1, 9, 2, 3]
[1,9,2,3].take(2) ==> Answers [1, 9]
[1,9,2,3].take(-2) ==> Answers []
[].take(2) ==> Answers []
drop(n)
Answers a new list dropping first n elements of a list.
This operation has no side effect.
Examples:
[1, 9, 2, 3].drop(3) ==> Answers [3]
[1, 9, 2, 3].drop(1) ==> Answers [9, 2, 3]
[1, 9, 2, 3].drop(-2) ==> Answers [1, 9, 2, 3]
[].drop(2) ==> Answers []
reverse()
Answers a new list reversing the elements,
so that first element becomes last element of the new list and so on.
This operation has no side effect.
Example:
[1, 9, 2, 3].reverse() ==> Answers [3, 2, 9, 1]
[1, 2].reverse() ==> Answers [2, 1]
[].reverse() ==> Answers []
filter(closure) --override
Answers a new list with the elements meeting
a given condition. The condition is a closure argument that
takes a single element and answers a boolean.
Example:
[1, 2, 3, 4, 5].filter { number => number.even() } => Answers [2, 4]
[].filter { number => number.even() } => Answers []
@see Collection#filter(closure)
contains(obj) --override
Answers whether this collection contains the specified element.
Example:
[].contains(3) => Answers false
[1, 2, 3].contains(2) => Answers true
[1, 2, 3].contains(4) => Answers false
@see Collection#contains(obj)
max() --override
Answers the element that represents the maximum value in the collection.
The criteria is by direct comparison of the elements (they must be sortable).
If collection is empty, an ElementNotFound exception is thrown.
Example:
[11, 1, 4, 8, 3, 15, 6].max() => Answers 15
[].max() => Throws error, list must not be empty
@see Collection#max()
fold(initialValue, closure) --override
Reduce a collection to a certain value, beginning with a seed or initial value
Examples
[1, 9, 3, 8].fold(0, {acum, each => acum + each})
=> Answers 21, the sum of all elements
[].fold(0, {acum, each => acum + each})
=> Answers 0, the seed.
const numbers = [3, 2, 9, 1, 7]
numbers.fold(numbers.anyOne(), { acum, number => acum.max(number) })
=> Answers 9, the maximum of all elements
findOrElse(predicate, continuation) --override
Finds the first element matching the boolean closure,
or evaluates the continuation block closure if no element is found
Examples:
[1, 9, 3, 8].findOrElse({ n => n.even() }, { 100 }) => Answers 8
[1, 5, 3, 7].findOrElse({ n => n.even() }, { 100 }) => Answers 100
add(element) --override
Adds the specified element as last one
Example:
const list = []
list.add(3) => list = [3]
list.add(2) => list = [3, 2]
list.add(2) => list = [3, 2, 2]
remove(element) --override
Removes an element in this list, if it is present.
Example:
const list = [2, 3]
list.remove(3) => list = [2]
list.remove(4) => list = [2], remove operation produces no effect
size() --override
Answers the number of elements
Example:
[2, 3].size() => Answers 2
[].size() => Answers 0
clear() --override
Removes all of the mappings from this Dictionary.
This is a side effect operation.
Example:
const list = [2, 3]
list.clear() => list = []
join(separator) --override
Answers the concatenated string representation of the elements in the given set.
You can pass an optional character as an element separator (default is ",")
Examples:
[1, 5, 3, 7].join(":") => Answers "1:5:3:7"
["you","will","love","wollok"].join(" ") => Answers "you will love wollok"
join() --override
Answers the concatenated string representation of the elements in the given set,
using default element separator (",")
Examples:
["you","will","love","wollok"].join() => Answers "you,will,love,wollok"
==(other) --override
A list is == another list if all elements are equal (defined by == message)
Examples:
[] == [] => Answers true
[1, 2] == [2, 1] => Answers false
[1, 2] == [1, 2] => Answers true
withoutDuplicates()
Answers the list without duplicate elements. Preserves order of elements.
[1, 3, 1, 5, 1, 3, 2, 5].withoutDuplicates() => Answers [1, 3, 5, 2]
[].withoutDuplicates() => Answers []
randomize()
Shuffles the order of the elements in the list.
This is a side effect operation.
Examples:
const list = [1, 2 ,3]
list.randomize() => list = [2, 1, 3]
randomized()
Answers a new list of the same type and with the same content in a random order
Examples:
[1, 2, 3, 4].randomized() => Answers [2, 3, 1, 4]
[1, 2, 3, 4].randomized() => Answers [2, 1 ,4 ,3]
class Dictionary
Represents a set of key -> values
initialize() --override
No description
put(_key, _value)
Adds or updates a value based on a key.
If key is not present, a new value is added.
If key is present, value is updated.
This is a side effect operation.
Example:
const phones = new Dictionary()
phones.put("4004-4004", rolo)
=> phones == a Dictionary ["4004-4004" -> rolo]
basicGet(_key)
Answers the value to which the specified key is mapped,
or null if this Dictionary contains no mapping for the key.
Example, assuming phones is the dictionary created in put example:
phones.basicGet("4004-4004") => Answers rolo
phones.basicGet("4004-4005") => Answers null
getOrElse(_key, _closure)
Answers the value to which the specified key is mapped,
or evaluates a non-parameter closure otherwise.
Example, assuming phones is the dictionary created in put example:
phones.getOrElse("4004-4004", { 0 }) => Answers rolo
phones.getOrElse("4004-4005", { 0 }) => Answers 0
get(_key)
Answers the value to which the specified key is mapped.
If this Dictionary contains no mapping for the key, an error is thrown.
Example, assuming phones is the dictionary created in put example:
phones.get("4004-4004") => Answers rolo
phones.get("4004-4005") => Throws ElementNotFoundException
size()
Answers the number of key-value mappings in this Dictionary.
Example, assuming phones is the dictionary created in put example:
phones.size() => Answers 1
new Dictionary().size() => Answers 0
isEmpty()
Answers whether the dictionary has no elements
Example, assuming phones is the dictionary created in put example:
phones.isEmpty() => Answers false
new Dictionary().isEmpty() => Answers true
containsKey(_key)
Answers whether this Dictionary contains a mapping for the specified key.
Example, assuming phones is the dictionary created in put example:
phones.containsKey("4004-4004") => Answers true
phones.containsKey("4004-4005") => Answers false
new Dictionary().containsKey(1) => Answers false
containsValue(_value)
Answers whether if this Dictionary maps one or more keys to the specified value.
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.containsValue(2) => Answers true
numbers.containsValue(5) => Answers false
new Dictionary().containsValue(3) => Answers false
remove(_key)
Removes the mapping for a key from this Dictionary if it is present.
If key is not present nothing happens.
This is a side effect operation.
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.remove("one") => numbers is a dictionary ("two" -> 2)
numbers.remove("three") => nothing happens
keys()
Answers a list of the keys contained in this Dictionary.
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.keys() => ["one", "two"]
values()
Answers a list of the values contained in this Dictionary.
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.values() => [1, 2]
forEach(closure)
Performs the given action for each entry in this Dictionary
until all entries have been processed or the action throws an exception.
Expected closure with two parameters: the first associated with key and
second with value.
Example:
mapaTelefonos.forEach({ k, v => result += k.size() + v.size() })
clear()
Removes all of the mappings from this Dictionary.
This is a side effect operation.
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.clear() => phones == empty dictionary
toString() --override
String representation of a Dictionary
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
=> Answers a Dictionary ["one" -> 1, "two" -> 2]
==(other) --override
Two dictionaries are equal if they have the same keys and values
class Number
In Wollok we have numbers as an immutable representation.
By default Wollok uses a maximum of 5 decimal places and rounds up if
the last decimal is 5 or greater, otherwise rounds down.
To print numbers, it removes non-representative zeros.
> 5.2912413 ==> Answers 5.29124
> 5.2912488 ==> Answers 5.29125
> 1.000005 - 1.000004 ==> Answers 0.00001
> 1.000002 - 1.000001 ==> Answers 0
@noInstantiate
===(other) --override
Two references are identical if they are the same number
+(other)
Example:
2 + 3 ==> Answers 5
-(other)
Example:
5 - 3 ==> Answers 2
*(other)
Example:
2 * 3 ==> Answers 6
/(other)
Example:
8 / 4 ==> Answers 2
div(other)
Integer division between self and other
Example:
8.div(3) ==> Answers 2
15.div(5) ==> Answers 3
8.2.div(3.3) ==> Answers 2
**(other)
raisedTo operation
Example:
3.2 ** 2 ==> Answers 10.24
3 ** 2 ==> Answers 9
%(other)
Answers remainder of division between self and other
toString() --override
String representation of self number
..(end)
Builds a Range between self and end
Example:
1..4 Answers ==> a new Range object from 1 to 4
>(other)
Example:
3 > 2 ==> Answers true
2 > 3 ==> Answers false
2 > 2 ==> Answers false
<(other)
Example:
2 < 3 ==> Answers true
3 < 2 ==> Answers false
2 < 2 ==> Answers false
>=(other)
Example:
3 >= 2 ==> Answers true
2 >= 3 ==> Answers false
2 >= 2 ==> Answers true
<=(other)
Example:
2 <= 3 ==> Answers true
3 <= 2 ==> Answers false
2 <= 2 ==> Answers true
abs()
Answers absolute value of self
Example:
2.abs() ==> 2
(-3).abs() ==> 3 (be careful with parentheses)
2.7.abs() ==> Answers 2.7
(-3.2).abs() ==> Answers 3.2 (be careful with parentheses)
invert()
Inverts sign of self
Example:
3.invert() ==> Answers -3
(-2).invert() ==> Answers 2 (be careful with parentheses)
3.2.invert() ==> -3.2
(-2.4).invert() ==> 2.4 (be careful with parentheses)
max(other)
Answers the greater number between two
Example:
5.max(8) ==> Answers 8
min(other)
Answers the lower number between two. @see max
Example:
5.min(8) ==> Answers 5
limitBetween(limitA, limitB)
Given self and a range of integer values,
answers self if it is in that range
or nearest value from self to that range
Examples
4.limitBetween(2, 10) ==> Answers 4, because 4 is in the range
4.limitBetween(6, 10) ==> Answers 6, because 4 is not in range 6..10, and 6 is nearest value to 4
4.limitBetween(1, 2) ==> Answers 2, because 4 is not in range 1..2, but 2 is nearest value to 4
between(min, max)
Answers whether self is between min and max
Example:
2.between(2, 3) ==> Answers true
6.between(4, 6) ==> Answers true
3.between(4, 6) ==> Answers false
squareRoot()
Answers squareRoot of self
Example:
9.squareRoot() => Answers 3
square()
Answers square of self
Example:
3.square() => Answers 9
even()
Answers whether self is an even number
(divisible by 2, mathematically 2k).
Self must be an integer value
odd()
Answers whether self is an odd number
(not divisible by 2, mathematically 2k + 1).
Self must be an integer value
rem(other)
Answers remainder between self and other
Example:
5.rem(3) ==> Answers 2
5.5.rem(3) ==> Answers 2
stringValue()
Self as String value. Equivalent: toString()
roundUp(_decimals)
Rounds up self up to a certain amount of decimals.
Amount of decimals must be a positive and integer value.
Example:
1.223445.roundUp(3) ==> 1.224
-1.223445.roundUp(3) ==> -1.224
14.6165.roundUp(3) ==> 14.617
5.roundUp(3) ==> 5
truncate(_decimals)
Truncates self up to a certain amount of decimals.
Amount of decimals must be a positive and integer value.
Example:
1.223445.truncate(3) ==> 1.223
14.6165.truncate(3) ==> 14.616
-14.6165.truncate(3) ==> -14.616
5.truncate(3) ==> 5
randomUpTo(max)
Answers a random number between self and max
roundUp()
Answers the next integer greater than self
Example:
13.224.roundUp() ==> 14
-13.224.roundUp() ==> -14
15.942.roundUp() ==> 16
round()
Returns the value of a number rounded to the nearest integer.
floor()
Converts a decimal number into an integer truncating the decimal part.
Example:
5.5.floor() ==> Answers 5
5.floor() ==> Answers 5
gcd(other)
greater common divisor.
Both self and "other" parameter are coerced to be integer values.
Example:
8.gcd(12) ==> Answers 4
5.gcd(10) ==> Answers 5
lcm(other)
least common multiple.
Both self and "other" parameter are coerced to be integer values.
Example:
3.lcm(4) ==> Answers 12
6.lcm(12) ==> Answers 12
digits()
Number of digits of self (without sign)
Examples:
600.digits() ==> Answers 3
6.00012.digits() ==> Answers 6
-100.digits() ==> Answers -3
isInteger()
Tells if this number can be considered an integer number.
Examples:
2.isInteger() ==> Answers true
(2.0).isInteger() ==> Answers true
(2.3).isInteger() ==> Answers false
This could depend also on the rounding strategy, for example:
(2.0001).isInteger() ==> Answers false if rounding strategy is set to 5 decimal places (default)
(2.0001).isInteger() ==> Answers true if rounding strategy is set to 3 decimal places
isPrime()
Answers whether self is a prime number,
like 2, 3, 5, 7, 11 ...
Self must be an integer positive value
times(action)
Executes the given action n times (n = self)
Self must be a positive integer value.
The closure must have one argument (index goes from 1 to self)
Example:
4.times({ i => console.println(i) }) ==> Answers
1
2
3
4
plus()
Allows users to define a positive number with 1 or +1
class String
Strings are constant;
their values cannot be changed after they are created.
@noInstantiate
length()
Answers the number of elements
charAt(index)
Answers the char value at the specified index. An index ranges
from 0 to length() - 1. The first char value of the sequence is
at index 0, the next at index 1, and so on, as for array indexing.
Parameter index must be a positive integer value.
+(other)
Concatenates the specified string to the end of this string.
Example:
"cares" + "s" => Answers "caress"
concat(other)
Concatenates the specified string to the end of this string. Same as +.
Example:
"cares".concat("s") => Answers "caress"
startsWith(prefix)
Tests if this string starts with the specified prefix.
It is case sensitive.
Examples:
"mother".startsWith("moth") ==> Answers true
"mother".startsWith("Moth") ==> Answers false
endsWith(suffix)
Tests if this string ends with the specified suffix.
It is case sensitive.
@see startsWith
indexOf(other)
Answers the index within this string of the first occurrence
of the specified character.
If character is not present, Answers -1
Examples:
"pototo".indexOf("o") ==> Answers 1
"unpredictable".indexOf("o") ==> Answers -1
lastIndexOf(other)
Answers the index within this string of the last
occurrence of the specified character.
If character is not present, Answers -1
Examples:
"pototo".lastIndexOf("o") ==> Answers 5
"unpredictable".lastIndexOf("o") ==> Answers -1
toLowerCase()
Converts all of the characters in this String to lower case
Examples:
"Fer".toLowerCase() ==> Answers "fer"
"".toLowerCase() ==> Answers ""
toUpperCase()
Converts all of the characters in this String to upper case
Examples:
"Fer".toUpperCase() ==> Answers "FER"
"".toUpperCase() ==> Answers ""
trim()
Answers a string whose value is this string,
with any leading and trailing whitespace removed.
Example:
" emptySpace ".trim() ==> "emptySpace"
reverse()
Answers a string reversing this string,
so that first character becomes last character of the new string and so on.
Example:
"hola".reverse() ==> "aloh"
takeLeft(length)
@see take
Example:
"word".takeLeft(3) ==> Answers "wor"
"word".takeLeft(0) ==> Answers ""
"word".takeLeft(-1) ==> Throws error
"".takeLeft(2) ==> Answers ""
takeRight(_length)
Takes last n characters of this string.
n must be zero-positive integer.
Example:
"word".takeRight(3) ==> Answers "ord"
"word".takeRight(0) ==> Answers ""
"word".takeRight(-1) ==> Throws error
"".takeRight(2) ==> Answers ""
<(aString)
Example:
"apple" < "book" ==> Answers true
"book" < "car" ==> Answers true
"apple" < "apple" ==> Answers false
<=(aString)
Example:
"apple" <= "book" ==> Answers true
"book" <= "car" ==> Answers true
"apple" <= "apple" ==> Answers true
>(aString)
Example:
"car" > "book" ==> Answers true
"book" > "apple" ==> Answers true
"apple" > "apple" ==> Answers false
>=(aString)
Example:
"car" >= "book" ==> Answers true
"book" >= "apple" ==> Answers true
"apple" >= "apple" ==> Answers true
contains(element)
Answers whether this string contains the specified sequence of char values.
It is a case sensitive test.
Examples:
"unusual".contains("usual") ==> Answers true
"become".contains("CO") ==> Answers false
isEmpty()
Answers whether this string has no characters
equalsIgnoreCase(aString)
Compares this String to another String, ignoring case considerations.
Example:
"WoRD".equalsIgnoreCase("Word") ==> Answers true
substring(index)
Answers a substring of this string beginning from
an inclusive index. Parameter index must be a positive
integer value.
Examples:
"substitute".substring(6) ==> Answers "tute", second "t" is in position 6
"effect".substring(0) ==> Answers "effect", has no effect at all
substring(startIndex, endIndex)
Answers a substring of this string beginning
from an inclusive index up to another inclusive index
Examples:
"walking".substring(2, 4) ==> Answers "lk"
"walking".substring(3, 5) ==> Answers "ki"
"walking".substring(0, 5) ==> Answers "walki"
"walking".substring(0, 45) ==> throws an out of range exception
split(expression)
Splits this string around matches of the given string.
Answers a list of strings.
Example:
"this,could,be,a,list".split(",")
==> Answers ["this", "could", "be", "a", "list"]
"Esto Es una prueba".split(" ")
==> Answers ["Esto", "Es", "una", "prueba"]
"Esto Es una".split("")
==> Answers ["E","s","t","o"," ","E","s"," ","u","n","a"] , splitting into a character list
"Esto Es una".split("|")
==> Answers ["Esto Es una"], the same original string
"texto de prueba".split("texto de prueba")
==> Answers ["",""]
"a,b,,c,".split(",")
==> Answers ["a", "b", "", "c", ""]
"texto de prueba".split("de")
==> Answers ["texto ", " prueba"]
"aaaa".split("aa")
==> Answers ["", "", ""]
replace(expression, replacement)
Answers a string resulting from replacing all occurrences of
expression in this string with replacement
Example:
"stupid is what stupid does".replace("stupid", "genius")
==> Answers "genius is what genius does"
toString() --override
This object (which is already a string!) is itself returned
printString() --override
String implementation of printString,
simply adds quotation marks
==(other) --override
Compares this string to the specified object.
The result is true if and only if the
argument is not null and is a String object
that represents the same sequence of characters as this object.
size()
A synonym for length
take(n)
Takes first n characters of this string.
n must be zero-positive integer.
Examples:
"lowercase".take(3) ==> Answers "low"
"lowercase".take(0) ==> Answers ""
"lowercase".take(-1) ==> Throws error
"".take(2) ==> Answers ""
drop(n)
Answers a new string dropping
first n characters of this string.
n must be zero-positive integer.
Examples:
"caption".drop(4) ==> Answers "ion"
"caption".drop(0) ==> Answers "caption"
"caption".drop(-1) ==> Throws error
"".drop(2) ==> Answers ""
words()
Splits this strings into several words.
Examples:
"how does words work?".words()
==> Answers ["how", "does", "words", "work?"]
"".words() ==> Answers []
capitalize()
Changes the first letter of every word to
upper case in this string.
Example:
"javier fernandes".capitalize() ==> Answers "Javier Fernandes"
class Range
Represents a finite arithmetic progression
of integer numbers with optional step
If start = 1, end = 8, Range will represent [1, 2, 3, 4, 5, 6, 7, 8]
If start = 1, end = 8, step = 3, Range will represent [1, 4, 7]
Both start and end must be integer values.
Example:
new Range(start = 1, end = 3) ==> Answers [1, 2, 3]
(1..3) ==> Answers [1, 2, 3]
new Range(start = 2, end = 8, step = 2) ==> Answers [2, 4, 6, 8]
start()
Getter for start attribute
end()
Getter for end attribute
step(_step)
Setter for step attribute.
forEach(closure)
Iterates over a Range from start to end, based on step.
Example:
new Range(start = 1, end = 3).forEach { value => console.println(value) }
=> prints 1, 2, 3
map(closure)
Answers a new collection that contains the result of
transforming each of self collection's elements using
a given closure.
The condition is a closure argument that takes an integer
and answers an object.
@returns another list
Example:
(1..10).map({ n => n * 2}) ==> Answers [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
flatMap(closure)
Map + flatten operation
@see map
@see flatten
Example:
(1..4).flatMap({ n => 1 .. n }) ==> Answers [1, 1, 2, 1, 2, 3, 1, 2, 3, 4]
isEmpty()
Answers whether this range contains no elements
@see Collection#isEmpty()
fold(seed, foldClosure)
Reduce a range to a certain value, beginning with a seed or initial value.
Examples
(1..5).fold(0, {acum, each => acum + each})
=> Answers 15, the sum of all elements
@see List#fold(seed, foldClosure)
size()
Answers the number of elements
Examples:
new Range(start = 0, end = 2).size() ==> Answers 3
new Range(start = -2, end = 2).size() ==> Answers 5
any(closure)
Tells whether at least one element of range satisfies a
given condition. The condition is a closure argument that takes a
number and answers a boolean value.
@returns true/false
Example:
(1..5).any { number => number.even() } ==> Answers true
@see List#any(closure)
all(closure)
Answers whether all the elements of range satisfy a given
condition. The condition is a closure argument that takes a number
and answers a boolean value.
@returns true/false
Example:
(1..5).all { number => number.odd() } => Answers false
@see List#all(closure)
filter(closure)
Answers a new list with the elements meeting
a given condition. The condition is a closure argument that
takes a single element and answers a boolean.
Example:
(1..4).filter({ number => number.even() }) => Answers [2, 4]
@see List#filter(closure)
min()
Answers the element that represents the minimum value in the range.
The criteria is by direct comparison of the elements (they must be sortable).
Example:
(1..5).min() => Answers 1
@see List#min()
max()
Answers the element that represents the maximum value in the range.
Example:
(1..15).max() => Answers 15
new Range(start = 2, end = 5).max() => Answers 5
@see List#max()
anyOne()
Answers a random integer contained in the range
Example:
new Range(start = 1, end = 3).anyOne() ==> Answers 1 or 2 or 3
contains(element)
Tests whether a number is contained in the range
Examples:
new Range(start = 2, end = 5).contains(4) ==> Answers true
(new Range(start = 2, end = 5)).contains(0) ==> Answers false
sum()
Sums all elements in the collection.
@returns a number
Example:
(1..5).sum() => Answers 15
@see List#sum()
sum(closure)
Sums all elements that match the boolean closure
Example:
(1..9).sum({ i => if (i.even()) i else 0 }) ==> Answers 20
count(closure)
Counts how many elements match the boolean closure
Example:
(1..9).count({ i => i.even() }) ==> Answers 4 (2, 4, 6 and 8 are even)
find(closure)
Answers the number of the range that satisfies a given condition.
@throws ElementNotFoundException if no element matched the given predicate
Example:
(1..5).find { number => number.even() } ==> Answers 2
@see List#find(closure)
findOrElse(closure, continuation)
Finds the first number matching the boolean closure,
or evaluates the continuation block closure if no element is found
Examples:
(1..5).findOrElse({ number => number < 0 }, { 100 }) => Answers 100
(1..5).findOrElse({ number => number.even() }, { 100 }) => Answers 2
@see List#findOrElse(predicate, continuation)
findOrDefault(predicate, value)
Answers the number of the range that satisfies a given condition,
or the given default otherwise, if no element matched the predicate.
Example:
(1..5).findOrDefault({ number => number.even() }, 0) => Answers 2
(1..5).findOrDefault({ number => number < 0 }, 0) => Answers 0
@see List#findOrDefault(predicate, value)
sortedBy(closure)
Answers a new List that contains the elements of self collection
sorted by a criteria given by a closure. The closure receives two objects
X and Y and answers a boolean, true if X should come before Y in the
resulting collection.
@returns a new List
Example:
(1..5).sortedBy { a, b => a > b } => Answers [5, 4, 3, 2, 1]
@see List#sortBy
toString() --override
String representation of this range object
class Date
Represents a Date (without time). A Date is immutable, once created you can not change it.
toString() --override
String representation of a date
==(_aDate) --override
Two dates are equals if they represent the same date
plusDays(_days)
Answers a copy of this Date with the specified number of days added.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Example:
new Date(day = 12, month = 5, year = 2018).plusDays(1)
==> Answers 13/5/2018, a day forward
new Date(day = 12, month = 5, year = 2018).plusDays(-1)
==> Answers 11/5/2018, a day back
plusMonths(_months)
Answers a copy of this Date with the specified number of months added.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Example:
new Date(day = 31, month = 1, year = 2018).plusMonths(1)
==> Answers 28/2/2018, a month forward
new Date(day = 12, month = 5, year = 2018).plusMonths(-1)
==> Answers 12/4/2018, a month back
plusYears(_years)
Answers a copy of this Date with the specified number of years added.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Example:
new Date(day = 31, month = 1, year = 2018).plusYears(1)
==> Answers 31/1/2019, a year forward
new Date(day = 12, month = 5, year = 2018).plusYears(-1)
==> Answers 12/5/2017, a year back
isLeapYear()
Checks if the year is a leap year, like 2000, 2004, 2008...
Example:
new Date(day = 12, month = 5, year = 2018).isLeapYear() ==> Answers false
dayOfWeek()
Answers the day of the week of the Date with an object representation.
There is a wko (well known object) for every day of the week.
Example:
new Date(day = 24, month = 2, year = 2018).dayOfWeek() ==> Answers "saturday"
internalDayOfWeek()
Answers the day of week of the Date, where
1 = MONDAY
2 = TUESDAY
3 = WEDNESDAY
...
7 = SUNDAY
Example:
new Date(day = 24, month = 2, year = 2018).internalDayOfWeek() ==> Answers 6 (SATURDAY)
-(_aDate)
Answers the difference in days between two dates, assuming self is minuend and _aDate is subtrahend.
Examples:
new Date().plusDays(4) - new Date() ==> Answers 4
new Date() - new Date().plusDays(2) ==> Answers -2
minusDays(_days)
Answers a copy of this date with the specified number of days subtracted.
This instance is immutable and unaffected by this method call.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Examples:
new Date(day = 1, month = 1, year = 2009).minusDays(1)
==> Answers 31/12/2008, a day back
new Date(day = 1, month = 1, year = 2009).minusDays(-1)
==> Answers 2/1/2009, a day forward
minusMonths(_months)
Answers a copy of this date with the specified number of months subtracted.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Examples:
new Date(day = 1, month = 1, year = 2009).minusMonths(1)
==> Answers 1/12/2008, a month back
new Date(day = 1, month = 1, year = 2009).minusMonths(-1)
==> Answers 1/2/2009, a month forward
minusYears(_years)
Answers a copy of this date with the specified number of years subtracted.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).
Examples:
new Date(day = 1, month = 1, year = 2009).minusYears(1)
==> Answers 1/1/2008, a year back
new Date(day = 1, month = 1, year = 2009).minusYears(-1)
==> Answers 1/1/2010, a year forward
<=(_aDate)
No description
>=(_aDate)
No description
between(_startDate, _endDate)
Answers whether self is between two dates (both inclusive comparison)
Example:
new Date(day = 2, month = 4, year = 2018).between(new Date(day = 1, month = 4, year = 2018), new Date(day = 2, month = 4, year = 2018))
==> Answers true
shortDescription() --override
Shows a short, internal representation of a date
(the result varies depending on user's locale)
Example:
new Date(day = 2, month = 4, year = 2018).shortDescription()
==> Answers 2/4/2018
isWorkDay()
Answer whether the day is a work day (between monday and friday)
Example:
new Date(day = 01, month = 11, year = 2024).isWorkDay()
==> Answers true
new Date(day = 02, month = 11, year = 2024).isWorkDay()
==> Answers false
isWeekendDay()
Answer whether the day is a weekend day (saturday or sunday)
Example:
new Date(day = 02, month = 11, year = 2024).isWeekendDay()
==> Answers true
new Date(day = 01, month = 11, year = 2024).isWeekendDay()
==> Answers false