HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Apply function to array of decimals

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
arrayapplydecimalsfunction

Problem

I am writing some code to transform an array of decimals so that they are discounted over time.

I initially wrote the following code:

private decimal[] deflateArray(decimal[] items, decimal deflateRate)
{
    decimal[] deflatedItems = new decimal[items.Count()];

    decimal factorAdjustment = 1;

    for (int i = 0; i < items.Length; i++)
    {
        deflatedItems[i] = items[i] / factorAdjustment;
        factorAdjustment = factorAdjustment * (1 + deflateRate);
    }

    return deflatedItems;
}


Then I realised that instead of having the factorAdjustment variable, I can just use to the power of:

private decimal[] deflateArray(decimal[] items, decimal deflateRate)
{
    decimal[] deflatedItems = new decimal[items.Count()];

    for (int i = 0; i < items.Length; i++)
    {
        deflatedItems[i] = items[i] / (decimal)Math.Pow((double)(1 + deflateRate), i);
    }

    return deflatedItems;
}


As Math.Pow() only accepts and returns doubles, I think it's easier to create a wrapper function for it:

private decimal PowerOf(decimal x, decimal y)
{
    return (decimal)Math.Pow((double)x, (double)y);
}


and then simplify:

private decimal[] deflateArray(decimal[] items, decimal deflateRate)
{
    decimal[] deflatedItems = new decimal[items.Count()];

    for (int i = 0; i < items.Length; i++)
    {
        deflatedItems[i] = items[i] / PowerOf(1 + deflateRate, i);
    }

    return deflatedItems;
}


Then I realised that I really just want to transform the array and don't need a second array:

private void deflateArray(decimal[] items, decimal deflateRate)
{
    for (int i = 0; i < items.Length; i++)
    {
        items[i] = items[i] / PowerOf(1 + deflateRate, i);
    }
}


and then I can simplify this to:

private void deflateArray(decimal[] items, decimal deflateRate)
{
    for (int i = 0; i < items.Length; i++)
    {
        items[i] /= PowerOf(1 + deflateRate, i);
    }
}


so my final version is:

```
private v

Solution

Arguably a better variant for DeflateArray:

private decimal[] DeflateArray(decimal[] items, decimal deflateRate)
{
    return items
        .Select((item, index) => item /= PowerOf(1 + deflateRate, index))
        .ToArray();
}


It's better in a way it helps avoiding OBOE.

And by the way, another thing you can do is define it as an extension method in a separate static class, and will be able to invoke it as myArrayOfDecimals.DeflateArrayWithRate(1.5m):

public static class DecimalArrayExtensions
{
    public static decimal[] DeflateArrayWithRate(this decimal[] items, decimal deflateRate)
    {
        return items
            .Select((item, index) => item /= PowerOf(1 + deflateRate, index))
            .ToArray();
    }

    private static decimal PowerOf(decimal x, decimal y)
    {
        return (decimal)Math.Pow((double)x, (double)y);
    }
}

Code Snippets

private decimal[] DeflateArray(decimal[] items, decimal deflateRate)
{
    return items
        .Select((item, index) => item /= PowerOf(1 + deflateRate, index))
        .ToArray();
}
public static class DecimalArrayExtensions
{
    public static decimal[] DeflateArrayWithRate(this decimal[] items, decimal deflateRate)
    {
        return items
            .Select((item, index) => item /= PowerOf(1 + deflateRate, index))
            .ToArray();
    }

    private static decimal PowerOf(decimal x, decimal y)
    {
        return (decimal)Math.Pow((double)x, (double)y);
    }
}

Context

StackExchange Code Review Q#162001, answer score: 8

Revisions (0)

No revisions yet.