Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I have also tried to avoid the use of map because I really can never understand what it is doing

I translate this statement as "I'm scared, so I refuse to address my fear." If you really don't want to address your fear, stop now. I plan on getting you to understand map so that you never have to worry again.

map, grep, and sort all operate the exact same way. They take a list of stuff and return a list of stuff.

  • sort takes the list and sorts it, returning back every item that was in the original list, but possibly in a different place.
  • grep takes the list and applies a function to each item. If the function returns true, that item is in the result list. If the function returns false, that item is skipped. The items that make it are in the same order as they were originally.
  • map takes the list and applies a function to each item. It takes the return value from the function and puts it into the result list. The items are returned in the same order.

So, an example:

my @l = ( 5, 4, 3, 2, 1 ); my @sorted = sort { $a <=> $b } @l; # Sorted is ( 1, 2, 3, 4, 5 ) my @grepped = grep { $_ > 2 } @l; # Grepped is ( 5, 4, 3 ) my @mapped = map { $_ * 2 } @l; @ Mapped is ( 10, 8, 6, 4, 2 )

Now, the biggest problem people usually have with map and grep (though, funnily enough, not sort) is that you have to read them from right to left. This is in direct opposition to how you read everything else (which is from left to right). Perl 6 will provide a mechanism where you can have map, grep, and other list operators that can be read from left to right. It will look something like:

my @l = ( 5, 4, 3, 2, 1 ); @l ==> grep { $_ > 2 } ==> sort { $a <=> $b } ==> map { $_ * 2 } ==> @final; # Final contains ( 6, 8, 10 )
Is that easier to read?

Oh, and fearing unless is just plain old laziness - the bad kind. unless(...) is exactly identical to if(!(...)). Nothing less, nothing more. Feel free to revise as desired.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?

In reply to Re: How do I un-map this code? by dragonchild
in thread How do I un-map this code? by Plankton

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (2)
    As of 2018-05-28 01:52 GMT
    Find Nodes?
      Voting Booth?