A challenge for you

Post

Posted
Rating:
#1 (In Topic #126)
Avatar
Guru
cogier is in the usergroup ‘Guru’
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…not_implemented_in_Gambas

It can be fun. Give it a go! :D
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
jornmo is in the usergroup ‘Regular’
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.

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.
Online now: No Back to the top

Post

Posted
Rating:
#4
Avatar
Regular
jornmo is in the usergroup ‘Regular’
I see!

Online now: No Back to the top

Post

Posted
Rating:
#5
Avatar
Guru
cogier is in the usergroup ‘Guru’
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
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#7
Avatar
Regular
stevedee is in the usergroup ‘Regular’

sjsepan said

…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:-

Code (gambas)

  1. Public Sub Form_Open()
  2.  
  3.     Repeat("Hello", 5)
  4.  
  5.  
  6.     For i = 1 To n
  7.         Hello()
  8.     Next
  9.  
  10. Private Sub Hello()
  11.  
  12.     Me.Text &= "Hello-"

…but I guess that is not in the spirit of the project.
Online now: No Back to the top

Post

Posted
Rating:
#8
Avatar
Regular
stevedee is in the usergroup ‘Regular’
 OK, I've just been reading about "Delegates" in Visual Basic, so I don't think my answer above is very helpful…sorry.
Online now: No Back to the top

Post

Posted
Rating:
#9
Avatar
Regular
stevedee is in the usergroup ‘Regular’
Just for completeness, I think this is a valid (but crude) example of passing pointers;

Code (gambas)

  1. ' Gambas class file
  2.  
  3. Public pPointer As Pointer
  4. Public hMemory As Stream
  5.  
  6. Public Sub Form_Open()
  7.  
  8.   TextBox1.Text = 5
  9.   pPointer = Alloc(4)
  10.   hMemory = Memory pPointer For Read Write
  11.   Write #hMemory, CStr(TextBox1.Text) As Integer
  12.  
  13.  
  14. Public Sub SetValue(psetPointer As Pointer, iValue As Integer)
  15. Dim hsetMemory As Stream
  16.  
  17.   hsetMemory = Memory psetPointer For Read Write
  18.   seek">Seek #hsetMemory, 0
  19.   Write #hsetMemory, iValue As Integer
  20.  
  21.  
  22. Public Function ReadValue(preadPointer As Pointer) As Integer
  23. Dim hreadMemory As Stream
  24.  
  25.   hreadMemory = Memory preadPointer For Read Write
  26.   seek">Seek #hreadMemory, 0
  27.   Return Read #hreadMemory As Integer
  28.  
  29. Public Sub Button1_Click()
  30. Dim p1Pointer As Pointer
  31. Dim h1Memory As Stream
  32.  
  33.   p1Pointer = pPointer
  34.   h1Memory = hMemory
  35.   SetValue(p1Pointer, CInt(TextBox1.Text))
  36.  
  37.  
  38.  
  39. Public Sub Button2_Click()
  40. Dim p2Pointer As Pointer
  41. Dim h2Memory As Stream
  42.  
  43.   p2Pointer = pPointer
  44.   h2Memory = hMemory
  45.   Label1.Text = ReadValue(p2Pointer)
  46.  
  47.  
  48. Public Sub Form_Close()
  49.  
  50.   Free(pPointer)
  51.  
  52.  

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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Avatar
Regular
sjsepan is in the usergroup ‘Regular’
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 said

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:

Code (gambas)

  1. '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.
  2. 'What does work is using Object.Call, which is intended for executing procedures from external libraries.
  3. 'However the accepting procedure must refer to the object containing the procedure, and refer to the procedure by a String name.
  4. 'In this case, the current module/class reference (Me) is used, but the String name must be passed.
  5. 'This arrangement will only work within the same module/class.
  6. 'It may be possible to pass the parent reference to a method (taking 3 parameters) in another class if the named procedure is Public.
  7. '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.
  8. Public Sub Main()
  9.  
  10.     RepeatIt("RepeatableOne", 2)
  11.  
  12.     RepeatIt("RepeatableTwo", 3)
  13.  
  14.  
  15. 'Cannot pass procedure pointer in Gambas; must pass procedure name and use Object.Call()
  16. Public Sub RepeatIt(sDelegateName As String, iCount As Integer)
  17.  
  18.     For iCounter As Integer = 1 To iCount
  19.         Object.Call(Me, sDelegateName, [])
  20.     Next
  21.  
  22.  
  23. Public Sub RepeatableOne()
  24.  
  25.     Print "RepeatableOne"
  26.  
  27.  
  28. Public Sub RepeatableTwo()
  29.  
  30.     Print "RepeatableTwo"
  31.  
Online now: No Back to the top
1 guest and 0 members have just viewed this.