Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: sort of misunderstanding of sort

by LanX (Saint)
on Mar 21, 2023 at 11:38 UTC ( [id://11151095]=note: print w/replies, xml ) Need Help??


in reply to sort of misunderstanding of sort

I assume you want the cross-product of a list with itself.

sort can't do this because it's designed to be fast with O(n*log(n))

There used to be a way to change the applied algorithm, but that's deprecated now https://perldoc.perl.org/sort

Cheers Rolf
(addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: sort of misunderstanding of sort
by LanX (Saint) on Mar 21, 2023 at 11:51 UTC
    > I assume you want the cross-product

    DB<34> sub cross (&$$) { my ($code,$list1,$list2) = @_; local ($a,$b +); my @res; for $a (@$list1) { for $b (@$list2) { push @res, &$code } +}; return @res; } DB<35> x cross { $a + $b } [0..2], [10,20,30] 0 10 1 20 2 30 3 11 4 21 5 31 6 12 7 22 8 32 DB<36>

    you can just let $list2 default to $list1 if undef to have a self-operation with only one list.

    $list2 //= $list1

    Cheers Rolf
    (addicted to the 𐍀𐌴𐍂𐌻 Programming Language :)
    Wikisyntax for the Monastery

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11151095]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-26 05:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found