Documentation for gololang.StandardAugmentations

This module defines the set of standard augmentations.

Augmentations

gololang.Tuple

Augmentations for Golo tuples.

filter(this, func)

Filters elements using a predicate, returning a new tuple.

find(this, pred)

Returns the first element that satisfies a predicate, or null if none matches.

join(this, separator)

Joins the elements of a tuple into a string and using a separator.

map(this, func)

Maps the elements of a tuple, and returns a tuple with the transformed values.

java.lang.Iterable

Augmentations over iterable collections.

count(this, pred)

Counts the number of elements that satisfy a predicate:

println([1, 2, 3, 4]: count(|n| -> (n % 2) == 0))

each(this, func)

Applies a function over each element:

[1, 2, 3]: each(|e| -> println(e))

exists(this, pred)

Checks wether any element satisfied a predicate:

println([1, 2, 3, 4]: exists(|n| -> n > 3))

reduce(this, initialValue, func)

General purpose reducing operation:

let data = [1, 2, 3, 4, 5]
println("sum = " + data: reduce(0, |acc, next| -> acc + next))

java.lang.Number

Number augmentations.

downTo(high, low, func)

Similar to upTo, except that the interval iteration is made from high down to low.

times(count, func)

Repeats a function many times, as in:

3: times(-> println("Hey!")
4: times(|i| -> println(i))

The func function may take 0 or 1 argument. In the later case the argument is the iteration count index.

upTo(low, high, func)

Repeats a function over a discrete interval:

1: upTo(3, -> println("Hello"))
1: upTo(3, |i| ->println(i))

As in the case of times, func may take an optional index parameter.

java.lang.String

Useful string augmentations.

format(this, args...)

Convenience wrapper over java.lang.String.format(...).

toDouble(this)

Wrapper over java.lang.Integer.parseDouble.

toFloat(this)

Wrapper over java.lang.Integer.parseFloat.

toInt(this)

Wrapper over java.lang.Integer.parseInt.

toInteger(this)

rapper over java.lang.Integer.parseInt.

toLong(this)

Wrapper over java.lang.Integer.parseLong.

java.lang.invoke.MethodHandle

Augmentations over method handles.

Given that Golo closures are method handles, these augmentations work on closure references.

andThen(this, filter)

Function composition:

let f = |x| -> x + 1
let g = |y| -> y * 10
let h = f: andThen(g)
println(h(2))

bindAt(this, pos, val)

Partial application:

let adder = |x, y| -> x + y
let add2 = adder: bindAt(1, 2)    # binds 'y'
println(add2(1))

spread(this, args)

Arguments spreading.

Returns an argument-spreading function.

to(this, interfaceClass)

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)

java.util.Collection

Java collections augmentations.

newWithSameType(this)

Returns an empty collection of the same type as this.

java.util.List

Java lists augmentations.

append(this, element)

Appends an element to a list.

append(this, head, tail...)

Appends a variable number of arguments to a list.

filter(this, pred)

Filters elements based on a predicate:

println(list[1, 2, 3, 4]: filter(|n| -> (n % 2) == 0))

filter returns a new collection of the same type as the original one, hence the original list is kept intact.

find(this, pred)

Finds the first element of a list matching a predicate:

println(list[1, 2, 3, 4]: find(|n| -> n > 3))

find returns null when no element satisfies pred.

head(this)

Returns a list first element.

insert(this, index, element)

Inserts an element at some index.

join(this, separator)

Join the elements into a string:

println(list[1, 2, 3]: join(", "))

The returned string is "" when the list is empty.

map(this, func)

Maps elements of a list using a function:

println(list[1, 2, 3]: map(|n| -> n * 10))

map returns a new list with the same type, keeping the original list intact.

order(this)

Sorts the list elements and returns the list.

order(this, comparator)

Sorts the element using a comparator, see java.util.Collections.sort(...).

ordered(this)

Returns a new list where the elements have been sorted.

ordered(this, comparator)

Returns a new list where the elements have been sorted using a comparator. See java.util.Collections.sort.:w

prepend(this, element)

Prepends an element to a list.

prepend(this, head, tail...)

Prepends a variable number of arguments to a list.

reverse(this)

Reverse the elements of the list and returns the list.

reversed(this)

Same as reverse, but the returned list is a new one, leaving the original list order intact.

tail(this)

Returns the rest of a list after its head.

unmodifiableView(this)

Convenience wrapper over java.util.Collections.unmodifiableList.

java.util.Map

Augmentations over maps.

add(this, key, value)

Alias for put that returns the map.

addIfAbsent(this, key, value)

Adds an element to the map only if there is no entry for that key.

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.

count(this, pred)

Counts the number of elements satisfying a predicate.

delete(this, key)

Alias for remove that returns the map.

each(this, func)

Iterates over each entry of a map.

func takes 2 arguments: the entry key and its value.

exists(this, pred)

Returns true if there is any value satisfying pred, false otherwise.

filter(this, pred)

Filters elements using a predicate, and returns a new map.

pred takes 2 arguments: a key and a value, and returns a boolean.

find(this, pred)

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.

getOrElse(this, key, replacement)

Returns a value from a key or a default value if the entry is not defined.

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.

map(this, func)

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 predefinedmapEntry(key, value)` function as it returns such object.

newWithSameType(this)

Returns a new empty map of the same type.

reduce(this, initialValue, func)

Reduces the entries of a map.

func takes 3 arguments:

unmodifiableView(this)

Wrapper for java.util.Collections.unmodifiableMap(...).

java.util.Set

Augmentations over set collections.

exclude(this, element)

Alias for remove that returns the set.

exclude(this, first, rest...)

Excludes a variable number of elements, and returns the set.

filter(this, pred)

Filters the elements using a predicate, and returns a new collection.

find(this, pred)

Finds the first element that satisfies a predicate pred, and returns it, or null if no element matches.

has(this, element)

Alias for contains.

has(this, first, rest...)

Alias for contains over a variable number of elements.

include(this, element)

Alias for add that returns the set.

include(this, first, rest...)

Includes a variable number of elements, and returns the set.

map(this, func)

Transform each value using the func function, and returns a new set.

unmodifiableView(this)

Convenience wrapper for java.util.Collections.unmodifiableSet(...).