> On 30 Apr 2018, at 15:12, ToddAndMargo <toddandma...@zoho.com> wrote:
> 
> On 04/30/2018 05:44 AM, Elizabeth Mattijsen wrote:
>>> On 30 Apr 2018, at 12:43, ToddAndMargo <toddandma...@zoho.com> wrote:
>>> 
>>> 
>>>> Am 30.04.2018 um 08:47 schrieb ToddAndMargo:
>>>>> Hi All,
>>>>> 
>>>>> I know it would only take me 25 seconds to write one,
>>>>> but do we have an odd and even function build in?
>>>>> 
>>>>> 
>>>>> Many thanks,
>>>>> -T
>>>>> 
>>>>> $ perl6 -e 'my $x=3; say $x.odd;'
>>>>> No such method 'odd' for invocant of type 'Int'. Did you mean 'ord'?
>>>>>   in block <unit> at -e line 1
>>>>> 
>>>>> $ perl6 -e 'my $x=3; say $x.even;'
>>>>> No such method 'even' for invocant of type 'Int'
>>>>>   in block <unit> at -e line 1
>>> 
>>> On 04/30/2018 02:05 AM, Martin Barth wrote:
>>>> Are you aware of the %% operator?
>>>> 
>>>> $var %% 2 checks wether it is dividable by 2.
>>>> 
>>>> 
>>> 
>>> No I was not.  Thank you!
>>> 
>>> I was just going to do a
>>> 
>>>     sub  odd( $Num ) { return $Num % 2; }
>>>     sub even( $Num ) { return not $Num.odd; }
>> If you create this sub to do value checking, then maybe this approach is 
>> better for you:
>>     subset EvenInt of Int where * %% 2;  # all the even Ints
>>     subset OddInt of Int where * !%% 2;  # all the odd Ints
>>     # works
>>     my EvenInt $x = 42;
>>     # dies with: Type check failed in assignment to $y; expected EvenInt but 
>> got Int (42)
>>     my OddInt  $y = 42;
>> Liz
> 
> I don't get it.  Why would I want to do this?   I only want to know if
> an integer is odd or even.  What am I missing?
> 
> For example, I just coded this:
> 
>    sub odd( $Num ) { return $Num % 2; }
> 
>    for split( "\n", $ClipStr ).kv -> $LineNum, $Line {
>        if odd $LineNum
>               { $PartsStr ~= '<font color="#006600">'; }  # Green
>         else  { $PartsStr ~= '<font color="#663366">'; }  # Purple
>    ...
>    }

Perhaps this is a simpler solution:

    for split( "\n", $ClipStr ) -> $evenline, $oddline? {
        say “Purple $evenline”;
        say “Green $_” with $oddline;
    }

Note that in this case if you have a odd total number of lines, you can have 
$oddline come is as a type object.  So you need the ? in the signature of the 
block and the check with “with” in the code (which evaluates to True if the 
value is *not* a type object, and it also sets $_ for you then).


Hope this helps
        

Reply via email to