How do I get the elements from a hyperoperator in original order?

I wanted to split the line into words and print each word on a separate line, so I tried the following:

"foo bar baz".words>>.say

      

However, the results were not ok:

baz
foo
bar

      

What's wrong with that?

+3


source to share


3 answers


Hyperoperators do return results in order. The problem with your code is that hyperopes are not loop strings; they are list operators. Thus, their execution is not guaranteed, and the code that uses them as if it were wrong. If you are not indifferent to the order of execution, you should use a type construct for

that guarantees it.

For example this

my @a = "foo bar baz".words>>.split("a");

      



results in @a

one containing the elements in the expected order, regardless of the order in which each element was evaluated:

foo b r b z

      

+4


source


To quote design docs ,

The hyperoperator is one of the ways you can promise the optimizer that your code is parallelizable.

Other ways to do it: hyper

and race

, which I think are not implemented in Rakudo.

If you care about the order of evaluation, use a operators instead for

, map

or .map

, for example



"foo bar baz".words.map(&say)

      

The direct equivalent of the original code using the method form say

would read

"foo bar baz".words.map(*.say)

      

+4


source


When providing a simple list, the hyperoperators are allowed to process the elements in any order. This property makes parallelization easier.

If you just want to act on each element of the array, use in order for

, instead:

for "foo bar baz".words { .say }

      

Which prints what you are looking for:

foo
bar
baz

      


UPDATE

I configured execution and assignment. See Darch for a more correct (and accepted) answer.

+1


source







All Articles