Re: I need syntax to sub declare return a hash and an array
On Tue, Jan 21, 2020 at 07:16:17PM -0800, Todd Chester via perl6-users wrote: > > > On 2020-01-21 18:57, Tom Browder wrote: > > On Tue, Jan 21, 2020 at 18:34 Todd Chester via perl6-users > > mailto:perl6-users@perl.org>> wrote: > > > > On 2020-01-21 16:09, Todd Chester via perl6-users wrote: > > >> 4) A block (that is the { ... } bit) will always 'return' the last > > >> expression evaluated. > > > > > > Seems to me I have see the last expression returned even without > > > the {...}. Maybe I am misremembering. > > > > > > Todd, the {} is the block defining the subroutine. > > > > > sub AplusB( $a, $b --> Int ){$a+$b;} > > > > > > The above is the sub's definition. > > > > > > > > > > > > > > The above should generate an error because the mandatory args are > > missing, depending on the context (don't quote me on that). > > is printer out by REPL. I don't know why. REPL > does that a lot. The REPL tells you that the result of the expression you entered (the sequence of characters "sub AplusB..{$a+$b}") was to define a subroutine called "AplusB", which is what you wanted it to do. The "&" sigil (a sigil is basically the character before a thing's name in Raku and Perl 5) means that the name "AplusB" refers to a subroutine, not a variable or anything else. > > > AplusB 2, 3 > > 5 > > > > The above shows the sub being called with the required two args, and the > > 5 is the returned value which is probably shown in the REPL since there > > is no semicolon after the call and the 5 is not usually seen otherwise. > > $ p6 'sub AplusB( $a, $b --> Int ){$a+$b}; say AplusB 2, 3;' > 5 > > > > -Tom > > Hi Tom, > > I was trying to cut things down to the simplest terms. > I was responding to Richard's statement > > >> 4) A block (that is the { ... } bit) will always 'return' the last > >>expression evaluated. > > I created a sub without the "{...}" and showed where it would > return the last equation without the "{...}". I may have > misunderstood Richard. You created a sub with "$a+$b" in a "{...}" block. The "{$a+$b}" sequence of characters in what you typed is the "{...}" block that Richard is referring to - a block that starts with a "{" character, ends with a "}" character and contains a series of Raku expressions separated by semicolons. In your case the block only contains a single expression, "$a+$b", which represents the sum of the two arguments to the subroutine - this is *exactly* what Richard meant. If you were trying to illustrate his point that a block without an explicit "return" statement would return the last expression, you succeeded. If you were trying to demonstrate something that does not have a "{...}", well, nope, "{$a+$b}" is exactly the "{...}" that he was talking about, so the fact that AplusB returns the sum of $a and $b is exactly what he meant. G'luck, Peter -- Peter Pentchev roam@{ringlet.net,debian.org,FreeBSD.org} p...@storpool.com PGP key:http://people.FreeBSD.org/~roam/roam.key.asc Key fingerprint 2EE7 A7A5 17FC 124C F115 C354 651E EFB0 2527 DF13 signature.asc Description: PGP signature
Re: REPL and arrows problem
On Wed, 22 Jan 2020 at 13:30, Todd Chester via perl6-users < perl6-users@perl.org> wrote: > On 2020-01-21 16:38, Todd Chester via perl6-users wrote: > > Fedora31 > I have Fedora 31 and Readline does not seem to be working for me in the raku REPL as well. But see below! > $ rpm -qa \*raku\* > xfce4-terminal-0.8.9.1-1.fc31.x86_64 > Hmm, I don't think that is output from that command. Mine: $ rpm -qa \*raku\* rakudo-XML-0.0.3-0.6.20190728git417f637.fc31.x86_64 rakudo-MIME-Base64-1.2.1-6.fc31.x86_64 rakudo-zef-0.8.2-1.fc31.x86_64 rakudo-0.2019.11-3.fc31.x86_64 rakudo-Readline-0.1.5-2.fc31.x86_64 rakudo-URI-0.2.2-1.fc31.x86_64 > $ raku -v > This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1 > implementing Perl 6.d. > Looks like you might have your own rakudo installed not from the default system packages. Mine: $ which raku /usr/bin/raku $ raku -v This is Rakudo version 2019.11 built on MoarVM version 2019.11 implementing Perl 6.d. In any case, I also have the problem with raku REPL not using Readline despite rakudo-Readline-0.1.5-2 being installed. $ raku You may want to `zef install Readline` or `zef install Linenoise` or use rlwrap for a line editor To exit type 'exit' or '^D' > my $history^[[A^[[A This seems like a Fedora 31 packaging problem to me. However, after this: $ zef install Readline The arrow keys once again work for me. -- Norman Gaywood, Computer Systems Officer School of Science and Technology University of New England Armidale NSW 2351, Australia ngayw...@une.edu.au http://turing.une.edu.au/~ngaywood Phone: +61 (0)2 6773 2412 Mobile: +61 (0)4 7862 0062 Please avoid sending me Word or Power Point attachments. See http://www.gnu.org/philosophy/no-word-attachments.html
Re: I need syntax to sub declare return a hash and an array
On 2020-01-21 18:57, Tom Browder wrote: On Tue, Jan 21, 2020 at 18:34 Todd Chester via perl6-users mailto:perl6-users@perl.org>> wrote: On 2020-01-21 16:09, Todd Chester via perl6-users wrote: >> 4) A block (that is the { ... } bit) will always 'return' the last >> expression evaluated. > > Seems to me I have see the last expression returned even without > the {...}. Maybe I am misremembering. Todd, the {} is the block defining the subroutine. > sub AplusB( $a, $b --> Int ){$a+$b;} The above is the sub's definition. The above should generate an error because the mandatory args are missing, depending on the context (don't quote me on that). is printer out by REPL. I don't know why. REPL does that a lot. > AplusB 2, 3 5 The above shows the sub being called with the required two args, and the 5 is the returned value which is probably shown in the REPL since there is no semicolon after the call and the 5 is not usually seen otherwise. $ p6 'sub AplusB( $a, $b --> Int ){$a+$b}; say AplusB 2, 3;' 5 -Tom Hi Tom, I was trying to cut things down to the simplest terms. I was responding to Richard's statement >> 4) A block (that is the { ... } bit) will always 'return' the last >>expression evaluated. I created a sub without the "{...}" and showed where it would return the last equation without the "{...}". I may have misunderstood Richard. -T
Re: I need syntax to sub declare return a hash and an array
On Tue, Jan 21, 2020 at 18:34 Todd Chester via perl6-users < perl6-users@perl.org> wrote: > On 2020-01-21 16:09, Todd Chester via perl6-users wrote: > >> 4) A block (that is the { ... } bit) will always 'return' the last > >> expression evaluated. > > > > Seems to me I have see the last expression returned even without > > the {...}. Maybe I am misremembering. Todd, the {} is the block defining the subroutine. > sub AplusB( $a, $b --> Int ){$a+$b;} The above is the sub's definition. > > The above should generate an error because the mandatory args are missing, depending on the context (don't quote me on that). > AplusB 2, 3 > 5 > The above shows the sub being called with the required two args, and the 5 is the returned value which is probably shown in the REPL since there is no semicolon after the call and the 5 is not usually seen otherwise. As usual, you are a bit sloppy in your email text so it's not clear exactly what you are doing and what the real result is and in what context the question or statement is being shown. -Tom
Re: Using raku/perl6 as unix "cat"....
On 2020-01-21 11:08, William Michels via perl6-users wrote: Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: Hi William, I don't know if I contributed anything at all, but you are most welcome. Now for your next homework assignment, write a "less" substitute in Raku! You have to read the keyboard, do a forwards and backwards, and a search. man less DESCRIPTION Less is a program similar to more (1), but which allows backward movement in the file as well as forward movement. Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi (1). Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals. (On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.) Commands are based on both more and vi. Commands may be preceded by a decimal number, called N in the descriptions below. The number is used by some commands, as indicated. Like "cat" on steroids! Probably more fun than writing Winn API calls! :-) -T
Re: REPL and arrows problem
On 2020-01-21 16:38, Todd Chester via perl6-users wrote: Fedora31 Xfce 4.14 xfce4-terminal-0.8.9.1-1.fc31.x86_64 $ rpm -qa \*raku\* xfce4-terminal-0.8.9.1-1.fc31.x86_64 $ raku -v This is Rakudo version 2019.07.1 built on MoarVM version 2019.07.1 implementing Perl 6.d.
REPL and arrows problem
Hi All, Fedora31 Xfce 4.14 xfce4-terminal-0.8.9.1-1.fc31.x86_64 I have the same setup on both computers. On my shop computer, in REPL, the arrow keys do not read correctly: > a todd special booboo^[[D^[[D^[[D^[[D^[[D How do I get my arrow back in REPL on mu shop computer? Many thanks, -T
Re: I need syntax to sub declare return a hash and an array
On 2020-01-21 16:09, Todd Chester via perl6-users wrote: 4) A block (that is the { ... } bit) will always 'return' the last expression evaluated. Seems to me I have see the last expression returned even without the {...}. Maybe I am misremembering. > sub AplusB( $a, $b --> Int ){$a+$b;} > AplusB 2, 3 5
Re: how do you --> two variables?
> On 22 Jan 2020, at 01:17, Todd Chester via perl6-users > wrote: > > Hi All, > > What is the syntax for returning two variable from a sub? > > > sub x(--> Int, UInt) { return(-2,4) }; > > ===SORRY!=== Error while compiling: > Malformed return value (return constraints only allowed at the end of the > signature) > --> sub x(--> Int⏏, UInt) { return(-2,4) }; Raku *ALWAYS* only returns a *SINGLE* value. That value may be a List. The comma is the operator that creates Lists. `return 1,2,3` is therefore returning a List with 1, 2 and 3 as its elements. if you say: `my ($a,$b,$c)`, you're creating a List with three variables in them. if you say: `my ($a,$b,$c) = foo` and `sub foo() { return 1,2,3 }` you will effectively assign a list of values to a list of variables, assigning 1 to $a, 2 to $b, 3 to $c.
how do you --> two variables?
Hi All, What is the syntax for returning two variable from a sub? > sub x(--> Int, UInt) { return(-2,4) }; ===SORRY!=== Error while compiling: Malformed return value (return constraints only allowed at the end of the signature) --> sub x(--> Int⏏, UInt) { return(-2,4) }; Many thanks, -T
Re: I need syntax to sub declare return a hash and an array
On 2020-01-21 14:03, Elizabeth Mattijsen wrote: On 21 Jan 2020, at 22:37, Richard Hainsworth wrote: 2) 'returns' in the declaration (not the part in the block) used to be used, but for some arcane reason that I never really understood, it is deprecated. Not sure it is *the* reason, but the return type is part of the signature of a block. A signature of a block is expressed as ":()", not allowing anything outside of the parentheses. sub a() returns Int { }; dd # :( --> Int) Not sure what you just said.
Re: I need syntax to sub declare return a hash and an array
> On 21/01/2020 04:09, ToddAndMargo via perl6-users wrote: >> On 2020-01-20 19:55, ToddAndMargo via perl6-users wrote: >>> Hi All, >>> >>> What is the proper way to state that I am returning a >>> hash from a sub? `sub x() returns % {}` >>> >>> And an array? `sub x() returns @ {}` >>> >>> Many thanks, >>> -T >> >> >> I think this is it: >> >> > sub x() returns Associative { my %h= A=>"a"; return %h} >> >> > x >> {A => a} >> >> >> > sub y() returns Array { my @a=(1,2,3,2,1) ; return @a} >> >> > y >> [1 2 3 2 1] >> >> >> Am I too high up the food chain with Associative and Array? >> >> -T On 2020-01-21 13:37, Richard Hainsworth wrote: sub x( --> Hash) { my %h = A => 'a' } 1) '-->' in the signature is the best way to provide information to the compiler about what the subroutine should return 2) 'returns' in the declaration (not the part in the block) used to be used, but for some arcane reason that I never really understood, it is deprecated. 3) 'Hash' or 'Associative'. These all refer to roles that a hash will 'do'. 4) A block (that is the { ... } bit) will always 'return' the last expression evaluated. Seems to me I have see the last expression returned even without the {...}. Maybe I am misremembering. So there is no need for the 'return %h' in your code. 'return ...' is only needed if you have some logic where the block can exit in several places, not just the last expression. 5) To specify an 'array', you can use 'sub x( --> Array) { my @a = 1..* }' 6) HOWEVER, you really do need to read the documentation about Sequences, lists and arrays. Richard I have always used "return" to make it more readable. And sometimes I will return before end of the sub. I can type, so extra words don't bother me. Nice exposition! Thank you!
Re: I need syntax to sub declare return a hash and an array
> On 21 Jan 2020, at 22:37, Richard Hainsworth wrote: > 2) 'returns' in the declaration (not the part in the block) used to be used, > but for some arcane reason that I never really understood, it is deprecated. Not sure it is *the* reason, but the return type is part of the signature of a block. A signature of a block is expressed as ":()", not allowing anything outside of the parentheses. sub a() returns Int { }; dd # :( --> Int)
Re: I need syntax to sub declare return a hash and an array
sub x( --> Hash) { my %h = A => 'a' } 1) '-->' in the signature is the best way to provide information to the compiler about what the subroutine should return 2) 'returns' in the declaration (not the part in the block) used to be used, but for some arcane reason that I never really understood, it is deprecated. 3) 'Hash' or 'Associative'. These all refer to roles that a hash will 'do'. 4) A block (that is the { ... } bit) will always 'return' the last expression evaluated. So there is no need for the 'return %h' in your code. 'return ...' is only needed if you have some logic where the block can exit in several places, not just the last expression. 5) To specify an 'array', you can use 'sub x( --> Array) { my @a = 1..* }' 6) HOWEVER, you really do need to read the documentation about Sequences, lists and arrays. Richard On 21/01/2020 04:09, ToddAndMargo via perl6-users wrote: On 2020-01-20 19:55, ToddAndMargo via perl6-users wrote: Hi All, What is the proper way to state that I am returning a hash from a sub? `sub x() returns % {}` And an array? `sub x() returns @ {}` Many thanks, -T I think this is it: > sub x() returns Associative { my %h= A=>"a"; return %h} > x {A => a} > sub y() returns Array { my @a=(1,2,3,2,1) ; return @a} > y [1 2 3 2 1] Am I too high up the food chain with Associative and Array? -T
Re: Using raku/perl6 as unix "cat"....
" 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Wasn't that the point of p5's defined while ( defined(my $line = <> ) ) { or (previously lexified '$val'): print "$val\n" while defined($val = pop(@ary)); From: William Michels via perl6-users Sent: Tuesday, January 21, 2020 1:08 PM To: Trey Harris ; perl6-users Cc: Andrew Shitov ; Curt Tilmes ; Joseph Brenner ; ToddAndMargo ; yary ; Elizabeth Mattijsen ; ngayw...@une.edu.au Subject: Re: Using raku/perl6 as unix "cat" Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: mydir$ perl6 -e '.say for lines' ab_cd.txt a b c d mydir$ perl6 -ne '.say' ab_cd.txt a b c d mydir$ # below two single quotes as empty arg: mydir$ perl6 -pe '' ab_cd.txt a b c d mydir$ Finally, a question about the 'get' docs, which use 'while' as an example for the (IO::CatHandle) 'get' method. The docs say "(IO::CatHandle) method get: Returns a single line of input from the handle... . Returns Nil when there is no more input. ... ." And, "(IO::Handle) routine get: Reads a single line of input from the handle... . Returns Nil, if no more input is available... ." See: https://docs.raku.org/routine/get Should a different example other than "while/get" be used on the docs page? Or should some of the comments Yary made regarding "while/get" be incorporated into the docs--so programmers can be warned about truncating their output on blank lines with "while/get"? Yary, from Jan. 18th: " 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Best Regards, Bill. On Mon, Jan 20, 2020 at 4:13 PM Trey Harris wrote: > > On Mon, Jan 20, 2020 at 19:03 Trey Harris wrote: >> >> On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users >> wrote: >>> >>> Hi Yary (and Todd), >>> >>> Thank you both for your responses. Yary, the problem seems to be with >>> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 >>> actually returns fewer lines with "for" than it did with "while": >> >> >> If you want to do line-oriented input, use `.lines` with `for`; it returns >> something `for` can iterate over. > > > Sorry, in a setting where a handle isn’t the context, I meant `lines`, not > `.lines`, though I was referring _to_ a thing called `.lines`, the multi > method. I don’t think we’ve all yet agreed on how multis that can be called > as plain routines should be referred to in umbrella term. `.lines` is more > “correct”, but it’s less likely to actually work without understanding more, > which is a strange conundrum for documentation. > > Trey
Re: Using raku/perl6 as unix "cat"....
Good answers, all. Thanks to everyone for contributing. For anyone who wants a golfed "cat" replacement, command line arguments can give you shorter code: mydir$ perl6 -e '.say for lines' ab_cd.txt a b c d mydir$ perl6 -ne '.say' ab_cd.txt a b c d mydir$ # below two single quotes as empty arg: mydir$ perl6 -pe '' ab_cd.txt a b c d mydir$ Finally, a question about the 'get' docs, which use 'while' as an example for the (IO::CatHandle) 'get' method. The docs say "(IO::CatHandle) method get: Returns a single line of input from the handle... . Returns Nil when there is no more input. ... ." And, "(IO::Handle) routine get: Reads a single line of input from the handle... . Returns Nil, if no more input is available... ." See: https://docs.raku.org/routine/get Should a different example other than "while/get" be used on the docs page? Or should some of the comments Yary made regarding "while/get" be incorporated into the docs--so programmers can be warned about truncating their output on blank lines with "while/get"? Yary, from Jan. 18th: " 'while' ...will stop when it encounters a false line--typically an empty line or '0' ". Best Regards, Bill. On Mon, Jan 20, 2020 at 4:13 PM Trey Harris wrote: > > On Mon, Jan 20, 2020 at 19:03 Trey Harris wrote: >> >> On Mon, Jan 20, 2020 at 02:59 William Michels via perl6-users >> wrote: >>> >>> Hi Yary (and Todd), >>> >>> Thank you both for your responses. Yary, the problem seems to be with >>> "get". I can change 'while' to 'for' below, but using 'get' raku/perl6 >>> actually returns fewer lines with "for" than it did with "while": >> >> >> If you want to do line-oriented input, use `.lines` with `for`; it returns >> something `for` can iterate over. > > > Sorry, in a setting where a handle isn’t the context, I meant `lines`, not > `.lines`, though I was referring _to_ a thing called `.lines`, the multi > method. I don’t think we’ve all yet agreed on how multis that can be called > as plain routines should be referred to in umbrella term. `.lines` is more > “correct”, but it’s less likely to actually work without understanding more, > which is a strange conundrum for documentation. > > Trey
Re: My keeper on hashes
New addition to my hash keeper: "returns" hash on a sub declaration: Note: "Associative" will return a hash or a map or even a pair > sub x() returns Associative { my %h= A=>"a"; return %h} > x {A => a} > sub x(--> Hash) { my %h= A=>"a", B=>"b"; return %h} > x {A => a, B => b} > > sub x() returns Hash { my %h= A=>"a"; return %h} > x {A => a}
Re: My Windows Modules
On 2020-01-21 00:58, Shlomi Fish wrote: Hi ITodd! On Mon, 20 Jan 2020 22:20:29 -0800 ToddAndMargo via perl6-users wrote: Hi All, I have my Win API modules to the point I like them now. I will be a few months before I get them up on GIT. You should already be using version control (git, likely): I agree. I will be a few months though. https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#what-are-some-best-practices-in-programming-that-i-should-adopt (short URL - https://is.gd/kUMG3s ) If anyone wants to look at them now, I will vpaste.net them for you. I will only look at them if they are FOSS: https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#i-want-to-release-my-code---which-open-source-licence-should-i-use (short URL - https://is.gd/1gzZB6 ) I will check it out. Thank you! Regards, Shlomi
Re: I need syntax to sub declare return a hash and an array
On 2020-01-21 05:00, Tom Browder wrote: The preferred syntax is: sub x(--> Hash) {...} See that in the docs. Larry Wall once warned that the "return..." syntax may be removed at some point. -Tom Hi Tom, Interesting. I have been showing both in my keepers. I currently use "returns " as it reads better (for me), but is Larry is thinking of dropping it, I need to get use to --> Thank you! -T
Re: I need syntax to sub declare return a hash and an array
On Mon, Jan 20, 2020 at 22:16 ToddAndMargo via perl6-users < perl6-users@perl.org> wrote: > On 2020-01-20 20:09, ToddAndMargo via perl6-users wrote: > > On 2020-01-20 19:55, ToddAndMargo via perl6-users wrote: ... > > I think this is it: > > > > > sub x() returns Associative { my %h= A=>"a"; return %h} > > > > > x > > {A => a} > ... > Yup, too high up the food chain: > > > sub x() returns Hash { my %h= A=>"a"; return %h} The preferred syntax is: sub x(--> Hash) {...} See that in the docs. Larry Wall once warned that the "return..." syntax may be removed at some point. -Tom
Re: My Windows Modules
Hi Todd! On Mon, 20 Jan 2020 22:20:29 -0800 ToddAndMargo via perl6-users wrote: > Hi All, > > I have my Win API modules to the point I > like them now. I will be a few months > before I get them up on GIT. > You should already be using version control (git, likely): https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#what-are-some-best-practices-in-programming-that-i-should-adopt (short URL - https://is.gd/kUMG3s ) > If anyone wants to look at them now, I will > vpaste.net them for you. > I will only look at them if they are FOSS: https://github.com/shlomif/Freenode-programming-channel-FAQ/blob/master/FAQ_with_ToC__generated.md#i-want-to-release-my-code---which-open-source-licence-should-i-use (short URL - https://is.gd/1gzZB6 ) Regards, Shlomi > :-) > > -T -- Shlomi Fish https://www.shlomifish.org/ “So, who the hell is Qoheleth?” - https://shlom.in/qoheleth JATFM == “Just answer the fabulous man” Please reply to list if it's a mailing list post - http://shlom.in/reply .