Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added x and xx example, fixes #278. Also fixes tutorial. #286

Merged
merged 7 commits into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion examples/hello-world.007

This file was deleted.

39 changes: 39 additions & 0 deletions examples/x-and-xx.007
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
macro infix:<xx>(left, right) is equal(infix:<*>) {
# Flattens one layer.
sub flatten (inputList) {
my result = [];
for inputList -> i {
if (type(i) == type([])) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but more idiomatic to use type matching: if i ~~ Array.

for i -> j {
result.push(j);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shorter and no less clear than the for loop might be to do result = result.concat(i); — this appends all the elements "at once" at the expense of creating a brand new array and letting the old one be collected.

} else {
result.push(i);
}
}
return result;
}
return quasi {
flatten((^{{{right}}}).map(sub getValue(ignore) {
return {{{left}}};
}))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is beautiful. ❤️

}
}

sub infix:<x>(left, right) is equal(infix:<*>) {
return (left xx right).join("");
}

# Test string x number.
say("testing" x 2);

# Test number xx number.
my i = 0;
say((i = i + 1) xx 3);

# Test list xx number.
say([1, 2, 3] xx 2);

# Test (list or number) xx number.
my j = 0;
say((j = [1, [1, 2, 3]][j && 1]) xx 2);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries for now, but given the way #279 is heading, this expression might become illegal soon. Just a heads-up. 😄 It's an interesting way to check for something — if we outlaw assignment expressions, we'll have to make sure to have an alternative in place for this one.

9 changes: 0 additions & 9 deletions t/examples/hello-world.t

This file was deleted.

13 changes: 13 additions & 0 deletions t/examples/x-and-xx.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use Test;
use _007::Test;

my @lines = run-and-collect-output("examples/x-and-xx.007");

is +@lines, 4, "correct number of lines";

is @lines[0], "testingtesting", "first line";
is @lines[1], "[1, 2, 3]", "second line";
is @lines[2], "[1, 2, 3, 1, 2, 3]", "third line";
is @lines[3], "[1, 1, 2, 3]", "fourth line";

done-testing;
20 changes: 14 additions & 6 deletions tutorial/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,12 @@ this, the assignment in the `constant` declaration is mandatory.

## Setting

There's a scope outside the program scope, containing a utility
There's a scope outside the program scope, containing a few utility
subroutines. These should be fairly self-explanatory.

say(any)
prompt(any)
type(any)
str(any)
int(any)
min(a, b)
max(a, b)

## Objects

Expand Down Expand Up @@ -233,19 +230,30 @@ The following built-in types have methods associated with them:
.trim()
.split(sep)
.index(substr)
.charat(pos)
.contains(substr)
.substr(pos, chars)
.prefix(pos)
.suffix(pos)
.charat(pos)

### `Array`

.size()
.reverse()
.sort()
.shuffle()
.concat(array)
.join(sep)
.map(fn)
.filter(fn)
.push(newelem)
.pop()
.shift()
.unshift(newelem)

### `Object`

.size()

## Q objects

Expand Down