- Ko
- 2. Language
- 2.4. Macros
**2.4.2. Macros for sequences**

**2.4.2. Macros for sequences**## Len

`Len`

returns the length of its (monadic) argument. If the argument is a sequence type, its length is returned. If the argument is a non-sequence type (basic, opaque, variety or structure),`Len`

returns 1. If argument is empty,`Len`

returns 0.For example,

`Len("abc")`

returns`1`

, whereas`Len("a", "b", "c")`

returns`3`

.## Merge

`Merge`

concatenates multiple sequences holding the same type of elements into a single sequence. Non-sequence arguments are lifted to singleton sequences for convenience. For example,`Merge((1, 2, 3), (4, 5)) // returns (1, 2, 3, 4, 5) Merge ((1, 2, 3), 4) // returns (1, 2, 3, 4) Merge(1, 2, 3) // returns (1, 2, 3)`

## Range

`Range`

recognizes three arguments:`start`

,`over`

and`with`

.`Range`

applies the variety`with`

to each element of the sequence`over`

sequentially and returns the results.The iterator

`with`

must recognize two arguments:`elem`

and`elem`

. The`carry`

argument holds the element of the sequence`over`

being processed. The`carry`

argument holds a “carry” value returned from the previous iteration of`with`

, or the value of`start`

in the case of the first iteration.The iterator

`with`

must return a structure with two fields:`emit`

and`carry`

. Values (or sequences) stored in`emit`

will be concatenated across all iterations and returned by`Range`

. The value stored in`carry`

will passed on to the next iteration. The carry of the final iteration will be returned by`Range`

.The

`Range`

macro returns a structure with two fields:`image`

and`residue`

. Field`image`

holds the sequence of values (if any) emitted by the iterations. Field`residue`

holds the value of the carry returned by the final iteration.The following example computes the individual string lengths in a sequence of strings, as well as the total length and prints them out.

`import "integer" import "github.com/kocircuit/kocircuit/strings" ComputeStringsLengthsAndTotal(strings) { // string is a sequence of strings result: Range( start: 0 over: strings with: processString(carry, elem) { // carry is Int64, elem is String stringLen: strings.Len(elem) // compute length of string elem return: ( emit: stringLen carry: integer.Sum(carry, stringLen) // aggregate total length in carry ) } ) return: Show( individualStringLengths: result.image totalLength: result.residue ) }`