|
-
Apr 19th, 2023, 09:25 AM
#11
Re: TwinBasic
 Originally Posted by Schmidt
For dynamically interpreted stuff, the VBScript-interpreter works decently enough (as a VB6-companion):
Code:
Private Sub Form_Load()
Dim Arr: Arr = Array(10, 15, 200, 50, 25, 90, 23, 78, 1000, 2000)
Debug.Print "Even numbers: "; Join(Filter(Arr, "Item Mod 2 = 0"), ",")
Debug.Print "Multiples of five: "; Join(Filter(Arr, "Item Mod 5 = 0"), ",")
Debug.Print "Greater than 100: "; Join(Filter(Arr, "Item > 100"), ",")
Debug.Print "Multiple of 10 greater than 100: "; Join(Filter(Arr, "Item > 100 AND Item Mod 10=0"), ",")
End Sub
Function Filter(Arr, Expr As String)
Dim i As Long, j As Long, SC
Filter = Array(): ReDim Res(UBound(Arr))
Set SC = CreateObject("ScriptControl")
SC.Language = "VBScript": SC.AddCode "Public Item"
For i = 0 To UBound(Arr)
SC.CodeObject.Item = Arr(i)
If SC.Eval(Expr) Then Res(j) = Arr(i): j = j + 1
Next
If j Then ReDim Preserve Res(j - 1): Filter = Res
End Function
Olaf
This is actually pretty clever and it's as close as you can get to using lambdas in VB6.
 Originally Posted by PlausiblyDamp
I hadn't even considered something like that, I am still surprised at exactly what can be done with VB6.
I am not sure if I would be happy to lose the type-safety and instellisense support lambdas provide (that also includes the JIT being to optimise them etc) to use embedded strings etc. to try and achieve the same result.
True. I also wonder if it could receive object references from the host so you could do things like this:-
Code:
Dim points = {New Point(10, 100), New Point(23.6, 70), New Point(30, 90)}
Dim filteredPoints = Filter(points, Function(p) p.X > 10)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|