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

178
Visualizações
lodash funcional programming how to do partial application in _flow

I am using the latest lodash, the following code works, but I would like to remove the x => part, now I am forced to use because I cannot use partial application with _.orderBy

const x  = (r, f, o) => _.flow([_.cloneDeep, x => _.orderBy(x, [r], [o])])(r);

Do you have any idea how to change the code so it would

const x  = (r, f, o) => _.flow([_.cloneDeep, _.orderBy([r], [o])])(r);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use _.rearg() and _.curry() to get a new function which accepts the data (x) as the last parameter, and returns a new function if it receives less than 3 arguments:

const orderByCurried = _.curry(_.rearg(_.orderBy, [2, 0, 1]), 3);

const fn = (r, f, o) => orderByCurried([f], [o])(r);

It's not shorter than an arrow function x =>, but if you use this a lot...

Note: _.orderBy() doesn't effect the original array, so you don't need to clone it first.

Example (adapted from VLAZ's answer):

const orderByCurried = _.curry(_.rearg(_.orderBy, [2, 0, 1]), 3);

const fn = (r, f, o) => orderByCurried([f], [o])(r);

const users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 36 }
];

const sorted = fn(users, 'age', 'asc');

console.log(sorted);
console.log(users);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

Another option is to switch to lodash/fp, which has all functions with the arguments in the correct order, and all are curried.

about 4 years ago · Juan Pablo Isaza Relatório

0

The easiest way to accomplish this task is to use Lodash FP the functional programming friendly build of Lodash. It exposes the same functions but it rearranges the arguments to move the data to be last, then automatically curries them. It's more convenient as it simplifies many functional programming tasks and you do not need to change your code (other than using the correct build):

const x  = (r, f, o) => _.flow([_.cloneDeep, _.orderBy([f], [o])])(r);

const users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 36 }
];

console.log(x(users, 'age', 'asc'));
console.log(users); //unchanged
<script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>

If you are in a module environment, you can easily pick and choose which imports you use:

import flow from 'lodash/flow';
import cloneDeep from 'lodash/cloneDeep';
// or
import { flow, cloneDeep } from 'lodash';

import orderBy from 'lodash/fp/orderBy';
// or
import { orderBy } from 'lodash/fp';

But if you only have the regular Lodash build, then you can use the answer from Ori Drori to do the same sort of modification that Lodash FP does.

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