List Functions

Karma.run's model collection behave as if they were simple lists.

reverseList

available since: 1.0.0

Takes a list and returns it in reverse order. Note that this requires loading the list into memory.

listTlistT

Example:

Query:
e.reverseList(e.data(d.list(d.int8(1), d.int8(2), d.int8(3))))
Result:
[ 3, 2, 1 ]

rightFoldList

available since: 1.0.0

Takes a list and reduces it to a single value of different type from right to left.

Example:

Query:
e.rightFoldList(e.data(d.list(d.string("bar"), d.string("baz"))), e.data(d.struct({
  • value: d.string("foo"),
}
)
)
, f.function(["_aggregator_0","_value_0"], e.setField({
  • name: "value",
  • value: e.joinStrings({
    • separator: d.string(" "),
    • strings: e.data(d.list(d.expr(e.field("value", {
      • scope: "_aggregator_0",
      }
      )
      )
      , d.expr({
      • scope: "_value_0",
      }
      )
      )
      )
      ,
    }
    )
    ,
  • in: {
    • scope: "_aggregator_0",
    }
    ,
}
)
)
)
Result:
{ "value": "foo baz bar" }

length

available since: 1.0.0

Length takes a list and returns its length as an int.

listTint64

Example:

Query:
e.length(e.all(e.tag(d.string("_tag"))))
Result:
6
Notes:

Counts the amount of tags.

memSortFunction

available since: 1.0.0

Takes a list value and returns it sorted by invoking the given comparator function. Note that, as the name implies, this function needs to load a list fully into memory.

Example:

Query:
e.memSortFunction(e.data(d.list(d.int8(2), d.int8(8), d.int8(4))), f.function(["_valueA_0","_valueB_0"], e.gtInt8({
  • scope: "_valueA_0",
}
, {
  • scope: "_valueB_0",
}
)
)
)
Result:
[ 8, 4, 2 ]

concatLists

available since: 1.0.0

Takes a list of lists and returns a concatenate list.

struct {
  • expression :expressionTbool
  • value :listT
}
listT

Example:

Query:
e.concatLists(e.data(d.list(d.int8(1), d.int8(2), d.int8(3))), e.data(d.list(d.int8(4), d.int8(5), d.int8(6))))
Result:
[ 1, 2, 3, 4, 5, 6 ]

inList

available since: 1.0.0

Reports whether a value is present in a list.

struct {}bool

Example:

Query:
e.inList({
  • value: d.string("foo"),
  • in: e.data(d.list(d.string("foo"), d.int8(10), d.int8(15))),
}
)
Result:
true
Notes:

yields true because a foo value is in the list

Example:

Query:
e.inList({
  • value: d.string("bar"),
  • in: e.data(d.list(d.string("foo"), d.int8(10), d.int8(15))),
}
)
Notes:

yields false because there is no bar value in the list

filterList

available since: 1.0.0

Filter takes a list and returns a list of equal or smaller length. It passes every element in the list through a boolean expression and keeps only the ones that evaluate to true.

struct {
  • expression :expressionTbool
  • value :listT
}
listS

Example:

Query:
e.filterList(e.data(d.list(d.int8(3), d.int8(2), d.int8(1))), f.function(["_index_0","_value_0"], e.gtInt8({
  • scope: "_value_0",
}
, e.data(d.int8(2))
)
)
)
Result:
[ 3 ]

first

available since: 1.0.0

Takes a list value and returns its first element or an error if the list is empy.

listTT

Example:

Query:
e.first(e.data(d.list(d.int8(3), d.int8(2), d.int8(1))))
Result:
3

mapList

available since: 1.0.0

Takes a list value and builds a new one of equal size by passing each value through a mapping expression.

struct {
  • expression :expressionTS
  • value :listT
}
listS

Example:

Query:
e.mapList(e.all(e.tag(d.string("_tag"))), f.function(["_index_0","_value_0"], e.field("tag", {
  • scope: "_value_0",
}
)
)
)
Result:
[ "_role", "_tag", "_model", "_user", "_expression", "_migration" ]
Notes:

This example yields all existing tag names in a list. The result of virgin Karma instance would be ['_migration', '_user', '_tag', '_role', '_expression', '_model'].

slice

available since: 1.0.0

Takes a list and slices it at "offset" returning "length" elements.

struct {}listT

Example:

Query:
e.slice({
  • value: e.data(d.list(d.int8(1), d.int8(2), d.int8(3))),
  • offset: d.int64(1),
  • length: d.int64(2),
}
)
Result:
[ 2, 3 ]

leftFoldList

available since: 1.0.0

Takes a list and reduces it to a single value of different type from left to right.

Example:

Query:
e.leftFoldList(e.data(d.list(d.string("bar"), d.string("baz"))), e.data(d.struct({
  • value: d.string("foo"),
}
)
)
, f.function(["_aggregator_0","_value_0"], e.setField({
  • name: "value",
  • value: e.joinStrings({
    • separator: d.string(" "),
    • strings: e.data(d.list(d.expr(e.field("value", {
      • scope: "_aggregator_0",
      }
      )
      )
      , d.expr({
      • scope: "_value_0",
      }
      )
      )
      )
      ,
    }
    )
    ,
  • in: {
    • scope: "_aggregator_0",
    }
    ,
}
)
)
)
Result:
{ "value": "foo bar baz" }

reduceList

available since: 1.0.0

Takes a list and reduces it to a single value by passing its elements pairwise as a tuple to a reducer expression.

struct {
  • expression :S
  • value :listT
}
S

Example:

Query:
e.reduceList({
  • value: e.data(d.list(d.int8(5), d.int8(10), d.int8(15))),
  • initial: d.int8(),
  • reducer: f.function(["_aggregator_0","_value_0"], e.addInt8({
    • scope: "_aggregator_0",
    }
    , {
    • scope: "_value_0",
    }
    )
    )
    ,
}
)
Result:
30

memSort

available since: 1.0.0

Takes a list value and returns it sorted by invoking "expression" (which must return a scalar) on each element. Note that, as the name implies, this function needs to load a list fully into memory.

Example:

Query:
e.memSort(e.data(d.list(d.int8(2), d.int8(8), d.int8(4))), f.function(["_value_0"], {
  • scope: "_value_0",
}
)
)
Result:
[ 2, 4, 8 ]