This module defines the set of standard augmentations.
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.
Augmentations over iterable collections.
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 wether 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.
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
.
Augmentations over method handles.
Given that Golo closures are method handles, these augmentations work on closure references.
Function composition:
let f = |x| -> x + 1
let g = |y| -> y * 10
let h = f: andThen(g)
println(h(2))
this
: a function.filter
: a function that takes the return value of this
as an argument.Partial application:
let adder = |x, y| -> x + y
let add2 = adder: bindAt(1, 2) # binds 'y'
println(add2(1))
this
: a function.pos
: a 0-based index of the argument to apply.val
: the value to apply.Arguments spreading.
this
: a function.args
: an array of arguments to spread over those of this
.Returns an argument-spreading function.
Converts a closure to an instance of a single method interface.
let f = |event| -> println("click")
let handler = f: to(java.awt.event.ActionListener.class)
this
: a Golo closure.interfaceClass
a class object.Java collections augmentations.
Returns an empty collection of the same type as this
.
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.
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.
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
.:w
Prepends an element to a list.
Prepends a variable number of arguments to a list.
Reverse the elements of the list and returns the list.
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.
Convenience wrapper over java.util.Collections.unmodifiableList
.
Augmentations over maps.
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: putIfAbsent(key, expensiveOperation())
one may delay the evaluation as follows:
map: putIfAbsent(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(...)
.
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(...)
.