4 ms·
An example from my field: in financial modeling the typical pattern is to calculate an initial cash flow followed by further projected cash flows at fixed inter
by tfehring 6y ago
An example from my field: in financial modeling the typical pattern is to calculate an initial cash flow followed by further projected cash flows at fixed intervals (e.g., monthly). The natural way to store a series of cash flows (besides as a column in Excel) is just as an array, and if that array is 0-indexed, the index represents each cash flow's offset from the initial cash flow. This is both more natural and more convenient for lots of calculations - e.g., when calculating the net present value the number of discounting periods for each cash flow is just its index in the array.
I haven't encountered a code base that uses non-default array indices, but it sounds like a serious anti-pattern, especially if you're just changing from 1-based to 0-based.
- michaelcampbell 6y ago> I haven't encountered a code base that uses non-default array indices, If you're referring to <parent>'s mention of Ada with arbitrary ranges of indices, that's less about thinking of them as array indices and more of "a range of indices that make sense for your domain". Thinking about your problem in your problem's space rather than "how would my computer think of this"; making the map more like the territory, as it were.
- Jtsummers 6y agoSomehow I missed this comment. But as michaelcampbell wrote, in Ada you'd select the range that made sense for your application. 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.
- mcabbott 6y agoThe 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.
- Jtsummers 6y agoThanks. 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).