Has anyone experimented with memo functions in LC?

Dick Kriesel dick.kriesel at mail.com
Thu Jun 16 04:07:02 EDT 2011


So there, for the rumor mill, is another feature of the CanyonLands pre-compiler ...

Actually, the memoize function below would work for just about all functions where you'd ever want memoizing (barring polymorphism and array parameters).  It would work even in a stack-in-use or a back script.  Unlike the J function with the "M." in the function definition, this approach lets the caller decide whether to apply memoizing.  That's plenty general -- for a solution that lacks a problem.

Any more hints about that pre-compiler?

-- Dick


On Jun 15, 2011, at 11:12 PM, Geoff Canyon Rev wrote:

> This would work for a particular function. I was looking for a general
> solution that would allow memo-ing any function without further
> consideration. In J, for example, you just and "M." to the function
> definition, and you're done. If I were to do something like this in LC I'd
> want to implement something similarly robust and simple to use.
> 
> As to why I want to: no particular reason, other than memo seems vaguely
> possible in LC, and a useful thing to have. I like pushing the language
> forward.
> 
> gc
> 
> On Tue, Jun 14, 2011 at 4:27 PM, Dick Kriesel <dick.kriesel at mail.com> wrote:
> 
>> 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 ]
>>  else
>>     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
>> 
>> 
>> 
>> _______________________________________________
>> 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
>> 
> _______________________________________________
> 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