Re: Strange behavior with sequence of functions

[email protected] (William Michels via perl6-users)
Newsgroups perl.perl6.users
Message-ID <[email protected]>

> On Jun 29, 2023, at 12:21, Sean McAfee <[email protected]> wrote:
> 
> I was trying to construct a sequence of functions using the sequence operator, and encountered some very puzzling behavior.  I was able to reduce it down to some very simple examples.
> 
> [1] > my @s = +*, -> &f { &f } ... *
> [...]
> [2] > @s[0]
> Too few positionals passed; expected 1 argument but got 0
>   in block <unit> at <unknown file> line 1
> 
> I expected that this would create a lazy sequence where the first element is the WhateverCode function +*, and the generator function -> &f { &f } just returns the previous element unchanged.  But it doesn't work that way.
> 
> Strangely, I can call any element of this sequence, even the first one that I supplied explicitly and is somehow unprintable, with any arguments and get a list of the arguments back:
> 
> [3] > @s[0](1, 'x', pi)
> (1 x 3.141592653589793)
> 
> Here's another couple of odd cases:
> 
> [4] > (1, +*, 2, 3, 4 ... *)[^10]
> (1 1 1 1 1 1 1 1 1 1)
> 
> My experience has led me to expect that the ... operator only uses the list element immediately preceding it as a generator, so I can't explain this.  Similarly:
> 
> [5] > (1, * + 1, 0, 0, 0, * + 2 ... *)[^10]
> (1 2 3 4 5 6 7 8 9 10)
> 
> It looks like maybe the ... operator looks at the list on its left-hand side, uses the first callable element it finds as the generator, and discards everything after that...?  Is that the correct behavior?  Is that the intended behavior?
> 
> Oh, actually, I see that the operator doc page <https://docs.raku.org/language/operators> says that "Custom generators need to be the last element of the list before the '...' operator."  So, I guess this is a bug...?
> 
> This is all in Rakudo 2023.06.
> 


Hi Sean,

Well, I get your first example to work if I use the "." dot between @s and [0], as in "@s.[0]". By "work" I mean it doesn't error-out, returning (Any) instead. See:

~ % raku
Welcome to Rakudo™ v2023.05.
Implementing the Raku® Programming Language v6.d.
Built on MoarVM version 2023.05.

To exit type 'exit' or '^D'
[0] > my @s = +*, -> &f { &f } ... *
[...]
[1] > @s[0]
Too few positionals passed; expected 1 argument but got 0
  in block <unit> at <unknown file> line 1

[1] > @s.[0]
(Any)
[2] > @s[0](1, 'x', pi)
(1 x 3.141592653589793)

HTH, Bill.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.