Foreach

Foreach( array_or_page, lambda )
Foreach( array_or_page, lambda )
Foreach( array_or_page, lambda )
Foreach( array_or_page, lambda )
foreach( lambda, array_or_page )
Foreach( array_or_page, lambda )

Description

The Foreach function applies the Lambda serially to each member of an Array or Page, and returns the original, unmodified array.

The Foreach function is very useful when the original array does not need to be modified, but a side effect is required for every value in an array. Later invocations of the Lambda can see the side effects of earlier invocations of the Lambda.

Parameters

Parameter Type Definition and Requirements

array_or_page

Array or Page

The Array or Page over which the lambda function iterates/operates.

lambda

The anonymous function to be executed.

Returns

The original array_or_page value without any modifications.

Examples

The following query iterates over the results returned by the Paginate function, executing the Lambda for each value in the page of results. The page of results contains an array of references, and each reference’s document is updated by the Lambda.

try
{
    Value result = await client.Query(
        Foreach(
            Paginate(Match(Index("spells_by_element"), "fire")),
            Lambda(
                "spell",
                Update(
                    Var("spell"),
                    Obj(
                        "data", Obj(
                            "spellbook",
                            Ref(Collection("spellbooks"), "181388642139243008")
                        )
                    )
                )
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(data: Arr(RefV(id = "181388642046968320", collection = RefV(id = "spells", collection = RefV(id = "collections"))), RefV(id = "181388642071085568", collection = RefV(id = "spells", collection = RefV(id = "collections")))))
result, err := 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.Ref(f.Collection("spellbooks"), "181388642139243008")}}))))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[data:[{181388642046968320 0xc00008e300 0xc00008e300 <nil>} {181388642071085568 0xc00008e4b0 0xc00008e4b0 <nil>}]]
System.out.println(
    client.query(
        Foreach(
            Paginate(
                Match(Index("spells_by_element"), Value("fire"))
            ),
            Lambda(
                Value("spell"),
                Update(
                    Var("spell"),
                    Obj(
                        "data", Obj(
                            "spellbook",
                            Ref(
                                Collection("spellbooks"),
                                Value("181388642139243008")
                            )
                        )
                    )
                )
            )
        )
    ).get());
{data: [ref(id = "181388642046968320", collection = ref(id = "spells", collection = ref(id = "collections"))), ref(id = "181388642071085568", collection = ref(id = "spells", collection = ref(id = "collections")))]}
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.Collection('spellbooks'),
              '181388642139243008',
            ),
          },
        },
      ),
    ),
  )
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  data: [
    Ref(Collection("spells"), "181388642046968320"),
    Ref(Collection("spells"), "181388642071085568")
  ]
}
result = client.query(
  q.foreach(
    lambda spell: q.update(
      spell,
      {
        "data": {
          "spellbook": q.ref(q.collection("spellbooks"), "181388642139243008")
        }
      }
    ),
    q.paginate(q.match(q.index("spells_by_element"), "fire"))
  )
)
print(result)
{'data': [Ref(id=181388642046968320, collection=Ref(id=spells, collection=Ref(id=collections))), Ref(id=181388642071085568, collection=Ref(id=spells, collection=Ref(id=collections)))]}
Foreach(
  Paginate(
    Match(Index('spells_by_element'), 'fire')
  ),
  Lambda(
    'spell',
    Update(
      Var('spell'),
      {
        data: {
          spellbook: Ref(
            Collection('spellbooks'),
            '181388642139243008',
          ),
        },
      },
    ),
  ),
)
{
  data: [
    Ref(Collection("spells"), "181388642046968320"),
    Ref(Collection("spells"), "181388642071085568")
  ]
}
Query metrics:
  •    bytesIn:  261

  •   bytesOut:  259

  • computeOps:    1

  •    readOps:    1

  •   writeOps:    2

  •  readBytes:  341

  • writeBytes:  260

  •  queryTime: 29ms

  •    retries:    0

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!