I guess the trouble is where you start trying to reference which gate to open with strings, but open("front")
opens a file instead
Figured Iâd add on to this thread because I had a similar thought. Mostly posting for posterity, as I assume likelihood of doing something like this is low.
Comparing the readability of these ways of calculating a result and adding it to a vector (assuming that it canât be neatly done as a loop or generator):
a = []
push!(a, lengthyfunction1(lotsofargs))
push!(a, lengthyfunction2(lotsofargs))
a = []
b1 = lengthyfunction1(lotsofargs)
push!(a, b1)
b2 = lengthyfunction2(lotsofargs)
push!(a, b2)
a = Vector{Any}(undef, 2)
a[1] = lengthyfunction1(lotsofargs)
a[2] = lengthyfunction2(lotsofargs)
a = []
pusha! = Base.Fix1(push!, a)
lengthyfunction1(lotsofargs) |> pusha!
lengthyfunction2(lotsofargs) |> pusha!
a = []
â(x, y) = push!(x, y)
a â lengthyfunction1(lotsofargs)
a â lengthyfunction2(lotsofargs)
(or †as noted upthread; just couldnât find it in the latex lookup, so it might not be accessible enough)
To me the last option is definitely the cleanest. And it would be nice for a very common operation to have a clean operator. I hear the critique that it may be unclear that it is mutating though.
I prefer to define
module std
export <<, endl, cout
<<(io::IO, x) = (print(io, x); io)
const endl = '\n'
const cout = stdout
end
so that I can do:
julia> using .std; import .std: <<
julia> cout << "Hello world!" << endl;
Hello world!
which is so much cleaner and more natural than println
.
[In case anyone misreads it, @stevengj does not do this. Itâs a joke about C++âs notoriously confusing abuse of bitshift operators for input and output.]
Honestly, âenough power to make your code look and run like C++, and enough common sense to only choose the secondâ would be a pretty good moto for Julia.
\mapsfrom
is valid in julia-REPL
touché
Though @stevengjâs example is fine from the standpoint of not calling different actions the same thing, as his <<
is in a different name space.
I assume you wanted to do
<<(io::IO, x) = (print(io, x); io)
so that
const cerr = stderr
cerr << "Hello world!" << endl;
works as expected
Whoops, fixed. Forgetting to use the io
argument in custom show
-like methods seems to be a pretty common mistake, unfortunately.