mirror of
https://github.com/Dummi26/mers.git
synced 2025-03-10 05:43:53 +01:00
fix examples
This commit is contained in:
parent
754a661f83
commit
424bc82d68
0
examples/00_Hello_World.mers
Executable file → Normal file
0
examples/00_Hello_World.mers
Executable file → Normal file
0
examples/01_Hello_Name.mers
Executable file → Normal file
0
examples/01_Hello_Name.mers
Executable file → Normal file
4
examples/02_Calc_Sum.mers
Executable file → Normal file
4
examples/02_Calc_Sum.mers
Executable file → Normal file
@ -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
|
||||
|
4
examples/03_Basic_Calculator.mers
Executable file → Normal file
4
examples/03_Basic_Calculator.mers
Executable file → Normal file
@ -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
|
||||
|
4
examples/04_Greatest_Common_Divisor.mers
Executable file → Normal file
4
examples/04_Greatest_Common_Divisor.mers
Executable file → Normal file
@ -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
|
||||
))
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
21
examples/05_Matrix_Multiplicator.mers
Executable file → Normal file
21
examples/05_Matrix_Multiplicator.mers
Executable file → Normal file
@ -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<Float>] 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
|
||||
))
|
||||
)
|
||||
|
@ -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 {
|
||||
}
|
||||
(())
|
||||
}
|
||||
))
|
||||
)
|
||||
}
|
||||
|
@ -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
|
||||
)
|
||||
|
||||
|
||||
|
@ -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
|
@ -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
|
@ -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
|
Loading…
Reference in New Issue
Block a user