Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a counter-like predicate #24

Open
lazdmx opened this issue Sep 13, 2014 · 0 comments
Open

Add a counter-like predicate #24

lazdmx opened this issue Sep 13, 2014 · 0 comments

Comments

@lazdmx
Copy link

lazdmx commented Sep 13, 2014

I suggest to add some general counting functions into lib, which is useful with methods like partitionBy, #map, ...

function plus_if( fn, init ){
  var v = init || 0;
  return function( ){
    return v += fn.apply( null, arguments );
  }
}

It can be use like this:

var barriers = [ 1, 0, 0, 1, 0, 1, 1, 0, 0, 0 ];
sections = _.partitionBy( barriers, plus_if( _.identity ) );


// => [ [ 1, 0, 0 ], [ 1, 0 ], [ 1 ], [ 1, 0, 0, 0 ] ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants