Check if all elements of list are prime in Raku

Viewed 267
my @g = (1,2,3,4);
say reduce {is-prime}, @g; # ==> gives error
say reduce {is-prime *}, @g; #==> gives error
say reduce {is-prime}, (1,2,3,4); # ==> gives error
say so is-prime @g.all; # ==> gives error

How to check if all elements of list are prime in Raku?

5 Answers

The answers above are all helpful, but they fail to explain why your solution does not work. Basically reduce is not going to apply a function (in your case, is-prime) to every member of a list. You want map for that. The error says

Calling is-prime() will never work with signature of the proto ($, *%)

Because reduce expects an infix, thus binary, function, or a function with two arguments; what it does is to apply them to the first pair of elements, then to the result and the third element, and so on. Last statement does not work for a similar reason: you are calling is-prime with a list argument, not a single argument.

You're basically asking: are there any elements in this list which are not prime? I would write that as:

say "not all prime" if @g.first: !*.is-prime;

Please note though, that apparently 1 is not considered prime according to the is-prime function:

say 1.is-prime;  # False

so the first would trigger on the 1 in your example, not on the 4.

There are of course may ways to do this. A very explicit way is using a for loop:

for @g -> $g {
 if $g.is-prime {
  say $g;
 }
}

Or with a grep (you could leave the $_ implicit):

@g.grep({ $_.is-prime }).say

Both above are assuming you really want to filter the primes out. Of course you can also really check each number and get a boolean:

@g.map({ .is-prime }).say

There is a big problem with this:

say reduce {is-prime}, @g;

You created a lambda:

{  }

The only thing it does is calls a function:

is-prime

You didn't give the function any arguments though.
Is it just supposed to guess what the arguments should be?

If you meant to pass in is-prime as a reference, you should have used &is-prime rather than {is-prime}. Of course that still wouldn't have worked.

The other problem is that reduce operates by recursively combining values.
It can't do that if it operates on one argument at a time.
The bare block lambda {}, takes zero or one argument, not two or more.


reduce is often combined with map.

It happens so often that there is a Wikipedia page about MapReduce.

say ( map &is-prime, @g ==> reduce { $^a and $^b } );
# False

say ( map &is-prime, 2,3,5 ==> reduce { $^a and $^b } );
# True

I wrote it that way so that map would be in the line before reduce, but perhaps it would be more clear this way:

say reduce {$^a and $^b}, map &is-prime, 2,3,5;
# True

reduce with an infix operator is so common that there is a shorter way to write it.

say [and] map &is-prime, 2,3,5;
# True

Of course it would be better to just find the first value that isn't prime, and say the inverse.

Since if there is even a single value that isn't prime that would mean they can't all be primes.

You have to be careful though, as you may think something like this would always work:

not @g.first: !*.is-prime;

It does happen to work for the values you gave it, but may not always.
first returns Nil if it can't find the value.

not (2,3,5).first: !*.is-prime;
# not Nil === True

not (2,3,4).first: !*.is-prime;
# not 4   === False

not (2,3,0,4).first: !*.is-prime;
# not 0   === True

That last one returned 0 which when combined with not returns True.

You could fix this with defined.

not defined (2,3,0,4).first: !*.is-prime;
# False

This only works if first wouldn't return an undefined element that happens to be in the list.

(Int,Any).first: Real
# Int

defined (Int,Any).first: Real
# False

You could fix that by asking for the index instead of the value.
You of course still need defined.

(Int,Any).first: :k, Real
# 0

defined (Int,Any).first: :k, Real
# True

The other way to fix it is to just use grep.

not (2,3,0,4).grep: !*.is-prime;
# not (0,4) === False

Since grep always returns a List, you don't have to worry about checking for 0 or undefined elements.
(A List is True if it contains any elements, no matter what the values.)

grep is smart enough to know that if you coerce to Bool that it can stop upon finding the first value. So it short-circuits the same as if you had used first.


This results in some fairly funky code, with those two negating operators. So it should be put into a function.

sub all-prime ( +@_ ) {
  # return False if we find any non-prime
  not @_.grep: !*.is-prime
  # grep short-circuits in Bool context, so this will stop early
}

This could still fail if you give it something weird

all-prime 2,3,5, Date.today;
# ERROR: No such method 'is-prime' for invocant of type 'Date'

If you care, add some error handling.

sub all-prime ( +@_ ) {
  # return Nil if there was an error
  CATCH { default { return Nil }}

  # return False if we find any non-prime
  not @_.grep: !*.is-prime
}

all-prime 2,3,5, Date.today;
# Nil

use the all junction:

say so all @g».is-prime; # False
Related