Re:Quick Overview of Julia language [pdf]
by mcabbott on Monday January 11, 2021 @05:26AM
(#25727027)
The Julia translations of these might look as follows:
using OffsetArrays
cash_flows = zeros(0:max_time)
altitude = zeros(Bool, -100:100)
for I in eachindex(cash_flows)
some_function(cash_flows[I])
end
# or in this case avoiding indexing:
for cf in cash_flows
some_function(cf)
end
foreach(some_function, cash_flows)
OffsetArrays is not quite a standard library but it's close. A lot of library code will work like this, calling `eachindex` or `axes` so as to be indifferent to how the arrays are indexed, and to pass this behaviour through to outputs as appropriate.
Re:Quick Overview of Julia language [pdf]
by Jtsummers on Monday January 11, 2021 @10:55AM
(#25730583)
Thanks. I'll be taking a look a that for my code. I probably won't use this extensively, but it is a feature I have found useful in Ada (though I'm only a hobbyist, I gave up on convincing work to use it). I'm still a novice with Julia, but I'm doing some math heavy code and started exploring it for its relative ease of use (REPL, fast execution after compilation, ergonomics-focused language design).
So if your application makes sense with 0-based (time series in your example) then you can do it:
It may even make sense to go negative for some things. For instance: Or characters: Or a type that you've created: But a particularly nice thing is that arrays in Ada, unlike C, don't lose their size, so you can always do: So really the index never even has to be directly touched or known after creation.