BAM: [new] _MAPSET and _MAPGET
#1
https://basicanywheremachine-news.blogsp...apget.html
Reply
#2
An example use case:

Code: (Select All)
INIT:

  _initaudio

  _mapset("A", 1)
  _mapset("B", 2)
  _mapset("C", 3)

MAIN_PROGRAM:

  getselection:
    print "Press one of the following keys: A B C"
    let selection$ = ucase$(input$(1))
    if selection$ < "A" or selection$ > "C" then beep : goto getselection

  on _mapget(selection$) gosub handle_A, handle_B, handle_C

  goto getselection

  end
 
SUBROUTINES: 

  handle_A:
    print "stuff to do for selection A"
    RETURN

  handle_B:
    print "stuff to do for selection B"
    RETURN

  handle_C:
    print "stuff to do for selection C"
    RETURN
Reply
#3
Something as this already exists:

Code: (Select All)
INIT:

'  _initaudio

'  _mapset("A", 1)
'  _mapset("B", 2)
'  _mapset("C", 3)


'MAIN_PROGRAM:

getselection:
Print "Press one of the following keys: A B C"
Let selection$ = UCase$(Input$(1))
If selection$ < "A" Or selection$ > "C" Then Beep: GoTo getselection
Selected = Abs(64 - Asc(selection$))
Print Selected

'  on _mapget(selection$) gosub handle_A, handle_B, handle_C
On Selected GOSUB Handle_A, Handle_B, Handle_c


GoTo getselection

End

SUBROUTINES:

Handle_A:
Print "stuff to do for selection A"
Return

Handle_B:
Print "stuff to do for selection B"
Return

Handle_c:
Print "stuff to do for selection C"
Return


Reply
#4
Mine was a ridiculous use case scenario.


I am not particularly good at inventing hypothetical examples.

Maybe this is better:

Code: (Select All)
INIT:

  _initaudio

  _mapset("CARROT", 1)
  _mapset("CORN", 2)
  _mapset("POTATO", 3)

MAIN_PROGRAM:

  getselection:
    input "Search for recipes: enter one ingredient:", selection$
    selection$ = ucase$(selection$)
    if _mapget(selection$) = "" then beep : print "sorry, no recipes for " + selection$ : goto getselection

  on _mapget(selection$) gosub CARROT, CORN, POTATO

  goto getselection

  end

SUBROUTINES:

  CARROT:
    print "setup special processing for carrot-related recipes and info"
    RETURN

  CORN:
    print "setup special processing for corn-related recipes and info"
    RETURN

  POTATO:
    print "setup special processing for potato-relatd recipes and info"
    RETURN
Reply
#5
(03-12-2023, 04:16 PM)CharlieJV Wrote: Mine was a ridiculous use case scenario.


I am not particularly good at inventing hypothetical examples.

Maybe this is better:

Code: (Select All)
INIT:

  _initaudio

  _mapset("CARROT", 1)
  _mapset("CORN", 2)
  _mapset("POTATO", 3)

MAIN_PROGRAM:

  getselection:
    input "Search for recipes: enter one ingredient:", selection$
    selection$ = ucase$(selection$)
    if _mapget(selection$) = "" then beep : print "sorry, no recipes for " + selection$ : goto getselection

  on _mapget(selection$) gosub CARROT, CORN, POTATO

  goto getselection

  end

SUBROUTINES:

  CARROT:
    print "setup special processing for carrot-related recipes and info"
    RETURN

  CORN:
    print "setup special processing for corn-related recipes and info"
    RETURN

  POTATO:
    print "setup special processing for potato-relatd recipes and info"
    RETURN

Hmm.  That last sample would have been better with "ORANGE", "CARROT", "BEEF".   Just to highlight that the processing might be pretty different for fruit vs veggies vs meat.
Reply
#6
Here is it - MapSet and MapGet Smile - but first DIM definitons are need.


Code: (Select All)
Type MapSet_Internal
    v As String
    value As Integer
End Type
ReDim Shared MU(0) As MapSet_Internal


MapSet "CARROT", 1
MapSet "CORN", 2
MapSet "POTATO", 3

MAIN_PROGRAM:

getselection:
Input "Search for recipes: enter one ingredient:", selection$
selection$ = UCase$(selection$)
If MapGet(selection$) = 0 Then Beep: Print "sorry, no recipes for " + selection$: GoTo getselection

On MapGet(selection$) GOSUB CARROT, CORN, POTATO

GoTo getselection

End

SUBROUTINES:

CARROT:
Print "setup special processing for carrot-related recipes and info CARROT"
Return

CORN:
Print "setup special processing for corn-related recipes and info CORN"
Return

POTATO:
Print "setup special processing for potato-relatd recipes and info POTATO"
Return


Sub MapSet (something As String, value)
    u = UBound(MU)
    MU(u).v = something$
    MU(u).value = value
    ReDim _Preserve MU(u + 1) As MapSet_Internal
End Sub

Function MapGet (value$)
    U = LBound(MU)
    Do Until m = UBound(MU)
        V$ = _Trim$(MU(m).v)
        If V$ = value$ Then MapGet = MU(m).value: Exit Function
        m = m + 1
    Loop
    MapGet = 0
End Function


Reply
#7
Total aside.

One of the things I'm working on: debugging/fine-tuning meta-programming (macro-handling).  For example:

Code: (Select All)
<$vars ingredients="APPLE CARROT BEEF BARLEY CHICKEN PAPRIKA">

INIT:
  _initaudio
<$list variable="ingredient" filter="[enlist<ingredients>sort[]]" counter="i">
_mapset("<<ingredient>>",<<i>>)  </$list>
'
MAIN_PROGRAM:
'
  getselection:
    input "Search for recipes: enter one ingredient:", selection$
    selection$ = ucase$(selection$)
    if _mapget(selection$) = "" then beep : print "sorry, no recipes for " + selection$ : goto getselection
'
  on _mapget(selection$) gosub {{{ [enlist<ingredients>sort[]join[, ]] }}}
'
  goto getselection
'
  end
'
SUBROUTINES:
'
<$list variable="ingredient" filter="[enlist<ingredients>sort[]]" counter="i">

  <<ingredient>>:
    print "setup special processing for <<ingredient>>-related recipes and info"
    RETURN
'
</$list>
</$vars>

The above results in:

Code: (Select All)
INIT:
_initaudio

_mapset("APPLE",1)
_mapset("BARLEY",2)
_mapset("BEEF",3)
_mapset("CARROT",4)
_mapset("CHICKEN",5)
_mapset("PAPRIKA",6)
'
MAIN_PROGRAM:
'
getselection:
input "Search for recipes: enter one ingredient:", selection$
selection$ = ucase$(selection$)
if _mapget(selection$) = "" then beep : print "sorry, no recipes for " + selection$ : goto getselection
'
on _mapget(selection$) gosub APPLE, BARLEY, BEEF, CARROT, CHICKEN, PAPRIKA
'
goto getselection
'
end
'
SUBROUTINES:
'
APPLE:
print "setup special processing for APPLE-related recipes and info"
RETURN
'
BARLEY:
print "setup special processing for BARLEY-related recipes and info"
RETURN
'
BEEF:
print "setup special processing for BEEF-related recipes and info"
RETURN
'
CARROT:
print "setup special processing for CARROT-related recipes and info"
RETURN
'
CHICKEN:
print "setup special processing for CHICKEN-related recipes and info"
RETURN
'
PAPRIKA:
print "setup special processing for PAPRIKA-related recipes and info"
RETURN
'
Reply
#8
That's a nice job, filtering out strings like that. I just now read the definition of MapGet - what I wrote is only for numeric values. I don't know of a way to use a function that is supposed to return a string to return a number so that the string function gives a numeric output and other functions that expect a number accept this if I were to strictly follow the notation used. The same with MapSet - there both parameters would have to be written in quotation marks. The C language could do the above, but I can't serve there. Unfortunately, in QB64 you cannot write two functions with the same name so that one returns strings and the other with the same name returns numbers.


Reply
#9
(03-12-2023, 06:55 PM)Petr Wrote: That's a nice job, filtering out strings like that. I just now read the definition of MapGet - what I wrote is only for numeric values. I don't know of a way to use a function that is supposed to return a string to return a number so that the string function gives a numeric output and other functions that expect a number accept this if I were to strictly follow the notation used. The same with MapSet - there both parameters would have to be written in quotation marks. The C language could do the above, but I can't serve there. Unfortunately, in QB64 you cannot write two functions with the same name so that one returns strings and the other with the same name returns numbers.

Yeah, BAM being a javascript-under-the-hood critter...

My mindset, from early training and throughout my career, has always been about strong typing.

Experiencing loose typing in BAM has me questioning much of what I've always been used to.  Kind of like having a slight identity crisis ...

Aside: I got into this _MAPSET and _MAPGET thing because I was at first wondering if a "swtich" function could be useful.  When I started thinking about the implementation of it, my mind wandered over to javascript maps, and my flickering-light-bulb-moment: switch function would be a javascript pain in the caboose to implement when I've got better things to do, but a global map and the statement+function will take me all of an hour.  (all in the context of giving ON GOSUB a new trick.)
Reply




Users browsing this thread: 2 Guest(s)