This module defines the set of standard augmentations imported by default.
Augment functions to make them behave more like objects from java.util.function
Alias for FunctionReference::invoke
Alias for FunctionReference::invoke
Call this function with no arguments.
Augmentations for Golo tuples.
Filters elements using a predicate, returning a new tuple.
Returns the first element that satisfies a predicate, or null
if none matches.
Joins the elements of a tuple into a string and using a separator.
Maps the elements of a tuple, and returns a tuple with the transformed values.
Makes a BufferedReader
an Iterable on its lines.
Augmentations over CharSequence
to view it as a “real” char
collection.
Returns the first char
of the sequence, of null
if empty.
Checks if the sequence is empty.
Returns the remaining subsequence as a String (i.e. an immutable sequence)
Augmentations over iterable collections.
New style destructuring helper
If a remainer is included, it will be an iterable reusing the underlying iterator.
number
: number of variable that will be affected.substruct
: whether the destructuring is complete or should contains a remainer.toSkip
: a boolean array indicating the elements to skip.Counts the number of elements that satisfy a predicate:
println([1, 2, 3, 4]: count(|n| -> (n % 2) == 0))
this
: an iterable.pred
: a predicate function, taking an element and returning a boolean.Applies a function over each element:
[1, 2, 3]: each(|e| -> println(e))
this
: an iterable.func
: the function to apply, taking the current element as a parameter.Checks whether any element satisfied a predicate:
println([1, 2, 3, 4]: exists(|n| -> n > 3))
this
: an iterable.pred
a predicate function, taking an element and returning a boolean.General purpose reducing operation:
let data = [1, 2, 3, 4, 5]
println("sum = " + data: reduce(0, |acc, next| -> acc + next))
this
: an iterable.initialValue
: the initial accumulator value for the reducing operation.func
: the function to apply over an accumulator and the next value.Number augmentations.
Similar to upTo
, except that the interval iteration is made from high
down to low
.
Repeats a function many times, as in:
3: times(-> println("Hey!")
4: times(|i| -> println(i))
count
: how many times the func
function must be repeated.func
: a function to execute.The func
function may take 0 or 1 argument. In the later case the argument is the iteration
count index.
Repeats a function over a discrete interval:
1: upTo(3, -> println("Hello"))
1: upTo(3, |i| ->println(i))
low
: the start value (inclusive).high
: the end value (inclusive).func
: the function to execute.As in the case of times
, func
may take an optional index parameter.
See also downTo
.
Objects augmentations.
Checks if this object is an instance of one of the given class.
Useful string augmentations.
Convenience wrapper over java.lang.String.format(...)
.
Wrapper over java.lang.Integer.parseDouble
.
Wrapper over java.lang.Integer.parseFloat
.
Wrapper over java.lang.Integer.parseInt
.
rapper over java.lang.Integer.parseInt
.
Wrapper over java.lang.Integer.parseLong
.
Java collections augmentations.
New style destructuring helper
If a remainer is included, it will be a new collection of the same type.
number
: number of variable that will be affected.substruct
: whether the destructuring is complete or should contains a remainer.toSkip
: a boolean array indicating the elements to skip.Destructuration helper.
*Deprecated since 3.4. This method should not be called directly and is no more used by new style destructuring.
Maps a function returning a collection and flatten the result (a.k.a bind)
let l = list[1, 2, 3]: flatMap(|e| -> list[e, e, e])
require(l == list[1, 1, 1, 2, 2, 2, 3, 3, 3], "err")
this
: a collectionfunc
: a mapping function returning a collectionReturns an empty collection of the same type as this
.
New style destructuring helper
If a remainer is included, it will contain the iterator itself.
number
: number of variable that will be affected.substruct
: whether the destructuring is complete or should contains a remainer.toSkip
: a boolean array indicating the elements to skip.Java lists augmentations.
Appends an element to a list.
Appends a variable number of arguments to a list.
head
: an element to append.tail
: a variable number of elements to append.Filters elements based on a predicate:
println(list[1, 2, 3, 4]: filter(|n| -> (n % 2) == 0))
this
: a list.pred
: a predicate function taking an element and returning a boolean.filter
returns a new collection of the same type as the original one, hence the original list is
kept intact.
Finds the first element of a list matching a predicate:
println(list[1, 2, 3, 4]: find(|n| -> n > 3))
this
: a list.pred
: a predicate function taking an element and returning a boolean.find
returns null
when no element satisfies pred
.
Returns a list first element, of null
if empty.
Inserts an element at some index.
Join the elements into a string:
println(list[1, 2, 3]: join(", "))
this
: a list.separator
: the element separator string.The returned string is ""
when the list is empty.
Returns a list last element.
Maps elements of a list using a function:
println(list[1, 2, 3]: map(|n| -> n * 10))
this
: a list.func
: a transformation function.map
returns a new list with the same type, keeping the original list intact.
Sorts the list elements and returns the list.
Sorts the element using a comparator, see java.util.Collections.sort(...)
.
Returns a new list where the elements have been sorted.
Returns a new list where the elements have been sorted using a comparator.
See java.util.Collections.sort
.
Prepends an element to a list.
Prepends a variable number of arguments to a list.
Removes the element at the specified position.
This method has the same behaviour as java.util.List.remove(int)
, but is
needed since for Golo everything is an Object
and remove
is overloaded.
Reverse the elements of the list and returns the list.
See also reversed
.
Same as reverse
, but the returned list is a new one, leaving the original list order intact.
Returns the rest of a list after its head, as an unmodifiable list.
Convenience wrapper over java.util.Collections.unmodifiableList
.
Augmentations over maps.
Adds a tuple [key, value]
or a map entry and returns the map.
Alias for put
that returns the map.
Adds an element to the map only if there is no entry for that key.
this
: a map.key
: the element key.value
: the element value or a function to evaluate to get a value.The fact that value
can be a function allows for delayed evaluation which can be useful for
performance reasons. So instead of:
map: addIfAbsent(key, expensiveOperation())
one may delay the evaluation as follows:
map: addIfAbsent(key, -> expensiveOperation())
addIfAbsent
returns the map.
Counts the number of elements satisfying a predicate.
Alias for remove
that returns the map.
Iterates over each entry of a map.
func
takes 2 arguments: the entry key and its value.
Returns true
if there is any value satisfying pred
, false
otherwise.
Filters elements using a predicate, and returns a new map.
pred
takes 2 arguments: a key and a value, and returns a boolean.
Returns the first element that satisfies a predicate, or null
if none matches.
pred
takes 2 arguments: a key and a value, and returns a boolean.
Returns a value from a key or a default value if the entry is not defined.
this
: a map.key
: the key to look for.replacement
: the default value, or a function giving the default value.As it is the case for addIfAbsent
,
one can take advantage of delayed evaluation:
println(map: getOrElse(key, "n/a"))
println(map: getOrElse(key, -> expensiveOperation())
Note that replacement
yields the return value also when there is an entry for key
but the
value is null
.
Maps entries of the map using a function.
func
takes 2 arguments: a key and a value. The returned value must have getKey()
and
getValue()
to represent a map entry. We suggest using the predefined mapEntry(key, value)
function as it returns such object.
Returns a new empty map of the same type.
Reduces the entries of a map.
func
takes 3 arguments:
initialValue
,Wrapper for java.util.Collections.unmodifiableMap(...)
.
New style destructuring helper
The destructuring must be to exactly two values. No remainer syntax is allowed.
number
: number of variable that will be affected.substruct
: whether the destructuring is complete or should contains a remainer.toSkip
: a boolean array indicating the elements to skip.Destructurate a map entry in key and value
*Deprecated since 3.4. This method should not be called directly and is no more used by new style destructuring.
Convert then entry into an array containing the key and the value.
Augmentations over set collections.
Alias for remove
that returns the set.
Excludes a variable number of elements, and returns the set.
Filters the elements using a predicate, and returns a new collection.
Finds the first element that satisfies a predicate pred
, and returns it, or null
if no element
matches.
Alias for contains
.
Alias for contains
over a variable number of elements.
Alias for add
that returns the set.
Includes a variable number of elements, and returns the set.
Transform each value using the func
function, and returns a new set.
Convenience wrapper for java.util.Collections.unmodifiableSet(...)
.