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

253
Visualizações
I'm trying to understand how to remove arguments with partialy applying a function

How come words2 and words are giving the same result? I know some currying but I can't understand this example below.

function curry(fn) {
  const arity = fn.length;
  return function $curry(...args) {
    if (args.length < arity) {
      return $curry.bind(null, ...args);
    }
    return fn.call(null, ...args);
  };
}

const split = curry((sep, str) => str.split(sep));
const words = split(' ');
const words2 = str => split(' ', str)

console.log(words('something cool'))
console.log(words2('something cool'))

words equals to split(' ') function but what happens when I call words again with a string and how can it be equal to words2 function?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

When you apply curry to the split function it's like getting back two versions of the split function:

function split(sep, str){
  str.split(sep);
} 

and

function split(sep) {
  return function(str) {
   return str.split(sep);
  }
}

and which one is called is based on the number of parameters provided. So for the case of words you call the 2nd version with ' ' and then you call the returned function with 'something cool' which means that the actual code running is:

'something cool'.split(' ')

In the other case of words2 you call the 1st version and the actual code running:

'something cool'.split(' ')

That's why you get the same result.

about 4 years ago · Juan Pablo Isaza Relatório

0

Because split(' ', str) and split(' ')(str) gives the same result
And calling words2 calls the former while calling words calls the latter

about 4 years ago · Juan Pablo Isaza 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