Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda