Re: Could this be any more obscure?

2018-09-27 Thread Alessandro Costantini


> Il giorno 26 set 2018, alle ore 08:57, Todd Chester  
> ha scritto:
> 
> Hi All,
> 
> Over on
>https://docs.perl6.org/routine/words
> I see
> 
>   multi method words(Str:D $input: $limit = Inf --> Positional)

$input is the method invocant (hence the “:”) constrained to a defined string 
object.

$limit is the number of words (chunks separated by whitespace) you may want 
from $input

- -> constrains the return type to an object that consumes the role of 
Positional


> HOW IN THE WORLD did they convert `$limit = Inf` into an
> array index!?!?

I think $limit is the number of words you want, not an array index.

> https://docs.perl6.org/type/Num#Inf
> And `Inf` means a number larger than Perl can handle or Infinity.
> What does that have to do with ANYTHING!?

I think the method returns a lazy Seq, so no limits.

> Yours in confusion,
> -T
> 
> And the worst part is that I know how to use "words" and use it
> frequently.  And the documentation is a nightmare to understand.
> 
> Link to Positional:
> https://docs.perl6.org/type/Positional
>   Role for objects which support indexing them using
>   postcircumfix:«[ ]» (usually list-like objects).
>   Example types with Positional role include List,
>   Array, Range, and Buf.
> 
> I like him using `Str:D $input:` rather than just `Str:D:`
> and it is more intuitive


Re: Could this be any more obscure?

2018-09-27 Thread Brad Gilbert
In Perl 6 most normal operators are subroutines:

@a[1..3]
:« [  ] »( @a,1..3 ) # same as above

Since they are just subroutines they often just call something else.

# an overly simplified version
sub postcircumfix:« [  ] » ( @array, **@indicies ) {
gather {
for @indicies -> $index {
take @array.AT-POS( $index )
}
}
}

When you define such a subroutine for the first time, it modifies the parser.

In the case of :« [  ] », it can take ANY VALUE as its
first argument.
On `Positional` arguments it calls `.AT-POS`.
If it isn't `Positional`, it will pretend that it is a list of one value.

   1[0];   # pretend that `1` is the same as `(1,)`

This means it can be found everywhere throughout the language.

---

We do not want to describe every bit of the language that interacts
with the `.words` method.
This is because that would be the entire language on one page.

When you talked about [], this is inadvertently what you asked for.

---

In the case of the `.words()` document we say that it is called on
`Str` and returns a `Seq`.
(The docs were wrong about it returning a `Positional`)

We document that it returns the parts of the string that aren't whitespace.

We document that you can give a limit to how many values it returns.
(arguably, this isn't needed because you can just call `.head($limit)` instead)

If we changed it to `selection` it would make it seem more complicated
than it really is.
Selection could mean that it could select the second and third values.
`.words()` doesn't do that.

The closest synonyms would be:
cap
maximum
upper bound
cutoff point
farthest point
end
max count

We do not document every single operator that operates on a `Seq`, on that page.
(Almost every single one of them.)
Again, [] is one of them.

We do not document on that page all of the methods of `Seq`.
(There is a page specifically for that)

On Wed, Sep 26, 2018 at 9:18 PM ToddAndMargo  wrote:
>
> On 9/26/18 7:11 PM, Brandon Allbery wrote:
> > That's not helping. What didn't you understand?
>
> >>  It just improves error messages.
>
> My understanding it that if it uses "--Positinal"
> I can use []


Re: Could this be any more obscure?

2018-09-27 Thread Laurent Rosenfeld via perl6-users
> I am NOT asking it to limit my request to Infinity.

Yes you are, implicitly. If you don't pass any parameter for $limit, $limit
will take the default value supplied by the signature, i.e. Inf.



Le jeu. 27 sept. 2018 à 02:48, ToddAndMargo  a
écrit :

> On 9/26/18 4:33 PM, The Sidhekin wrote:
> > On Wed, Sep 26, 2018 at 11:40 PM ToddAndMargo  > > wrote:
> >
> > What I don't understand is:
> >
> >multi method words(Str:D $input: $limit = Inf --> Positional)
> >
> > Specifically "$limit = Inf".  Why are we using "$limit" instead
> > of "$selection"
> >
> >Perhaps this would be easier if you explain where you're getting
> > "$selection", and what you think it is.
>
> $ p6 '"a b c d e".words[ 2, 4 ].say;'
> (c e)
>
> or
>
> $ p6 '"a b c d e".words()[ 2, 4 ].say;'
> (c e)
>
> I am selecting the 2nd and 4th word starting from zero.  Inside
> the brackets I am asking it to select th e 2nd and 4th words for me.
>
> I am NOT asking it to limit my request to Infinity.
>
>
>
> >
> > And where is it stated what goes in the () and what goes
> > in the []?
> >
> >
> >The () is part of a method call syntax; method arguments go there:
> > https://docs.perl6.org/language/syntax#Subroutine_calls
>
> Where does it state that
>
> $ p6 '"a b c d e".words(3).say;'
> (a b c)
>
> means the first three words, starting at zero?
>
>
> >The [] is a postcircumfix operator; index arguments go there:
> > https://docs.perl6.org/language/operators#postcircumfix_[_]
>
> Where does
>
>  multi method words(Str:D $input: $limit = Inf --> Positional)
>
> state that I can do such?
>
> I ask this because not all methods will take []
>
>
> $ p6 '"a b c d e".contains( "a","b" ).say;'
> Cannot convert string to number: base-10 number must begin with valid
> digits or '.' in '⏏b' (indicated by ⏏)
>in block  at -e line 1
>
> $ p6 '"a b c d e".contains( 1, 3 ).say;'
> Ambiguous call to 'contains(Str: Int, Int)'; these signatures all match:
> :(Str:D: Cool:D $needle, Cool:D $pos, *%_)
> :(Str:D: Cool:D $needle, Cool:D $pos, *%_)
>in block  at -e line 1
>
>
> Also, where is it stated that
>
> $ p6 '"a b c d e".words(3)[ 2, 4 ].say;'
> (c Nil)
>
> will send the first three words to [2,4] ?
>
> Thank you for helping me with this!
>
> -T
>


Re: Could this be any more obscure?

2018-09-27 Thread JJ Merelo
El jue., 27 sept. 2018 a las 3:51, ToddAndMargo ()
escribió:

> On 9/26/18 6:31 PM, ToddAndMargo wrote:
> > On 9/26/18 6:18 PM, Curt Tilmes wrote:>
> >  >  > The methods don't take [].  You are calling [] on the thing
> > that the
> >  >  > methods return.
> >
> >>>
> >>> Yes, I know.  And it is human readable too.  It is one of the
> >>> many reasons I adore Perl 6.
> >>>
> >>> Where in
> >>>  multi method words(Str:D $input: $limit = Inf --> Positional)
> >>> does it state that "words" will do that?  Not all methods will.
> >>> So it need to be stated when they will.
> >>>
> >>>
> >
> >
> >
> >  > The part where it says "--> Positional" says the thing that gets
> >  > returned is Positional.
> >  >
> >  > A Positional thing has all sorts of methods and operators you can use,
> >  > including []
> >  >
> >  > Not all methods will, of course.  Only those that say "--> Positional"
> >  > return a Positional that acts like that.
> >  >
> >  > Curt
> >
> > Hi Curt,
> >
> > Perfect! Thank you!
> >
> > So all methods that respond with --> Positional will accept []
> >
> > Awesome!
> >
> > -T
>
>
>
> I do believe the reason I spaced on this was that when I see "-->"
> what goes through my head is "this is the value(s) returned".
> I had not idea it would reflect backwards and affect the method.
>

It does not.

say "Flim Flam Flum".words[2] # OUTPUT: «Flum␤»

is exactly the same as

say "Flim Flam Flum".words()[2] # OUTPUT: «Flum␤»

And exactly the same as

say "Flim Flam Flum".words(Inf)[2] # OUTPUT: «Flum␤»

And exactly the same as

my @flim-flam-flum = "Flim Flam Flum".words; # @flim-flam-flum carries
an @, ergo it's a Positional
say @flim-flam-flum[2] # OUTPUT: «Flum␤»

In Perl 6 you can chain calls, that's what is meant by postcircumfix, it
means you can put the operator like thing (in this case, []) _behind_
(post) the thing you are calling, plus you are putting the arguments
_inside_ the operator (that's the circumfix part). You can also do

say "Flim Flam Flum".words[1,2][0] # OUTPUT: «Flam␤»

You are first post-circumfixing [] over the return value of words, getting
2 elements in a Positional (an List in this case, Positional is a Role, not
a Class), and them post-circumfixing again getting the first of these two
elements. You can do this to exhaustion, as long as it's an object method
or a post-circumfix operator, chaining the one after the other. None of
them is "reflecting" on anything, you are just chaining calls, which is a
nice and compact thing to do.

Cheers
-- 
JJ


Re: Could this be any more obscure?

2018-09-27 Thread JJ Merelo
El jue., 27 sept. 2018 a las 3:19, Brandon Allbery ()
escribió:

> Additionally: Perl 5 docs don't run into this because perl 5 has only 3
> types: scalar, list, hash.
>
> Perl 6 has lots of types, each of which has its own behavior. We use roles
> to package up common
>

_and_ roles _and_ contexts.

behaviors. Positional is the role for "can be indexed" / "understands []".
> Don't just assume you know what Positional is because you know its English
> meaning; look at what it actually does, and you will find the []
> documentation.
>

Right: https://docs.perl6.org/type/Positional Just type "Positional" into
the search box, and scroll down to where it says "Roles".

Cheers

JJ


Re: Could this be any more obscure?

2018-09-27 Thread JJ Merelo
El mié., 26 sept. 2018 a las 23:31, Laurent Rosenfeld via perl6-users (<
perl6-users@perl.org>) escribió:

> You can set a limit to the number of items (words) you want to retrieve:
> you will get only the first $limit words.
>
> If you don't supply any limit, Inf can be thought as the default value for
> the number of items, i.e. there is no limit and the routine will return as
> many words as it can from the source input.
>

And this is one of the things I love Perl 6 for, its consistency. Infinity
is literally no limit. Using it meaning "no limit" is genius. Not "0 in
this case means no limit" or "-1 means no limit" or "this constant meaning
unavailable" or whatever. Infinity has no limit, we use them as a parameter
to imply that argument has no limit.

Cheers

JJ