While investigating Protobuf I shot from the hip and wrote that: to model money, one should use BigDecimal.
That’s the conventional wisdom and it is correct - in a lot of cases.
It’s fine when you deal with one single currency and you are most of all concerned with the precision of financial calculations.
But what are some other options? let’s find out.
The iterate
function create a lazy, infinitive sequence based on function calls. The iterate
function takes a function and an initial value as arguments. The first element in the sequence is the initial value, next the function is invoked with the previous element as argument and this continues for each new element. Suppose we have a function #(+ 2 %)
that adds 2
to the input argument. Then if we use this function with iterate
and start with value 1
the first elements of the sequence will be 1
, (+ 2 1)
, (+ 2 3)
, (+ 2 5)
. So first element is the initial value, next element is the invocation of the function with input argument 1
. The result of this function is 3
, which is then the input for the function to calculate the next element and so on.
In the following example code we use iterate
in different scenario’s:
Continue reading →