develooper Front page | perl.perl6.users | Postings from June 2018

Re: need sort help

Thread Previous | Thread Next
From:
Xin Cheng
Date:
June 9, 2018 18:58
Subject:
Re: need sort help
Message ID:
CEFC9510-7A9E-41E8-8FE0-2E3FA88A8193@gmail.com
I got the point for //.

Another question is about calling the method sort with a code block. I can understand

@x .= sort({ ... }); 

But I don't quite understand why this form also works.

@x .= sort: { ... };

I look into the documentation for infix ":", https://docs.perl6.org/routine/: <https://docs.perl6.org/routine/:> , and it explains something like this:

Used as an argument separator just like infix , and marks the argument to its left as the invocant. That turns what would otherwise be a function call into a method call.

substr('abc': 1);       # same as 'abc'.substr(1) 
Infix : is only allowed after the first argument of a non-method call. In other positions, it's a syntax error.


How does the above explanation related to the case in hand @x .= sort: { ... }; ? Is sort an invocant? Or I miss something.

Regards

Xin



> On Jun 9, 2018, at 12:44 PM, Brandon Allbery <allbery.b@gmail.com> wrote:
> 
> More precisely, at that point you have a bunch of numbers, but possibly not as many as expected if some of the components weren't numeric (or all of them, as when there are files present that aren't the expected logs). Which means some or all of those variables will be undefined instead of numbers. The // replaces those with the following value (0), so they do something sensible when sorted instead of producing warnings.
> 
> On Sat, Jun 9, 2018 at 11:40 AM Xin Cheng <xincheng99@gmail.com <mailto:xincheng99@gmail.com>> wrote:
> This is very interesting. But I wonder how it works. I can understand the first line
> 
>  my ($month, $day, $year, $hour, $minute, $second) = .comb(/\d+/);
> 
> Which extract the variables from $_. What is the second line doing, it is very concise.
> 
>  ($year // 0, $month // 0, $day // 0, $hour // 0, $minute // 0,
> $second // 0, $_);
> 
> Could somebody explain in some more words.? What does  // do? Why it sorts the array?
> 
> Regards
> 
> Xin
> 
>> On Jun 9, 2018, at 12:51 AM, Timo Paulssen <timo@wakelift.de <mailto:timo@wakelift.de>> wrote:
>> 
>> That's unnecessarily long and complicated, here's how you can do it much
>> easier:
>> 
>>     @x.sort: {
>>         my ($month, $day, $year, $hour, $minute, $second) = .comb(/\d+/);
>>         ($year // 0, $month // 0, $day // 0, $hour // 0, $minute // 0,
>> $second // 0, $_);
>>     }
>> 
>> Trying it on some input data:
>> 
>>     cimtrak.log.06-08-2018_16:07:39.zip
>>     cimtrak.log.06-08-2018_17:07:39.zip
>>     cimtrak.log.07-08-2018_06:07:39.zip
>>     cimtrak.log.07-08-2018_16:07:39.zip
>>     cimtrak.log.12-08-2016_06:07:39.zip
>>     cookies
>>     asbestos
>>     fire engine
>>     perl6
>>     butterflies
>> 
>> results in:
>> 
>>     asbestos
>>     butterflies
>>     cookies
>>     fire engine
>>     perl6
>>     cimtrak.log.12-08-2016_06:07:39.zip
>>     cimtrak.log.06-08-2018_16:07:39.zip
>>     cimtrak.log.06-08-2018_17:07:39.zip
>>     cimtrak.log.07-08-2018_06:07:39.zip
>>     cimtrak.log.07-08-2018_16:07:39.zip
>> 
>> This is the schwartzian transform that was mentioned in another mail.
>> why it wasn't actually shown, i have no clue :)
>> 
>> Hope that helps
>>   - Timo
> 
> 
> 
> -- 
> brandon s allbery kf8nh                               sine nomine associates
> allbery.b@gmail.com <mailto:allbery.b@gmail.com>                                  ballbery@sinenomine.net <mailto:ballbery@sinenomine.net>
> unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net <http://sinenomine.net/>

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About