by Jtsummers · 5y ago · 0 comments
So if your application makes sense with 0-based (time series in your example) then you can do it:
Cash_Flows : array (0..max_time) of Double;
It may even make sense to go negative for some things. For instance: Object_at_Altitude : array (-100..1000) of Object;
Or characters: Selected_Answers : array (range 'a'..'z') of Boolean;
Or a type that you've created: type Color is Red, Green, Blue, Yellow, Orange;
Histogram_of_Colors : array (Color'Range) of Natural;
But a particularly nice thing is that arrays in Ada, unlike C, don't lose their size, so you can always do: for I in Cash_Flows'Range loop
Some_Function(Cash_Flows(I));
end loop;
So really the index never even has to be directly touched or known after creation. 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.