pprzetacznik / IElixir

Jupyter's kernel for Elixir programming language
Apache License 2.0
357 stars 44 forks source link

Add ans and out for every output #42

Closed clouds56 closed 5 years ago

clouds56 commented 5 years ago

as the ipython _ and _1, _2, etc.

coveralls commented 5 years ago

Coverage Status

Coverage decreased (-0.3%) to 53.968% when pulling c8a8ed8a6f17778896139c9ed8a71bd71e03ba24 on clouds56:patch-1 into abeeb2f7ab071b78919ed90d85c6c544267b66e4 on pprzetacznik:master.

pprzetacznik commented 5 years ago

Could you write more about the reason why we need ans and out for every output?

clouds56 commented 5 years ago

Not every time before running a function you would know the result is useful, so most of the time you would not give an name to the result in the quick prototype. Of course you could prepend out[execution_count] = ans = to your actual code every time, but it could be fantastic if the kernel have done it for you. In fact, in ipython there's already _ (means ans here) and _1, _2, ... (means out[1], out[2] here), and original in mathematica, there's also Ans and Out[1], Out[2]. For example, you've got a large matrix

m = Matrex.magic(1000)

and you would like to know if the matrix is reversible, you would likely to run

determinant(m)

to check if the determinant is 0, now say you would like to reverse the matrix and without knowledge, you would not likely to know that the determinant in the last execution of the matrix is useful, but never mind, we could access it in ans

Matrex.divide(adjugate(m), ans)

This could avoid lots of cost to recalculate the determinant.

Another example could be spawn

spawn(&some_heavy_function/0) # out[1]

after some execution you found something wrong and would like to kill the pid, out would save you

Process.exit(out[1], :kill)

The only concern to me is that should we use ans or _ans or just __ (double underscore) as the name? And should out be a map or bind it to multiple names out1, out2, etc.?

pprzetacznik commented 5 years ago

Commit has been merged as https://github.com/pprzetacznik/IElixir/commit/5772ae9beb0c94102781904230c7b3368167b992 to the master. Unfortunately, github hasn't shown this ;_; Thanks for the input!