Simpler identifier for last result than ans

Hello,
With ans I can refer to the last result in Julia.
In ipython you can do the same with _ (just an underscore).

Is there a way to use a different identifier than ans to refer
to the last result? Preferably using only one letter or symbol?

Uwe

1 Like

There is not. It is set here:

https://github.com/JuliaLang/julia/blob/f6049d6d25d720bc90d82aadae6be98bc948ef24/stdlib/REPL/src/REPL.jl#L89

1 Like

I am curious why you need this. In the last year, I have use ans less than 10 times. Usually when I forget to assign

some_complicated_calculation_that_takes_10_minutes(input)

to something, I do an

result_that_justified_a_coffee_break = ans

In the moment I am using ipython as kind of scientific calculator. Every day I do a lot of small calculations with it, and using _ for the last result is very practical for me. I would prefer to use julia as calculator, for example because it has functions like sind and cosd which come in handy, and also to stick to one language.

So perhaps I can hack the source code to get what I want?

But there should be a better way to make the REPL more adaptable.

1 Like

I would just use an IDE where you write the code in a window and send it to a REPL in a simple manner (a couple of keystrokes at most). This should be no additional effort compared to a standard workflow, and should be a lot more organized and transparent.

I am very skeptical of the “calculator” workflow even for simple calculations. I think that assigning a single-letter variable is not a great cost. YMMV.

Well, an IDE is very heavy weight, compared to a REPL. IPython is very adaptable, using a configuration file. This should also be possible with the Julia REPL. We want to be superior to Python, or not?

1 Like

I don’t know what the concern is here, startup time? I don’t know other IDEs, but an Emacs set up for Julia can start up in about 1s on my computer.

I don’t quite understand what it means in this context. For me, this per se would not justify porting features directly from Python. In some cases, not doing something can be understood as an improvement.

Its not so much about the startup time, but about the screen usage. A scientific calculator is used aside of a text document, web site or spread sheet. I do not know emacs, I only know vscode and atom. And they need the whole screen.

I am talking about REPL features, this means I compare IPython with the Julia REPL. Adding more features from IPython to the Julia REPL would be very valuable.

AFAICT, you just want to use as close a syntax you use in ipython as possible, this is in general never going to happen.

For customizing the REPL, I believe this is certainly doable using a OhMyREPL level of hack though I don’t think you should do it.

Making the REPL customizable is nice, but that doesn’t necessarily include this feature since what you are asking for is much more than customizing the display. You are asking for changing of the meaning of valid code, even if it’s only for the REPL. That comes at the cost of not being able to understand/use other’s code. In another word, it will only not cause any problem if you never copy and paste your REPL anywhere and you never paste anything into your REPL, otherwise, you are bound to get something unexpected sooner or later.

Yes, but that doesn’t mean changing the julia (REPL) syntax to match python’s.

Edit: Also, as for _ and ans. I don’t think _ is too much better. At least on a normal (english) keyboard, _ requires pressing down two keys simultaneously (and typing it on a phone is even harder) while ans takes three key sequantial presses which is a wash IMO…

3 Likes

I do not agree. The point is not to mimic the behaviour of IPython as close as possible, but to make to possible to adapt to the easy of use of IPython in contrast to the complicated way Matlab handles this issue.
I do not understand why you insist that the complicated approach of Matlab (using ans) is the only valid way for Julia.

To be more precise:
_ gives you the last result in IPython
___ gives you the last-last result in IPython

How can you access this value in Julia?

This is just a feature request. And you say we shall never add features to the Julia REPL?

1 Like

I think it would only require 5 - 10 short lines of code to do this.
Here’s a start

while true
   print("> ")
   println(global ans = eval(Meta.parse(readline())))
end

Instead of ans, you can use any legal variable name. (A slight complication is that all-underscore variable names are not allowed in Julia, so one would have to do some substitutions in the parsed expression to get the exact IPython syntax.)

1 Like
  1. I do not agree ans is more complicated, not by much anyway, as explained above. I have also had one reply on this so I’m hardly insisting. I also give the reason for all my statements so if you don’t understand please quote which reasoning you don’t understand.
  2. I did not say ans is the only valid way, only it is not something that should be “adaptive”, which I interpreted as configurable/customizable. In another word, as I said above already, it should not be something that you should configure yourself, the reason for which is also included above.

Correct (even though I didn’t say anything about this above), this should not be added since it conflicts with possible other uses of the syntax that have not been finalized. Other than that, changing to this isn’t as much of a big deal as long as everyone get the change. It’ll be breaking and as I said, I personally don’t think it’s better than ans.

This is a completely different feature that you didn’t even mentioned before so I have never said anything about adding such a feature.

Such feature, or even the more general In/Out array could be added, with the caveat of memory consumption for the array version. Still, it affects code execution on valid syntax so it’s not something that you should easily configure on your own. If you mean “adaptive” as in changing the REPL to implement this it is certainly something that’s easy to do, picking the name will likely be the hard part.

This will let you use a symbol instead of ans:

julia> using ReplMaker, MacroTools

julia> last_key(ex) = MacroTools.postwalk(x -> x==:% ? :ans : x, Meta.parse(ex));

julia> initrepl(last_key; prompt_text="Julia> ", prompt_color=:red, start_key='%', mode_name="last-key")
REPL mode last-key initialized. Press % to enter and backspace to exit.

Julia> 1+2
3

Julia> [i^2 for i=1:%]
3-element Array{Int64,1}:
 1
 4
 9

Whether you can easily make a repl mode which saves older output I don’t know, certainly I can’t in 5 minutes. But if you find a way, others may find that useful.

2 Likes

Would it be possible or advisable to make some non-valid identifier contain the last, or next-to-last etc. value, for use in the REPL? Such as 1_, 2_, etc. or something starting with #? I’m just thinking a bit freely, grasping for something that would not change the meaning of valid code.

2 Likes