-
-
Notifications
You must be signed in to change notification settings - Fork 294
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
Assign a variable more than once #276
Comments
This restriction is exactly what makes reactivity possible! It is common for programming environments to impose/encourage a new restriction to make it easier for computers to help the programmer, and for other humans to reason about the code. For example, Dijkstra talked about how removing the In the case of a reactive notebook, allowing multiple definitions would mean that there is no longer an obvious way for the computer to figure out in which order to run your cells. For example, if you write:
then what should the output be? |
But perhaps you are trying to do one of the following things: Run the same code multiple times with different inputsIn this case, you should write functions, and call the function multiple times, with different inputs. Modify a 'state variable' accross multiple cellsBecause Jupyter makes this possible, it is common for Jupyter notebooks to use this technique. In Pluto, however, it is not recommended to do so in Pluto. It makes reactivity confusing, and it makes your notebook harder to understand. You can probably formulate your problem in a different way, such that it does not require a mutable state. Have a look here: |
Hmmm... since one has the ability to "execute"
Since one could execute a code cell in Pluto, whichever cell gets executed is decided by the user |
But now the history of which cells were executed when becomes part of the notebook state! This is exactly what reactivity wants to avoid. When you send someone a notebook, and they open your notebook, they should see exactly the same as you. |
You're right on this one |
Thats an awesome advice!I'll try applying it in the future!Thanks😄 |
Your reactive idea is absolutely awesome! But the conflicts you mentioned could be resolved if cells are executed in the order they're written. And, to my understanding, the possibility to write code cells in an arbitrary order is exactly an example of the behaviour that should be discouraged. And if there is a restriction for the order, then multiple assignments of the same variable is not a problem: only the last assignment above the given cell matters for it. If I remember right, this idea is implemented in the Datalore notebooks. Are you sure that having unordered cells is more important than multiple assignments? :)
One more reason for re-assigning variables is usage of temporary values with short names. Let's say, I want to show and save multiple plots. It can be done with the following code: using Plots
x_vals = rand(100)
begin
plt = plot(x_vals)
savefig(plt, "line.png")
plt
end
begin
plt = scatter(x_vals)
savefig(plt, "scatter.png")
plt
end Of course, there are ways to write it without the |
For local variables there are two solutions: let
plt = scatter(x_vals)
savefig(plt, "scatter.png")
plt
end and begin
local plt = scatter(x_vals)
savefig(plt, "scatter.png")
plt
end Both do not create a global variable called About your suggested alternative: it is likely a matter of taste, but to me, this sounds like a step backwards. What I like about the paradigm is that it is declarative instead of imperative (at the notebook level), and that it encourages concepts from functional programming like immutability. These are limitations, but limitations that are known to make computer code easier to reason about. In the case of a notebook, it means that you can understand single cells without knowing about the structure (i.e. state) of the notebook. |
Specific examples like the one you posted @VPetukhov are really helpful for us to learn what the common difficulties with reactivity are! So feel free to send some more :) |
Thanks so much for the answer, @fonsp! The local variables solution should actually work! And your comment about declarative paradigm helped a lot to perceive the idea. Maybe it worth adding to the readme :) And I agree that this idea is quite extraordinary and we shouldn't step backwards. |
Jump to definition is definitely a must! I've added an issue #304 Oh interesting idea. The way I thought about it - the author gets to decide what order to put their cells in - if you want linear order, then you just write your notebook linearly. But what I found interesting is that you can move "helper functions" to the end of your notebook, if you (the author) think that their implementation is not relevant to understanding the code. Do you think the "linear switch" would be useful for your own notebooks or when viewing notebooks by others? |
Another exampleBefore begin
x = 123
apple = sqrt(x)
end begin
x = 123
orange = x ^ 2
end This is not legal, because both cells define a global After apple = let
x = 123
sqrt(x)
end orange = let
x = 123
x ^ 2
end
In Julia, most control blocks ( So why does Pluto suggest a |
This was good discussion and very helpful. I would recommend that Pluto let users know about using |
From @ChetanVardhan:
The fact that you can't assign a variable in Pluto more than once, kinda makes all the Jupyter notebooks non working when converted. Any chance that assigning more than once would be allowed from top to bottom, like normal Julia code?
Originally posted by @ChetanVardhan in #182 (comment)
The text was updated successfully, but these errors were encountered: