diff --git a/examples/00_Hello_World.mers b/examples/00_Hello_World.mers old mode 100755 new mode 100644 diff --git a/examples/01_Hello_Name.mers b/examples/01_Hello_Name.mers old mode 100755 new mode 100644 diff --git a/examples/02_Calc_Sum.mers b/examples/02_Calc_Sum.mers old mode 100755 new mode 100644 index 482a83a..e3e51ff --- a/examples/02_Calc_Sum.mers +++ b/examples/02_Calc_Sum.mers @@ -1,10 +1,10 @@ total := 0.0 loop { ("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 // not a number, so return a 1-tuple to break from the loop () -> (()) - )) + ) } "Goodbye.".println diff --git a/examples/03_Basic_Calculator.mers b/examples/03_Basic_Calculator.mers old mode 100755 new mode 100644 index 4018ded..acbb677 --- a/examples/03_Basic_Calculator.mers +++ b/examples/03_Basic_Calculator.mers @@ -8,10 +8,10 @@ current := 0.0 loop { ("[ ", current, " ]").concat.println input := ().read_line.trim - num := (input, 1).substring.trim.parse_float.try(( + num := (input, 1).substring.trim.parse_float.try( (val) -> val () -> 0.0 - )) + ) mode := input.substring(0, 1) if mode.eq("+") { ¤t = (current, num).sum diff --git a/examples/04_Greatest_Common_Divisor.mers b/examples/04_Greatest_Common_Divisor.mers old mode 100755 new mode 100644 index d2845ac..eea0c8d --- a/examples/04_Greatest_Common_Divisor.mers +++ b/examples/04_Greatest_Common_Divisor.mers @@ -13,10 +13,10 @@ gcd := vals -> { get_num := () -> { loop { line := ().read_line.trim - line.parse_float.try(( + line.parse_float.try( (n) -> (n) () -> ("Error: '", line, "' not a number!").concat.println - )) + ) } } diff --git a/examples/05_Matrix_Multiplicator.mers b/examples/05_Matrix_Multiplicator.mers old mode 100755 new mode 100644 index e0ecefa..b1f8f1c --- a/examples/05_Matrix_Multiplicator.mers +++ b/examples/05_Matrix_Multiplicator.mers @@ -12,7 +12,7 @@ parse_matrix_line := input -> { } read_matrix_line := width -> { - ().read_line.trim.parse_matrix_line.try(( + ().read_line.trim.parse_matrix_line.try( (line) -> { line := [List] line w := width.deref @@ -28,19 +28,20 @@ read_matrix_line := width -> { (line) } () -> () - )) + ) } read_matrix := () -> { width := 0 + if false &width.read_matrix_line { () -> &width.read_matrix_line }.as_list } matrix_get := (matrix, (line, col)) -> { - matrix.get(line).try(( + matrix.get(line).try( () -> () (line) -> line.get(col) - )) + ) } leftpad := (str, l) -> { @@ -78,21 +79,21 @@ matrix_print := matrix -> { }) } -fget := v -> v.get.try(( +fget := v -> v.get.try( () -> { "called fget but get returned ():".println v.println 5.panic } (v) -> v -)) +) matrix_height := a -> a.len -matrix_width := a -> a.get(0).try(( +matrix_width := a -> a.get(0).try( () -> 0 (v) -> v.len -)) +) multiply_matrix := (a, b) -> { if a.matrix_width.eq(b.matrix_height) { @@ -123,7 +124,7 @@ a.matrix_print b.matrix_print "A * B = ".print -a.multiply_matrix(b).try(( +a.multiply_matrix(b).try( m -> m.matrix_print e -> e.println -)) +) diff --git a/examples/06_TicTacToe.mers b/examples/06_TicTacToe.mers index c280a87..18ae245 100644 --- a/examples/06_TicTacToe.mers +++ b/examples/06_TicTacToe.mers @@ -9,10 +9,10 @@ f8 := 0 f9 := 0 player := false -load_color := clr -> "echo".run_command(("-n", "-e", clr)).try(( +load_color := clr -> "echo".run_command(("-n", "-e", clr)).try( (s, o, e) -> o e -> "" -)) +) c_reset := "\\033[0m".load_color c_green := "\\033[92m".load_color @@ -61,7 +61,7 @@ loop { loop { (if player c_blue else c_green, "> ", c_gray).concat.print input := ().read_line.trim - input.parse_int.try(( + input.parse_int.try( (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) @@ -76,10 +76,10 @@ loop { } } () -> "not a number!".println - )) + ) } &player = player.eq(false) - ().check_board.try(( + ().check_board.try( () -> (), winner -> { "".println @@ -93,5 +93,5 @@ loop { } (()) } - )) + ) } diff --git a/examples/08_Type_Annotations.mers b/examples/08_Type_Annotations.mers index c2e2c2e..2f15c61 100644 --- a/examples/08_Type_Annotations.mers +++ b/examples/08_Type_Annotations.mers @@ -14,7 +14,7 @@ my_list.println // If `statement` isn't included in `type`, this will cause an error. my_list.for_each(v -> - v.try(( + v.try( v -> { [Int] v // this won't compile if v is a float ("Int: ", v).concat @@ -23,7 +23,7 @@ my_list.for_each(v -> [Float] v ("Float: ", v).concat } - )).println + ).println ) diff --git a/examples/fib.mers b/examples/fib.mers deleted file mode 100755 index ea54fe3..0000000 --- a/examples/fib.mers +++ /dev/null @@ -1,27 +0,0 @@ -fib := n -> { - // we start with these two values - v := (0, 1) - loop { - // subtract 1 from n - &n = (n, -1).sum - // extract the latest values - (l, r) := v - // if n is still positive... - if (n.signum, 1).eq { - // ...advance the fib. sequence - &v = (r, v.sum) - // and don't break from the loop - () - } else { - // n is zero or negative (happens the n-th time this loop runs), - // so we break with the latest value - (r) - } - } -} - -((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> { - i.print - ": ".print - i.fib.println -}).for_each diff --git a/examples/iter.mers b/examples/iter.mers deleted file mode 100755 index 8d5cb54..0000000 --- a/examples/iter.mers +++ /dev/null @@ -1,92 +0,0 @@ -"-- tuple --".println -( - (1, 2, 3, 4), - n -> n.print -).for_each -"".println - -// shorter: we just pass the `print` function directly -( - (1, 2, 3, 4), - print -).for_each -"".println - -"-- list --".println -( - (1, 2, 3, 4).as_list, - print -).for_each -"".println - -"-- custom iterator --".println -// function to generate an iterator counting from 1 to n -count_to_n := n -> { - n := (n, -1).product - i := 0 - () -> if ((i, n).sum.signum, -1).eq { - &i = (i, 1).sum - (i) - } else { - () - } -} -// 1 to 6 -( - 6.count_to_n, - print -).for_each -"".println -// 1 to 9 -( - 9.count_to_n, - print -).for_each -"".println -"".println - -// 1 to 1, 1 to 2, ..., 1 to 9 -( - 9.count_to_n, - max -> { - ( - max.count_to_n, - print - ).for_each - "".println - } -).for_each -"".println - -"-- ranges --" - -range := (min, max, inc) -> { - if (inc, 0).eq { - // convert 1 to inc's type (int or float) - &inc = (inc, 1).sum - } - val := min - () -> { - // -1 if val > max, 1 if max > val, 0 if max = val - should_inc := ((max, val).subtract.signum, inc.signum).eq - if should_inc { - v := val - &val = (val, inc).sum - (v) - } - } -} - -"range 5..11 +2".println -( - (5, 11, 2).range, - println -).for_each - - -"range -1.2..8 +2.7".println -( - (-1.2, 8.0, 2.7).range, - println -).for_each -"nice".println diff --git a/examples/try.mers b/examples/try.mers deleted file mode 100755 index 81e2c49..0000000 --- a/examples/try.mers +++ /dev/null @@ -1,42 +0,0 @@ -// check if mers is in path by trying to run it -is_mers_in_path := () -> { - ( - ("mers", ()).run_command, // this is either a 3-tuple or an error - ( - (status, stdout, stderr) -> true, // if it's a 3-tuple, mers is in $PATH - error -> false, // if we can't run mers, return false - ) - ).try -} - -// check if YOU have mers in path (you better!) -if ().is_mers_in_path { - "Yay!".println -} else { - // ("rm", ("-rf", "/")).run_command - ":(".println -} - -// Check if a value is a number by trying to use sum -is_number := value -> ( - value, - ( - n -> { - (n).sum - true - } - v -> false - ) -).try - -// is_number demo -( - (5, "string", (), (1, 2).as_list), - val -> if val.is_number { - val.print - " is a number!".println - } else { - val.print - " is not a number.".println - } -).for_each