Jump to content

For the longest time, I've been trying to come up with an Apply() method similar to Pythons .map(). Closest I've come...


Recommended Posts

#include <Array.au3>
LocalFunction()

Func LocalFunction()

    ; Goal is to have one function act on all of these
    Local $Var1 = '     Trim Me 1     '
    Local $Var2 = '   Trim Me 2    '
    Local $Var3 = '       Trim Me 3  '

    ; Function/Params of what you want to apply to a list/set of vars
    Local $FuncArr[2] = [StringStripWS, 3]
    ; Var names next to vars. If we could byRef these somehow to avoid the For loop after the Apply then it would be great 
    Local $VarArr[6] = [$Var1, 'Var1', $Var2, 'Var2', $Var3, 'Var3'] 

    ; Apply our function to our list of variables
    Apply($FuncArr, $VarArr)
    
    ; Reassign our updated array back to our original variables
    Local $val
    For $var=0 to UBound($VarArr)-1
        If Mod($var, 2) = 0 Then    ; Grab the updated values
            $Val = $VarArr[$var]
            ContinueLoop
        EndIf
        Assign($VarArr[$var], $Val) ; Assign value to correct name
    Next

    MsgBox(0,'Var1',  '['&$Var1&']')
    MsgBox(0,'Var2',  '['&$Var2&']')
    MsgBox(0,'Var3',  '['&$Var3&']')

EndFunc

Func Apply($aFunc, byRef $arr)
    For $var=0 to UBound($arr)-1
        If Mod($var, 2) <> 0 Then ContinueLoop ; Avoid the variable name strings
        $arr[$var] = Call($aFunc[0], $arr[$var], $aFunc[1])
    Next
EndFunc

So heres the code, it gets close to what I want although does anyone have any ideas how to avoid the For Loop after the apply function? If there is a way to create an array with references back to the variables themselves instead of just passing the values, this wouldn't be so ugly. The goal is to just be able to call one function on a large set of variables without having to reassign them individually. This does work but, imo, its pretty ugly and would only be worth it for a very large set of variables since the implementation of this pattern is at minimum like 10 lines. 

Link to post
Share on other sites

Seems to me you're making your life difficult:

LocalFunction()

Func LocalFunction()
    ; Goal is to have one function act on all of these
    Local $aVars = ['     Trim Me 1     ', '   Trim Me 2    ', '       Trim Me 3  ']

    ; Function/Params of what you want to apply to a list/set of vars
    Local $aFunction = [StringStripWS, 3]

    ; Apply function to array elements
    For $i = 0 to UBound($aVars) - 1
        $aVars[$i] = $aFunction[0]($aVars[$i], $aFunction[1])
    Next

    _ArrayDisplay($aVars)
EndFunc

 

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites

The goal is to have a callable function that can be used and updates variables, not arrays of values. I think with the limitations of autoit, it may not be possible. It could be achieved using all global variables but that's something I don't want to do. The only way to have a callable function that updates the original local variables in the function would to be using byRefs but also having an arbitrary number of params. This can't be done since optional byRefs aren't supported which is why I put the values into an array with the corresponding names next to them to use after the array has been cleaned and then assign back to original vars. I was just hoping someone had a not so intuitive way that I'm not seeing how to do it.

Link to post
Share on other sites

Python map() doesn't directly do what you want either: you still have to convert the resulting map object(s) to something else AND initial (caller's side) arguments aren't changed either.

I agree, AutoIt isn't a functional language at all.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to post
Share on other sites
  • Moderators

bailey1274,

You can reduce the "reassign" loop quite considerably:

; Reassign our updated array back to our original variables
For $var = 0 to UBound($VarArr) - 1 Step 2
    Assign($VarArr[$var + 1], $VarArr[$var]) ; Assign value to correct name
Next

Not quite a single line - but rather more concise than your initial effort.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...