actorx/builder

Builder module for ActorX

Provides use-friendly functions for composing observables in a monadic style, similar to F#’s computation expressions.

Example

import actorx
import actorx/builder.{bind, return}

pub fn example() -> Observable(Int) {
  use x <- bind(actorx.single(10))
  use y <- bind(actorx.single(20))
  use z <- bind(actorx.from_list([1, 2, 3]))
  return(x + y + z)
}
// Emits: 31, 32, 33 then completes

The use keyword in Gleam desugars to callback passing:

use x <- bind(obs)
rest...

becomes:

bind(obs, fn(x) { rest... })

Values

pub fn bind(
  source: types.Observable(a),
  continuation: fn(a) -> types.Observable(b),
) -> types.Observable(b)

Bind an observable to a continuation function. This is flatMap with arguments ordered for Gleam’s use syntax.

Example

use value <- bind(actorx.single(42))
return(value * 2)
pub fn combine(
  first: types.Observable(a),
  second: types.Observable(a),
) -> types.Observable(a)

Combine two observables sequentially (concat).

pub fn empty() -> types.Observable(a)

Empty observable - completes immediately with no values.

pub fn filter_with(
  source: types.Observable(a),
  predicate: fn(a) -> Bool,
) -> types.Observable(a)

Filter with use syntax.

Example

use x <- filter_with(actorx.from_list([1, 2, 3, 4, 5]))
x > 2

Returns observable of values where the predicate returns True.

pub fn for_each(
  items: List(a),
  f: fn(a) -> types.Observable(b),
) -> types.Observable(b)

For each item in a list, apply a function and concat results. Similar to F#’s for in computation expressions.

Example

for_each([1, 2, 3], fn(x) {
  actorx.single(x * 10)
})
// Emits: 10, 20, 30
pub fn map_over(
  source: types.Observable(a),
  mapper: fn(a) -> b,
) -> types.Observable(b)

Map over an observable (functor map). Can also be used with use for transformations.

Example

use x <- map_over(actorx.from_list([1, 2, 3]))
x * 10
pub fn pure(value: a) -> types.Observable(a)

Alias for return - lifts a value into an observable.

pub fn return(value: a) -> types.Observable(a)

Lift a pure value into an observable (like return or pure). Alias for single but named for monadic style.

Example

use x <- bind(some_observable)
return(x * 2)
pub fn yield_from(
  source: types.Observable(a),
) -> types.Observable(a)

Yield from another observable (identity for observables). Useful for yielding an existing observable in a use chain.

pub fn zero() -> types.Observable(a)

Zero for the monoid - same as empty.

Search Document