OPX//77
opx_lib

Lib.Array

Last updated on

List helpers — filter, map, find, fold, sort, group and edit in place.

Pure. No permission. Every function answers a new table unless it says in place.

Lib.Array.Filter

The entries a test keeps, in order.

Lib.Array.Filter(list, test)

Parameters

  • list: table
  • test: function(value, index): boolean

Returns

  • table
Example
local armed = Lib.Array.Filter(rows, function(row) return row.armed end)

Lib.Array.Map

Each entry through a function, in order.

Lib.Array.Map(list, change)

Parameters

  • list: table
  • change: function(value, index): any

Returns

  • table

Lib.Array.Find

The first entry a test accepts, and its index.

Lib.Array.Find(list, test)

Parameters

  • list: table
  • test: function(value, index): boolean

Returns

  • any?, integer?

Lib.Array.Any

Whether any entry passes.

Lib.Array.Any(list, test)

Parameters

  • list: table
  • test: function

Returns

  • boolean

Lib.Array.All

Whether every entry passes. true for an empty list.

Lib.Array.All(list, test)

Parameters

  • list: table
  • test: function

Returns

  • boolean

Lib.Array.Fold

Folds the list into one value.

Lib.Array.Fold(list, step, initial)

Parameters

  • list: table
  • step: function(carried, value, index): any
  • initial: any

Returns

  • any
Example
local total = Lib.Array.Fold(items, function(sum, item) return sum + item.price end, 0)

Lib.Array.Reverse

A reversed copy.

Lib.Array.Reverse(list)

Parameters

  • list: table

Returns

  • table

Lib.Array.Sorted

A sorted copy; the caller's order survives.

Lib.Array.Sorted(list, before)

Parameters

  • list: table
  • before?: function(a, b): boolean

Returns

  • table

Lib.Array.Unique

A copy with duplicates dropped; first occurrence wins.

Lib.Array.Unique(list, by)

Parameters

  • list: table
  • by?: function(value): any — the key to compare on

Returns

  • table

Lib.Array.GroupBy

Entries grouped into lists under a derived key.

Lib.Array.GroupBy(list, by)

Parameters

  • list: table
  • by: function(value): any

Returns

  • table<key, list>
Example
local byJob = Lib.Array.GroupBy(players, function(p) return p.job end)

Lib.Array.Take

Up to count entries from the front.

Lib.Array.Take(list, count)

Parameters

  • list: table
  • count: integer

Returns

  • table

Lib.Array.Push

Appends in place.

Lib.Array.Push(list, value)

Parameters

  • list: table
  • value: any

Returns

  • integer — the new length

Lib.Array.Remove

Removes the first equal entry, in place.

Lib.Array.Remove(list, value)

Parameters

  • list: table
  • value: any

Returns

  • boolean — whether anything was removed

Lib.Array.Holds

Whether the list holds a value.

Lib.Array.Holds(list, value)

Parameters

  • list: table
  • value: any

Returns

  • boolean

On this page