in reply to coderefs and (&) prototypes
to find all the elements $_ of @list for which $code->($_) is true, for a hypothetical mygrep (&@), one would callgrep &$code, @list
to achieve the same goal.mygrep \&$code, @list
ikegami pointed out yet another subtlety, which is that grep cannot accept a ‘calculated’ first argument. Thus,
(While I'm at it, ikegami also pointed out that my first example could be written as grep $code->(), @list, which I find completely bizarre.)sub mygrep (&@) { my ( $code, @list ) = @_; return grep &$code, @list +} sub gt { my ( $test ) = @_; return sub { $_ > $test } } grep &{ gt 1 }, qw/1 2 3 1 4/; => () mygrep \&{ gt 1 }, qw/1 2 3 1 4/; => ( 2, 3, 4 )
UPDATE: Actually, having just run the code, it seems to me that the grep and mygrep invocations actually return the same thing (namely, (2, 3, 4)), so I must have misunderstood. Can anyone (like ikegami :-) ) clarify for me?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: coderefs and (&) prototypes
by ikegami (Patriarch) on Jul 28, 2009 at 01:42 UTC | |
by JadeNB (Chaplain) on Jul 28, 2009 at 17:59 UTC | |
by ikegami (Patriarch) on Jul 28, 2009 at 18:28 UTC | |
|
Re^2: coderefs and (&) prototypes
by LanX (Saint) on Jul 27, 2009 at 23:42 UTC |