Celixir.Ext.Lists (Celixir v0.2.0)

Copy Markdown View Source

Lists extension for CEL — mirrors ext.Lists() from cel-go.

Provides extended list functions. Functions are registered into the environment and also available as built-ins.

Usage

env = Celixir.Environment.new() |> Celixir.Ext.Lists.register()
Celixir.eval!("lists.range(5)", env)          # => [0, 1, 2, 3, 4]
Celixir.eval!("[3, 1, 2].distinct()", env)    # => [3, 1, 2]
Celixir.eval!("[1, 2, 2].distinct()", env)    # => [1, 2]
Celixir.eval!("[1, 2, 3].first().value()", env)  # => 1
Celixir.eval!("[1, 2, 3].last().value()", env)   # => 3
Celixir.eval!("[3, 1, 2].sort()", env)        # => [1, 2, 3]
Celixir.eval!("[3, 1, 2].reverse()", env)     # => [2, 1, 3]
Celixir.eval!("[1, [2, 3]].flatten()", env)   # => [1, 2, 3]
Celixir.eval!("[1, [2, [3]]].flatten(1)", env)# => [1, 2, [3]]

Two-variable comprehensions (parser macros, always available)

  • list.sortBy(e, e.field) — sort list by computed key expression
  • list.transformMapEntry(k, v, {v: k}) — build map with custom key-value pairs

Functions

  • lists.range(int) — generate [0, 1, ..., n-1]
  • list.distinct() — deduplicate preserving order
  • list.first() — optional first element
  • list.last() — optional last element
  • list.slice(int, int) — sub-list
  • list.flatten() / list.flatten(int) — recursive flatten with optional depth
  • list.sort() — sort comparable elements
  • list.reverse() — reverse

Summary

Functions

Registers list extension functions into the given environment.

Functions

distinct(list)

first(list)

flatten(list, depth)

last(list)

range(n)

register(env \\ Environment.new())

Registers list extension functions into the given environment.