mlld
Version:
mlld: llm scripting language
35 lines (27 loc) • 754 B
Markdown
---
id: foreach
title: Foreach
brief: Transform collections by applying a function to each element
category: control-flow
tags: [iteration, transformation, arrays]
related: [for-arrow, for-collection]
related-code: [interpreter/eval/foreach.ts]
updated: 2026-01-05
---
`foreach` applies a function to each element, returning transformed array.
```mlld
var = ["alice", "bob", "charlie"]
exe = `Hi !`
var = foreach
>> ["Hi alice!", "Hi bob!", "Hi charlie!"]
```
**In exe:**
```mlld
exe = foreach
show >> ["[a]", "[b]"]
```
**With options:**
```mlld
show foreach with { separator: " | " }
>> "Hi alice! | Hi bob! | Hi charlie!"
```