Σάββατο 2 Νοεμβρίου 2024

New Accumulator Example

By default literal 1 and 1.388 are type double for M2000 Interpreter.

Numeric Types: Byte (8bit, unsigned), Integer (16bit), Long, Long Long (64bit), signle, double, currency, decimal, date

examples:1ub, 1%, 1&, 1&&, 1~, 1, 1#, 1@, 1ud

Date type is a double which displayed as a string of date/time format. We can do this: Date dt="2025-12-31" or Date dt=46022. To display the numeric value we use Val(), so ? Val(dt) display 46022.

These are the rules fot an Accumulator Factory:

  1. Takes a number n and returns a function (lets call it g), that takes a number i, and returns n incremented by the accumulation of i from every call of function g(i).
    Although these exact function and parameter names need not be used
  2. Works for any numeric type-- i.e. can take both ints and floats and returns functions that can take both ints and floats. (It is not enough simply to convert all input to floats. An accumulator that has only seen integers must return integers.) (i.e., if the language doesn't allow for numeric polymorphism, you have to use overloading or something like that)
  3. Generates functions that return the sum of every number ever passed to them, not just the most recent. (This requires a piece of state to hold the accumulated value, which in turn means that pure functional languages can't be used for this task.)
  4. Returns a real function, meaning something that you can use wherever you could use a function you had defined in the ordinary way in the text of your program. (Follow your language's conventions here.)
  5. Doesn't store the accumulated value or the returned functions in a way that could cause them to be inadvertently modified by other code. (No global variables or other such things.)


Module CheckIt { 
    Def VarType(n)=Type$(n)
    foo=lambda (acc) -> {
          =lambda acc -> {
                if empty then =acc : exit
                read x
                acc+=x
                =acc
          }
    }
    Double a=1
    x = foo(a)
    call void x(5) ' Without Void a non zero value count as Error.
    call foo(3)
    print VarType(x())="Double", x(2.3)=8.3 ' Double literal
    Single m=1
    z=foo(m)
    long L=5
    ? VarType(z(L))="Single", z(2.3)=8.3~ ' Signle literal
    Currency c=1
    zc=foo(c)
    ? VarType(zc(5))="Currency", zc(2.3)=8.3# ' Currency literal
    Decimal d=1
    zd=foo(d)
    ? VarType(zd(5))="Decimal", zd(2.3)=8.3@ ' Decimal literal
    Date dt=1
    zdt=foo(dt)
    ? VarType(zdt(5))="Date", zdt(2.3)=8.3ud ' Date literal
    Long Long LL=1
    zdt=foo(LL)
    ? VarType(zdt(5))="Long Long", zdt(2.3)=8&& ' Long Long literal
}
CheckIt

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου

You can feel free to write any suggestion, or idea on the subject.