Wiki improvements

Forum for discussion about the documentation project.
Post Reply
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

D.J.Peters wrote:if the FOR "counter" = "from" to "end" STEP "value" must be the same datatype !
FOR iterator [AS typename] = start_param TO end_param STEP step_param

- 'start_param', 'end_param' and 'step_param' can be of any type, of different types among themselves, and also of different types from the one of 'iterator' ('typename').
- The only constraint is that a 'typename' constructor (in case of local 'iterator') or a 'typename' assignment operator (in case of global 'iterator') matches with the passed 'start_param' argument, because the 'iterator' is implicitly constructed or assigned under the hood with 'start_param'.
- For the other arguments 'end_param' and 'step_param', they must be able to be converted into objects of the same type as the 'iterator'.


[edit]
The documentation seems to imply that these 3 arguments are of the same type as the iterator, perhaps because this is the most common use.
Maybe the documentation should be updated, or rather add a paragraph ("Advanced Usage") at the description end that mentions this less common usage.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Post by dodicat »

It is a bit restrictive I think, having all the parameters of the typename.
It just means you have to fiddle about to get what you want.

Code: Select all

Type vector
    As Single x,y,z
    Static As vector v
    As Single stepval
    Declare Operator For(As vector)
    Declare Operator Next(As vector,As vector) As Integer
    Declare Operator Step(As vector)
    Declare Function length() As Single
    Declare Function length(v2 As vector) As Single
    Declare Function lineto(As vector,As Single) As vector
    Declare Function start() Byref As vector
    Declare Constructor
    Declare Constructor(As Single,As Single,As Single)
    Declare Constructor(As Single)
End Type
Dim As vector vector.v

Constructor vector
End Constructor

Constructor vector(x1 As Single,y1 As Single,z1 As Single)
x=x1
y=y1
z=z1
End Constructor

Constructor vector(s As Single)
stepval=s
End Constructor

Function vector.length() As Single
    Return Sqr(x^2+y^2+z^2)
End Function

Function vector.length(v2 As vector) As Single
    Return Sqr((v2.x-x)^2+(v2.y-y)^2+(v2.z-z)^2)
End Function

Function vector.start() Byref As vector
    Static As vector g
    Return g
End Function

Function vector.lineto(p As vector,distance As Single) As vector
    Dim As Single dx=p.x-x,dy=p.y-y,dz=p.z-z
    Dim As Single L=Sqr(dx*dx+dy*dy+dz*dz)
    Return vector(x+distance*dx/L,y+distance*dy/L,z+distance*dz/L)
End Function

Operator vector.For(Stepper As vector)
this.start=vector(0,0,0)
End Operator

Operator vector.Next (ender As vector,stepper As vector) As Integer
If start().length=0 Then start()=This
v=ender
Return start.length(This)<=start.length(ender)
End Operator

Operator vector.Step(p As vector)
This=lineto(v,p.stepval)
End Operator

Dim As vector v

For v=vector(1,1,1) To vector(4,5,-6) Step Vector(.1)
    Print v.x,v.y,v.z
Next v
Print "Press a key . . ."
Print
Sleep
Color 3

For u As vector=vector(4,5,-6) To vector(1,1,1) Step vector(.1)
    Print u.x,u.y,u.z
Next u

Sleep


 
Last edited by dodicat on Oct 12, 2020 11:57, edited 3 times in total.
speedfixer
Posts: 606
Joined: Nov 28, 2012 1:27
Location: CA, USA moving to WA, USA
Contact:

Re: Wiki improvements

Post by speedfixer »

re: EVENT/ERROR handling

Seeing each possible choice next to each other does not improve my confidence that someone navigating to these topics will find what they want.
Better to put something/anything on the table now than waiting for 'sometime' in the future.
Oh, well. Perhaps perfection will have to wait for inspiration.

I guess whatever you are more comfortable with using. They are your topics, ultimately. Thanks for the attention.

david
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

dodicat wrote:It is a bit restrictive I think, having all the parameters of the typename.
It just means you have to fiddle about to get what you want.

Code: Select all


Type vector
    As Single x,y,z
    As vector Ptr v
    Declare Operator For(As vector)
    Declare Operator Next(As vector,As vector) As Integer
    Declare Operator Step(As vector)
    Declare Function length() As Single
    Declare Function lineto(As vector,As Single) As vector
    Declare Constructor(x As Single,y As Single,z As Single)
    Declare Destructor
    #define lengthofvector vector
End Type

Constructor vector(x As Single,y As Single,z As Single)
this.x=x
this.y=y
this.z=z
End Constructor

Destructor vector
Deallocate v
End Destructor

Function vector.length() As Single
    Return Sqr(x^2+y^2+z^2)
End Function

Function vector.lineto(p As vector,distance As Single) As vector
    Dim As Single diffx=p.x-this.x,diffy=p.y-this.y,diffz=p.z-this.z
    Dim As Single L=Sqr(diffx*diffx+diffy*diffy+diffz*diffz)
    Return vector(this.x+distance*diffx/L,this.y+distance*diffy/L,this.z+distance*diffz/L)
End Function

Operator vector.For(Stepper As vector)
End Operator

Operator vector.Next (ender As vector,stepper As vector) As Integer
v =Callocate(Sizeof(vector))
*v=ender
Return length<=ender.length
End Operator

Operator vector.Step(p As vector)
This=lineto(*v,p.length)
End Operator

Dim As vector v=Any

For v=vector(1,1,1) To vector(4,5,-6) Step LengthOfVector(.2,0,0)
    Print v.x,v.y,v.z
Next v

Sleep
 
By fiddling about, you said it!
Dynamic allocations are both useless and not deallocated.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

fxm wrote:
D.J.Peters wrote:if the FOR "counter" = "from" to "end" STEP "value" must be the same datatype !
FOR iterator [AS typename] = start_param TO end_param STEP step_param

- 'start_param', 'end_param' and 'step_param' can be of any type, of different types among themselves, and also of different type from the one of 'iterator' ('typename').
- The only constraint is that at least a 'typename' constructor (in case of local 'iterator') or a 'typename' assignment operator (in case of global 'iterator') matches with the passed 'start_param' parameter, because the 'iterator' is implicitly constructed or assigned under the hood with 'start_param'.
- For the other parameters 'end_param' and 'step_param', the user is free to elaborate as he wants the "end condition" and the "increment process" by adding in each operator body its own code which can use these parameters.


[edit]
The documentation seems to imply that these 3 arguments are of the same type as the iterator, perhaps because this is the most common use.
Maybe the documentation should be updated, or rather add a paragraph ("Advanced Usage") at the description end that mentions this less common usage.
Jeff and the others: What is your opinion?
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Post by dodicat »

fxm wrote:
dodicat wrote:It is a bit restrictive I think, having all the parameters of the typename.
It just means you have to fiddle about to get what you want.
By fiddling about, you said it!
Dynamic allocations are both useless and not deallocated.
I have simplified the code to another method.
Last edited by dodicat on Oct 12, 2020 11:49, edited 1 time in total.
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Wiki improvements

Post by D.J.Peters »

fxm wrote:[edit]
The documentation seems to imply that these 3 arguments are of the same type as the iterator, perhaps because this is the most common use.
Maybe the documentation should be updated, or rather add a paragraph ("Advanced Usage") at the description end that mentions this less common usage.
That was my primary problem.
Now it's more clear now.

But you should edit the page to make clear that this important restriktion does not exists.

Thank you.

Joshy
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

I prefer the solution of adding a paragraph at the end of the description of each keyword (For, Next, and Step operators) which extends its applicability regarding typenames of the passed arguments:
Advanced usage
  • The above description seems to imply that the 3 arguments start_value, end_value, and step_value must be of the same type as the iterator (this is the more obvious use), but it is not quite true:
    • - The start_value, end_value, and step_value arguments can be of any type (of different types among themselves and also of different types from the one of the iterator).
      - The only constraint is that the iterator could be constructed (in case of local iterator) or assigned (in case of global iterator) from the start_value argument (because the iterator is implicitly constructed or assigned under the hood).
      - Similarly, the other arguments end_value, and step_value must be able to be converted into objects of the same type as the iterator.
[edit]
Done:
- KeyPgOpFor → fxm [the For, Next, and Step overload operators can use arguments of different types than the one of iterator]
- KeyPgOpNext → fxm [the For, Next, and Step overload operators can use arguments of different types than the one of iterator]
- KeyPgOpStep → fxm [the For, Next, and Step overload operators can use arguments of different types than the one of iterator]
- ProPgTypeIterators → fxm [the For, Next, and Step overload operators can use arguments of different types than the one of iterator]
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Wiki improvements

Post by D.J.Peters »

In the FOR loop algorithm chart the (optional step operator)

Code: Select all

'     .----------------------->|
'     |                        |
'     |              calling Operator Next
'     |     (if end-condition verified: =0 returned) >-------------.
'     |               (else: <>0 returned)                         |
'     |                        v                                   |
'     |                        |                                   |
'     |            executing For...Next body                       |
'     |                        |                                   |
'     |              calling Operator Step                         |
'     |                        |                                   |
'     '------------------------'                                   |
'                                                                  |
'                                                                  V]
if the overloaded iterator isn't a scalar/number and the step operator are not overloaded (optional)
how does the iterator are advanced by FreeBASIC ?
or with other words if the iterator isn't a scalar/number than the step operator must be implemented and isn't optional at all right ?

Joshy
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

In the usage, the step_value is optional, but in the declaration and definition, the Step operator must always be overloaded.
The only difference in that in the Set operator body (operator declared without parameter), the code to "increment" the iterator uses a predefined "value".
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Post by dodicat »

Everybody makes their own rule of thumb I reckon for any use of a computer language, especially tricky bits like overloaded for/next/step in fb.
My rule of thumb is:
The for/next/step bear no one to one relationship with the standard for/next/step loops for numerical iterators.
Rather for/next/step act like sub/ function/ sub in that order.
The for() sets a starting state.
the next() compares a present state with the end state.
The step() does the increments.
Simple example.

Code: Select all


Type udt
      As Ulongint prime
      Declare Operator For()
      Declare Operator Next(As udt) As Integer
      Declare Operator Step()
      Declare Constructor(As Ulongint)
End Type

Function isprime(n As Ulongint) As Integer
      If (n=2) Or (n=3) Then Return -1
      If n Mod 2 = 0 Then Return 0
      If n Mod 3 = 0 Then Return 0
      Dim As Ulongint limit=Sqr(N)+1
      For I As Ulongint = 6 To limit Step 6
            If N Mod (i-1) = 0 Then Return 0
            If N Mod (i+1) = 0 Then Return 0
      Next I
      Return -1
End Function

Function nextprime(p As Ulongint) As Ulongint
      Do
            p+=1
            If isprime(p) Then Return p
      Loop
End Function

Constructor udt(x As Ulongint)
prime=x
End Constructor


Operator udt.for()
If isprime(prime)=0 Then
      Do
            prime+=1
      Loop Until isprime(prime)'starts at the first prime from the start number, from now on prime is a prime number
End If

End Operator

Operator udt.next(n As udt) As Integer
Return prime<=n.prime 'checks the current prime against the end number
End Operator

Operator udt.step()
Var p=nextprime(prime) 'increments within the prime number space.
prime=p
End Operator

For n As udt=200000000000 To 200000000300
      Print n.prime
Next n
print
print "done"
Sleep
 

 
Last edited by dodicat on Oct 13, 2020 8:28, edited 1 time in total.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

I think I described the process well in the Programmer's Guide:
Programmer's Guide / User Defined Types / Iterators
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

@dodicat

In your For..Next usage, you can elude the explicit call to the constructor because a constructor with one parameter allows implicit conversion:

Code: Select all

For n As udt=200000000000 To 200000000300
      Print n.prime
Next n
Similarly, if you use a global iterator instead of the local iterator (you must first define an implicit constructor to construct it), you can elude the explicit call to the constructor if you define in addition a matching Let operator (mandatory for the start value):

Code: Select all

Dim n As udt
For n=200000000000 To 200000000300
      Print n.prime
Next n
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Wiki improvements

Post by dodicat »

I have adjusted the simple example
to
For n As udt=200000000000 To 200000000300
skipping out udt(~~)
Actually what confuses me (I have no rule of thumb for) is when to use
u=type<udt>(~)
u=type(~)
u=udt~)
u=(~)
u=~
At the moment I use trial and error to get the simlest (cleanest looking) way.
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Wiki improvements

Post by fxm »

I tried to describe that in the Programmer's Guide:
Programmer's Guide / Declarations / Initialization
Post Reply