Παρασκευή 27 Απριλίου 2018

Example for 9.3 version (rev 5), with group pointers and lambda function

I minor revision change to 5,  with Wine support, and a small bug in Module.Name$ when used with non secure interpreter option.

In this example we will see how M2000 Interpreter can use recursion in lambda function, as a member of group. There are two examples. At the first example we see a static group, with a name Alfa, which is not movables, and dies when going out of scope (at the end of module's code). For these examples we have an assumption that the code are in a module named A
And let say that we have this to try out with "secure names" (+sec) or "non secure names" (-sec). Using set switches from module we turn switch for the running session of interpreter. But if we use it (without set) in M2000 console, this will be the setting for all new instances of interpreter. Not all of switches have global rights. Switches can be placed in command line, for temporary use, when open a gsb file, e.g "c:\.....\testme.gsb" -sec
Also in the compact snippet bellow we can learn about the two calling procedures for modules (also we can Call a function, but this we will see another time). Faster call is the first one. We can place this code in a Module A (no worry about the same name).

set switches "+sec"   ' try with -sec
Module A {
Print Module$ ' this is a Global Name assigned from Interpreter
Print Module.Name$ ' this is the "nice name"
\\ we can't call, normal way,  A from here
\\ we can get recursion using Call A
\\ place here any example

}
A  ' this is a normal call for modules

Module B {
Print Module$ ' this is a Global Name assigned from Interpreter
Print Module.Name$ ' this is the "nice name", only for display
\\ place here any example

}
Call B
' using Call is slight different, inside, from a normal call
' A call to a module place the interpreter to give recursion acces






First Example


Group alfa {
      N=5
      b=lambda ->0
}
\\ we can manke a lambda with access to group N
\\ as is we can't use it
b=lambda Z=100 -> {
      if .N<2 then =Z : exit
      .N--
      =(.N+1)*lambda()
}
\\ but we can copy it ro alfa.b which is a lambda also
alfa.b=b
\\ now alfa.b( ) run using .N of alfa
M=alfa.b()
Print alfa.N, M
\\ And now the 9.3 version part
\\ we make a pointer to a copy of alfa
k->(alfa)
Print Alfa.N=5 ' false
\\ we set  the N member
k=>N=5
\\ and we call it
Print k=>b()
\\ Now we set
\\ pointer to alfa
k->alfa
k=>N=5
Print Alfa.N=5 ' true
Print k=>b()
k->0
Print Type$(k)="Group" ' True is a group but an empty one
Print valid(k=>N)=false ' Return true





Second Example



Group alfa {
      N=5
      b=lambda ->0

}
Dim A(10)=alfa
b=lambda Z=100 -> {
      if .N<2 then =Z : exit
      .N--
      =(.N+1)*lambda()
}
A(3).b=b
M=A(3).b()
Print A(3).N, M
\\ And now the 9.3 version part
\\ we make a pointer to a copy of A(3)
k->(A(3))
Print A(3).N=5 ' false
\\ we set  the N member
k=>N=5
\\ and we call it
Print k=>b()
\\ Now we set
\\ pointer to alfa
k->A(3)
k=>N=5
Print A(3).N=5 ' true
Print k=>b()
k->0
Print Type$(k)="Group" ' True is a group but an empty one
Print valid(k=>N)=false ' Return true




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

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

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