Fun

Module fun

import fun
Module fun
Source fun/fun.zirr, fun/module-docs.zirr

Composing functions, and streaming through them.

fun has two halves. The first composes functions: pipe and compose chain them, negate and flip adjust them, and identity and constant fill in where an API wants a function and you have a value.

The second is map, filter, flatMap, take, skip and zip over anything carrying prelude.Iterable. These are lazy: they build a new iterable and nothing runs until something consumes it — a for loop, reduce, or any other consumer. That is what lets take stop an otherwise unbounded sequence.

For the eager equivalents that always walk the whole collection, see arrays.

Contents


Data

Zipped

`fun/fun.zirr:137`
data Zipped {
	first
	second
}

A pair of values at the same position in zip’s two source sequences.

Fields

Field Description
first
second

Functions

compose

`fun/fun.zirr:35`
fn compose(f: fn(Any) -> Any, g: fn(Any) -> Any) -> fn(Any) -> Any

Combines f and g into a single function that applies g first, then f — compose(f, g)(v) is f(g(v)). The mirror image of pipe, which applies its functions left to right instead.


constant

`fun/fun.zirr:25`
fn constant(value: Any) -> fn(Any) -> Any

Returns a function that always returns value, ignoring whatever it’s called with.


filter

`fun/fun.zirr:67`
fn filter(v: @Iterable, predicate: fn(Any) -> Bool) -> @Iterable

Returns a lazy @Iterable containing only the elements of v for which predicate returns true, in order.


flatMap

`fun/fun.zirr:80`
fn flatMap(v: @Iterable, transform: fn(Any) -> @Iterable) -> @Iterable

Returns a lazy @Iterable that applies transform to each element of v and flattens each resulting @Iterable into a single sequence, in order.


flip

`fun/fun.zirr:40`
fn flip(f: fn(Any, Any) -> Any) -> fn(Any, Any) -> Any

Returns a function that calls f with its two arguments swapped — flip(f)(a, b) is f(b, a).


identity

`fun/fun.zirr:20`
fn identity(v: Any) -> Any

Returns v unchanged. Useful wherever an API expects a function but you already have the value.


map

`fun/fun.zirr:56`
fn map(v: @Iterable, transform: fn(Any) -> Any) -> @Iterable

Returns a lazy @Iterable that applies transform to each element of v, in order.


negate

`fun/fun.zirr:30`
fn negate(predicate: fn(Any) -> Bool) -> fn(Any) -> Bool

Returns a predicate that’s the boolean negation of predicate — negate(p)(v) is !p(v).


pipe

`fun/fun.zirr:9`
fn pipe(funs: [fn(Any) -> Any]) -> Any

Combines funs into a single function that applies them in order, left to right, passing each result to the next.


reduce

`fun/fun.zirr:93`
fn reduce(v: @Iterable, initial: Any, combine: fn(Any, Any) -> Any) -> Any

Combines v’s elements into a single value, starting from initial and applying combine(accumulator, element) left to right. Unlike map/filter/flatMap, this consumes v eagerly.


skip

`fun/fun.zirr:121`
fn skip(v: @Iterable, n: Int) -> @Iterable

Returns a lazy @Iterable that omits v’s first n elements, yielding the rest unchanged.


take

`fun/fun.zirr:102`
fn take(v: @Iterable, n: Int) -> @Iterable

Returns a lazy @Iterable containing at most the first n elements of v.


with

`fun/fun.zirr:4`
fn with(val, fun: fn(Any))

Applies fun to val and returns the result. Useful for starting a pipe()-style chain from a value without naming an intermediate variable.


zip

`fun/fun.zirr:144`
fn zip(a: @Iterable, b: @Iterable) -> @Iterable

Returns a lazy @Iterable of Zipped(x, y) for each x in a paired with the element at the same position in b, stopping as soon as either source is exhausted.
b is buffered into memory up front — pairing needs random access into it, which the push-based @Iterable protocol can’t give incrementally for two independent sources — so b must be finite, even though a is still walked one element at a time.