4 ms·
I 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
by newen 8y ago
I 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.