CrowdHailer / fn.js

A JavaScript library built to encourage a functional programming style & strategy. - http://eliperelman.com/fn.js
MIT License
399 stars 30 forks source link

Change signature of concat? #11

Closed jpotterm closed 10 years ago

jpotterm commented 10 years ago

I was wondering if you'd consider changing the signature of the concat method to take an array of arrays which would match Haskell and Underscore.js. The way I see it, concat has two use cases: concatenating individual arrays, and concatenating an array of arrays.

For concatenating individual arrays the current way vs. proposed way would not be much of a change:

fn.concat(a, b)   // current
fn.concat([a, b]) // proposed

But concatenating an array of arrays would be much simpler and more readable:

fn.apply(fn.concat, collection) // current
fn.concat(collection)           // proposed

It would also be mappable without a partial application:

fn.map(fn.partial(fn.apply, fn.concat), collection) // current
fn.map(fn.concat, collection)                       // proposed

If you'd be willing to accept this change, I can submit a pull request.

jpotterm commented 10 years ago

Actually, after thinking about this a little more, the best of both worlds would probably be to keep fn.concat as it is and just add another function, something like:

fn.flatten = fn.partial(fn.apply, fn.concat);

I'll close this, and submit a pull request with the above function.

netpoetica commented 10 years ago

It may be worth creating a check early on in concat that checks for the proposed syntax (arguments[0] instanceof Array) and then performs a flatten internally