Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

List::MoreUtils::first_value() question

by tqisjim (Beadle)
on Jul 30, 2013 at 14:56 UTC ( #1047049=perlquestion: print w/ replies, xml ) Need Help??
tqisjim has asked for the wisdom of the Perl Monks concerning the following question:

I'm a little confused by the man page for List::MoreUtils::first_value(). I've concluded that it's functionally identical to List::Util::first(). Can anyone please confirm?

I'm looking for the equivalent of the following:

## first example $result = first { defined $_ } map { foobar( $_ ) } @list ; ## second example foreach ( @list ) { last if defined( $result = foobar( $_ ) ) ; }

In the first example, foobar is called on every list element, and first()'s economic magic is only applied to the defined operator. The second example demonstrates precisely the functionality I'm looking for.

Specifically, is there any list operator in any module that solves this problem? Otherwise, has anyone else encountered this missing functionality?

Quite sincerely- Jim

Comment on List::MoreUtils::first_value() question
Download Code
Re: List::MoreUtils::first_value() question
by BrowserUk (Pope) on Jul 30, 2013 at 15:15 UTC
    In the first example, foobar is called on every list element, and first()'s economic magic is only applied to the defined operator. The second example demonstrates precisely the functionality I'm looking for.

    Interpreting your question to mean that you want a list operator that short ciruits at teh first defined value it is passed, I think you are misinterpreting the results from both List::Util::first() & List::MoreUtils::first_value().

    Both will short circuit if you test with defined (the use of grep is to demonstrate the contrast):

    use List::Util qw[ first ];; use List::MoreUtils qw[ first_value ];; sub x{ say "<<<@_>>>"; return shift(); };; $x = grep{ defined( x($_) ) } 1, undef,3;; <<<1>>> Use of uninitialized value in join or string at (eval 17) line 1, <STD +IN> line 10. <<<>>> <<<3>>> $x = first{ defined( x( $_ ) ) } 1, undef, 3;; <<<1>>> $x = first_value{ defined( x( $_ ) ) } 1, undef, 3;; <<<1>>>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
Re: List::MoreUtils::first_value() question
by tobyink (Abbot) on Jul 30, 2013 at 15:19 UTC

    Yes; they're the same. Did you want this...?

    my $result; first { defined($result = foobar $_) } @list;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      The blunt truth is that I'm trying to assemble a body of code to show potential employers. So maybe I should qualify my posts with the following:

      1. Acquiring an essential understanding of CPAN.
      2. Demonstrating best practices.
      3. Initiating best practices.

      That last one requires that I fake it until I make it approach. Your recommendation strikes at the very heart of my metamorphosis.

        Well then a best practice might be to use List::Util::first unless you are already using List::MoreUtils for something else, and not using List::Util for anything, in which case, use List::MoreUtils::firstval.

        I suggest this because it means you'll be sticking to a core module (List::Util), avoiding adding a CPAN dependency. However, if you're already using the CPAN dependency, and you're not using List::Util in the module, then there's no reason to use both modules; stick with List::MoreUtils.

        I say "might", because there are always "it depends" criteria, and best practices suggestions are rarely as simple as "just do it this way."


        Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1047049]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (19)
As of 2014-07-28 16:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (204 votes), past polls