Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create chain in lodash with custom functions

Is there way to get my own custom function in a chain of lodash. So for example like this:

var l = [1,2,3]
var add = function(a, b){return a+b}

var r =_.chain(l).find(function(a){return a>1}).add(5).value()

=>r = 7
like image 647
Andreas Köberle Avatar asked Jan 16 '14 13:01

Andreas Köberle


People also ask

Can you chain lodash functions?

To chain with lodash, you first have to wrap the object: _(foundUser). assignIn(rows[0]). omit(['blah']).

What is lodash FP?

The lodash/fp module promotes a more functional programming (FP) friendly style by exporting an instance of lodash with its methods wrapped to produce immutable auto-curried iteratee-first data-last methods.

What does lodash compose do?

compose — a function that accepts any number of functions as arguments. It then calls them from right to left, just the same as functions are called when you pass them as an argument. Fp. SortBy, uniqBy, filter, and map all accept the data last.


3 Answers

After @stride anwswer I came up with a more generic solution using _.mixin:

function add(a, b, c) {
  return a + b + c
}

function sub(a, b, c) {
  return a - b - c
}

_.mixin({
  run: function (v, f) {
    var args = Array.prototype.slice.call(arguments, 2)
    args.unshift(v)
    return f.apply(this, args)
  }
})
var r = _.chain(1).run(add, 1, 1).run(sub, 2, 2).value()

console.log(r) ->  -1
1 + 1 + 1 - 2 - 2 = -1

http://jsbin.com/iyEhaMa/1/

After all I wonder why this not a build in function in lodash.

like image 144
Andreas Köberle Avatar answered Oct 09 '22 01:10

Andreas Köberle


What you look for is a way to extend the lodash prototype. It so nicely turns out that you can do it easily with a mixin utility function. Check here the docs: http://lodash.com/docs#mixin

In your example it will look like:

var l = [1,2,3];
var  add = function(a, b){return a+b}


_.mixin({
    add: add 
});


var r =_.chain(l).find(function(a){return a>1}).add(5).value()
console.log(r); ==> 7

and here is live sample on fiddle: http://jsfiddle.net/g2A9C/

like image 17
stride Avatar answered Oct 09 '22 02:10

stride


Another option is to just drop chaining and leverage function composition through _.flow.

From the DOCS:

[Flow] Creates a function that returns the result of invoking the given functions with the this binding of the created function, where each successive invocation is supplied the return value of the previous.

This means that every function inside Flow will receive as input the output of the previous one. In practice this means we are not limited to using only Lodash API methods, but we can mix and match whatever function we fancy, as long as the next one is able to handle that return value.

var l = [1,2,3]
var add = _.curry((a, b) => a + b);

_.flow(
  _.find(a => a > 1),
  add(5),
)(l);

// => 7

NB - This example is using the Functional version of Lodash, if you don't want or can't use that one you can still achieve the same result, check my other answer to another question about Lodash.

like image 5
Aurelio Avatar answered Oct 09 '22 02:10

Aurelio