"be consistent" PerlMonks

### Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)

by moritz (Cardinal)
 on Sep 24, 2011 at 20:18 UTC ( #927681=note: print w/replies, xml ) Need Help??

My first impulse was to write basically the List::MoreUtils solutions in Perl 6. It has all() and .all built in, but no firstidx, so the solution goes through the detour of creating (index => value) pairs first:

```my @array =
[ qw/ 1 1 1 1 1 / ],
[ qw/ 0 1 1 0 0 / ],
[ qw/ 0 0 0 0 1 / ],
[ qw/ 0 0 0 0 0 / ],
[ qw/ 0 1 0 -1 0 / ],
;

say @array.pairs.first({.value.all == 0 }).key;
[download]```

Now .all == 0 should ring a bell for Perl 6 developers, there's a simplification around the corner:

```say @array.pairs.first(*.value.none).key;
[download]```

A good solution always plays to the strength of the language that it is implemented in. If Perl 6 didn't have junctions, I'd go the route of calculating the sum instead, which is readily available:

```say @array.pairs.first({ 0 == [+] .value.list}).key;
[download]```

(This assumes that no negative entries are allowed. If they are allowed, one could add a >>.abs after the .list to fix it).

Another approach is to use the fact that 0 is the only number that evaluates to False in boolean context:

```say @array.pairs.first({ not [||] .value.list}).key;
[download]```

In both cases I make use of the fact that [op] applies the operator op to a list of values.

• Comment on Re: An exploration of higher-level-language approaches to the "First all-zero row in an NxN matrix" problem (goto Considered Harmful)
• Select or Download Code

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://927681]
help
Chatterbox?
 [1nickt]: Ya gotta love the google. If you set accept=".txt" in a form input field where type="file", Chrome will allow you to select local files with extensions like .pl, .pm, .yaml, .ini etc. Nice!

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (6)
As of 2017-07-22 18:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (340 votes). Check out past polls.