Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^4: Pipe dream

by Dominus (Parson)
on Sep 12, 2005 at 13:33 UTC ( [id://491256]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Pipe dream
in thread Pipe dream

Anonymous Monk says:
Well, you can't sort infinite streams.
Sometimes you can sort infinite streams. It depends on the stream and the sort order. Section 6.5.3 of Higher-Order Perl discusses it in some detail. The example on page 293-300 is to sort the contents of qmail's log file while the log is being written.

One kind of cool thing about the lazy streams is that if you implement quicksort in the straightforward way, you get a reasonably good lazy sorting function that does run in O(n log n) average time. And if you're only looking at a few elements from the front of the sorted result, the time is O(n).

Put another way, we sometimes laugh when beginners write this:

my ($minimum) = sort @items;
because we know that the sort is O(n log n), and there's a straightforward algorithm for the minimum that is O(n). But if your sort algorithm is implemented lazily, as it probably is if the array is actually a lazy list, then the code above does run in O(n) time, because the sort call only sorts enough of the list to find the minimum element. Code like that above does run in linear time in Haskell, for example. Of course, as you say, the whole stream must be resident in memory.

I didn't put this into HOP because although I thought it was delightful and fascinating, it didn't seem to have any immediate practical use.

Replies are listed 'Best First'.
Re^5: Pipe dream
by Anonymous Monk on Sep 12, 2005 at 15:14 UTC
    Sometimes you can sort infinite streams. It depends on the stream and the sort order.
    I guess I'm still skeptical. Can you show some code? If you can't get to the end of the list, how can you know which element is smallest, so that you can put it first in the output? I assume we're not talking something trivial like...
    sorted = sort [1..] where sort = id

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (None)
    As of 2024-04-19 00:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found