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(
(line) -> line.trim.parse_float.try(
(n) -> &total = (total, n).sum (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 "- 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(
(line) -> {
input := line.trim
num := (input, 1).substring.trim.parse_float.try( num := (input, 1).substring.trim.parse_float.try(
(val) -> val (val) -> val
() -> 0.0 () -> 0.0
@ -23,7 +25,13 @@ loop {
&current = (current, num).div &current = (current, num).div
} else if mode.eq("=") { } else if mode.eq("=") {
&current = num &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 := () -> { 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,7 +60,9 @@ 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(
(line) -> {
input := line.trim
input.parse_int.try( input.parse_int.try(
(field) -> { (field) -> {
if (field.gt(0), field.ltoe(9)).all { if (field.gt(0), field.ltoe(9)).all {
@ -78,6 +80,9 @@ loop {
() -> "not a number!".println () -> "not a number!".println
) )
} }
() -> (())
)
}
&player = player.eq(false) &player = player.eq(false)
().check_board.try( ().check_board.try(
() -> (), () -> (),