How to resolve the algorithm Accumulator factory step by step in the Fantom programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Accumulator factory step by step in the Fantom programming language

Table of Contents

Problem Statement

A problem posed by Paul Graham is that of creating a function that takes a single (numeric) argument and which returns another function that is an accumulator. The returned accumulator function in turn also takes a single numeric argument, and returns the sum of all the numeric values passed in so far to that accumulator (including the initial value passed when the accumulator was created).

The detailed rules are at http://paulgraham.com/accgensub.html and are reproduced here for simplicity (with additions in small italic text).

Create a function that implements the described rules.

It need not handle any special error cases not described above. The simplest way to implement the task as described is typically to use a closure, providing the language supports them. Where it is not possible to hold exactly to the constraints above, describe the deviations.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Accumulator factory step by step in the Fantom programming language

Source code in the fantom programming language

class AccumulatorFactory
{
  static |Num -> Num| accumulator (Num sum)
  {
    return |Num a -> Num| 
    { // switch on type of sum
      if (sum is Int)
      { // and then type of a
        if (a is Int)
          return sum = sum->plus(a)
        else if (a is Float)
          return sum = sum->plusFloat(a)
        else
          return sum = sum->plusDecimal(a)
      }
      else if (sum is Float)
      {
        if (a is Int)
          return sum = sum->plusInt(a)
        else if (a is Float)
          return sum = sum->plus(a)
        else
          return sum = sum->plusDecimal(a)
      }
      else // if (sum is Decimal)
      {
        if (a is Int)
          return sum = sum->plusInt(a)
        else if (a is Float)
          return sum = sum->plusFloat(a)
        else
          return sum = sum->plus(a)
      }
    }
  } 

  public static Void main () 
  {
    x := accumulator (3.1)
    y := accumulator (3f)
    echo (x(5))              // the Decimal sum combines with an Int
    echo (x(2))
    echo (y(5.1))            // the Float sum combines with a Decimal

    x = accumulator (1)
    x (5)
    accumulator (3)
    echo (x(2.3))          // the Int sum is now a Decimal
  }
}

  

You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Maxima programming language
You may also check:How to resolve the algorithm Averages/Median step by step in the FutureBasic programming language
You may also check:How to resolve the algorithm Set step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Break OO privacy step by step in the D programming language
You may also check:How to resolve the algorithm Active object step by step in the SuperCollider programming language