change loop function so you can write ().loop(() -> do_smth)

This commit is contained in:
Mark
2023-11-08 15:11:28 +01:00
parent cab81059f3
commit 39fca08541
5 changed files with 37 additions and 23 deletions

View File

@@ -1,10 +1,10 @@
total := 0.0
{() -> {
().loop(() -> {
("Total: ", total, ". Type a number to change.").concat.println
().read_line.trim.parse_float.try((
n -> &total = (total, n).sum,
// not a number, so return a 1-tuple to break from the loop
() -> (())
))
}}.loop
})
"Goodbye.".println

View File

@@ -5,7 +5,7 @@
current := 0.0
{() -> {
().loop(() -> {
("[ ", current, " ]").concat.println
input := ().read_line.trim
num := (input, 1).substring.trim.parse_float.try((
@@ -24,4 +24,4 @@ current := 0.0
} else if input.eq("exit") {
(())
}
}}.loop
})

View File

@@ -1,5 +1,5 @@
gcd := vals -> {
() -> {
().loop(() -> {
(a, b) := vals
if a.eq(b)
(a)
@@ -7,8 +7,8 @@ gcd := vals -> {
&vals = (a, b.subtract(a))
else
&vals = (a.subtract(b), b)
}
}.loop
})
}
get_num := () -> {
line := ().read_line.trim

View File

@@ -17,7 +17,7 @@ split_once := (s, delim) -> {
split_ := (s, delim, require_nonempty) -> {
out := (s).as_list
&out.pop
{
().loop(
() -> s.split_once(delim).try((
(s1, s2) -> {
&s = s2
@@ -30,7 +30,7 @@ split_ := (s, delim, require_nonempty) -> {
(())
}
))
}.loop
)
out
}
@@ -62,9 +62,11 @@ read_matrix_line := width -> {
if w.eq(0) {
width = line.len
} else {
{() -> if line.len.subtract(w).signum.eq(-1) {
&line.push(0.0)
} else (())}.loop
().loop(() ->
if line.len.subtract(w).signum.eq(-1) {
&line.push(0.0)
} else (())
)
}
(line)
}
@@ -87,10 +89,12 @@ matrix_get := (matrix, (line, col)) -> {
leftpad := (str, l) -> {
str := (str).concat
d := l.subtract(str.len)
{() -> if d.signum.eq(1) {
&str = (" ", str).concat
&d = d.subtract(1)
} else {(())}}.loop
().loop(() ->
if d.signum.eq(1) {
&str = (" ", str).concat
&d = d.subtract(1)
} else (())
)
str
}