Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if all elements of list are prime in Raku

Tags:

mapreduce

raku

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?

like image 249
Lars Malmsteen Avatar asked Jun 07 '20 14:06

Lars Malmsteen


1 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.

like image 90
jjmerelo Avatar answered Dec 14 '22 09:12

jjmerelo