essential.js icon indicating copy to clipboard operation
essential.js copied to clipboard

promised sequence

Open coderofsalvation opened this issue 9 years ago • 3 comments

I tried to make minimal changes, because I like the micro-weight of essentialjs (opposed to ramda which is a whopping 1.6MB and offers a too much)

  • added aliases ('pipe' and 'seq' for 'sequence') to resemble other highorder libs
  • added documentation (using 'npm run-script gendoc') for README.md
  • map() and filter() now support objects as well (as show in Brian Lonsdorf's talk, 'Hey Underscore, You're Doing it Wrong')

this raises the question what difference 'forOwn' kinda o

coderofsalvation avatar Jan 02 '16 10:01 coderofsalvation

This looks great! Thanks for the good work. Let me review it in more detail.

elclanrs avatar Feb 09 '16 15:02 elclanrs

  1. Aliases: makes sense

  2. Documentation: great!

  3. Support for objects in map and filter: I like this in lodash myself. My question is, should all other functions like reduce, all, every, etc work with objects as well ala lodash? If that's the case, perhaps we can make a reusable HOF given that these look pretty similar:

map = ncurry 2, ->
  obj = false
  input = ( if isType 'Object', arguments[1] then (obj = unzipObject(arguments[1]))[1] else arguments[1] )
  result = input.map arguments[0]
  return ( if obj then zipObject obj[0], result else result )

filter = ncurry 2, ->
  obj = false
  input = ( if isType 'Object', arguments[1] then (obj = unzipObject(arguments[1]))[1] else arguments[1] )
  result = input.filter arguments[0]
  return ( if obj then zipObject obj[0], result else result )

Is it worth the weight? Otherwise, perhaps we name the object functions mapObject and filterObject?

  1. add, mul, sub: Similar concern as above. Does this mean people are going to miss a curried pow for example?

  2. either, bindAll: I like.

  3. module.exports.local: I think this would make more sense as an external library. Perhaps I'm missing the point of this, but with destructuring assignment neither expose or local would be ideal; I'd do the following:

function foo() {
  var {map, filter} = _;
}

I think this would be best practice, since it is more explicit, and there is no need for globals or evaling.

elclanrs avatar Feb 09 '16 16:02 elclanrs

3: agreed, it looks very similar. map and filter could be an shortcutfunction which calls one HOF. My preference would be to not split things up into mapObject/filterObject. I know, the general rule against it is garbage-in-garbage-out. But since its only purpose is to loop over key-based datastructures I think the sacrifice is worth the convenience.

.

Btw. I totally agree that the tiny-size of essentialjs has its beauty. Heck, the curry-ness and size is even the reason why i preferred it over lodash/lamda/underscore (uncurried or big in size) . 4: how about ditching add/mull/sub. I agree that it kinda invites future commits with pow / divide / log etc :)

.

6: Yes that works for ES6, not for ES5. I guess we could strip out local() and put it into a gist, and reference that url in the wiki/README for ES5 users etc. The privilege to use the HOF's in local scope (without a cluttering '_' or using global scope) has been a pleasure so far imho. This might be because I secretly love the functional looks of haskell,ruby,coffeescript etc.

Btw. have you checked the mapAsync function? I've found it to be very useful, and it pretty much eliminated my need for modules like async. It gives you control over the iteration in an async way, while the traditional map or for-loop does not.

coderofsalvation avatar Feb 26 '16 09:02 coderofsalvation