fix tuple type parsing and fix examples

This commit is contained in:
Mark 2024-02-22 20:21:11 +01:00
parent dc2db1d0e8
commit f3dc26a5a7
9 changed files with 42 additions and 89 deletions

View File

@ -1,10 +1,10 @@
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.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, so return a 1-tuple to break from the loop
() -> (()) () -> (())
)) ))
}) }
"Goodbye.".println "Goodbye.".println

View File

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

View File

@ -1,5 +1,5 @@
gcd := vals -> { gcd := vals -> {
().loop(() -> { loop {
(a, b) := vals (a, b) := vals
if a.eq(b) if a.eq(b)
(a) (a)
@ -7,21 +7,17 @@ gcd := vals -> {
&vals = (a, b.subtract(a)) &vals = (a, b.subtract(a))
else else
&vals = (a.subtract(b), b) &vals = (a.subtract(b), b)
}) }
} }
get_num := () -> { get_num := () -> {
line := ().read_line.trim loop {
( line := ().read_line.trim
line.parse_float, line.parse_float.try((
( (n) -> (n)
() -> { () -> ("Error: '", line, "' not a number!").concat.println
("error: '", line, "' not a number!").concat.println ))
1.panic }
}
n -> n,
)
).try
} }
("gcd of 899 and 2900 is ", (899, 2900).gcd).concat.println // 29 ("gcd of 899 and 2900 is ", (899, 2900).gcd).concat.println // 29

View File

@ -1,53 +1,9 @@
// if string is a + delim + b, returns (a, b),
// otherwise returns (). b can contain delim.
split_once := (s, delim) -> {
(
(s, delim).index_of,
(
index -> (
(s, 0, index).substring,
(s, (index, delim.len).sum).substring
)
() -> ()
)
).try
}
// repeats split_once until no delim is left and returns a list of strings
split_ := (s, delim, require_nonempty) -> {
out := (s).as_list
&out.pop
().loop(
() -> s.split_once(delim).try((
(s1, s2) -> {
&s = s2
if s1.len.signum.eq(1)
&out.push(s1)
}
() -> {
if s.len.signum.eq(1)
&out.push(s)
(())
}
))
)
out
}
split := (s, delim) -> s.split_(delim, true)
parse_matrix_line := input -> { parse_matrix_line := input -> {
vals := input.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.pop.try(( nums := vals_rev.filter_map(v -> v.parse_float).as_list
() -> ()
(v) -> v.parse_float.try((
() -> ()
v -> (v)
))
))}.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)
@ -58,15 +14,16 @@ parse_matrix_line := input -> {
read_matrix_line := width -> { read_matrix_line := width -> {
().read_line.trim.parse_matrix_line.try(( ().read_line.trim.parse_matrix_line.try((
(line) -> { (line) -> {
line := [List<Float>] line
w := width.deref w := width.deref
if w.eq(0) { if w.eq(0) {
width = line.len width = line.len
} else { } else {
().loop(() -> loop {
if line.len.subtract(w).signum.eq(-1) { if line.len.subtract(w).signum.eq(-1) {
&line.push(0.0) &line.push(0.0)
} else (()) } else (())
) }
} }
(line) (line)
} }
@ -89,12 +46,12 @@ matrix_get := (matrix, (line, col)) -> {
leftpad := (str, l) -> { leftpad := (str, l) -> {
str := (str).concat str := (str).concat
d := l.subtract(str.len) d := l.subtract(str.len)
().loop(() -> loop {
if d.signum.eq(1) { if d.signum.eq(1) {
&str = (" ", str).concat &str = (" ", str).concat
&d = d.subtract(1) &d = d.subtract(1)
} else (()) } else (())
) }
str str
} }

View File

@ -55,14 +55,14 @@ check_board := () -> {
else if (f3.eq(0).eq(false), (f3, f5, f7).eq).all f3 else if (f3.eq(0).eq(false), (f3, f5, f7).eq).all f3
} }
().loop(() -> { loop {
(if player c_blue else c_green, "Turn: ", if player "x" else "o", c_reset).concat.println (if player c_blue else c_green, "Turn: ", if player "x" else "o", c_reset).concat.println
().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 input := ().read_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 {
(l, m, r) := if field.gt(6) (&f1, &f2, &f3) else if field.gt(3) (&f4, &f5, &f6) else (&f7, &f8, &f9) (l, m, r) := if field.gt(6) (&f1, &f2, &f3) else if field.gt(3) (&f4, &f5, &f6) else (&f7, &f8, &f9)
col := field.subtract(1).modulo(3) col := field.subtract(1).modulo(3)
@ -77,7 +77,7 @@ check_board := () -> {
} }
() -> "not a number!".println () -> "not a number!".println
)) ))
}) }
&player = player.eq(false) &player = player.eq(false)
().check_board.try(( ().check_board.try((
() -> (), () -> (),
@ -94,4 +94,4 @@ check_board := () -> {
(()) (())
} }
)) ))
}) }

View File

@ -1,20 +1,20 @@
is_prime := n -> { is_prime := n -> {
0.1.sleep // wow, what an inefficient algorithm, oh no 0.1.sleep // wow, what an inefficient algorithm, oh no
d := 1 d := 1
().loop(() -> { loop {
&d = (d, 1).sum &d = (d, 1).sum
if d.lt(n) { if d.lt(n) {
if n.modulo(d).eq(0) (false) if n.modulo(d).eq(0) (false)
} else (true) } else (true)
}) }
} }
find_primes := start_at -> { find_primes := start_at -> {
() -> (().loop(() -> { () -> (loop {
out := if start_at.is_prime (start_at) out := if start_at.is_prime (start_at)
&start_at = (start_at, 1).sum &start_at = (start_at, 1).sum
out out
})) })
} }
primes_count := 0 primes_count := 0
@ -24,10 +24,10 @@ background_thread := {() ->
}.thread }.thread
// Show status to the user while the background thread works // Show status to the user while the background thread works
().loop(() -> { loop {
0.2.sleep 0.2.sleep
(" Found ", primes_count, " primes...\r").concat.print (" Found ", primes_count, " primes...\r").concat.print
if background_thread.thread_finished (()) if background_thread.thread_finished (())
}) }
("\nPrimes: ", background_thread.thread_await).concat.println ("\nPrimes: ", background_thread.thread_await).concat.println

View File

@ -77,4 +77,4 @@ make_list_maybe_none := (val_1, val_2) -> {
maybe_none_list := ("one", 2).make_list_maybe_none maybe_none_list := ("one", 2).make_list_maybe_none
maybe_none_list.println maybe_none_list.println
maybe_none_list // use `--check only` to see the type: `List<String/Int/()>` maybe_none_list // use `check` to see the type: `List<String/Int/()>`

View File

@ -1,7 +1,7 @@
fib := n -> { fib := n -> {
// we start with these two values // we start with these two values
v := (0, 1) v := (0, 1)
{() -> { loop {
// subtract 1 from n // subtract 1 from n
&n = (n, -1).sum &n = (n, -1).sum
// extract the latest values // extract the latest values
@ -17,7 +17,7 @@ fib := n -> {
// so we break with the latest value // so we break with the latest value
(r) (r)
} }
}}.loop }
} }
((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> { ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> {

View File

@ -60,14 +60,14 @@ pub fn parse_single_type(src: &mut Source, srca: &Arc<Source>) -> Result<ParsedT
let t = parse_type(src, srca)?; let t = parse_type(src, srca)?;
src.skip_whitespace(); src.skip_whitespace();
match src.peek_char() { match src.peek_char() {
Some(')') => { Some(',' | ')') => {
src.next_char(); let last = src.peek_char().is_some_and(|c| c == ')');
break;
}
Some(',') => {
if inner_f.is_empty() { if inner_f.is_empty() {
inner_t.push(t); inner_t.push(t);
src.next_char(); src.next_char();
if last {
break;
}
} else { } else {
let pos1 = src.get_pos(); let pos1 = src.get_pos();
src.next_char(); src.next_char();