5 ms·
I'm using Julia (because of the hype) to prototype out some numerical optimization stuff. There is a million functions for reshaping multidimensional arrays. Th
by legerdemain 6y ago
I'm using Julia (because of the hype) to prototype out some numerical optimization stuff. There is a million functions for reshaping multidimensional arrays. The syntax is uncannily like Matlab: retrieving the last element of an array with `[end]`, indexing into a collection with an array of booleans, element-wise versions of operators prepended with dot, etc.
However, I keep running into niggling corner cases that kind of make Julia's promise of a powerful, extensible, yet intuitive type system less convincing.
ME: I want to write a custom getproperty() for Tuple!
JULIA: No.
ME: I want to broadcast over the fields of a NamedTuple!
JULIA: Not allowed.
ME: I want to get a view, not copy, with `@view M[m:n, r:s]`, but also get the ability to specify a default for out-of-range indices, like `get()` allows.
JULIA: I'm afraid I can't let you do that.
- oivey 6y agoFor the last thing, is this what you want? https://github.com/JuliaArrays/PaddedViews.jl https://github.com/JuliaArrays/PaddedViews.jl There may be other packages or methods for doing the other things you want. I’d think that broadcasting over a NamedTuple would iterate over the key => value pairs, but I haven’t tried it.
- legerdemain 6y agoThanks for the good find!
- oivey 6y agoLooks like vectorizing over NamedTuples is explicitly disallowed. Probably you can still vectorize things over the keys and values separately, along with some helper functions, but it is a bit annoying. Looks like the reason was due to questions on whether iteration should be over values or pairs.
- StefanKarpinski 6y agoIndeed, that is precisely the decision that must be made. Neither one is obviously correct. And once a decision is made and goes into a release, it cannot be unmade — we all have to live with it forever.
- newen 6y agoIt's not generally recommended but you can do a custom getproperty() for Tuple. julia> function Base.getproperty(x::Tuple, f::Symbol) if f == :a return x[1] elseif f == :b return x[2] else return x[3] end end julia> (3,4,5).a 3 Edit: Okay, why am I getting randomly downvoted here?
- StefanKarpinski 6y agoAs sibling posts have pointed out, you can do all of those things: 1. You can trivially write a `getproperty` method for a tuple. It is considered to be type piracy and thus runs the risk of colliding with someone else's definition, but the language absolutely lets you do it. 2. You can broadcast over the fields of a `NamedTuple` by defining appropriate methods. Again, it's type piracy, so take that into consideration, but the language lets you do this easily as well. 3. The https://github.com/JuliaArrays/PaddedViews.jl https://github.com/JuliaArrays/PaddedViews.jl package implements exactly what you're saying Julia won't let you do. If anything, Julia errs on the side of allowing you to do too many things! There are very few things the language really won't let you do.
- legerdemain 6y agoI meant literally this: julia> (x = 1, y = 2) .+ (x = 1, y = 2) ERROR: ArgumentError: broadcasting over dictionaries and `NamedTuple`s is reserved
- eigenspace 6y agoStefan's point was that you are free to commit type piracy and make this do whatever you want julia> Base.broadcasted(f, x::NamedTuple, y::NamedTuple) = "my custom method!" julia> (x=1, y=2) .+ (x=1, y=2) "my custom method!"
- StefanKarpinski 6y agoAnd as the phrase "reserved" in the error message indicates, it will likely be given a meaning once all the ramifications of doing so are worked out and the best choice of meaning is decided upon. If you're impatient and don't want to wait for that, define it to do what you want. Your code won't even break when it is given an official behavior since your method will overwrite the built-in one.
- legerdemain 6y agoBut this is exactly the kind of thing I called a bothersome corner case. Needing to redefine a global function in order to use fairly intuitive behavior is not great developer experience.