3 ms·
1-indexed ranges can still be composable if we define them as open on the right at N+1 (which is how I imagine one would want to define things in their implemen
by hhmc 8y ago
1-indexed ranges can still be composable if we define them as open on the right at N+1 (which is how I imagine one would want to define things in their implementation).
[1...N] = [1...N+1) = [1...K+1) + [K+1..N+1)
So the real loss is your point (2) - which, I agree, makes the implementation much less elegant and simple.
- Iwan-Zotow 8y ago> 1-indexed ranges can still be composable you could make them work, but improper abstraction leaks out in so many fugly ways. F.e. in Python you could compose functions, not only ranges. What do you pass in? Simple, [0, len(a)). What you get out? Simple, len(a). So you could operate on ranges composing function calls like in FP. Even works for unknown beforehand sequences/streams, just count along the sequence how many events you processed, and return it as length, and it could go into composition function. With Julia you have to think what is passed and what is returned. Shall I always return len(a)? Or maybe len(a)+1? What to return in case of streaming events? Sometimes len(a) and sometimes len(a)+1 with tons of comments and warnings? It is not a good way to deal with all that and not a good way to build API. Could be done and probably was already done, sure. BUt simplicity, elegance and composability is missing in the base design.
- newen 8y agoI don't buy it. Inclusive ranges are much more intuitive than left side inclusive and right side exclusive ranges. And they compose just fine; you just have to add a plus one at the right places. E.g. function mean(a, l, r) s = 0 N = r-l+1 for k in l:r s += a[k] end s/N, N end function mean2(a) N = length(a) lr1 = (1,div(N,2)) lr2 = (div(N,2)+1,N) v1, N1 = mean(a, lr1...) v2, N2 = mean(a, lr2...) N = N1+N2 (v1 * N1 + v2 * N2) / N, N end function compose(M1, M2) v1, N1 = M1 v2, N2 = M2 N = N1+N2 (v1 * N1 + v2 * N2) / N end function mean3(a) N = length(a) lr1 = (1,div(N,2)) lr2 = (div(N,2)+1,N) compose(mean(a, lr1...), mean(a, lr2...)) end You have to think about what is passed in and out anyway. Arguments of elegance etc. are most of the time personal preferences and very biased.
- Iwan-Zotow 8y ago> Arguments of elegance etc. are most of the time personal preferences and very biased. No, they are not. I'm talking about ability to build (and build upon) underlying algebraic structure. ok, could you make a monoid out of your/Julia closed ranges? What would be a unit in this monoid? How would you make a null/empty range?
- newen 8y agoEmpty range is 1:0 or a+1:a or a:a-1 for any integer a. Closed ranges (for integers) are equivalent to open ranges since [a,b] is the same as [a,b+1). You just put the plus ones in the right places.
- deleted 8y ago[deleted]
- deleted 8y ago[deleted]