Back to my MATLAB days at the University, I remember striving to write functions that behaved well with either scalars and arrays (typically applying the same "scalar" operation on every element in the array). This was generally repaid by the fact that I used lots of arrays.

Writing such functions in Perl, I always ask myself if I should stick to the same convention. As a matter of fact, yesterday I realised that I should not - it can take me only 8 characters of typing overhead to get the array version of any "simple one-scalar" function if I really need it (and I usually don't need these days :).

I understand this is some very basic example of a trivial application of map, but I wonder if there's any trick to do even less typing.

# Build an array-ized version of a sub sub a { my $s = shift; sub{map {&$s($_)} @_}; } # A simple function workin only on one scalar parameter sub do_something {$_[0] * 2} # Use 'em print do_something 10; print "\n", join (', ', a(\&do_something)->(1 .. 5)), "\n";

In reply to Array-ize a function by polettix

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.