Τετάρτη 9 Σεπτεμβρίου 2026

Revision 42 Version 15 - Functions for Structures

With functions for structures we can declare buffers (memory area) attaching functions, and we can use them to apply values also. 

These functions stored to structure only and interpreter attach one when need to execute it. So we can't used them as normal functions passing them by reference. We use name of structure as the one item of structured populated by original buffer (as a copy). So everything we do applied to a copy of an item of a buffer. When we define a buffer using name of structure and then name of buffer we can apply the computed item to every item of buffer.   So, alfa kappa[20]#new(30,40)#mul(3)  make 20 copies of the result of #new(30,40)#mul(3), and later alfa kappa[200]#new(31,41)#mul(3)  make 180 copies of new(31,41)#mul(3) and apply these from items [20] to [199]. 

Also, structure can be used in classes, and now we can use the declaration format struct_name buffer_name.

structure alfa {
x as double,
y as double
function mul(a) {
alfa|x=alfa|x*a
alfa|y=alfa|y*a
=alfa
}
function new(a=100, b=200) {
alfa|x=a
alfa|y=b
=alfa
}
function str() {
="("+(alfa|x)+", "+(alfa|y)+")"
}
}
alfa kappa
[20]#new(30,40)#mul(3)
print kappa[3]#str()="(90, 120)", kappa=>items=20, len(kappa)=320 ' bytes
alfa kappa
[200]#new(31,41)#mul(3)
print kappa[19]#str()="(90, 120)"
print kappa[20]#str()="(93, 123)", kappa=>items=200, len(kappa)=3200 ' bytes


class beta {
structure alfa {
x as double,
y as double
function mul(a) {
alfa|x=alfa|x*a
alfa|y=alfa|y*a
=alfa
}
function new(a=100, b=200) {
alfa|x=a
alfa|y=b
=alfa
}
function str() {
="("+(alfa|x)+", "+(alfa|y)+")"
}
}
{read many}
alfa kappa[many]#new(30,40)#mul(3)
}
beta=beta(10)
print beta.kappa[3]#str()="(90, 120)", beta.kappa=>items=10, len(beta.kappa)=160 ' bytes




Another example:


structure alfa {
x as double
y as double
function inc() {
alfa|x++
alfa|y++
=alfa
}
function arr {
=(alfa|x, alfa|y)
}
function str(s as string="") {
="(" +alfa#arr()#str$(s)+")"
}
}
alfa kappa
for i=1 to 10
kappa=kappa#inc()
? kappa#arr()#stuff(0)
? kappa#str(", ")
next

Or we can use Str$() as function name:

structure alfa {
x as double
y as double
function inc() {
alfa|x++
alfa|y++
=alfa
}
function arr {
=(alfa|x, alfa|y)
}
function str$(s as string="") {
="(" +alfa#arr()#str$(s)+")"
}
}
alfa kappa
for i=1 to 10
kappa=kappa#inc()
? kappa#arr()#stuff(0)
? kappa#str$(", ")
next

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

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

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