memoize continuation passing style function
Asked Answered
R

1

5

I'm wondering if there is a way to implement a generic "memoize" functional (as in a function with a function as input and a function as output, as python's decorators) capable of handling also cps-style functions.

for a normal function (as in "the result value comes back by the return, the parameters are only for input!") a memoize function can be as simple as (in javascript)

function memoize(fun) {
    var cache = {};
    return function () {
        var args = Array.prototype.slice.call(arguments);
        if (args in cache)
            return cache[args];
        var ret = fun.apply(this, arguments);
        cache[args] = ret;
        return ret;
    };
}

but a cps-style function cannot be memoized by my simple memoize function, cause I need to evaluate "again" the arguments of type function, knowing also the parameter to pass to them.

For example, given the function

function cps(param, next) {
    var ret = param + 1;

    // setTimeout for simulate async behaviour
    setTimeout(function () {
            next(ret);
    }, 0);
}

maybe I can find that next is a function, but its signature (well... maybe, but it's tricky), and definitely not the parameters used in the function!

Can someone tell me I'm wrong? :D

I'm interested to be able to memoize an half dozen of cps-style functions and I don't want to mess with the logic inserting a "cache" in every one of them.

Rafaelarafaelia answered 5/4, 2012 at 10:38 Comment(2)
if you pass an hashtable as argument of your function (defining n-pairs of key: values) it would simplyfy the logic for your purpose?Ganoid
it's not so simple: I'm using cps-style functions because I'm dealing with ajax call: the continueIfTrue / continueIfFalse are not called directly by my functions, are registered as callbacks and called by the browser when the response came back... I cannot see how using an hashtable can help me (maybe I'm just blind :D enlight me!).Rafaelarafaelia
C
2

I'm new to CPS, but I think you'll have to construct your functions in a particular way.

Your CPS functions have the following structure (generalising from your example):

function cps(param, next) {
    var ret = someFunctionOfParam(param);

    // setTimeout for simulate async behaviour
    setTimeout(function () {
        next(ret);
    }, 0);
}

So, you could use your standard memoizer, and construct the CPS function as well. Keeping this separate for the sake of it, first the CPS-maker (assumes the last argument for the functions is always the function to pass to):

function cpsMaker(transformFunc) {
    return function() {
               var args = Array.prototype.slice.call(arguments);
               var next = args.pop(); // assume final arg is function to call
               var ret = transformFunc.apply(this,args);
               // setTimeout for simulate async behaviour
               setTimeout(function () {
                   next(ret);
               }, 0);
           }
}

And then the memoizer can be used in conjunction with it:

function plusOne(val) {
    return val+1;
}

var memoPlusOne = memoize(plusOne);
var cpsMemPlusOne = cpsMaker(memoPlusOne);

cpsMemPlusOne(3,function(n){console.log(n)});

The point is to separate the memoization of the transform from the CPS construction.

Thank you for introducing the idea of memoized CPS; even if this answer is rubbish, it has been an eye-opener for me!

Cryptoclastic answered 14/4, 2012 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.