Inspired by having recently worked my way through Aristotle's nice article on the Y Combinator in Perl, I wondered if Howto avoid large hard-coded else-if function calls could be done using a similar technique. I eventually got it to go, but only by moving to 5.8.8 (rather than 5.8.6) and applying some hacks to work around (further) limitations of the List::Util::reduce() operator.

In the following code, the list of functions to be applied are built up (curried?) together to produce a single code ref that can be applied to the inputs and produce the outputs in a single pass (kinda):

#! perl -slw require 5.8.8; use strict; use List::Util qw[ reduce ]; $a = $b; my %dispatch = ( A => sub{ map{ $_ +1 } @_ }, B => sub{ map{ $_ *2 } @_ }, C => sub{ map{ $_**2 } @_ }, D => sub{ map{ $_ +3 } @_ }, E => sub{ map{ $_ *5 } @_ }, ); my $func_com = $ARGV[ 0 ] || 'ABC'; my @some_val = 1 .. 10; my $combo = reduce{ { ## 1 my( $x, $y ) = ( $a, $b ); ## 3 $a = sub{ $y->( $x->( @_ ) ) } } ## 2 } map{ $dispatch{ $_ }||sub{ @_ } } split '', $func_com; printf "[ %s ]", join ',', $combo->( @some_val ); __END__ c:\test>\AS817\perl\bin\perl5.8.8.exe junk6.pl ABCDE [ 95,195,335,515,735,995,1295,1635,2015,2435 ]

The hacks (this doesn't work before 5.8.8 because of closure bugs with reduce() that got recently fixed):

  1. (## 1 & ## 2) It doesn't work unless I nest a bare block inside the er, bare block passed to reduce().

    Shouldn't the outer bare block be sufficient to form the closures?

  2. (## 3) It also doesn't work unless I assign the globals $a & $b to lexicals.

    It's perfectly possible to close over package globals in other instances, so is this a bug?

    Or is it the alias nature of $a & $b that prevents the closure working correctly?

Both hacks are still required of 5.8.9 5.9.x (circa a month or so ago), so are either or both worth raising a perlbug over?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Bugs? Or only in my expectations? by BrowserUk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.