Loops
for iterates. The word after it is the mode — what kind of traversal
this is — and it is required, so a loop always says what it walks.
| Mode | Walks |
|---|---|
from | the values of a collection |
ref | the keys of a table, or the indices of a list |
range | a numeric range |
Over values#
faber convert --to en — English reader surfacemain {
let list<int> numeri ← [1, 2, 3]
for from numeri let n {
print n
}
}faber convert --to la — canonical Faberincipit {
sit lista<numerus> numeri ← [1, 2, 3]
itera ex numeri sit n {
nota n
}
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<จํานวน> numeri ← [1, 2, 3]
วน ออก numeri กำหนด n {
บันทึก n
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<整数> numeri ← [1, 2, 3]
遍历 取自 numeri 设 n {
显示 n
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<整數> numeri ← [1, 2, 3]
遍歷 取自 numeri 設為 n {
註記 n
}
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<số> numeri ← [1, 2, 3]
lặp từ numeri đặt n {
ghi_chú n
}
}faber convert --to ar — Arabicبداية {
ليكن قائمة<عدد> numeri ← [1, 2, 3]
كرر من numeri ليكن n {
اعرض n
}
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<संख्या> numeri ← [1, 2, 3]
दोहराओ सेवन numeri बैठा n {
दिखाओ n
}
}In lockstep#
from also walks several lists together. Join the sources with and (English
and) and the binders the same way, and the loop walks them all in lockstep —
one shared index, one value from each list per turn:
main {
let list<string> nomina ← ["Alia", "Brunus"]
let list<int> aetates ← [34, 27]
for from nomina and aetates at [i] let name and aetas {
print i, name, aetas
}
}at (English at) names the shared index. The sources are listas, and the
binders are plain names, one per source. The lists must have the same length:
lengths known to differ are refused at check (`SEM010
itera_lockstep_length_mismatch`), and the walk never truncates to the shorter
list. Maps and sets cannot be lockstep sources.
Since 1.19 a comma in a loop head means the next loop in, and every clause after
it starts with its own from or range. The old comma series for from a, b is
refused (PARSE001 itera_bare_comma_source). Nested comma clauses such as
for range 0 ‥ m, range 0 ‥ n let i, j parse but are not lowered in 1.19
(SEM008 itera_nested_clause_unsupported): write two nested loops.
Over keys#
faber convert --to en — English reader surfacemain {
let map<string, string> persona ← { "nomen": "Marcus", "urbs": "Roma" }
for ref persona let clavis {
print clavis, persona[clavis]
}
}faber convert --to la — canonical Faberincipit {
sit tabula<textus, textus> persona ← { "nomen": "Marcus", "urbs": "Roma" }
itera de persona sit clavis {
nota clavis, persona[clavis]
}
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด ตาราง<ข้อความ, ข้อความ> persona ← { "nomen": "Marcus", "urbs": "Roma" }
วน จาก persona กำหนด clavis {
บันทึก clavis, persona[clavis]
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 映射<文本, 文本> persona ← { "nomen": "Marcus", "urbs": "Roma" }
遍历 借自 persona 设 clavis {
显示 clavis, persona[clavis]
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 表格<文字, 文字> persona ← { "nomen": "Marcus", "urbs": "Roma" }
遍歷 從 persona 設為 clavis {
註記 clavis, persona[clavis]
}
}faber convert --to vi — Vietnamesebắt_đầu {
đặt bảng<văn_bản, văn_bản> persona ← { "nomen": "Marcus", "urbs": "Roma" }
lặp ra persona đặt clavis {
ghi_chú clavis, persona[clavis]
}
}faber convert --to ar — Arabicبداية {
ليكن جدول<نص, نص> persona ← { "nomen": "Marcus", "urbs": "Roma" }
كرر عن persona ليكن clavis {
اعرض clavis, persona[clavis]
}
}faber convert --to hi — Hindiआरंभ {
बैठा तालिका<पाठ, पाठ> persona ← { "nomen": "Marcus", "urbs": "Roma" }
दोहराओ से persona बैठा clavis {
दिखाओ clavis, persona[clavis]
}
}The same mode gives you list indices, which is what you want when the position matters as much as the value:
faber convert --to en — English reader surfacemain {
let list<int> numeri ← [10, 20, 30]
for ref numeri let index {
print index, numeri[index]
}
}faber convert --to la — canonical Faberincipit {
sit lista<numerus> numeri ← [10, 20, 30]
itera de numeri sit index {
nota index, numeri[index]
}
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<จํานวน> numeri ← [10, 20, 30]
วน จาก numeri กำหนด index {
บันทึก index, numeri[index]
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<整数> numeri ← [10, 20, 30]
遍历 借自 numeri 设 index {
显示 index, numeri[index]
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<整數> numeri ← [10, 20, 30]
遍歷 從 numeri 設為 index {
註記 index, numeri[index]
}
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<số> numeri ← [10, 20, 30]
lặp ra numeri đặt index {
ghi_chú index, numeri[index]
}
}faber convert --to ar — Arabicبداية {
ليكن قائمة<عدد> numeri ← [10, 20, 30]
كرر عن numeri ليكن index {
اعرض index, numeri[index]
}
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<संख्या> numeri ← [10, 20, 30]
दोहराओ से numeri बैठा index {
दिखाओ index, numeri[index]
}
}Over a range#
‥ builds a range. The lower bound is included, the upper bound is not.
faber convert --to en — English reader surfacemain {
for range 0 ‥ 3 let i {
print i
}
}faber convert --to la — canonical Faberincipit {
itera ab 0 ‥ 3 sit i {
nota i
}
}faber convert --to th-TH — Thaiเริ่ม {
วน ช่วง 0 ‥ 3 กำหนด i {
บันทึก i
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
遍历 范围 0 ‥ 3 设 i {
显示 i
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
遍歷 範圍 0 ‥ 3 設為 i {
註記 i
}
}faber convert --to vi — Vietnamesebắt_đầu {
lặp khoảng 0 ‥ 3 đặt i {
ghi_chú i
}
}faber convert --to ar — Arabicبداية {
كرر نطاق 0 ‥ 3 ليكن i {
اعرض i
}
}faber convert --to hi — Hindiआरंभ {
दोहराओ सीमा 0 ‥ 3 बैठा i {
दिखाओ i
}
}step sets the step:
faber convert --to en — English reader surfacemain {
for range 0 ‥ 10 step 2 let i {
print i
}
}faber convert --to la — canonical Faberincipit {
itera ab 0 ‥ 10 per 2 sit i {
nota i
}
}faber convert --to th-TH — Thaiเริ่ม {
วน ช่วง 0 ‥ 10 ต่อ 2 กำหนด i {
บันทึก i
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
遍历 范围 0 ‥ 10 步 2 设 i {
显示 i
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
遍歷 範圍 0 ‥ 10 每 2 設為 i {
註記 i
}
}faber convert --to vi — Vietnamesebắt_đầu {
lặp khoảng 0 ‥ 10 qua 2 đặt i {
ghi_chú i
}
}faber convert --to ar — Arabicبداية {
كرر نطاق 0 ‥ 10 كل 2 ليكن i {
اعرض i
}
}faber convert --to hi — Hindiआरंभ {
दोहराओ सीमा 0 ‥ 10 प्रति 2 बैठा i {
दिखाओ i
}
}A range is also a value. Store it, print it, and walk it later; the English
type word is range<int>.
faber convert --to en — English reader surfacemain {
let range<int> r ← 0 ‥ 3
print r
for range r let i {
print i
}
}faber convert --to la — canonical Faberincipit {
sit intervallum<numerus> r ← 0 ‥ 3
nota r
itera ab r sit i {
nota i
}
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด อันตรภาค<จํานวน> r ← 0 ‥ 3
บันทึก r
วน ช่วง r กำหนด i {
บันทึก i
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 区间<整数> r ← 0 ‥ 3
显示 r
遍历 范围 r 设 i {
显示 i
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 區間<整數> r ← 0 ‥ 3
註記 r
遍歷 範圍 r 設為 i {
註記 i
}
}faber convert --to vi — Vietnamesebắt_đầu {
đặt miền<số> r ← 0 ‥ 3
ghi_chú r
lặp khoảng r đặt i {
ghi_chú i
}
}faber convert --to ar — Arabicبداية {
ليكن فترة<عدد> r ← 0 ‥ 3
اعرض r
كرر نطاق r ليكن i {
اعرض i
}
}faber convert --to hi — Hindiआरंभ {
बैठा अंतराल<संख्या> r ← 0 ‥ 3
दिखाओ r
दोहराओ सीमा r बैठा i {
दिखाओ i
}
}A stored range can carry a step, and the step is part of its type:
let range<int, 2> evens ← 0…10 step 2 walks 0, 2, … 10, and
evens.length() is 6. See ranges.
While#
while loops while a condition holds.
faber convert --to en — English reader surfacemain {
mut int i ← 0
while i ≺ 3 {
i ← i + 1
print i
}
}faber convert --to la — canonical Faberincipit {
varia numerus i ← 0
dum i ≺ 3 {
i ← i + 1
nota i
}
}faber convert --to th-TH — Thaiเริ่ม {
แก้ไขได้ จํานวน i ← 0
ขณะ i ≺ 3 {
i ← i + 1
บันทึก i
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
可变 整数 i ← 0
当 i ≺ 3 {
i ← i + 1
显示 i
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
可變 整數 i ← 0
當 i ≺ 3 {
i ← i + 1
註記 i
}
}faber convert --to vi — Vietnamesebắt_đầu {
khả_biến số i ← 0
trong_khi i ≺ 3 {
i ← i + 1
ghi_chú i
}
}faber convert --to ar — Arabicبداية {
قابل_للتغيير عدد i ← 0
طالما i ≺ 3 {
i ← i + 1
اعرض i
}
}faber convert --to hi — Hindiआरंभ {
परिवर्तनीय संख्या i ← 0
जबतक i ≺ 3 {
i ← i + 1
दिखाओ i
}
}Leaving early#
break breaks out; continue skips to the next iteration.
faber convert --to en — English reader surfacemain {
let list<int> numeri ← [1, 2, 3, 4, 5]
for from numeri let n {
if n ≡ 2 {
continue
}
if n ≻ 3 {
break
}
print n
}
}faber convert --to la — canonical Faberincipit {
sit lista<numerus> numeri ← [1, 2, 3, 4, 5]
itera ex numeri sit n {
si n ≡ 2 {
perge
}
si n ≻ 3 {
rumpe
}
nota n
}
}faber convert --to th-TH — Thaiเริ่ม {
กำหนด รายการ<จํานวน> numeri ← [1, 2, 3, 4, 5]
วน ออก numeri กำหนด n {
ถ้า n ≡ 2 {
ไปต่อ
}
ถ้า n ≻ 3 {
หยุด
}
บันทึก n
}
}faber convert --to zh-Hans — Simplified Chinese入口 {
设 列表<整数> numeri ← [1, 2, 3, 4, 5]
遍历 取自 numeri 设 n {
如果 n ≡ 2 {
继续
}
如果 n ≻ 3 {
中断
}
显示 n
}
}faber convert --to zh-Hant — Traditional Chinese入口 {
設為 列表<整數> numeri ← [1, 2, 3, 4, 5]
遍歷 取自 numeri 設為 n {
若 n ≡ 2 {
繼續
}
若 n ≻ 3 {
中斷
}
註記 n
}
}faber convert --to vi — Vietnamesebắt_đầu {
đặt danh_sách<số> numeri ← [1, 2, 3, 4, 5]
lặp từ numeri đặt n {
nếu n ≡ 2 {
tiếp
}
nếu n ≻ 3 {
dừng
}
ghi_chú n
}
}faber convert --to ar — Arabicبداية {
ليكن قائمة<عدد> numeri ← [1, 2, 3, 4, 5]
كرر من numeri ليكن n {
إذا n ≡ 2 {
تابع
}
إذا n ≻ 3 {
اكسر
}
اعرض n
}
}faber convert --to hi — Hindiआरंभ {
बैठा सूची<संख्या> numeri ← [1, 2, 3, 4, 5]
दोहराओ सेवन numeri बैठा n {
यदि n ≡ 2 {
जारी
}
यदि n ≻ 3 {
तोड़ो
}
दिखाओ n
}
}A loop can also carry its own error handler — see
catch attaches to more than do.
Thread loops#
A loop marked thread (la thread) runs its iterations as independent lanes:
for from src at [i, j] thread lane let v { … }. Because the lanes run
independently, one lane cannot stop the others. continue skips the rest of
this lane, but break is SEM010 break_in_thread_body and a plain return is
SEM010 return_in_thread_body. To use a lane's own stores, write only that
lane's coordinate; a loop whose independence the compiler cannot prove is
SEM010 filum_independence_unproven.
return thread (la return thread) ends the current lane from any depth of the
lane body's own blocks. It takes no value, because lanes deliver results
through their stores. Anywhere else it is refused: outside a thread loop
(SEM010 return_thread_outside_lane), with a value
(SEM010 return_thread_with_value), or inside a nested closure or reduction
body (SEM010 return_thread_in_nested_body).
kernel lane_exit(tensor<f32, [4]> values) → void {
for from values at [i] thread lane let value {
return thread
}
}fn run() → void {
return thread
}
main {
run()
}Known issue in 1.19: return thread is checked but not yet compiled. The code
generators and the lowering to the runner's form refuse it by name
(return_thread_lowering_pending), so a program that uses it passes `faber
check and stops at faber check --lower, faber build and faber run`. The
formatter keeps it as written.
Related: Control flow · Types and widths