develooper Front page | perl.perl6.users | Postings from December 2019

My keeper on "contains"

Thread Next
From:
ToddAndMargo via perl6-users
Date:
December 9, 2019 01:45
Subject:
My keeper on "contains"
Message ID:
57753442-af64-0dca-5392-190e1547d583@zoho.com
Hi All,

I have 190 of these.  191 in the hopper for unicodes.
This is one of my better one.  It is also unique in
that it takes apart the IEEE-eese on the
documentation and explain what in the world is
going on.  Do you notice the ":" is sometimes a delimiter
and sometimes not.  Eee Gads!

Enjoy!

-T


perl 6: method:

Also: "starts-with" and "ends-with"

A method is a routine that you feed: .foo

    $ p6 'say "abc".contains( "a" );'
    True

    $ p6 'say "abc".contains( "z" );'
    False

    $ p6 'my $x="\na\nb\nc\n"; for $x.lines -> $i {print "<$i>\n"};'
    <>
    <a>
    <b>
    <c>


Case insensitive contains:
     $ p6 'if "2018 Jul 7".fc.contains( "jul".fc ) {say "Yes";}'
     Yes

     $ p6 'if "2018 xJul 7".fc.contains( "jul".fc ) {say "Yes";}'
     Yes

     p6 'if "2018 xul 7".fc.contains( "jul".fc ) {say "Yes";}'
     <nothing>

     $ p6 'my $x="strong>2018.2.0; Coming Soon</strong>"; say so 
$x.fc.contains("strong" & not "coming soon");'
     False



multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos --> Bool)

"multi method"
     This means there are multiple ways to address this, as in

          multi method contains(Str:D: Cool:D $needle --> Bool)
          multi method contains(Str:D: Str:D $needle --> Bool)
          multi method contains(Str:D: Cool:D $needle, Int(Cool:D) $pos 
--> Bool)
          multi method contains(Str:D: Str:D $needle, Int:D $pos --> Bool)

"Str"
     means it want to be fed data as a string.  Str.foo

":D"
     means it wants actual data in the string and not a Nil.
     The jargon for this requirement is that is is constrained
     to an actual value

     If it wanted a Nil, it would say ":U" or constrained to
     a Nil

":"
     is the delimiter that tells you it is finished defining
     what is wants to be fed

"Cool:D $needle"
     means it wants a type Cool (string or number) for the
     substring ($needle) it is looking for in the Haystack (Str)

"Int(Cool:D)"
     Means it will change a type Cool into a type Int (integer)
     and the ":D" means it want some actual data and not a Nil.

"$pos"
     Is short of Position.  It is the starting index in the
     haystack (Str) to start looking for the substring
     (needle).  Index starts at zero by way of string
     convention in Perl 6

     $pos is an optional parameter.  Instead of $pos?
     for optional, they stated it with several "multi method"
     that do not include $pos.

"-->Bool"
     means it return True or False

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About