Has anyone experimented with memo functions in LC?

Dick Kriesel dick.kriesel at mail.com
Tue Jun 14 17:27:05 EDT 2011

Hi, Geoff.  Here's a way to memoize without parsing the functions' arguments, so commas won't be a pain.

local sMemo
function memoize pFunctionName, pParam1, pParam2
   local tDigest
   put md5( the params ) into tDigest
   if tDigest is among the keys of sMemo then
      get sMemo[ tDigest ]
      get value( pFunctionName & "(" & item 2 to -1 of the params )
      put it into sMemo[ tDigest ]
   end if
   return it
end memoize

Calling MD5 makes the array's keys shorter.  IIRC, that's good, although when I tried to find evidence about it in the current docs, I couldn't find it.

Does this help?

-- Dick

On Jun 11, 2011, at 2:23 PM, Geoff Canyon Rev wrote:

> http://en.wikipedia.org/wiki/Memoization
> I'm wondering if anyone has experimented with creating an automatic
> memoization function within LC? Given that we can access argument lists, and
> have "do," it seems possible, but I'm betting commas in the arguments would
> be a pain. Something like this:
> function m functionName
>   local callHistory,callHistoryValue
>   -- parse out addition arguments to m
>   -- concatenate functionName & arguments into functionCallString
>   if callHistory[functionCallString] is not empty then
>      return callHistoryValue[functionCallString]
>   end if
>   -- get the function result
>   put it into callHistoryValue[functionCallString]
>   put 1 into callHistory[functionCallString]
>   return it
> end m
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

More information about the Use-livecode mailing list