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
().loop(() -> {
loop {
("Total: ", total, ". Type a number to change.").concat.println
().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
() -> (())
))
})
}
"Goodbye.".println

View File

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

View File

@ -1,5 +1,5 @@
gcd := vals -> {
().loop(() -> {
loop {
(a, b) := vals
if a.eq(b)
(a)
@ -7,21 +7,17 @@ gcd := vals -> {
&vals = (a, b.subtract(a))
else
&vals = (a.subtract(b), b)
})
}
}
get_num := () -> {
loop {
line := ().read_line.trim
(
line.parse_float,
(
() -> {
("error: '", line, "' not a number!").concat.println
1.panic
line.parse_float.try((
(n) -> (n)
() -> ("Error: '", line, "' not a number!").concat.println
))
}
n -> n,
)
).try
}
("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 -> {
vals := input.split(" ")
vals := input.str_split(" ")
vals_len := vals.len
// reverse vals
vals_rev := {() -> &vals.pop}.as_list
nums := {() -> &vals_rev.pop.try((
() -> ()
(v) -> v.parse_float.try((
() -> ()
v -> (v)
))
))}.as_list
nums := vals_rev.filter_map(v -> v.parse_float).as_list
if nums.len.eq(vals_len)
if nums.len.signum.eq(1)
(nums)
@ -58,15 +14,16 @@ parse_matrix_line := input -> {
read_matrix_line := width -> {
().read_line.trim.parse_matrix_line.try((
(line) -> {
line := [List<Float>] line
w := width.deref
if w.eq(0) {
width = line.len
} else {
().loop(() ->
loop {
if line.len.subtract(w).signum.eq(-1) {
&line.push(0.0)
} else (())
)
}
}
(line)
}
@ -89,12 +46,12 @@ matrix_get := (matrix, (line, col)) -> {
leftpad := (str, l) -> {
str := (str).concat
d := l.subtract(str.len)
().loop(() ->
loop {
if d.signum.eq(1) {
&str = (" ", str).concat
&d = d.subtract(1)
} else (())
)
}
str
}

View File

@ -55,14 +55,14 @@ check_board := () -> {
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
().print_board
().loop(() -> {
loop {
(if player c_blue else c_green, "> ", c_gray).concat.print
input := ().read_line.trim
input.parse_int.try((
field -> {
(field) -> {
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)
col := field.subtract(1).modulo(3)
@ -77,7 +77,7 @@ check_board := () -> {
}
() -> "not a number!".println
))
})
}
&player = player.eq(false)
().check_board.try((
() -> (),
@ -94,4 +94,4 @@ check_board := () -> {
(())
}
))
})
}

View File

@ -1,20 +1,20 @@
is_prime := n -> {
0.1.sleep // wow, what an inefficient algorithm, oh no
d := 1
().loop(() -> {
loop {
&d = (d, 1).sum
if d.lt(n) {
if n.modulo(d).eq(0) (false)
} else (true)
})
}
}
find_primes := start_at -> {
() -> (().loop(() -> {
() -> (loop {
out := if start_at.is_prime (start_at)
&start_at = (start_at, 1).sum
out
}))
})
}
primes_count := 0
@ -24,10 +24,10 @@ background_thread := {() ->
}.thread
// Show status to the user while the background thread works
().loop(() -> {
loop {
0.2.sleep
(" Found ", primes_count, " primes...\r").concat.print
if background_thread.thread_finished (())
})
}
("\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.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 -> {
// we start with these two values
v := (0, 1)
{() -> {
loop {
// subtract 1 from n
&n = (n, -1).sum
// extract the latest values
@ -17,7 +17,7 @@ fib := n -> {
// so we break with the latest value
(r)
}
}}.loop
}
}
((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)?;
src.skip_whitespace();
match src.peek_char() {
Some(')') => {
src.next_char();
break;
}
Some(',') => {
Some(',' | ')') => {
let last = src.peek_char().is_some_and(|c| c == ')');
if inner_f.is_empty() {
inner_t.push(t);
src.next_char();
if last {
break;
}
} else {
let pos1 = src.get_pos();
src.next_char();