From 5da91cd4b1e13b6ebe2e457f0debbb1c40b8f7a7 Mon Sep 17 00:00:00 2001 From: Eric Willigers Date: Sat, 23 May 2026 10:34:20 +1000 Subject: [PATCH] map-index example --- config.json | 12 ++++++++---- .../backyard-birdwatcher/.docs/introduction.md | 13 +++++++++++++ .../concept/lasagna-luminary/.docs/introduction.md | 12 ++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/config.json b/config.json index 2dd8c76..4722385 100644 --- a/config.json +++ b/config.json @@ -1306,7 +1306,8 @@ "unicode", "arrays", "windows", - "locals" + "locals", + "while" ], "difficulty": 5 }, @@ -1469,7 +1470,8 @@ "sequences", "dynamic-variables", "mutation", - "bitwise-operations" + "bitwise-operations", + "while" ], "difficulty": 7 }, @@ -1501,7 +1503,8 @@ "higher-order-sequences", "sequences", "strings", - "locals" + "locals", + "while" ], "difficulty": 7 }, @@ -1556,7 +1559,8 @@ "combinators", "errors", "mutation", - "locals" + "locals", + "while" ], "difficulty": 8 }, diff --git a/exercises/concept/backyard-birdwatcher/.docs/introduction.md b/exercises/concept/backyard-birdwatcher/.docs/introduction.md index 0902aa4..ef9fa75 100644 --- a/exercises/concept/backyard-birdwatcher/.docs/introduction.md +++ b/exercises/concept/backyard-birdwatcher/.docs/introduction.md @@ -61,6 +61,19 @@ index-or-length ( seq n -- seq n' ) { 10 20 } 3 index-or-length head . ! => { 10 20 } ``` +The starred variants `head*` and `tail*` count from the end — +`head*` drops the last `n`, `tail*` keeps the last `n`: + +``` +head* ( seq n -- headseq ) ! everything except the last n +tail* ( seq n -- tailseq ) ! the last n elements +``` + +```factor +{ 1 2 3 4 5 } 2 head* . ! => { 1 2 3 } +{ 1 2 3 4 5 } 2 tail* . ! => { 4 5 } +``` + For an arbitrary slice between two indices, `subseq` takes the *start* (inclusive) and *end* (exclusive): diff --git a/exercises/concept/lasagna-luminary/.docs/introduction.md b/exercises/concept/lasagna-luminary/.docs/introduction.md index e83f67e..4c8e7a0 100644 --- a/exercises/concept/lasagna-luminary/.docs/introduction.md +++ b/exercises/concept/lasagna-luminary/.docs/introduction.md @@ -106,6 +106,18 @@ H{ { "a" 1 } { "b" 2 } } ! => H{ { "a" -1 } { "b" -2 } } ``` +The same shape works for `map-index` (in [`sequences`][sequences]), +whose quotation receives the element *and* its index. The names +make clear which operand plays which role: + +```factor +USING: locals sequences ; + +! Divide each element by its 1-based position: +{ 10 80 270 } [| elt i | elt i 1 + / ] map-index . +! => { 10 40 90 } +``` + The lambda's stack effect is determined by the input list and what its body produces.