Skip to content

Instantly share code, notes, and snippets.

@usev6
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save usev6/2e43b83757e5c0e719fc to your computer and use it in GitHub Desktop.
Save usev6/2e43b83757e5c0e719fc to your computer and use it in GitHub Desktop.
sequence for custom class
class A {
has $.val;
method Numeric { $.val };
method succ { say "DEBUG: method succ was called"; A.new( val => $.val+1) };
method pred { say "DEBUG: method pred was called"; A.new( val => $.val-1) };
}
multi sub infix:<cmp> (A $x, Int $n) { $x.val cmp $n };
multi sub infix:<cmp> (A $x, A $y) { $x.val cmp $y.val };
my $a = A.new( val => 3 );
my $b = A.new( val => 4 );
say 'Sequence 1: ($a ... 6)[^5]';
say ($a ... 6)[^5];
say "";
say 'Sequence 2: ($a, $b ... 6)[^5]';
say ($a, $b ... 6)[^5];
say "";
say $a cmp 6;
say $a.Stringy cmp 6.Stringy;
@usev6
Copy link
Author

usev6 commented Mar 23, 2015

$ perl6-m seq_cmp.p6 
Sequence 1: ($a ... 6)[^5]
DEBUG: method pred was called
DEBUG: method pred was called
DEBUG: method pred was called
DEBUG: method pred was called
A.new(val => 3) A.new(val => 2) A.new(val => 1) A.new(val => 0) A.new(val => -1)

Sequence 2: ($a, $b ... 6)[^5]
DEBUG: method succ was called
DEBUG: method succ was called
A.new(val => 3) A.new(val => 4) A.new(val => 5) A.new(val => 6)

Less
More

@usev6
Copy link
Author

usev6 commented Mar 23, 2015

I'm trying to generate a sequence with a custom class A. On MoarVM sequence 2 works as expected, but for sequence 1 (only $a given on the LHS) the sequence goes "downwards". S03 states:

For functions deduced when there is only one value on the left, the final value is used to determine whether *.succ or *.pred is more appropriate. The two values are compared with cmp to determine the direction of the progression.

Note: If I use $a, *.succ ... 6 I get the expected result.

On JVM sequence 1 also is generated by calling 'pred'. Interestingly for sequence 2 I see flapping behaviour: Most times I get A.new(val => 3) A.new(val => 4) A.new(val => 3) A.new(val => 2) A.new(val => 1) but sometimes I get A.new(val => 3) A.new(val => 4) A.new(val => 5) A.new(val => 6).

I tried to define a matching multi candidate for infix:<cmp>. It doesn't seem to work, though: If I comment it out I get the same results (except for the second to last 'say' command, of course).

It looks to me as if the supplied multi candidate is not "visible" within sub SEQUENCE from src/core/operators.pm and this routine instead calls the multi candidate multi sub infix:<cmp>(\a, \b) from src/core/Order.pm which calls Stringy an both elements and does a cmp with those values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment