develooper Front page | perl.fwp | Postings from May 2012

Re: seeking golfing advice

Peter Makholm
May 16, 2012 04:36
Re: seeking golfing advice
Message ID:
Peter Makholm <> writes:

> damien krotkine <> writes:
>> I'm using this code to get a list of only the odd elements of an
>> array. The resulting list must have the same order as the array.
>> map { state $f; ($_) x (++$f%2) }  @array;

Wow, you asked for golfing advice in the subject and a nice solution in
the actual text. Those requirements are quite often very incompatible.

> If you want only to get some elements of a list is is much more obvious
> to use grep instead of map:
>   grep { ++$f%2 } @array

This is probably as nice as it gets. For strictness I would just declare
$f as a lexical variable just outside the grep. But asking for golfing
advice is not compliant with strictness or niceness.

> And remove unneeded syntax
>   grep$|--,@array

I think this would be the golfing solution, but as with everything alse
related to perl golfing don't use it in production code.

//Makholm Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About