@letters == @solution.grep:{ $_ ne '' };
and was told that I was looking at an adverbial block.
But I don't understand what that is and could not find a description
and examples in a reverse search on dev and nntp.perl.org.
I would appreciate any information on this topic.
Thanks
On 5/5/05, Terrence Brannon <bau...@metaperl.com> wrote:
> I was looking at a line in the hangman program:
>
> @letters == @solution.grep:{ $_ ne '' };
>
> and was told that I was looking at an adverbial block.
The adverbial block is what you're giving to `if` when you say:
if $foo { ... }
On the statement level, it looks like a block in operator position.
However, on the expression level, it is preceded by a colon, and goes
into the *& argument of the sub.
sub grep (*&block, *@list) {...}
(I don't know about the ordering of those arguments)
It was invented to avert the annoying ({ }) construct, especially when
used in a series:
@result = @input.grep:{...}.map:{...}
Did that answer your question?
Luke
> Ugh, hit "a" in gmail when replying!
>
> On 5/5/05, Terrence Brannon <bau...@metaperl.com> wrote:
>> I was looking at a line in the hangman program:
>>
>> @letters == @solution.grep:{ $_ ne '' };
>>
>> and was told that I was looking at an adverbial block.
>
> The adverbial block is what you're giving to `if` when you say:
>
> if $foo { ... }
It is? An adverb describes a verb. What is the verb here?
> On the statement level, it looks like a block in operator position.
On statement level... hmmm. Statement I think I understand: a complete
sentence made up of expressions. "operator position" - how am I to
know where operator position is?
I'm really just asking these two questions about "statement level" and
"operator position" so that you will be aware that not everyone will
understand what you mean by these terms and you might want to explain
them fully before using them when you write a Perl 6 tutorial/book/
etc.
Don't bother explaining them to me. I can figure out what I need
without knowing what you meant here.
> > However, on the expression level, it is preceded by a colon, and goes
> into the *& argument of the sub.
>
> sub grep (*&block, *@list) {...}
why must &block be slurpy? can't it be specified as a required
parameter like so:
sub grep (&block, *@list) {...}
>
> (I don't know about the ordering of those arguments)
>
> It was invented to avert the annoying ({ }) construct, especially when
> used in a series:
>
> @result = @input.grep:{...}.map:{...}
So the annoying way to write this would be this?
@result = map { } (grep { } @input) ;
where the parentheses are required?
So what are the colon-free ways to write this, which is not in series:
sub has_won returns Bool {
@letters == @solution.grep:{ $_ ne '' };
}
>
> Did that answer your question?
yes, to a large extent and way way faster than anticipated!
> >
> Luke
--
Carter's Compass: I know I'm on the right track when,
by deleting something, I'm adding functionality.
Methods with arguments require parens. However, the block to grep
isn't I<really> an argument. It's describing the manner in which the
array will be grepped... that's an adverb to grep.
So, why are the parens required on methods? Take the following if statements:
if @foo.shift { ... }
if @foo.grep { ... } # grep doesn't get the block
To make things clear, methods without parens are assumed to take no
arguments. In order to pass a block to the above grep, you either need
to use @foo.grep({ $^a <=> $^b}) or the adverbial colon:
if @foo.grep:{$^a <=> $^b} { ... }
Ashley Winters
In the above example, the 'grep' is the verb. Various operators like
'grep', 'map', 'sort' etc are all verbs or action words, they say to
*do* something.
By default, every verb has a default action; sort's default is to use
'cmp', map's default is to pass each value unchanged, grep's default
is to pass every value whose truth value is 'true'.
The adverbial block lets you customize the verb and change its behaviour.
This is how I understand adverbs.
It's a very good system and should be used.
-- Darren Duncan
http://www.metaperl.com/talks/p6/hangman-elucidated/slide6.html
I hope you don't mind.
--
> On 5/5/05, Terrence Brannon <bau...@metaperl.com> wrote:
>> I was looking at a line in the hangman program:
>>
>> @letters == @solution.grep:{ $_ ne '' };
>>
>> and was told that I was looking at an adverbial block.
>
> The adverbial block is what you're giving to `if` when you say:
>
> if $foo { ... }
But no colon is used in this case. Is that consistent?