Home > Net >  Wrapping the arguments or result of an anonymous function
Wrapping the arguments or result of an anonymous function

Time:12-24

I want to reassign an anonymous function to add to what it does. I'm basically looking the below functionality.

let f1 = (x: number) => x   100;
// f1 = (x: number) => f1(x)   20; // becomes recursive -> stack overflow, not what I want
f1(3) // 123

The two options I see are either 1. wrapping the argument or 2. somehow wrapping the result. The first option seems easier but I'm not sure how to accomplish it (or if what I'm asking for is impossible).

Edit: Evidently my question was not clear enough. I've made a new example.

https://tsplay.dev/mpv3Mw

class Cat {
    eat() {
        const newCat = createFullCat(this);
        return newCat;
    }
    drink() {
        const newCat = createQuenchedCat(this);
        return newCat;
    }
    sleep() {
        const newCat = createRestedCat(this);
        return newCat;
    }
}

function makeCatActionChain(makeCatSleep: boolean, makeCatEat: boolean) {
    let f1 = (cat: Cat) => cat.drink();

    if (makeCatEat) {
        f1 = (cat: Cat) => f1(cat).eat(); // Need different way to do this
    }

    if (makeCatSleep) {
        f1 = (cat: Cat) => f1(cat).sleep(); // Need different way to do this
    }
    return f1;
}

If possible I would like to do this without creating another variable.

CodePudding user response:

First, you assigned a value (a function) to f1:

let f1 = (x: number) => x   100;

Then, you assigned a new value (another function) to f1:

f1 = (x: number) => f1(x)   20;

When you assigned the new function to it, the previous function no longer had any references. In the new function, the reference to f1 refers to itself, (so it will recurse forever when invoked).

If you want to maintain a reference to the first one so that it can be used in the second one, just give the second one a different name, like this:

TS Playground

const f1 = (x: number) => x   100;
const f2 = (x: number) => f1(x)   20;
console.log(f2(3)); // 123

If you want to apply a series of transformations on a number (but don't want to have to create a variable name for each transform function in your program), you could use an array of functions to process the number in order, like this:

TS Playground

type NumberTransform = (n: number) => number;

function transformNumber (n: number, transforms: NumberTransform[]): number {
  let current = n;
  for (const fn of transforms) current = fn(current);
  return current;
}

const transforms: NumberTransform[] = [];
transforms.push(n => n   100);
transforms.push(n => n   20);

const result = transformNumber(3, transforms);
console.log(result); // 123

CodePudding user response:

I think what you need is just two variables for your functions instead of reassign the f1.

const f1 = (x: number) => x   100;
const f2 = (x: number) => f1(x)   20;
f2(3) // 123

Based on the updated code sample, is this something that accomplish what you are talking about?

function makeCatActionChain(makeCatSleep: boolean, makeCatEat: boolean) {
    if(makeCatSleep) {
        return (cat: Cat) => cat.drink().sleep();
    }

    if(makeCatEat) {
        return (cat: Cat) => cat.drink().eat();
    }

    //Assuming you always want the cat to drink
    return (cat: Cat) => cat.drink();
}
  • Related