Filter

Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )
filter_( lambda, array_or_set )
filter array_or_set lambda
Filter( arrayOrSet, lambda )
Filter( arrayOrSet, lambda )

Description

The Filter function applies the lambda function to each member of arrayOrSet, which is an Array, Page, or Set. The return value matches the arrayOrSet type, and contains only those elements for which the lambda function returns true.

Providing a lambda function which does not return a Boolean results in an "invalid argument" error. If a Page is passed, its decorated fields are preserved in the result.

Parameters

Argument Type Definition and Requirements

arrayOrSet

Array, Page, or Set

The group of items over which the lambda function iterates/operates.

lambda

The anonymous function to be executed, which must return a boolean.

Returns

Having the same type as arrayOrSet, the items for which the lambda function returned true.

Examples

The query below iterates over the array containing the values 1, 2, 3, executing the lambda function for each value. The lambda function returns true if the Modulo 2 of the value is 0, otherwise it returns false.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "filter": {
            "lambda": "i",
            "expr": { "equals": [ 0, { "modulo": [ { "var": "i" }, 2 ] } ] }
          },
          "collection": [ 1, 2, 3 ]
        }'
client.Query(
  Filter(Arr(1, 2, 3), i => EqualsFn(0, Modulo(i, 2))));
System.out.println(
  client.query(
    Filter(
      Arr(Value(1), Value(2), Value(3)),
      Lambda(
            Value("i"),
            Equals(Value(0), Modulo(Var("i"), Value(2)))
      )
    )
  ).get());
result, _ := client.Query(
    f.Filter(
        f.Arr{1, 2, 3},
        f.Lambda("i", f.Equals(0, f.Modulo(f.Var("i"), 2))),
    ),
)

fmt.Println(result)
client.query(
  Filter(Arr(1, 2, 3), Lambda { i => Equals(0, Modulo(i, 2)) }))
client.query(
  q.filter_(
    lambda i: q.equals(0, q.modulo(i, 2)),
    [1, 2, 3]
  ))
$client.query do
  filter [1, 2, 3] do |i|
    equals(0, modulo(i, 2))
  end
end
client.query(
    Filter(
        collection: Arr(1, 2, 3),
        with: { i in Equals(0, Modulo(i, 2)) }
    )
)
client.query(
  q.Filter(
    [1, 2, 3],
    q.Lambda(
      'i',
      q.Equals(0, q.Modulo(q.Var('i'), 2)),
    ),
  )
)
.then((ret) => console.log(ret))
HTTP/1.1 200 OK
{ "resource": [ 2 ] }
[ 2 ]
[2]
[2]
[ 2 ]
[ 2 ]
[ 2 ]
[ 2 ]
[ 2 ]

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve! documentation@fauna.com

Thank you for your feedback!