You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Thinking out loud here a bit. Here's examples/99-bottles.007:
func verse(n) {
func plural(n, thing) {
if n == 1 {
return n ~ " " ~ thing;
}
else {
return n ~ " " ~ thing ~ "s";
}
}
say(plural(n + 1, "bottle") ~ " of beer on the wall,");
say(plural(n + 1, "bottle") ~ " of beer.");
say("Take one down, pass it around,");
say(plural(n, "bottle") ~ " of beer on the wall...");
}
for (^99).reverse() -> beerCount {
verse(beerCount);
if beerCount > 0 {
say("");
}
}
Here's how I would like to write it:
import * from syntax.op.conditional;
import * from syntax.str.template;
func plural(n, thing) {
`${n} ${thing}${n == 1 ?? "" !! "s"}`;
}
for (^99).reverse() -> n {
say(plural(n + 1, "bottle"), " of beer on the wall,");
say(plural(n + 1, "bottle"), " of beer.");
say("Take one down, pass it around,");
say(plural(n, "bottle"), " of beer on the wall...");
if n > 0 {
say("");
}
}
Here's examples/euclid-gcd.007:
macro swap(a, b) {
return quasi {
my t = {{{a}}};
{{{a}}} = {{{b}}};
{{{b}}} = t;
};
}
func gcd(a, b) {
if b {
return gcd(b, a % b);
}
return a.abs();
}
my bigger = +prompt("Enter the bigger integer: ");
my smaller = +prompt("Enter the smaller integer: ");
if bigger < smaller {
swap(bigger, smaller);
}
say();
say("Greatest common denominator: ", gcd(bigger, smaller));
And here's how I would like to write it:
import * from syntax.op.conditional;
import * from syntax.op.assign;
import * from syntax.expr.destructure;
func gcd(a, b) {
b ?? gcd(b, a % b) !! a.abs();
}
my bigger = +prompt("Enter the bigger integer: ");
my smaller = +prompt("Enter the smaller integer: ");
[bigger, smaller].=sort();
say("");
say("Greatest common denominator: ", gcd(bigger, smaller));
examples/fizzbuzz.007:
my n = 1;
while n <= 100 {
if n %% 15 {
say("FizzBuzz");
}
else if n %% 3 {
say("Fizz");
}
else if n %% 5 {
say("Buzz");
}
else {
say(n);
}
n = n + 1;
}
And my ideal:
import * from range;
import * from syntax.op.conditional;
for 1 .. 100 -> n {
say(n %% 15
?? "FizzBuzz"
!! n %% 3
?? "Fizz"
!! n %% 5
?? "Buzz"
!! n
);
}
examples/quicksort.007:
func quicksort(array) {
if array.size() > 1 {
my pivot = array[0];
my less = quicksort(array.filter(func (e) { return e < pivot }));
my equal = array.filter(func (e) { return e == pivot });
my greater = quicksort(array.filter(func (e) { return e > pivot }));
array = less.concat(equal.concat(greater));
}
return array;
}
my unsorted = (^20).shuffle();
say("Unsorted: " ~ unsorted);
say("Sorting...");
my sorted = quicksort(unsorted);
say("Sorted: " ~ sorted);
And how I think it ought to look:
import * from syntax.func.arrow.p6;
import * from syntax.array.spread;
func quicksort(array) {
if array.size() > 1 {
my pivot = array[0];
my less = quicksort(array.filter(-> e { return e < pivot }));
my equal = array.filter(-> e { return e == pivot });
my greater = quicksort(array.filter(-> e { return e > pivot }));
array = [...less, ...equal, ...greater];
}
return array;
}
my unsorted = (^20).shuffle();
say("Unsorted: ", unsorted);
say("Sorting...");
my sorted = quicksort(unsorted);
say("Sorted: ", sorted);
examples/quine.007:
my lines = [
"my lines = [",
" ",
"];",
"for lines -> line {",
" if line.chars() == 4 {",
" for lines -> quotedLine {",
" say(line ~ 34.chr() ~ quotedLine ~ 34.chr() ~ 44.chr());",
" }",
" }",
" else {",
" say(line);",
" }",
"}",
];
for lines -> line {
if line.chars() == 4 {
for lines -> quotedLine {
say(line ~ 34.chr() ~ quotedLine ~ 34.chr() ~ 44.chr());
}
}
else {
say(line);
}
}
And how I'd prefer to write it:
import * from syntax.str.template;
my program = `import * from syntax.str.template;
my program = ☃;
say(program.subst("☃", 96.chr() ~ program ~ 96.chr()));
`;
say(program.subst("☃", 96.chr() ~ program ~ 96.chr()));
The modules syntax.op.conditional, syntax.str.template, syntax.array.spread, swap, range and syntax.func.arrow.p6 don't have to be actual 007 modules in the beginning for us to have good use for them. I suggest we, at least in a branch, make all of the examples work in the above suggested forms, simply by "mocking" what those modules will do.
That is, the actual implementation will live in _007::Parser::Syntax, with enough support from runtime values etc. Not how we want it in the long run... but probably a very interesting way to get early feedback from those things. -Ofun and all that.
The text was updated successfully, but these errors were encountered:
Thinking out loud here a bit. Here's
examples/99-bottles.007
:Here's how I would like to write it:
Here's
examples/euclid-gcd.007
:And here's how I would like to write it:
examples/fizzbuzz.007
:And my ideal:
examples/quicksort.007
:And how I think it ought to look:
examples/quine.007
:And how I'd prefer to write it:
The modules
syntax.op.conditional
,syntax.str.template
,syntax.array.spread
,swap
,range
andsyntax.func.arrow.p6
don't have to be actual 007 modules in the beginning for us to have good use for them. I suggest we, at least in a branch, make all of the examples work in the above suggested forms, simply by "mocking" what those modules will do.That is, the actual implementation will live in
_007::Parser::Syntax
, with enough support from runtime values etc. Not how we want it in the long run... but probably a very interesting way to get early feedback from those things. -Ofun and all that.The text was updated successfully, but these errors were encountered: