After a long absence from Perl (alas, Java pays the bills), I was looking at getting back to competency with perl 6 and looking at a "try writing an idiomatic blackjack program." The 1 or 11 nature of the ace seems like a perfect use for a Junction. To make sure I knew how to use a Junction and sum with it, I started with a list of numbers and worked with the "most people write it this way" of https://docs.perl6.org/type/Array#(List)_routine_reduce :
my @list = (1, 2, 3); my $sum = [+] @list; say $sum;
And that worked. Adding a Junction to this:
my @list = (1, 2|3, 4); my $sum = [+] @list; say $sum;
And that spat out an error: "Type check failed in assignment to $sum; expected Numeric but got Junction (any(3, 4))"

Switching this code to another form of list summation

my @list = (1, 2|3, 4); my $sum = @list.reduce(*+*); say $sum;
That works fine.

This mismatch of expectations and errors also occurs with $sum = @list.sum; when there is a Junction in the list. So, what I'm wondering is "what is the difference between these invocations?" I suspect its something with the type system that I haven't wrapped my head around yet and that it is working as expected.

It's also quite possible that I'm introducing an XY problem in to this and that I shouldn't be doing it this way in the first place - and if that's the case, a pointer off to the more idiomatic way of dealing with what looks to be a good fit for the ace = 1|11 state would be appreciated.


In reply to [Perl6] [+] on a list... with a Junction by m_turner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.