I found this code recently. Does anybody know what it’s supposed to do?
(zip()...,)
I found this code recently. Does anybody know what it’s supposed to do?
(zip()...,)
Wow, interesting find. For folks who try to run this code: Ctrl-C
is your friend.
Good to know that this behavior is desired!
I spent some hours frustratedly trying to figure out why my program was just hanging, to find that it was because of this.
This is what I was doing. I swear I copypasted it from somewhere, but I can’t find it now. It converts Dict
s to NamedTuple
s:
mydict = Dict(:a=>1, :b=>2)
nt = (; zip(zip(mydict...)...)...)
Works fine until mydict
is empty. That part of the code was stable for a while and wasn’t part of the execution chain I was actively working on (it was part of a custom Base.show
), so it took a while for me to suspect it. Of course, in other places I used the more terse and reasonable:
nt = (; mydict...)
but for show
performance didn’t matter and it worked fine so I didn’t revisit it, until I ran into this edge case long after originally writing it.
It seems like a fun easter egg, something to give to people you don’t like so their programs nuke when they least expect it.
I disagree and clearly at least one other person disagree @stevengj
I probably deserved to be in debugging hell for writing such horrid code to begin with. Some form of cosmic coding karma.
I was thinking it could make a nice interview question to stump new hires. Is crash prevention really worth eliminating a possible interview question?
You can just use the NamedTuple
constructor:
julia> NamedTuple(Dict(:a => 1, :b => 2))
(a = 1, b = 2)
julia> NamedTuple(Dict())
NamedTuple()
Wow, it looks like I even knew this already but I forgot! Funny how that works.
Is this considered idiomatic?
for zip! in zip() zip, zip, zip, zip!, zip!, zip! end
or is this better:
for (zip!, zip!!) in zip(zip(), zip(zip())) zip, zip, zip, zip!, zip!, zip!, zip!!, zip!!, zip!! end
thinking of putting it into the catch
branch of try...catch
statements.