3 ms·
Use them like the first-class vectors they are. You can use _any_ array manipulation on ranges.
by 3JPLW 8y ago
Use them like the first-class vectors they are. You can use _any_ array manipulation on ranges.
- Iwan-Zotow 8y agoplease take a look at my rant below, do not want to write it twice
- 3JPLW 8y agoYour rant comes down to a distaste of +1s in certain situations. My point is that Julia's emphasis on arrays allows you to compose ranges with mathy operations in very powerful ways. You're concerned about composing APIs that take endpoints as arguments — I say you should just accept a range in the first place! Say I have an index `idx` into a very large array and I want to select a symmetric window of `N` indices before and after that index. In Julia: A[idx .+ (-N:N)] In Python: A[idx-N:idx+N+1] Python cannot even index lists by ranges (or other lists). And if I go out of bounds, well, it'll happily just give me back a list of a size I didn't expect. Heck, in Julia I can even easily determine what the OOB behavior should be. It's an error above, but I can easily change it. This, for example, will ensure no indices go out of bounds by repeating the final endpoint as necessary. A[clamp.(idx .+ (-N:N), 1, end)] Sometimes you want to work in terms of the fenceposts, and sometimes by the length of the fence. There certainly are cases where 0-based offsets are nice — or even arbitrary offsets. In those cases we have OffsetArrays.
- dagw 8y agoPython cannot even index lists by ranges (or other lists) True for python lists, but not true for numpy arrays.
- mbauman 8y agoOh, I'm well aware. That gets at my point, though: Julia's ranges are _just_ arrays, and behave like all other arrays in the language.
- dagw 8y agoAgreed. The fact that python has several 'array-like' data structures with subtly different interfaces is not my favorite feature of the language.
- Iwan-Zotow 8y agoIt is not about distaste, though idea about keeping and carrying 1 around is a recipe for bugs. It is all about composability. Could you make proper monoid out of Julia ranges? What would be a unit in that monoid? Could you tell me how empty range looks in Julia (in Python/C++ it looks trivial)? > Python cannot even index lists by ranges (or other lists) sure, Python has deficiencies as well. C++ STL iterators/ranges are made in the same way, and for a good reason - composability first. It described very well in Alex Stepanov book (https://www.amazon.com/Mathematics-Generic-Programming-Alexander-Stepanov/dp/0321942043/ref=sr_1_1?ie=UTF8&qid=1548359366&sr=8-1&keywords=stepanov+generic+programming https://www.amazon.com/Mathematics-Generic-Programming-Alexa...), as well as how it helps when we move to parallel algorithms (http://stepanovpapers.com/p5-austern.pdf http://stepanovpapers.com/p5-austern.pdf). It is even more explicit in upcoming Ranges library in C++20
- 3JPLW 8y agoAn empty range in Julia is `1:0`. So yes, they can form monoids just fine. I highly recommend you work with them _directly_ instead of carrying around the two endpoints separately. For example, you can divide any range into two parts with: split(r) = r[1:end÷2], r[end÷2+1:end] That'll happily recurse and eventually spit out empty ranges given an input like `split(32:56)`. A zealous adherence to one strategy or another will simply blind you to the ways in which the other might be easier at different times.