while-else loop and string.startsWith

This commit is contained in:
sergeych committed 2025-05-30 13:50:37 +04:00
1 parent f3b3ebbb52
commit f7b35c7576
7 files changed
+142 -64

No files matched your search

+16 -8
View File
@@ -46,11 +46,19 @@ __Important__ negative indexes works wherever indexes are used, e.g. in insertio
## Members
| name | meaning | type |
|----------------------------|----------------------------------------------|----------|
| `size` | current size | Int |
| `add(elements...)` | add one or more elements to the end | Any |
| `addAt(index,elements...)` | insert elements at position | Int, Any |
| `removeAt(index)` | remove element at position | Int |
| `removeAt(start,end)` | remove range, start inclusive, end exclusive | Int, Int |
| | | |
| name | meaning | type |
|-----------------------------------|-------------------------------------|----------|
| `size` | current size | Int |
| `add(elements...)` | add one or more elements to the end | Any |
| `addAt(index,elements...)` | insert elements at position | Int, Any |
| `removeAt(index)` | remove element at position | Int |
| `removeRangeInclusive(start,end)` | remove range, inclusive (1) | Int, Int |
| | | |
(1)
: end-inclisiveness allows to use negative indexes to, for exampe, remove several last elements, like `list.removeRangeInclusive(-2, -1)` will remove two last elements.
# Notes
Could be rewritten using array as a class but List as the interface
+26 -4
View File
@@ -378,7 +378,7 @@ Note that to add to the end you still need to use `add` or positive index of the
x.removeAt(2)
assert( x == [1, 2, 4, 5])
// or remove range (start inclusive, end exclusive):
x.removeAt(1,3)
x.removeRangeInclusive(1,2)
assert( x == [1, 5])
>>> void
@@ -390,9 +390,9 @@ Again, you can use negative indexes. For example, removing last elements like:
x.removeAt(-1)
assert( x == [1, 2, 3, 4])
// remove 3 last:
x.removeAt(-3,1)
assert( x == [1])
// remove 2 last:
x.removeRangeInclusive(-2,-1)
assert( x == [1, 2])
>>> void
# Flow control operators
@@ -514,6 +514,28 @@ occasional clash. Labels are also scoped to their context and do not exist outsi
Right now labels are implemented only for the while loop. It is intended to be implemented for all loops and returns.
## while - else statement
The while loop can be followed by the else block, which is executed when the loop
ends normally, without breaks. It allows override loop result value, for example,
to not calculate it in every iteration. Here is the sample:
### Else, labels, and break practical sample
// Get a first word that starts with a given previx and return it:
fun findPrefix(prefix,words) {
var index = 0
while( index < words.size ) {
val w = words[index++]
if( w.startsWith(prefix) ) break w
}
else null
}
val words = ["hello", "world", "foobar", "end"]
assert( findPrefix("bad", words) == null )
findPrefix("foo", words )
>>> "foobar"
# Self-assignments in expression
There are auto-increments and auto-decrements: