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

Re: seeking golfing advice

Thread Previous | Thread Next
damien krotkine
May 16, 2012 04:46
Re: seeking golfing advice
Message ID:
Hi all,

thanks for the quick answers :)

first of all, despite my attempt to clear things up with an example, I
failed :)  What I wante is "every other element, beginning with the
first", so :

( 'foo', 3, 42, 'bar) should become ( 'foo', 42).

Indeed grep is much better. As the code is used in a more complex
structured I got lost and confused and ended up using map, blah.

$|-- was what I was looking for :)

About golfing and strictness I am well aware that the two are
incompatible. What I originally meant is to get something with a sane
balance between the two.

Finally, one question : why is  $|-- not recommended in production, if
the following conditions are met : I'm not running in threads, not in
an event-loop that might be affected by $| being set, and I'll take
care of resetting $| appropriately at the end.

Even in this case, wouldn't you recommend using $| ?

Thanks again

On 16 May 2012 13:36, Peter Makholm <> wrote:
> 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

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