Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Allow state-declared variables to be instantiated/initialized to non-zero signed integers #458

Open
jubilatious1 opened this issue Dec 22, 2024 · 3 comments
Labels
language Changes to the Raku Programming Language

Comments

@jubilatious1
Copy link

REFERENCE:

https://docs.raku.org/syntax/state

Presently, state-declared variables can be instantiated within blocks and -ne / -pe one-liners. They provide a way of initializing a variable within a block only once, for easier readability, etc. Here's a typical usage:

Count number of lines with length condition

~$ raku -ne 'state $i; $i++ if .chars <= 300; END say $i // 0;' file.txt

REQUEST:

What would be really nice would being able to initialize the state-declared value to a signed integer. Let's say you want to count the days remaining until the end of the month. You instantiate a state-declared and initialize it to the total number of days in the month (e.g. state $December = 31, and then $December-- until New Year's Eve:

~$ raku -ne 'state $Dec = 31; 
             $Dec--; 
             say "Need $Dec more Advent Calendar articles!"; 
             last if $Dec == 0;'  ~/file_with_advent_calendar_titles.txt

Of course, the above doesn't work and all lines get read to the bitter end, regardless of the $Dec counter. This is because presently the $Dec state variable is initialized to 0 zero. To get the code to work I need to write last if $Dec == -31;. This means the the output looks odd (and also the code, if you've initialized the variable to state $Dec = 31):

~$ raku -ne 'state $Dec= 31; $Dec--; say "Need $Dec more Advent Calendar articles!"; last if $Dec == -31;' ~/file_with_advent_calendar_titles.txt
Need -1 more Advent Calendar articles!
Need -2 more Advent Calendar articles!
Need -3 more Advent Calendar articles!
Need -4 more Advent Calendar articles!
Need -5 more Advent Calendar articles!
Need -6 more Advent Calendar articles!
Need -7 more Advent Calendar articles!
Need -8 more Advent Calendar articles!
Need -9 more Advent Calendar articles!
Need -10 more Advent Calendar articles!
Need -11 more Advent Calendar articles!
Need -12 more Advent Calendar articles!
Need -13 more Advent Calendar articles!
Need -14 more Advent Calendar articles!
Need -15 more Advent Calendar articles!
Need -16 more Advent Calendar articles!
Need -17 more Advent Calendar articles!
Need -18 more Advent Calendar articles!
Need -19 more Advent Calendar articles!
Need -20 more Advent Calendar articles!
Need -21 more Advent Calendar articles!
Need -22 more Advent Calendar articles!
Need -23 more Advent Calendar articles!
Need -24 more Advent Calendar articles!
Need -25 more Advent Calendar articles!
Need -26 more Advent Calendar articles!
Need -27 more Advent Calendar articles!
Need -28 more Advent Calendar articles!
Need -29 more Advent Calendar articles!
Need -30 more Advent Calendar articles!
Need -31 more Advent Calendar articles!

I hope this Feature Request is adequately described. Thank you for your consideration.

@jubilatious1 jubilatious1 added the language Changes to the Raku Programming Language label Dec 22, 2024
@doomvox
Copy link

doomvox commented Dec 22, 2024

Definitely seems like a bug to me. For an input file with several lines, this works:

 raku -ne 'BEGIN my $c = 10; $c--; say $c;'  dat/alpha.data

You get a countdown, 10 9 8 7 6.

But using state doesn't behave as expected:

raku -ne 'state $c = 10; $c--; say $c;'  dat/alpha.data
-1
-2
-3
-4
-5

@raiph
Copy link

raiph commented Dec 22, 2024

Hi @jubilatious1! Happy holidays. :)

This is a dupe of a previously reported bug.

So:

  • Please close this issue.

  • This issue's title suggests you did not realize that state variables can be initialized. That in turn suggests you didn't notice or understand this example code in the user doc you linked. Please consider filing a corresponding PR or doc bug if you think it should be clearer.

  • If you read the issue I linked you'll see Lizmat++ has added a "Will be addressed in RakuAST" label. :)

  • It might make sense for a comment to be added to the existing linked issue showing the -ne case, preferably golfed, eg raku -ne 'state $foo = 42; say $foo'. I say it might make sense because it's possible it's a significantly different case than the other ones listed. That said, it probably isn't. I'll let you make the call on whether to add a comment.

  • To save yourself time in the future (others' too) please remember to always spend several minutes searching for existing related issues before filing a new one. I found the one I linked in a few seconds from a search of the rakudo issues queue for 'state'. Doing a search will always quickly yield one of four very useful results: you can't find a matching issue, and can say so in the new issue you're about to file; OR there's already a filed issue so you don't need to file one yourself; OR you still do need to file a new one but can reference one or more existing ones to keep them linked together and/or improve your issue and/or save time (yours and others') duplicating what they cover; OR the thing to do is add a new comment to one or more existing issues rather than opening a new one.

@jubilatious1
Copy link
Author

jubilatious1 commented Dec 23, 2024

Hi @raiph and Happy Holidays to you as well.

I did a search for state issues but probably not as thorough as required. Possibly I misread the title of the #4329 issue you referenced as 'State this-or-that' not realizing it was a relevant (prior) issue.

Regardless, I note with interest @raydiak shows -e one-liners having state vars initialized to strings.

This problem-solving issue demonstrates the usefulness of -ne (and presumably -pe) one-liners initialized to integers. My specific request is that within -ne/-pe one-liners (which presumably run a block/loop) state vars be initialized to non-zero signed integers.

I'll review the Issues you reference. Thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
language Changes to the Raku Programming Language
Projects
None yet
Development

No branches or pull requests

3 participants