Foreach

Foreach( collection, lambda )
Foreach( collection, lambda )
Foreach( collection, lambda )
Foreach( collection, lambda )
foreach( lambda, collection )
foreach collection, lambda
Foreach( collection, lambda )
Foreach( collection, lambda )

Description

The Foreach function applies the Lambda serially to each member of a "collection", and returns the original collection. The Foreach function is very useful when the original collection does not need to be modified, but a side effect is required for every value in a collection. Later invocations of the Lambda can see the side effects of earlier invocations of the Lambda.

Parameters

Argument Type Definition and Requirements

collection

Collection

The target collection over which the lambda function iterates/operates.

lambda

The anonymous function to be executed.

Returns

The original collection without any modifications.

Examples

The query below iterates over the results returned by the Paginate function, executing the Lambda for each value in the result collection. The result collection contains an array of references, each reference’s instance is updated by the lambda.

curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "foreach": {
            "lambda": "spell",
            "expr": {
              "update": { "var": "spell" },
              "params": {
                "object": {
                  "data": {
                    "object": {
                      "spellbook": { "@ref": "classes/spellbooks/181388642139243008" }
                    }
                  }
                }
              }
            }
          },
          "collection": {
            "paginate": {
              "match": { "index": "spells_by_element" },
              "terms": "fire"
            }
          }
        }'
client.Query(
  Foreach(
    Paginate(Match(Index("spells_by_element"), "fire")),
    spell => Update(
      spell,
      Obj(
        "data", Obj(
          "spellbook", Ref("classes/spellbooks/181388642139243008")
        )
      ))));
System.out.println(
      client.query(
          Foreach(
             Paginate(
                Match(Index(Value("spells_by_element")), Value("fire"))
             ),
             Lambda(
                Value("spell"),
                Update(
                   Var("spell"),
                   Obj("data",
                      Obj("spellbook",
                      Ref(Class(Value("spellbooks")),Value(181388642139243008L)))
                   )
                )
             )
         )
      ).get()
);
result, _ := client.Query(
    f.Foreach(
        f.Paginate(
            f.MatchTerm(f.Index("spells_by_element"), "fire"),
        ),
        f.Lambda(
            "spell",
            f.Update(
                f.Var("spell"),
                f.Obj{
                    "data": f.Obj{
                        "spellbook": f.RefClass(f.Class("spellbooks"), "181388642139243008"),
                    },
                },
            ),
        ),
    ),
)

fmt.Println(result)
client.query(
  Foreach(
    Paginate(Match(Index("spells_by_element"), "fire")),
    Lambda { spell =>
      Update(
        spell,
        Obj(
          "data" -> Obj(
            "spellbook" -> Ref("classes/spellbooks/181388642139243008")
          )
        ))
    }))
client.query(
  q.foreach(
    lambda spell: q.update(
      spell,
      {
        "data": {
          "spellbook": Ref("classes/spellbooks/181388642139243008")
        }
      }
    ),
    q.paginate(q.match(q.index("spells_by_element"), "fire"))
  ))
$client.query do
  foreach paginate(match(index('spells_by_element'), 'fire')) do |spell|
    update(spell,
           data: { spellbook: ref('classes/spellbooks/181388642139243008') })
  end
end
client.query(
    Foreach(
        collection: Paginate(
            Match(
                index: Index("spells_by_element"),
                terms: "fire"
            )
        ),
        in: { spell in
            Update(
                ref: spell,
                to: Obj(
                    "data" => Obj(
                        "spellbook" => Ref("classes/spellbooks/181388642139243008")
                    )
                )
            )
        }
    )
)
client.query(
  q.Foreach(
    q.Paginate(q.Match(q.Index("spells_by_element"), "fire")),
    q.Lambda("spell",
      q.Update(q.Var("spell"), { data: { spellbook: q.Ref(q.Class("spellbooks"), 181388642139243008) } }))))
.then((ret) => console.log(ret));
HTTP/1.1 200 OK
{
  "resource": {
    "data": [
      { "@ref": "classes/spells/181388642046968320" },
      { "@ref": "classes/spells/181388642071085568" }
    ]
  }
}
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
{
  data: [
    ref(id = "181388642046968320", class = ref(id = "spells", class = ref(id = "classes"))),
    ref(id = "181388642071085568", class = ref(id = "spells", class = ref(id = "classes")))
  ]
}
map[data:[{181388642046968320 0xc4201c3720 <nil>} {181388642071085568 0xc4201c3940 <nil>}]]
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
{
  "data": [
    { "@ref": "classes/spells/181388642046968320" },
    { "@ref": "classes/spells/181388642071085568" }
  ]
}
{ data:
   [ Ref(id=181388642046968320, class=Ref(id=spells, class=Ref(id=classes))),
     Ref(id=181388642071085568, class=Ref(id=spells, class=Ref(id=classes))) ] }