Τετάρτη 15 Φεβρουαρίου 2023

Map Range ( A Rosetta Code Task)

 https://rosettacode.org/wiki/Map_range#M2000_Interpreter

This are two modules which return the same output. We can use a class to make an object which return a value passing one parameter, or a lambda function which prepare another lambda function.

I didn't use types for numbers. Literal values 0, 10, -1 and 0 are of type Double. In class Map, private variables are by default double unless we give a type.  We can give either a as single or single a. The first typo has the type after the variable, which is the same for function/module's parameter list, and for constants using Const. Also used for Global/Local/Def. The second typo, has the type before the name. We can use it as is to declare or re-declare a variable. Also this typo supported by Local/Global.

Local make always new variables (and arrays) local scope

Global make always new variables (and arrays) global scope (but the life of these end when "named" code, like in a function and in a module exit.

Def make always local variables (and make local one line functions) , but raise error if we define a local variable second time. Values can be passed only literals

Boolean, Byte, Integer, Long, Long Long, Single, Double, Currency, Decimal, String define (local by default) variables and arrays (special type of arrays). We can combine this (not in a group/class definition) with Global/Local to make new variables for global or local scope.

Variables can be get type from the right expression (here M1 take Group (object) at the first example, and take lambda function at the second example). The I variable take double, so it is a double type.

There is an exception for these two types: Variant and Object. An object which have a tuple can get a list or a stack object. These are objects which we can enumerate with same functionality.

object a=(1,2,3,4)
Print a#sum()=10, a#val(2)=3
a
=list:=1,2,3,4,5
Print len(a)=5, exist(a, 3)= true, exist(a, 10)=false
a
=stack:=1,2,3,4
Print len(a)=4
stack a {
Push number*number
}
Print a ' 2, 3, 4


Variant type can take anything


variant b="ok"
Print b+b="okok"
b=100
Print b+b=200
class alfa {a=10}
b=alfa()
Print b=>a^2=100
b=(1,2,3,4)
Print "["+b#str$(", ")+"]"="[1, 2, 3, 4]"

All variables (except those in immediate mode) take only the same type, from the first assign (or first assign of type, which pass the "zero" value)

We can make constants, using Const and series of constants using Enum (we can define variables of specific enum type. Enum may have string or any numeric type. Operators ++ and -- change the "index" of the enum variable).

enum names {bob="Bob A", bob2="Bob B", jhon="John Doe"}
m=bob
for i=1 to 3
? m // we get the names
m++
next


v=each(names)
while v
Print eval$(v), eval(v) ' bob bobA // ...
end while

And these are the MapRange module's (We can include both of them in the same module. The second definition change the current definition of the module MapRange)


module MapRange {
class Map {
private:
a, b, f
public:
value (x){
=.b+(x-.a)*.f
}
class:
module Map (.a,a2,.b,b2) {
if a2-.a=0 then error "wrong parameters"
.f<=(b2-.b)/(a2-.a)
}
}

m1=Map(0,10, -1, 0)
for i=0 to 10
Print i," maps to ";m1(i)
next
}
MapRange


module MapRange {
Map=lambda (a,a2,b,b2) -> {
if a2-a=0 then error "wrong parameters"
f=(b2-b)/(a2-a)
=lambda a,b,f (x)->b+(x-a)*f
}
m1=Map(0,10, -1, 0)
for i=0 to 10
Print i," maps to ";m1(i)
next
}
MapRange

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

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

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