Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

154
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda