Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I copy a function with its *current* state?

Raku's state declarator can be used to give a subroutine or other block its own local state that persists across multiple invocations of the function:

sub f { state $n++ }
say f; # OUTPUT: «0»
say f; # OUTPUT: «1»
say f; # OUTPUT: «2»

I'm aware of two ways to "copy" a function that has internal state: First, I can assign it to a new &-sigiled variable with code like my &f1 = &f. This results in &f1 effectively being an alias to &f and means that they share state – anything that alters the state of &f or &f1 will also change the other function's state.

Second, I can make a clone of &f with code like my &f2 = &f.clone. This will create an independent function with state that is initialized to any default values in &f (that is, with $n being Any for the code above).

However, I'm hopping for a third way to copy &f that (like option 1) would preserve the current value of &f's state but that (like option 2) would make that state independent from &f's. In other words, I'd like to be able to use the commented-out lines below:

sub f { state $n++ }
say f; # OUTPUT: «0»
say f; # OUTPUT: «1»
say f; # OUTPUT: «2»
my &f1 = &f;
my &f2 = &f.clone;
# my &f3 = ???;
say f; # OUTPUT: «3»
say f; # OUTPUT: «4»
say f1; # OUTPUT: «5»
say f2; # OUTPUT: «0»
# say f3; # (desired) OUTPUT: «3»

Is there any way to save &f's state like that (maybe with something fancy with wrap or similar that I can't think of)? Or am I just asking to do something that isn't currently possible?

like image 473
codesections Avatar asked Oct 05 '21 18:10

codesections


People also ask

How do you copy a function?

Select the cell that contains the formula you want to move. Click Home > Cut (or press Ctrl + X). Select the cell you want the formula to be in, and then click Paste (or press Ctrl + V). Verify that the cell references are still what you want.

How to use copy () function in C++?

To use copy () function, we need to include <bits/stdc+.h> or header file. The syntax of std::copy () is as below: It copies all the elements pointed by first and last. first element is included in the output but last is not. output is the start position of the final result iterator.

How do you copy a formula without changing the cell reference?

Click Home> Find & Select >Replace… or press shortcuts CTRL+H, but this time enter “#” in the Find what box, and “=” in the Replace with box, and click Replace All. Then the formulas have been copied and pasted into another location without changing the cell references.

What will happen if I copy the function group?

if i copy the function group , it will be similar to standard. Help to improve this answer by adding a comment. If you have a different answer for this question, then please use the Your Answer form at the bottom of the page instead.

How to check if a Dataframe has been copied or not?

1 Step 1) Let us first make a dummy data frame, which we will use for our illustration 2 Step 2) Assign that dataframe object to a variable 3 Step 3) Make changes in the original dataframe to see if there is any difference in copied variable More ...


Video Answer


1 Answers

No, there's not a way - not even if one were willing to write a module that depends on unsupported Rakudo internals.

State variables are currently handled all the way down in the runtime (typically, MoarVM), where they are attached to a bytecode handle. Cloning a Block in turn clones the underlying bytecode handle, which explicitly does not clone the state variables.

like image 131
Jonathan Worthington Avatar answered Nov 12 '22 05:11

Jonathan Worthington