JuliaLang/julia — pull request #9287 by timholy (2014-12-09 23:32:21+00:00)
Title: Implement arithmetic and `min` on cartesian indexes

This makes our new iterators quite a bit more capable, and allows a wider range of algorithms to be written. In particular, arithmetic with the implicit zeros trick enables composition of iterators over different coordinates. `min` allows one to trivially implement reductions and broadcasting:
```julia
julia> function bcast(colvec, rowvec)
m1, n1 = size(colvec,1), size(colvec,2)
m2, n2 = size(rowvec,1), size(rowvec,2)
M, N = max(m1,m2), max(n1,n2)
out = Array(Int, M, N)
for I in IndexIterator((M,N))
out[I] = colvec[min(I, CartesianIndex((m1,n1)))] * rowvec[min(I, CartesianIndex((m2,n2)))]
end
out
end
bcast (generic function with 1 method)

julia> bcast(1:4, (1:3)')
4x3 Array{Int64,2}:
1 2 3
2 4 6
3 6 9
4 8 12
```

CC @Jutho.