Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • by int32 (6174) on 2009.01.29 16:44 (#67108)
    I played a bit with the concept, and found an issue I can't resolve, possibly you can help me here. The problem concerns whether $_ is an alias or a copy to the parameter passed. If it is a copy then you can't modify it unless it is a reference. If it is an alias you can't modify it if it is read-only. You may ask why should it be modified at all, and here comes the classic unfoldr:

    unfoldr { $_ ? ($_, $_ - 1) : () } 10;

    10,9,8,7,6,5,4,3,2,1

    which allows to pass arbitrary values to the callback . Now back to your example, if I want to send values other than the original parameter to myself (e.g. callback), then I can't. Also, I can't send an "undef" back to me that way. That seems too much of a limitation.

    So a question. Why not a good old unfoldr?

    unfoldr { @$_ ? ( pop @$_, $_ ) : () } [1..10];

    10,9,8,7,6,5,4,3,2,1

    • Read my post; I addressed that concern. To reiterate: because the empty list is a valid return value. I want to be able to make compute state transitions on the input value without them having to contribute to the resulting list.

      It’s not hard to convert a read-only value to modifiable copy.

      if I want to send values other than the original parameter to myself (e.g. callback), then I can’t.

      You can. You just have to capture them with a closure. Admittedly that’s cumbersome.

      So yeah, it’s

      • Oh well, I could say the same, read my post; I addressed that concern too :) unfoldr actually allows to return empty lists. Here's a hacked quote from haskell description:

        The unfoldr function is a `dual' to foldr: while foldr reduces a list to a summary value, unfoldr builds a list from a seed value. The function takes the element and returns empty list if it is done producing the list or returns (@a,$b), in which case, @a is a appended to the list and $b is used as the next element in a recursive call.

        • But then you need to drag it along through all of your return values. That is cumbersome.

          Using $_ for the purpose is more idiomatic. The problem is in reusing $_ to signal the last iteration – that prevents you from unfolding undef, should you want to, and it bloats the code by your having to be careful not to return that undef when you meant to stop iterating.

          However I can’t think of any good way to signal termination out of band.

    • Btw, no, your point about alias vs copy is mistaken. The induce function above takes a copy of the value and then aliases $_ to that copy, so you are guaranteed to be able to modify the value, although if you want to preserve its last value past the unfolding, you want to arrange for that yourself.