golo-lang/golo-lang — pull request #204 by yloiseau (2014-11-16 16:47:41+00:00)
Title: Adds head, tail, and isEmpty methods to several objects

This PR adds `head`, `tail`, and `isEmpty` methods on several collection-like objects:
* adds augmentation on `CharSequence`,
* changes the behaviour of augmentation on `List`,
* adds native (java) methods to `Tuple`,
* adds native (java) methods to ranges,
* adds special methods to arrays.

It also introduce a java interface, `gololang.HeadTail`, and makes `Tuple` and ranges to implement it.

These methods behaviour is:
* `isEmpty` tells if the container is empty,
* `head` returns the first element, or `null` if the collection is empty, to allow chaining methods with “elvis” operator `?:`,
* `tail` returns a new object of the same type containing the rest of the elements (or an empty object if empty).

The purpose is to provide to all collection-like (immutable) structures an homogeneous interface suitable for recursive algorithms.

As an example, a generic function computing the size of such an object can be:
```golo
function size = |struct| -> match {
when struct: isEmpty() then 0
otherwise 1 + size(struct: tail())
}
```
and the sum of all the elements:
```golo
function sum = |struct| -> match {
when struct: isEmpty() then 0
otherwise struct: head() + sum(struct: tail())
}
```
we can manipulate strings with the same aproach:
```golo
function upper = |str| -> match {
when str: isEmpty() then ""
otherwise toUpperCase(str: head()) + upper(str: tail()
}
```