Sie sind auf Seite 1von 42

12/11/2018 WollokDoc · Wollok

ub
itH
G
en
rk
fo
tu

  English version
é
ac
H

  Guía completa del lenguaje


Esta es la guía completa de las bibliotecas de objetos y clases que vienen con
Wollok.

lib.wlk vm.wlk lang.wlk mirror.wlk game.wlk

Exception
Base class for all Exceptions. Every exception and its subclasses
indicates conditions that a reasonable application might want to catch.

author jfernandes
since 1.0

Constructors
constructor()
Constructs a new exception with no detailed message.

constructor(_message)
Constructs a new exception with the speci ed detail message.

constructor(_message, _cause)
Constructs a new exception with the speci ed detail message and cause.

Behavior
Method Description

createStackTraceElement private
( D i i
https://www.wollok.org/documentacion/wollokdoc/ 1/42
12/11/2018 WollokDoc · Wollok
(contextDescription,
Method
location) Description

equals (other) Overrides the behavior to compare exceptions

getCause () Answers the cause of the exception, if present

getFullStackTrace () Provides programmatic access to the stack trace


native information
printed by printStackTrace() with full path les for linking

getMessage () Answers the detail message string of this exception.

getStackTrace () native Provides programmatic access to the stack trace


information
printed by printStackTrace().

getStackTraceAsString () Prints this exception and its backtrace as a string value

printStackTrace () Prints this exception and its backtrace to the console

printStackTrace (printer) Prints this exception and its backtrace to the speci ed
printer

printStackTraceWithPre x private
(pre x, printer)

StackOver owException
inherits from Exception

Thrown when a stack over ow occurs because an application recurses too deeply.

author jfernandes
since 1.5.1

ElementNotFoundException
inherits from Exception

An exception that is thrown when a speci ed element cannot be found

Constructors
constructor(_message)

constructor(_message, _cause)

https://www.wollok.org/documentacion/wollokdoc/ 2/42
12/11/2018 WollokDoc · Wollok

MessageNotUnderstoodException
inherits from Exception

An exception that is thrown when an object cannot understand a certain


message

Constructors
constructor()

constructor(_message)

constructor(_message, _cause)

StackTraceElement
An element in a stack trace, represented by a context and a location
of a method where a message was sent

Constructors
constructor(_contextDescription, _location)

Object
Representation of Wollok Object

Class Object is the root of the class hierarchy.


Every class has Object as a superclass.

author jfernandes
since 1.0

Behavior
Method Description

!= (other) Tells whether self object is not equal to the


https://www.wollok.org/documentacion/wollokdoc/ i 3/42
12/11/2018 WollokDoc · Wollok
given one
Method Description
!== (other) Tells whether self object is not identical (the
same) to the given one.
See === message.

-> (other) Generates a Pair key-value association. see


Pair.

== (other) Tells whether self object is "equal" to the


given object
The default behavior compares them in
terms of identity (===)

=== (other) Tells whether self object is identical (the


same) to the given one.
It does it by comparing their identities.
So self basically relies on the
wollok.lang.Integer equality (which is native)

className () native Full name of Wollok object class

equals (other) o1.equals(o2) is a synonym for o1 == o2

error (message) Builds an exception with a message

generateDoesNotUnderstandMessage private
(target, messageName,
parametersSize) native internal method: generates a does not
understand message
parametersSize must be an integer value

identity () native Answers object identity of a Wollok object,


represented by
a unique number in Wollok environment

instanceVariableFor (name) native Retrieves a speci c variable. Expects a name

instanceVariables () native Answers a list of instance variables for this


Wollok object

internalToSmartString (alreadyShown) private

kindName () native Object description in english/spanish/...


https://www.wollok.org/documentacion/wollokdoc/ (d di i18 i ) 4/42
12/11/2018 WollokDoc · Wollok
(depending on i18n con guration)
Method Description
Examples:
"2".kindName() => Answers "a String"
2.kindName() => Answers "a Integer"

messageNotUnderstood private
(messageName, parameters)

printString () Provides a visual representation of Wollok


Object
By default, same as toString but can be
overriden
like in String

resolve (name) native Accesses a variable by name, in a re exive


way.

simpli edToSmartString () private

toSmartString (alreadyShown) private

toString () String representation of Wollok object

void
Representation for methods that only have side effects

Pair
Representation of a Key/Value Association.
It is also useful if you want to model a Point.

Constructors
constructor(_x, _y)

Behavior
Method Description

key ()

value ()
https://www.wollok.org/documentacion/wollokdoc/ 5/42
12/11/2018 WollokDoc · Wollok

Collection
The root class in the collection hierarchy.
A collection represents a group of objects, known as its elements.

Behavior
Method Description

+ (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 #{}

absolute (closure, private


criteria)

add (element) see subclasses implementations

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

all (predicate) Answers whether all the elements of self collection satisfy a
https://www.wollok.org/documentacion/wollokdoc/ i 6/42
12/11/2018 WollokDoc · Wollok
given
Method Description
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


satis es 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

anyOne () see subclasses implementations

asList () abstract Converts a collection to a list

asSet () abstract Converts a collection to a set (no duplicates)

contains (e) Answers whether this collection contains the speci ed


element.

Example:
[].contains(3) => Answers false
[1, 2, 3].contains(2) => Answers true

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()

count (predicate) Counts all elements of self collection that satis es a given
https://www.wollok.org/documentacion/wollokdoc/ di i 7/42
12/11/2018 WollokDoc · Wollok
condition
Method Description
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

lter (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. lter({ user => user.age() >= 18 })
#{1, 2, 3, 4, 5}. lter { number => number.even() } => Answers
#{2, 4}
#{}. lter { number => number.even() } => Answers #{}

nd (predicate) Answers the element of self collection that satis es a given


condition.
If more than one element satis es the condition then it
depends
on the speci c 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. nd { user => user.name() == "Cosme Fulanito" }
#{1, 4, 5}. nd { number => number.even() } => Answers 4
#{1, 3}. nd { number => number.even() } => Throws
ElementNotFoundException
#{}. nd { number => number.even() } => Throws
ElementNotFoundException

ndOrDefault Answers the element of self collection that satis es a given


( di l )
https://www.wollok.org/documentacion/wollokdoc/ di i 8/42
12/11/2018 WollokDoc · Wollok
(predicate, value) condition,
Method Description
or the given default otherwise, if no element matched the
predicate.
If more than one element satis es the condition then it
depends on the speci c
collection class which element will be returned.

returns the element that complies the condition or the


default value

Example:
users. ndOrDefault({ user => user.name() == "Cosme
Fulanito" }, homer)
[1, 3, 5]. ndOrDefault({ number => number.even() }, 0) =>
Answers 0
[]. ndOrDefault({ number => number.even() }, 0) =>
Answers 0

ndOrElse (predicate, Answers the element of self collection that satis es a given
continuation) native condition,
or the the result of evaluating the given continuation.
If more than one element satis es the condition then it
depends on the
speci c collection class which element will be returned.

returns the element that complies the condition or the


result
of evaluating the continuation

Example:
users. ndOrElse({ user => user.name() == "Cosme Fulanito"
}, { homer })
[1, 3, 5]. ndOrElse({ number => number.even() }, { 6.max(4) })
=> Answers 6
[]. ndOrElse({ number => number.even() }, { false }) =>
Answers false

atMap (closure) Map + atten operation


https://www.wollok.org/documentacion/wollokdoc/ 9/42
12/11/2018 WollokDoc · Wollok
see map
Method Description
see atten

Example
object klaus {
method languages() = ["c", "cobol", "pascal"]
}

object fritz {
method languages() = ["java", "perl"]
}

program abc {
console.println([klaus, fritz]. atMap({ person =>
person.languages() }))
=> Answers ["c", "cobol", "pascal", "java", "perl"]
}

atten () Flattens a collection of collections

Example:
[ [1, 2], [3], [4, 0], [] ]. atten() => Answers [1, 2, 3, 4, 0]

fold (element, closure) see subclasses implementations

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() }

internalToSmartString private
(alreadyShown)

isEmpty () Tells whether self collection has no elements

Example:
[1, 6, 5].isEmpty() => Answers false
[].isEmpty() => Answers true

map (closure) Answers a new collection that contains the result of


https://www.wollok.org/documentacion/wollokdoc/ f i 10/42
12/11/2018 WollokDoc · Wollok
transforming
Method Description
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 []

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.
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

min (closure) Answers the element that is considered to be/have the


https://www.wollok.org/documentacion/wollokdoc/ i i l 11/42
12/11/2018 WollokDoc · Wollok
minimum value.
Method Description
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

newInstance () abstract Answers a new, empty collection of the same type as self.
returns a new collection

Example:
const newCollection = users.newInstance()

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

remove (element) see subclasses implementations

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 Removes those elements that meet a given condition.


( l )
https://www.wollok.org/documentacion/wollokdoc/ Thi i id ff i 12/42
12/11/2018 WollokDoc · Wollok
(closure) This is a side effect operation.
Method Description
Supports empty collections.

Example:
const list = [1, 6, 5]
list.removeAllSuchThat { e => e.even() } => list == [1, 5]

size () see subclasses implementations

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 []

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

sum () Sums all elements in the collection.


https://www.wollok.org/documentacion/wollokdoc/ i 13/42
12/11/2018 WollokDoc · Wollok
returns an integer
Method Description
Example:
[1, 2, 3, 4, 5].sum() => Answers 15
[].sum() => Answers 0

toStringPre x () abstract private

toStringSuf x () abstract private

Set
inherits from Collection

A collection that contains no duplicate elements.


It models the mathematical set abstraction.
A Set guarantees no order of elements.

author jfernandes
since 1.3

Constructors
constructor(elements)

Behavior
Method Description

== (other) native Set equality operator as de ned by equals

#{1, 2} == #{2, 1} => Answers true


#{} == #{} => Answers true

see Object#==

add (element) native Adds the speci ed element to this set if it is not already
https://www.wollok.org/documentacion/wollokdoc/ 14/42
12/11/2018 WollokDoc · Wollok
present.
Method Description
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

anyOne () native 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

asList () Converts this set to a list.

Examples
#{1, 2, 3}.asList() => Answers [1, 2, 3]
#{}.asList() => Answers []

see List

asSet () Converts an object to a Set. No effect on Sets.

Examples
#{1, 2, 3}.asSet() => Answers #{1, 2, 3}
#{}.asSet() => Answers #{}

clear () native Removes all of the elements from this set. This is a side
effect operation.

Example:
const set = #{2, 3}
set.clear() => 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

equals (other) native Two sets are equals if they have the same elements
https://www.wollok.org/documentacion/wollokdoc/ 15/42
12/11/2018 WollokDoc · Wollok

Method Description
Examples:
#{}.equals(#{}) => Answers true
#{1, 2}.equals(#{2, 1}) => Answers true
#{3, 2}.equals(#{2, 1}) => Answers false

ndOrElse (predicate, Tries to nd an element in a collection (based on a closure)


continuation) native or
applies a continuation closure.

Examples:
#{1, 9, 3, 8}. ndOrElse({ n => n.even() }, { 100 }) => Answers 8
#{1, 5, 3, 7}. ndOrElse({ n => n.even() }, { 100 }) => Answers 100

fold (initialValue, closure) Reduce a collection to a certain value, beginning with a


native 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

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

join (separator) native Answers the concatenated string representation of the


https://www.wollok.org/documentacion/wollokdoc/ l i h i 16/42
12/11/2018 WollokDoc · Wollok
elements in the given set.
Method Description
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 () native 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"

newInstance () private

remove (element) Removes the speci ed element from this set if it is present.
native
Example:
const set = #{2, 3}
set.remove(3) => set = #{2}
set.remove(4) => set = #{2}, remove operation produces no
effect

size () native Answers the number of elements in this set (its cardinality).

Example:
#{2, 3}.size() => Answers 2
#{}.size() => Answers 0

toStringPre x () private

toStringSuf x () private

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

Methods inherited from Collection

https://www.wollok.org/documentacion/wollokdoc/ 17/42
12/11/2018 WollokDoc · Wollok

+(elements) , absolute(closure, criteria) , addAll(elements) , all(predicate) ,


any(predicate) , contains(e) , copy() , count(predicate) , lter(closure) ,
ndOrDefault(predicate, value) , nd(predicate) , atMap(closure) , atten() ,
forEach(closure) , internalToSmartString(alreadyShown) , isEmpty() , map(closure) ,
max() , max(closure) , min() , min(closure) , occurrencesOf(element) ,
removeAllSuchThat(closure) , removeAll(elements) , sortedBy(closure) , sum() ,
sum(closure)

List
inherits from 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.

author jfernandes
since 1.3

Constructors
constructor()

constructor(head, tail)

Behavior
Method Description

== (other) native A list is == another list if all elements are equal (de ned by
== message)

Examples:
[].equals([]) => Answers true
[1, 2].equals([2, 1]) => Answers false
[1, 2].equals([1, 2]) => Answers true

add (element) native Adds the speci ed element as last one


https://www.wollok.org/documentacion/wollokdoc/ 18/42
12/11/2018 WollokDoc · Wollok

Method Description
Example:
const list = []
list.add(3) => list = [3]
list.add(2) => list = [3, 2]
list.add(2) => list = [3, 2, 2]

anyOne () 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

asList () Converts this collection to a list. No effect on Lists.

see List

asSet () Converts this list to a set (removing duplicate elements)

Examples:
[1, 2, 3].asSet() => Answers #{1, 2, 3}
[].asSet() => Answers #{}
[1, 2, 1, 1, 2].asSet() => Answers #{1, 2}

see Set

clear () native Removes all of the mappings from this Dictionary.


This is a side effect operation.

Example:
const list = [2, 3]
list.clear() => list = []

drop (n) Answers a new list dropping rst 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 []

equals (other) native see == message

ndOrElse (predicate, Finds the rst element matching the boolean closure,
i i )
https://www.wollok.org/documentacion/wollokdoc/ l h i i bl k l if l i 19/42
12/11/2018 WollokDoc · Wollok
continuation) native or evaluates the continuation block closure if no element is
Method Description
found

Examples:
[1, 9, 3, 8]. ndOrElse({ n => n.even() }, { 100 }) => Answers 8
[1, 5, 3, 7]. ndOrElse({ n => n.even() }, { 100 }) => Answers 100

rst () Answers rst element of the non-empty list

returns rst element

Example:
[1, 2, 3, 4]. rst() => Answers 1
[]. rst() => Throws error, list must not be empty

fold (initialValue, closure) Reduce a collection to a certain value, beginning with a


native 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

get (index) native Answers the element at the speci ed position in this non-
empty list.

The rst 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

head () Synonym for rst method

join (separator) native Answers the concatenated string representation of the


https://www.wollok.org/documentacion/wollokdoc/ l i h i 20/42
12/11/2018 WollokDoc · Wollok
elements in the given set.
Method Description
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 () native 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"

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

newInstance () Creates a new list

remove (element) Removes an element in this list, if it is present.


native
Example:
const list = [2, 3]
list.remove(3) => list = [2]
list.remove(4) => list = [2], remove operation produces no
effect

reverse () Answers a new list reversing the elements,


so that rst 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 []

size () native Answers the number of elements


https://www.wollok.org/documentacion/wollokdoc/ 21/42
12/11/2018 WollokDoc · Wollok

Method Description
Example:
[2, 3].size() => Answers 2
[].size() => Answers 0

sortBy (closure) native Sorts elements of a list by a speci c closure.


Order of elements is modi ed (produces effect).

Examples:
const list = [2, 9, 3]
list.sortBy { el1, el2 => el1 > el2 }
list.get(0) => Answers 9

see List#sortedBy

subList (start, end) Answers a view of the portion of this list between the
speci ed fromIndex
and toIndex, both inclusive. Remember rst 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 []

take (n) Takes rst 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 []

toStringPre x () private

toStringSuf x () private

withoutDuplicates () Answers the list without duplicate elements

[1, 3, 1, 5, 1, 3, 2, 5].withoutDuplicates() => Answers [1, 2, 3, 5]


[].withoutDuplicates() => Answers []

Methods inherited from Collection

+(elements) , absolute(closure, criteria) , addAll(elements) , all(predicate) ,


any(predicate) , contains(e) , copy() , count(predicate) , lter(closure) ,
ndOrDefault(predicate value) nd(predicate) atMap(closure) atten()
https://www.wollok.org/documentacion/wollokdoc/ 22/42
12/11/2018 WollokDoc · Wollok
ndOrDefault(predicate, value) , nd(predicate) , atMap(closure) , atten() ,
forEach(closure) , internalToSmartString(alreadyShown) , isEmpty() , map(closure) ,
max() , max(closure) , min() , min(closure) , occurrencesOf(element) ,
removeAllSuchThat(closure) , removeAll(elements) , sortedBy(closure) , sum() ,
sum(closure)

Dictionary
Represents a set of key -> values

Behavior
Method Description

basicGet (_key) native Answers the value to which the speci ed 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

clear () native 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

containsKey (_key) Answers whether this Dictionary contains a mapping for


the speci ed 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
https://www.wollok.org/documentacion/wollokdoc/ h i d l 23/42
12/11/2018 WollokDoc · Wollok
to the speci ed value.
Method Description
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

forEach (closure) native 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 rst associated


with key and
second with value.

Example:
mapaTelefonos.forEach({ k, v => result += k.size() + v.size() })

get (_key) Answers the value to which the speci ed 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

getOrElse (_key, Answers the value to which the speci ed key is mapped,
_closure) 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

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

keys () native Answers a list of the keys contained in this Dictionary.


https://www.wollok.org/documentacion/wollokdoc/ 24/42
12/11/2018 WollokDoc · Wollok

Method Description
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.keys() => ["one", "two"]

put (_key, _value) native 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]

remove (_key) native 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

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

toString () 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]

values () native Answers a list of the values contained in this Dictionary.


https://www.wollok.org/documentacion/wollokdoc/ 25/42
12/11/2018 WollokDoc · Wollok

Method Description
Example:
const numbers = new Dictionary()
numbers.put("one", 1)
numbers.put("two", 2)
numbers.values() => [1, 2]

Number
In Wollok we have numbers as immutable representation. You can customize
how many decimals you want to work with, and printing strategies. So
number two could be printed as "2", "2,00000", "2,000", etc.

Coercing strategy for numbers can be


1) rounding up: 2,3258 using 3 decimals will result in 2,326
2) rounding down or truncation: 2,3258 using 3 decimals will
result in 2,325
3) not allowed: 2,3258 using 3 decimals will throw an exception
since decimals exceeds maximum allowed

author jfernandes
author dodain (uni cation between Double and Integer in a single Number class)

since 1.3
noInstantiate

Behavior
Method Description

% (other) native Answers remainder of division between self and other

* (other) native

** (other) native raisedTo operation

Example:
3.2 ** 2 ==> Answers 10.24
3 ** 2 ==> Answers 9

+ (other) native

- (other) native

https://www.wollok.org/documentacion/wollokdoc/ 26/42
12/11/2018 WollokDoc · Wollok

Method
.. (end) Description
Builds a Range between self and end

Example:
1..4 Answers ==> a new Range object from 1 to 4

/ (other) native

< (other) native

<= (other)

=== (other) native Two references are identical if they are the same number

> (other) native

>= (other)

abs () native 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)

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

checkNotNull (value, private


operation) native

coerceToInteger () private
native
Applies coercing strategy to integer. If it is an integer,
nothing happens.
Otherwise, if it is a decimal, de ned coercing algorithm is
applied
(see de nition of class Number)

digits () Number of digits of self (without sign)


https://www.wollok.org/documentacion/wollokdoc/ 27/42
12/11/2018 WollokDoc · Wollok

Method Description
Examples:
600.digits() ==> Answers 3
6.00012.digits() ==> Answers 6
-100.digits() ==> Answers -3

div (other) native Integer division between self and other

Example:
8.div(3) ==> Answers 2
15.div(5) ==> Answers 3
8.2.div(3.3) ==> Answers 2

even () Answers whether self is an even number


(divisible by 2, mathematically 2k).

Self must be an integer value

gcd (other) native 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

internalToSmartString private
(alreadyShown)

invert () native 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)

isInteger () native Tells if this number can be considered an integer number.


https://www.wollok.org/documentacion/wollokdoc/ 28/42
12/11/2018 WollokDoc · Wollok

Method Description
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

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

limitBetween (limitA, Given self and a range of integer values,


limitB) 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

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

odd () Answers whether self is an odd number


https://www.wollok.org/documentacion/wollokdoc/ ( di i ibl b 2 h i ll 2k 1) 29/42
12/11/2018 WollokDoc · Wollok
(not divisible by 2, mathematically 2k + 1).
Method Description
Self must be an integer value

plus () Allows users to de ne a positive number with 1 or +1

randomUpTo (max) Answers a random number between self and max


native

rem (other) Answers remainder between self and other

Example:
5.rem(3) ==> Answers 2
5.5.rem(3) ==> Answers 2

roundUp (_decimals) Rounds up self up to a certain amount of decimals.


native 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

roundUp () Answers the next integer greater than self


13.224.roundUp() ==> 14
-13.224.roundUp() ==> -14
15.942.roundUp() ==> 16

simpli edToSmartString private


()

square () Answers square of self

Example:
3.square() => Answers 9

squareRoot () Answers squareRoot of self

Example:
9.squareRoot() => Answers 3

stringValue () Self as String value. Equivalent: toString()

times (action) Executes the given action n times (n = self)


https://www.wollok.org/documentacion/wollokdoc/ 30/42
12/11/2018 WollokDoc · Wollok

Method Description
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

toString () native String representation of self number

truncate (_decimals) Truncates self up to a certain amount of decimals.


native 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

String
Strings are constant;
their values cannot be changed after they are created.

author jfernandes
noInstantiate

Behavior
Method Description

+ (other) native Concatenates the speci ed string to the end of this string.
Example:
"cares" + "s" => Answers "caress"

< (aString) native

<= (aString)

== (other) native Compares this string to the speci ed object.


https://www.wollok.org/documentacion/wollokdoc/ Th l i if d l if h 31/42
12/11/2018 WollokDoc · Wollok
The result is true if and only if the
Method Description
argument is not null and is a String object
that represents the same sequence of characters as this
object.

> (aString) native

>= (aString)

capitalize () Changes the rst letter of every word to


upper case in this string.

Example:
"javier fernandes".capitalize() ==> Answers "Javier
Fernandes"

charAt (index) native Answers the char value at the speci ed index. An index
ranges
from 0 to length() - 1. The rst 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.

contains (other) native Answers whether this string contains the speci ed
sequence of char values.
It is a case sensitive test.

Examples:
"unusual".contains("usual") ==> Answers true
"become".contains("CO") ==> Answers false

drop (n) Answers a new string dropping


rst 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 ""

endsWith (other) native Tests if this string ends with the speci ed suf x.
It is case sensitive.
see startsWith

equalsIgnoreCase Compares this String to another String, ignoring case


( S i )
https://www.wollok.org/documentacion/wollokdoc/ id i 32/42
12/11/2018 WollokDoc · Wollok
(aString) considerations.
Method Description
Example:
"WoRD".equalsIgnoreCase("Word") ==> Answers true

indexOf (other) native Answers the index within this string of the rst occurrence
of the speci ed character.
If character is not present, Answers -1

Examples:
"pototo".indexOf("o") ==> Answers 1
"unpredictable".indexOf("o") ==> Answers -1

isEmpty () Answers whether this string has no characters

lastIndexOf (other) Answers the index within this string of the last
native occurrence of the speci ed character.
If character is not present, Answers -1

Examples:
"pototo".lastIndexOf("o") ==> Answers 5
"unpredictable".lastIndexOf("o") ==> Answers -1

length () native Answers the number of elements

printString () String implementation of printString,


simply adds quotation marks

replace (expression, Answers a string resulting from replacing all occurrences of


replacement) native expression in this string with replacement

Example:
"stupid is what stupid does".replace("stupid", "genius")
==> Answers "genius is what genius does"

size () A synonym for length

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"]

startsWith (other) Tests if this string starts with the speci ed pre x.
https://www.wollok.org/documentacion/wollokdoc/ I i i i 33/42
12/11/2018 WollokDoc · Wollok
native It is case sensitive.
Method Description
Examples:
"mother".startsWith("moth") ==> Answers true
"mother".startsWith("Moth") ==> Answers false

substring (index) native 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, Answers a substring of this string beginning


length) native 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

take (n) Takes rst 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 ""

toLowerCase () native Converts all of the characters in this String to lower case

Examples:
"Fer".toLowerCase() ==> Answers "fer"
"".toLowerCase() ==> Answers ""

toSmartString private
(alreadyShown) native

toString () native This object (which is already a string!) is itself returned

toUpperCase () native Converts all of the characters in this String to upper case
https://www.wollok.org/documentacion/wollokdoc/ 34/42
12/11/2018 WollokDoc · Wollok

Method Description
Examples:
"Fer".toUpperCase() ==> Answers "FER"
"".toUpperCase() ==> Answers ""

trim () native Answers a string whose value is this string,


with any leading and trailing whitespace removed.

Example:
" emptySpace ".trim() ==> "emptySpace"

words () Splits this strings into several words.

Examples:
"how does words work?".words()
==> Answers ["how", "does", "words", "work?"]

"".words() ==> Answers []

Boolean
Represents a Boolean value (true or false)

author jfernandes
noInstantiate

Behavior
Method Description

&& (other) native A synonym for and operation

== (other) native Compares this string to the speci ed object.


The result is true if and only if the
argument is not null and represents same value
(true or false)

and (other) native Answers the result of applying the logical AND operator
to the speci ed boolean operands self and other

negate () native NOT logical operation

or (other) native Answers the result of applying the logical OR operator


to the speci ed boolean operands self and other

toSmartString private
( l d Sh )
https://www.wollok.org/documentacion/wollokdoc/ 35/42
12/11/2018 WollokDoc · Wollok
(alreadyShown) native
Method Description
toString () native Answers a String object representing this Boolean's value.

|| (other) native A synonym for or operation

Range
Represents a nite 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]

author jfernandes
since 1.3

Constructors
constructor(_start, _end)
Instantiates a Range.
Both _start and _end must be integer values.

Behavior
Method Description

all (closure) see List#all(closure)

any (closure) see List#any(closure)

anyOne () native Answers a random integer contained in the range

Example:
new Range(1, 3).anyOne() ==> Answers 1 or 2 or 3

asList () private

contains (e) Tests whether a number e is contained in the range

Examples:
new Range(2, 5).contains(4) ==> Answers true
new Range(2, 5).contains(0) ==> Answers false

count (closure) Counts how many elements match the boolean closure
https://www.wollok.org/documentacion/wollokdoc/ 36/42
12/11/2018 WollokDoc · Wollok

Method Description
Example:
(1..9).count({ i => i.even() }) ==> Answers 4 (2, 4, 6 and 8 are
even)

lter (closure) see List# lter(closure)

nd (closure) see List# nd(closure)

ndOrDefault see List# ndOrDefault(predicate, value)


(predicate, value)

ndOrElse (closure, see List# ndOrElse(predicate, continuation)


continuation)

fold (seed, foldClosure) see List#fold(seed, foldClosure)

forEach (closure) native Iterates over a Range from start to end, based on step.

Example:
new Range(1, 3).forEach { value => console.println(value) }
=> prints 1, 2, 3

internalToSmartString private
(alreadyShown)

isEmpty () Answers whether this range contains no elements


see Collection#isEmpty()

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]

max () see List#max()

min () see List#min()

size () Answers the number of elements


https://www.wollok.org/documentacion/wollokdoc/ 37/42
12/11/2018 WollokDoc · Wollok

Method Description
Examples:
new Range(0, 2).size() ==> Answers 3
new Range(-2, 2).size() ==> Answers 5

sortedBy (closure) see List#sortBy

step (_step) Setter for step attribute.

sum () 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

Closure
Represents an executable piece of code. You can create a closure,
assign it to a reference, evaluate it many times,
send it as parameter to another object, and many useful things.

author jfernandes
since 1.3
noInstantiate

Behavior
Method Description

apply (parameters) Evaluates this closure passing its parameters


native
Example:
{ number => number + 1 }.apply(8) ==> Answers 9 // 1
parameter
{ "screw" + "driver" }.apply() ==> Answers "screwdriver" // no
parameter

toString () native Answers a string representation of this closure object

Date
Represents a Date (without time). A Date is immutable, once created you can not
change it.

https://www.wollok.org/documentacion/wollokdoc/ 38/42
12/11/2018 WollokDoc · Wollok

since 1.4.5

Constructors
constructor()
Default constructor instantiates the current day

constructor(_day, _month, _year)


Constructor: you should pass the day, month and year (integer values only).

Behavior
Method Description

- (_aDate) native 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

< (_aDate) native

<= (_aDate)

== (_aDate) native Two dates are equals if they represent the same date

> (_aDate) native

>= (_aDate)

between (_startDate, Answers whether self is between two dates (both inclusive
_endDate) comparison)

Example:
new Date(2, 4, 2018).between(new Date(1, 4, 2018), new
Date(2, 4, 2018))
==> Answers true

day () native Answers the day number of the Date

Example:
new Date(12, 7, 2019).day() ==> Answers 12

dayOfWeek () native Answers the day of week of the Date, where


https://www.wollok.org/documentacion/wollokdoc/ 1 MONDAY 39/42
12/11/2018 WollokDoc · Wollok
1 = MONDAY
Method Description
2 = TUESDAY
3 = WEDNESDAY
...
7 = SUNDAY

Example:
new Date(24, 2, 2018).dayOfWeek() ==> Answers 6
(SATURDAY)

initialize (_day, _month, private


_year) native

isLeapYear () native Checks if the year is a leap year, like 2000, 2004, 2008...

Example:
new Date(12, 5, 2018).isLeapYear() ==> Answers false

minusDays (_days) Answers a copy of this date with the speci ed number of
native 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(1, 1, 2009).minusDays(1)
==> Answers a Date[day = 31, month = 12, year = 2008], a day
back

new Date(1, 1, 2009).minusDays(-1)


==> Answers a Date[day = 2, month = 1, year = 2009], a day
forward

minusMonths (_months) Answers a copy of this date with the speci ed number of
https://www.wollok.org/documentacion/wollokdoc/ h b d 40/42
12/11/2018 WollokDoc · Wollok
native months subtracted.
Method Description
Parameter must be an integer value.
This operation has no side effect (a new date is returned).

Examples:
new Date(1, 1, 2009).minusMonths(1)
==> Answers a Date[day = 1, month = 12, year = 2008], a
month back

new Date(1, 1, 2009).minusMonths(-1)


==> Answers a Date[day = 1, month = 2, year = 2009], a
month forward

minusYears (_years) Answers a copy of this date with the speci ed number of
native years subtracted.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).

Examples:
new Date(1, 1, 2009).minusYears(1)
==> Answers a Date[day = 1, month = 1, year = 2008], a year
back

new Date(1, 1, 2009).minusYears(-1)


==> Answers a Date[day = 1, month = 1, year = 2010], a year
forward

month () native Answers the month number of the Date

Example:
new Date(12, 7, 2019).month() ==> Answers 7

plusDays (_days) native Answers a copy of this Date with the speci ed number of
days added.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).

Example:
new Date(12, 5, 2018).plusDays(1)
==> Answers a Date[day = 13, month = 5, year = 2018], a day
forward

new Date(12, 5, 2018).plusDays(-1)


==> Answers a Date[day = 11, month = 5, year = 2018], a day
back

plusMonths (_months) Answers a copy of this Date with the speci ed number of
https://www.wollok.org/documentacion/wollokdoc/ h dd d 41/42
12/11/2018 WollokDoc · Wollok
native months added.
Method Description
Parameter must be an integer value.
This operation has no side effect (a new date is returned).

Example:
new Date(31, 1, 2018).plusMonths(1)
==> Answers a Date[day = 28, month = 2, year = 2018], a
month forward

new Date(12, 5, 2018).plusMonths(-1)


==> Answers a Date[day = 12, month = 4, year = 2018], a
month back

plusYears (_years) native Answers a copy of this Date with the speci ed number of
years added.
Parameter must be an integer value.
This operation has no side effect (a new date is returned).

Example:
new Date(31, 1, 2018).plusYears(1)
==> Answers a Date[day = 31, month = 1, year = 2019], a year
forward

new Date(12, 5, 2018).plusYears(-1)


==> Answers a Date[day = 12, month = 5, year = 2017], a year
back

toSmartString Shows nicely an internal representation of a date


(alreadyShown)

toString () String representation of a date

year () native Answers the year number of the Date

Example:
new Date(12, 7, 2019).year() ==> Answers 2019

Last update: 05/09/2018 17:16:17

https://www.wollok.org/documentacion/wollokdoc/ 42/42