A challenge for you

Post your Gambas programming questions here.
Post Reply
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

A challenge for you

Post by cogier »

I have been working at putting Gambas on the map at Rosetta Code. http://rosettacode.org/wiki/Rosetta_Code I have 'solved' over 100 of their 'tasks'. There are still lots to do so why not have a go yourself.

These are the Gambas tasks 'solved' http://rosettacode.org/wiki/Category:Gambas

These are the Gambas tasks that need doing http://rosettacode.org/wiki/Reports:Tas ... _in_Gambas

It can be fun. Give it a go! :D
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: A challenge for you

Post by jornmo »

You know you can get permalinks by clicking the "Gist" button at https://gambas-playground.proko.eu/ ? That way you can skip the "copy/paste" step.
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A challenge for you

Post by cogier »

You know you can get permalinks by clicking the "Gist" button
I am aware of this but it was hard enough getting the link in. Every time I had to prove I was not a robot! I have completed 142 of the challenges of which 91 will run in the 'Playground' so it took some time to add the link to these anyway.
User avatar
jornmo
Site Admin
Posts: 224
Joined: Wednesday 21st September 2016 1:19pm
Location: Norway

Re: A challenge for you

Post by jornmo »

I see!
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: A challenge for you

Post by cogier »

OK I have started adding Gists, I didn't realise it was this easy.
The 'A's are done e.g. http://rosettacode.org/wiki/Align_columns#Gambas
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: A challenge for you

Post by sjsepan »

I was looking through the Tasks on RosettaCode, and one called 'Repeat' caught my interest: one procedure has to be passed to another and be execute N number of times. It got me thinking: Is there a concept of a 'Delegate' in Gambas, or a way to pass a pointer to a procedure, and then reference it in the destination to execute it? Note that we are not talking about external libraries but just within one class / module, for the purpose of simplicity.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: A challenge for you

Post by stevedee »

sjsepan wrote: Wednesday 20th November 2019 7:42pm ...Is there a concept of a 'Delegate' in Gambas, or a way to pass a pointer to a procedure, and then reference it in the destination to execute it? ...
Hi Steve, not sure what you have in mind to do with a pointer, or how this relates to the "Repeat" task.

But would using ByRef do what you wanted to do?

Maybe give us an example (...or write a pseudoCode example).

You may be interested in this: http://gambas.8142.n7.nabble.com/Callback-td6163.html

...and this: http://gambas.8142.n7.nabble.com/ByRef-td18173.html


-----------------------------------

BTW, for Rosetta Repeat, I just copied the VBA example & tweaked it:-
Public Sub Form_Open()

    Repeat("Hello", 5)
End

Private Sub Repeat(rid As String, n As Integer)
Dim i As Integer

    For i = 1 To n
        Hello()
    Next
End Sub
 
Private Sub Hello()

    Me.Text &= "Hello-"
End Sub
...but I guess that is not in the spirit of the project.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: A challenge for you

Post by stevedee »

OK, I've just been reading about "Delegates" in Visual Basic, so I don't think my answer above is very helpful...sorry.
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

Re: A challenge for you

Post by stevedee »

Just for completeness, I think this is a valid (but crude) example of passing pointers;
' Gambas class file

Public pPointer As Pointer
Public hMemory As Stream

Public Sub Form_Open()
  
  TextBox1.Text = 5
  pPointer = Alloc(4)
  hMemory = Memory pPointer For Read Write
  Write #hMemory, CStr(TextBox1.Text) As Integer
End


Public Sub SetValue(psetPointer As Pointer, iValue As Integer)
Dim hsetMemory As Stream
  
  hsetMemory = Memory psetPointer For Read Write
  Seek #hsetMemory, 0
  Write #hsetMemory, iValue As Integer
  
End

Public Function ReadValue(preadPointer As Pointer) As Integer
Dim hreadMemory As Stream
  
  hreadMemory = Memory preadPointer For Read Write
  Seek #hreadMemory, 0
  Return Read #hreadMemory As Integer
End

Public Sub Button1_Click()
Dim p1Pointer As Pointer
Dim h1Memory As Stream

  p1Pointer = pPointer
  h1Memory = hMemory
  SetValue(p1Pointer, CInt(TextBox1.Text))
  
End


Public Sub Button2_Click()
Dim p2Pointer As Pointer
Dim h2Memory As Stream

  p2Pointer = pPointer
  h2Memory = hMemory
  Label1.Text = ReadValue(p2Pointer)
  
End

Public Sub Form_Close()
  
  Free(pPointer)
End

It allocates some memory just big enough for an integer, then sets an initial value...just need a form, 2 buttons, a textbox & a label.
User avatar
sjsepan
Posts: 68
Joined: Saturday 12th October 2019 10:11pm
Location: Leeper, PA, USA
Contact:

Re: A challenge for you

Post by sjsepan »

stevedee,
The example of pointers is appreciated nonetheless -- didn't see any robust examples elsewhere yet. Thanks!
I did try ByRef the other day in the hope it would make a difference, but no.
Delegates are an interesting and powerful feature, so I hope something like it makes it into Gambas some day. As this was only for one of the RosettaCode task candidates, its not a showstopper, but its a good way to learn whether I know Gambas as well as I 'd like to think I do. ;-) Still have a way to go...

Steve
PS -- just saw the Nabble links. Looks like tobias-47 was on a similar quest. Will read through it and see what was discussed -- Thanks!!
stevedee wrote: Thursday 21st November 2019 2:25pm Just for completeness, I think this is a valid (but crude) example of passing pointers;

UPDATE: well the Nabble links got me far enoguh to submit a Gambas item, but with caveats noted in the description:
http://rosettacode.org/wiki/Repeat#Gambas
In case they flag it for not closely enough meeting the specs, I am posting the code here too:
'Note: Gambas (3.14.0) cannot perform this task as specified, as it does not have delegates, and pointers do not seem to work with procedures. 
'What does work is using Object.Call, which is intended for executing procedures from external libraries. 
'However the accepting procedure must refer to the object containing the procedure, and refer to the procedure by a String name. 
'In this case, the current module/class reference (Me) is used, but the String name must be passed. 
'This arrangement will only work within the same module/class. 
'It may be possible to pass the parent reference to a method (taking 3 parameters) in another class if the named procedure is Public. 
'The empty array ([]) in Object.Call represent a procedure without parameters, which are not an explicit requirement for this Task, but might require another parameter to the accepting procedure. 
Public Sub Main()
 
    RepeatIt("RepeatableOne", 2)
 
    RepeatIt("RepeatableTwo", 3)
 
End
 
'Cannot pass procedure pointer in Gambas; must pass procedure name and use Object.Call()
Public Sub RepeatIt(sDelegateName As String, iCount As Integer)
 
    For iCounter As Integer = 1 To iCount
        Object.Call(Me, sDelegateName, [])
    Next
 
End
 
Public Sub RepeatableOne()
 
    Print "RepeatableOne"
 
End
 
Public Sub RepeatableTwo()
 
    Print "RepeatableTwo"
 
End
Post Reply