fix examples

This commit is contained in:
Mark 2024-06-19 13:58:30 +02:00
parent cd21c2171e
commit 11eee72a61
6 changed files with 59 additions and 45 deletions

View File

@ -1,3 +1,3 @@
"What's you name?".println
name := ().read_line.trim
name := ().read_line.try((name) -> name.trim, () -> "<no name given>")
("Hello, ", name, "!").concat.println

View File

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

View File

@ -1,13 +1,15 @@
"- Calculator -".println
"Type =<num> to set the value to that number.".println
"Type +<num>, -<num>, *<num> or /<num> to change the value.".println
"Type exit to exit.".println
"Type exit or press CTRL+D to exit.".println
current := 0.0
loop {
("[ ", current, " ]").concat.println
input := ().read_line.trim
().read_line.try(
(line) -> {
input := line.trim
num := (input, 1).substring.trim.parse_float.try(
(val) -> val
() -> 0.0
@ -23,7 +25,13 @@ loop {
&current = (current, num).div
} else if mode.eq("=") {
&current = num
} else if (input.eq("exit"), input.eq("")).any {
} else if input.eq("exit") {
(())
} else {
"Expected one of +-*/= followed by a number".eprintln
}
}
// CTRL+D
() -> (())
)
}

View File

@ -12,7 +12,7 @@ gcd := vals -> {
get_num := () -> {
loop {
line := ().read_line.trim
line := ().read_line.try((line) -> line.trim, () -> "")
line.parse_float.try(
(n) -> (n)
() -> ("Error: '", line, "' not a number!").concat.println

View File

@ -2,8 +2,8 @@ parse_matrix_line := input -> {
vals := input.str_split(" ")
vals_len := vals.len
// reverse vals
vals_rev := {() -> &vals.pop}.as_list
nums := vals_rev.filter_map(v -> v.parse_float).as_list
// vals_rev := {() -> &vals.pop}.as_list
nums := vals.filter_map(v -> v.parse_float).as_list
if nums.len.eq(vals_len)
if nums.len.signum.eq(1)
(nums)
@ -12,7 +12,7 @@ parse_matrix_line := input -> {
}
read_matrix_line := width -> {
().read_line.trim.parse_matrix_line.try(
().read_line.try((line) -> line.trim, () -> "").parse_matrix_line.try(
(line) -> {
line := [List<Float>] line
w := width.deref
@ -81,9 +81,7 @@ matrix_print := matrix -> {
fget := v -> v.get.try(
() -> {
"called fget but get returned ():".println
v.println
5.panic
("called fget but get returned (): ", v).concat.panic
}
(v) -> v
)

View File

@ -60,7 +60,9 @@ loop {
().print_board
loop {
(if player c_blue else c_green, "> ", c_gray).concat.print
input := ().read_line.trim
().read_line.try(
(line) -> {
input := line.trim
input.parse_int.try(
(field) -> {
if (field.gt(0), field.ltoe(9)).all {
@ -78,6 +80,9 @@ loop {
() -> "not a number!".println
)
}
() -> (())
)
}
&player = player.eq(false)
().check_board.try(
() -> (),