mirror of
https://github.com/hi-language/hi-language.github.io.git
synced 2026-01-14 08:38:36 +00:00
Feat: Document unified iteration and control flow
This commit is contained in:
72
index.html
72
index.html
@@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
<!-- Google Fonts -->
|
<!-- Google Fonts -->
|
||||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||||
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
<link rel="preconnect" href="https://fonts.gstatic" crossorigin>
|
||||||
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
|
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&family=JetBrains+Mono:wght@400;700&display=swap" rel="stylesheet">
|
||||||
|
|
||||||
<!-- Markdown & Highlighting Styles -->
|
<!-- Markdown & Highlighting Styles -->
|
||||||
@@ -200,12 +200,21 @@ _(grade) // Prints "C"
|
|||||||
## Data Structures
|
## Data Structures
|
||||||
|
|
||||||
### Arrays
|
### Arrays
|
||||||
Arrays are 0-indexed lists of values.
|
Arrays are 0-indexed lists of values. They support random access via \`[]\` and are iterable.
|
||||||
|
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
primes: [2, 3, 5, 7]
|
primes: [2, 3, 5, 7]
|
||||||
primes[0] = 1 // Mutation
|
_(primes[0]) // Random access: 2
|
||||||
_(primes) // [1, 3, 5, 7]
|
primes[0] = 1
|
||||||
|
_(primes) // Mutation: [1, 3, 5, 7]
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
### Strings
|
||||||
|
Strings are sequences of characters. They are immutable but support random access and iteration.
|
||||||
|
|
||||||
|
\`\`\`javascript
|
||||||
|
greeting: "Hi"
|
||||||
|
_(greeting[1]) // "i"
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
## Destructuring
|
## Destructuring
|
||||||
@@ -236,29 +245,52 @@ _(y) // -5
|
|||||||
|
|
||||||
## Repetition
|
## Repetition
|
||||||
|
|
||||||
Loops are initiated with the \`*\` operator following a standard three-part condition or a simple boolean expression.
|
Repetition is handled by a unified iteration protocol: \`(source -> pattern) * { ... }\`. This single, concise syntax adapts to iterate over any collection type.
|
||||||
|
|
||||||
- \`>>\` is used for **continue**.
|
### Iterating Collections
|
||||||
- \`^\` is used for **return/break**.
|
The \`->\` operator extracts elements from a source into a pattern.
|
||||||
|
|
||||||
\`\`\`javascript
|
\`\`\`javascript
|
||||||
// for-style loop
|
// Iterate over an Array's values
|
||||||
(i: 0; i < 3; i = i + 1) * {
|
(["a", "b"] -> item) * { _(item) }
|
||||||
_("Iteration: " + i)
|
|
||||||
}
|
|
||||||
|
|
||||||
// while-style loop
|
// Iterate over an Array with index and value
|
||||||
active: !0
|
(["a", "b"] -> [i, item]) * { _(i + ": " + item) }
|
||||||
(active) * {
|
|
||||||
_("Looping..."); active = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
// Loop Control
|
// Iterate over a Block's key-value pairs
|
||||||
(i: 0; i < 10; i = i + 1) * {
|
({ id: 1 } -> [k, v]) * { _(k + " is " + v) }
|
||||||
(i == 5) ? { ^ } // Break at 5
|
|
||||||
|
// Iterate over a String's characters
|
||||||
|
("Hi" -> char) * { _(char) }
|
||||||
|
|
||||||
|
// Destructure directly in the loop signature
|
||||||
|
users: [{ name: "Orion" }]
|
||||||
|
(users -> { name }) * { _("User: " + name) }
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
### Numeric Ranges
|
||||||
|
The \`..\` operator creates an iterable numeric range for concise, traditional loops.
|
||||||
|
|
||||||
|
\`\`\`javascript
|
||||||
|
// A range from 0 up to (but not including) 3
|
||||||
|
(0..3 -> i) * {
|
||||||
|
_("Iteration: " + i) // Prints 0, 1, 2
|
||||||
|
}
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
### Loop Control
|
||||||
|
Control flow within loops is managed by distinct symbols:
|
||||||
|
- \`.\`: **Break**. Immediately terminates the loop.
|
||||||
|
- \`>>\`: **Continue**. Skips to the next iteration.
|
||||||
|
- \`^\`: **Return**. Exits the parent block, not just the loop.
|
||||||
|
|
||||||
|
\`\`\`javascript
|
||||||
|
(0..10 -> i) * {
|
||||||
|
(i == 2) ? { >> } // Skip 2
|
||||||
|
(i == 5) ? { . } // Break at 5
|
||||||
_("i is " + i)
|
_("i is " + i)
|
||||||
}
|
}
|
||||||
// Prints i is 0 through 4
|
// Prints: i is 0, i is 1, i is 3, i is 4
|
||||||
\`\`\`
|
\`\`\`
|
||||||
|
|
||||||
## Imports
|
## Imports
|
||||||
|
|||||||
Reference in New Issue
Block a user