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 "What's you name?".println
name := ().read_line.trim name := ().read_line.try((name) -> name.trim, () -> "<no name given>")
("Hello, ", name, "!").concat.println ("Hello, ", name, "!").concat.println

View File

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

View File

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

View File

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

View File

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

View File

@ -60,22 +60,27 @@ loop {
().print_board ().print_board
loop { loop {
(if player c_blue else c_green, "> ", c_gray).concat.print (if player c_blue else c_green, "> ", c_gray).concat.print
input := ().read_line.trim ().read_line.try(
input.parse_int.try( (line) -> {
(field) -> { input := line.trim
if (field.gt(0), field.ltoe(9)).all { input.parse_int.try(
(l, m, r) := if field.gt(6) (&f1, &f2, &f3) else if field.gt(3) (&f4, &f5, &f6) else (&f7, &f8, &f9) (field) -> {
col := field.subtract(1).modulo(3) if (field.gt(0), field.ltoe(9)).all {
f := if col.eq(0) l else if col.eq(1) m else r (l, m, r) := if field.gt(6) (&f1, &f2, &f3) else if field.gt(3) (&f4, &f5, &f6) else (&f7, &f8, &f9)
if f.deref.eq(0) { col := field.subtract(1).modulo(3)
f = if player 1 else -1 f := if col.eq(0) l else if col.eq(1) m else r
(()) if f.deref.eq(0) {
} else "field already occupied".println f = if player 1 else -1
} else { (())
"number must be 1 <= n <= 9".println } else "field already occupied".println
} } else {
"number must be 1 <= n <= 9".println
}
}
() -> "not a number!".println
)
} }
() -> "not a number!".println () -> (())
) )
} }
&player = player.eq(false) &player = player.eq(false)