Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
54
Task/Singleton/M2000-Interpreter/singleton.m2000
Normal file
54
Task/Singleton/M2000-Interpreter/singleton.m2000
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
Module CheckSingleton {
|
||||
\\ singleton
|
||||
\\ pointers and static groups are the same object because
|
||||
\\ each one has a pointer to same state (a tuple)
|
||||
\\ but from outside we do the miracle to have a static group to act as a pointer
|
||||
\\ We need a lambda function to hold the pointer to Singleton as closure
|
||||
Global One=lambda M=pointer() (aValue=0)-> {
|
||||
If M is type null then
|
||||
\\ one time happen
|
||||
Group Singleton {
|
||||
Type:One
|
||||
Private:
|
||||
state=(aValue,)
|
||||
Public:
|
||||
module Add (x) {
|
||||
.state+=x
|
||||
}
|
||||
Set {Drop}
|
||||
Value {
|
||||
=.state#val(0)
|
||||
}
|
||||
}
|
||||
M->group(Singleton)
|
||||
end if
|
||||
\\ return M which is a pointer
|
||||
=M
|
||||
}
|
||||
K=One(100)
|
||||
Print Eval(K)=100
|
||||
M=One()
|
||||
Print Eval(M)=100
|
||||
Print K is M = true
|
||||
Print K is type One = true
|
||||
K=>add 500
|
||||
Print eval(K)=600
|
||||
\\ copy K to Z (no pointer to Z, Z is named group)
|
||||
Z=Group(K)
|
||||
Print eval(z)=600, z=600
|
||||
Z.add 1000
|
||||
Print Z=1600, Eval(M)=1600, Eval(K)=1600
|
||||
\\ push a copy of Z, but state is pointer so we get a copy of a pointer
|
||||
Push Group(Z)
|
||||
Read beta
|
||||
Beta.add 1000
|
||||
Print Z=2600, Eval(M)=2600, Eval(K)=2600
|
||||
\\ convert pointer to group (a copy of group)
|
||||
group delta=One()
|
||||
delta.add 1000
|
||||
Print Z=3600, beta=3600, delta=3600, Eval(M)=3600, Eval(K)=3600
|
||||
\\ M and K are pointers to groups
|
||||
M=>add 400
|
||||
Print Z=4000, beta=4000, delta=4000, Eval(M)=4000, Eval(K)=4000
|
||||
}
|
||||
CheckSingleton
|
||||
Loading…
Add table
Add a link
Reference in a new issue