Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Wrapping the arguments or result of an anonymous function

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.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
about 4 years ago · Santiago Trujillo Report

0

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();
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!