ONLang/examples/factorial.json5

121 lines
2.9 KiB
Text
Raw Permalink Normal View History

2022-08-15 18:52:32 +03:00
[
{
fn: {
2022-08-18 14:12:51 +03:00
name: "fact_loop",
2022-08-15 18:52:32 +03:00
args: ["n"],
body: [
2022-08-18 14:12:51 +03:00
{ let: { result: { var: "n" } } },
2022-08-15 18:52:32 +03:00
{
loop: [
2022-08-18 14:12:51 +03:00
//using loop because recursion is not supported*
//* - already supported
2022-08-15 18:52:32 +03:00
{
if: {
2022-08-18 14:12:51 +03:00
condition: { comp: [{ var: "n" }, ">", 1] },
2022-08-15 18:52:32 +03:00
body: [
{
assign: {
2022-08-18 14:12:51 +03:00
n: {
calc: [{ var: "n" }, "-", 1],
2022-08-15 18:52:32 +03:00
},
},
},
{
assign: {
result: {
2022-08-18 14:12:51 +03:00
calc: [{ var: "result" }, "*", { var: "n" }],
2022-08-15 18:52:32 +03:00
},
},
},
],
else: ["break"],
},
},
],
},
{ return: { var: "result" } },
],
},
},
2022-08-18 14:12:51 +03:00
{
fn: {
name: "fact",
args: ["n"],
body: [
{
if: {
condition: { comp: [{ var: "n" }, "==", 1] },
body: [{ return: 1 }],
else: [
{
return: {
calc: [
{ var: "n" },
"*",
{ fact: [{ calc: [{ var: "n" }, "-", 1] }] },
],
},
},
],
},
},
],
},
},
2022-08-18 17:48:02 +03:00
{ import: { path: "examples/assertions.onla", as: "assertions" } },
2022-08-15 18:52:32 +03:00
2022-08-18 17:48:02 +03:00
["10! == 3 628 800: ", { "assertions.eq": [{ fact: [10] }, 3628800] }],
["11! == 39 916 800: ", { "assertions.eq": [{ fact: [11] }, 39916800] }],
2022-08-15 18:52:32 +03:00
2022-08-18 17:48:02 +03:00
["12! == 479 001 600: ", { "assertions.eq": [{ fact: [12] }, 479001600] }],
["13! == 6 227 020 800: ", { "assertions.eq": [{ fact: [13] }, 6227020800] }],
2022-08-15 18:52:32 +03:00
2022-08-18 17:48:02 +03:00
[
"14! == 87 178 291 200: ",
{ "assertions.eq": [{ fact: [14] }, 87178291200] },
],
[
"15! == 1 307 674 368 000: ",
{ "assertions.eq": [{ fact: [15] }, 1307674368000] },
],
2022-08-15 18:52:32 +03:00
2022-08-18 17:48:02 +03:00
[
"16! == 20 922 789 888 000: ",
{ "assertions.eq": [{ fact: [16] }, 20922789888000] },
],
[
"17! == 355 687 428 096 000: ",
{ "assertions.eq": [{ fact: [17] }, 355687428096000] },
],
2022-08-15 18:52:32 +03:00
[
"18! == 6 402 373 705 728 000: ",
2022-08-18 17:48:02 +03:00
{ "assertions.eq": [{ fact: [18] }, 6402373705728000] },
2022-08-15 18:52:32 +03:00
],
[
"19! == 121 645 100 408 832 000: ",
2022-08-18 17:48:02 +03:00
{ "assertions.eq": [{ fact: [19] }, 121645100408832000] },
2022-08-15 18:52:32 +03:00
],
[
"20! == 2 432 902 008 176 640 000: ",
2022-08-18 17:48:02 +03:00
{ "assertions.eq": [{ fact: [20] }, 2432902008176640000] },
2022-08-15 18:52:32 +03:00
],
2022-08-18 14:12:51 +03:00
"",
2022-08-18 17:48:02 +03:00
[
"(loop) 10! == 3 628 800: ",
{ "assertions.eq": [{ fact_loop: [10] }, 3628800] },
],
[
"(loop) 11! == 39 916 800: ",
{ "assertions.eq": [{ fact_loop: [11] }, 39916800] },
],
2022-08-18 14:12:51 +03:00
2022-08-15 18:52:32 +03:00
// [
// "21! == 51 090 942 171 709 440 000: ",
// { _eq: [{ fact: [21] }, 51090942171709440000] },
// ], some json and yaml troubles with number `51090942171709440000`
]