adverbial blocks: description and examples requested

3 views
Skip to first unread message

Terrence Brannon

unread,
May 5, 2005, 6:38:47 PM5/5/05
to perl6-l...@perl.org
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.

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


Luke Palmer

unread,
May 5, 2005, 6:49:11 PM5/5/05
to perl6-l...@perl.org
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 { ... }

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

Terrence Brannon

unread,
May 5, 2005, 7:11:47 PM5/5/05
to Luke Palmer, perl6-l...@perl.org
Luke Palmer <lrpa...@gmail.com> writes:

> 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.

Ashley Winters

unread,
May 6, 2005, 1:38:51 AM5/6/05
to Terrence Brannon, perl6-l...@perl.org
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.
>
> 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.

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

Darren Duncan

unread,
May 6, 2005, 2:50:06 AM5/6/05
to perl6-l...@perl.org
At 11:11 PM +0000 5/5/05, Terrence Brannon wrote:

>Luke Palmer <lrpa...@gmail.com> writes:
> > 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?

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

Terrence Brannon

unread,
May 10, 2005, 12:40:22 PM5/10/05
to perl6-l...@perl.org
Ashley, this is a great post. I have included it almost verbatim in my
p6 talk I'm giving tomorrow at our Perl Monger's meeting:

http://www.metaperl.com/talks/p6/hangman-elucidated/slide6.html

I hope you don't mind.

--

Terrence Brannon

unread,
May 10, 2005, 12:33:01 PM5/10/05
to Luke Palmer, perl6-l...@perl.org
Luke Palmer <lrpa...@gmail.com> writes:

> 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?

Reply all
Reply to author
Forward
0 new messages