This commit is contained in:
Mark
2023-10-19 18:46:15 +02:00
parent 2d79e75ba2
commit b39a768099
22 changed files with 822 additions and 94 deletions

27
examples/fib.mers Normal file
View File

@@ -0,0 +1,27 @@
fib := n -> {
// we start with these two values
v := (0, 1)
{() -> {
// 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 lates value
(r)
}
}}.loop
}
((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> {
i.print
": ".print
i.fib.println
}).for_each

92
examples/iter.mers Normal file
View File

@@ -0,0 +1,92 @@
"-- 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.as_list,
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 := ((val, max).diff.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

42
examples/try.mers Normal file
View File

@@ -0,0 +1,42 @@
// 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