minxomat

Stack-based LINQing in AutoIt

2 posts in this topic

This is purely for entertainment purposes.

It occurred to me that function pointers could be stored in Maps (why shouldn't they...), but the current beta mistakes them as objects when calling. Here's a workaround: Just explicitly cast the pointer to an expression (by using brackets) and go on from there. Of course, Au3Check needs to be disabled for this.

So I built a proof-of-concept boilerplate code for LINQing over 1D AutoIt arrays using a global stack. This example shows the following:

; Example 1
;
; 1) Get all three character strings from the input
; 2) Sort the list alphabetically
; 3) Delete items that don't match /M.+/
;

So with my code, this should work fine in theory:

LINQ($aNames).Where(StringLen, Is, 3).Sort().All("M.+")

But since this isn't implemented yet, we have to use two additional (ugly) brackets for this to work:

_ArrayDisplay( _
    (((((LINQ)($aNames).Where)(StringLen, Is, 3).Sort)()).All)("M.+") _
)

This works perfectly in the current Beta.

Every query starts by feeding the array into the LINQ function. This function, and all other keywords (Where, Sort etc.) provide all other LINQ keywords to the next one. This enables nested queries. A query ends with All() and returns the result array.

Anywho, here's the code:

#include <Array.au3>

Local $aNames = ["Hans", "James", "Alice", "Max", "Tom", "Mat"]

; Example 1
;
; 1) Get all three character strings from the input
; 2) Sort the list alphabetically
; 3) Delete items that don't match /M.+/
;
_ArrayDisplay( _
    (((((LINQ)($aNames).Where)(StringLen, Is, 3).Sort)()).All)("M.+") _
)

#cs

    This will be working when the func ptr is fixed:

    LINQ($aNames).Where(StringLen, Is, 3).Sort().All("M.+")

#ce

ConsoleWrite(@LF)

Func LINQ($Input, $ReturnTo = ConsoleWrite)
    #AutoIt3Wrapper_Run_AU3Check=n
    If Not IsDeclared("LINQMEM") Then Global $LINQMEM[0]
    __linq_alloc($Input)
    If Not IsDeclared("provideFuncs") Then Global $provideFuncs[]
    $provideFuncs.Where = __flinq_Where
    $provideFuncs.All   = __flinq_All
    $provideFuncs.Sort  = __flinq_Sort
    Return $provideFuncs
EndFunc

Func __flinq_Sort()
    Local $storeResults = $LINQMEM[UBound($LINQMEM)-1]
    _ArraySort($storeResults)
    __linq_alloc($storeResults)
    Return $provideFuncs
EndFunc

Func __flinq_Where($executeFunc, $compareFunc = Null, $expectedValue = Null)
    Local $storeResults[0]
    Local $doMatch = $compareFunc <> Null
    For $Each In $LINQMEM[UBound($LINQMEM)-1]
        $tempVal = Null
        If $doMatch And $compareFunc($executeFunc($Each), $expectedValue) Then
            $tempVal = $Each
        ElseIf Not $doMatch Then
            $tempVal = $executeFunc($Each)
        EndIf
        If $tempVal = Null Then ContinueLoop
        _ArrayAdd($storeResults, $tempVal)
    Next
    __linq_alloc($storeResults)
    Return $provideFuncs
EndFunc

Func Is($a, $b)
    Return $a=$b
EndFunc

Func __flinq_All($regExp = "")
    Local $aTop = $LINQMEM[UBound($LINQMEM)-1]
    ReDim $LINQMEM[UBound($LINQMEM)-1]
    If $regExp = "" Then Return $aTop
    Local $storeResults[0]
    For $Each In $aTop
        If Not StringRegExp($Each, $regExp) Then ContinueLoop
        _ArrayAdd($storeResults, $Each)
    Next
    Return $storeResults
EndFunc

Func __linq_alloc($newMem)
    ReDim $LINQMEM[UBound($LINQMEM)+1]
    $LINQMEM[UBound($LINQMEM)-1] = $newMem
EndFunc


As you can see, this code can be easily extended:

  • Add comparison keywords by defined clones of the Is() function,
  • Add LINQ keywords by defining new __flinq_* functions and providing them via the LINQ initializer,
  • Extend the handling for multidimensional arrays (and string (and numbers etc. (basically every AutoIt object)))

I thought this was an interesting experiment and would be nice to share,
Have fun.


I will answer every single PM, and you are free to ask anything anytime.

Share this post


Link to post
Share on other sites



Very cool


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


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