http://www.perlmonks.org?node_id=963861


in reply to Re^3: short sorts
in thread short sorts

Arunbear, you seem to be having some fun with this, though you have really made it explode beyond a simple silly subroutine that I plopped into my Nifty module. (My Nifty module is where I put subroutines that I use, but can't categorize anywhere else.) What you have written looks like it would have to be put in a stand alone module. You have added a lot of complexity that I am having a hard time following. It could be that I am having a hard time seeing past my own dispatch table.

I am not entirely sure what you mean when you say that you are making this functional. It could be that I see "subroutine", "sub", and "function" used almost interchangeably that I am not picking up on the subtle differences between them.

As to why the random sort is not acting as expected, I am not sure I can explain the results I was seeing returned. The best I can do is say that it was getting new sort parameters for each iteration of the list while it was being sorted. I also had trouble keeping numerical sorts out of the mix which of course would throw up all kinds of warnings when non-numbers were being sorted.

I hope that you found the idea useful and are using it. Thank you for taking the time to play with this and show me what else can be done. Really silly sorts like sorting from the second character or third to the last character will wait until later. I was thinking about assigning hex and ord numbers to each character and sort them that way, but I think that would be the same as a straight up $a cmp $b.

Have a cookie and a very nice day!
Lady Aleena

Replies are listed 'Best First'.
Re^5: short sorts
by Arunbear (Prior) on Apr 07, 2012 at 15:37 UTC

    I've been trying to learn Haskell for greater good. It's a functional language, which (among other things) means functions are first class citizens: you can pass them as arguments to other functions, return them from functions, and easily combine them with other functions.

    e.g. whereas in Perl you can create a new string with
    $foo = $bar . $ram . $ewe;
    in Haskell you can create a new function with
    f = g . h . i
    (assuming all the argument types match up).

    That's what the Compose function I wrote does. I thought of doing that after noticing how similar your various sort functions were, and how they could be built by composing/layering basic operations on top of one another.

    I'm not suggesting you write your code like that, I wrote it mainly out of curiosity to see what such a functional style would look like in Perl.

      If you're really interested in functional style programming in Perl, you should check out Higher Order Perl, by Mark Jason Dominus. The whole book is about applying functional programming techniques and style to Perl. It's a pretty amazing book. It's also available free online. Hard to beat that.
        I've got a copy already. It is very educational, but doesn't have much to say about composing functions (as far as I can tell).

      I am very happy that I gave you something with which to experiment. If you think of anything else you want this to do, please let me see! :)

      Have a cookie and a very nice day!
      Lady Aleena