Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add R.unapply#515

Merged
buzzdecafe merged 1 commit intoramda:masterfrom
davidchambers:unapply
Nov 13, 2014
Merged

add R.unapply#515
buzzdecafe merged 1 commit intoramda:masterfrom
davidchambers:unapply

Conversation

@davidchambers
Copy link
Member

I've wanted this confusing function several times in the last month or so. It stewed in the back of my mind while I thought of possible names. Today I decided the placeholder name, unapply, is in fact the right name, given the f/unapply(apply(f)) equivalence.

This function could be used to define the identity functions discussed in #511:

var identity1 = R.compose(R.nth(1), R.unapply(R.identity));
var identity2 = R.compose(R.nth(2), R.unapply(R.identity));

identity1('a', 'b', 'c');  // => 'b'
identity2('a', 'b', 'c');  // => 'c'

R.unapply is useful for the same reason these identity functions are useful: one can reference specific elements of the arguments object, facilitating composition. R.unapply is more general, though, so I'm confident other uses will emerge. Even having a function for creating arrays is useful at times:

R.unapply(R.identity)(1, 2, 3, 4, 5);  // => [1, 2, 3, 4, 5]

@CrossEye
Copy link
Member

👍 (once the build is passing!)

@davidchambers
Copy link
Member Author

once the build is passing!

That was deedpoll scolding me for using i as an identifier. I was seduced by the prospect of var a, b, c, d, e, f, g, i;. :)

@megawac
Copy link
Contributor

megawac commented Nov 13, 2014

Theres already a name for this which I'm trying to recall

-- Found it see lodash/lodash#756

Also, I've found used a method in the past in my own code, but don't see it being useful enough for inclusion 😐

@fyyyyy
Copy link
Contributor

fyyyyy commented Nov 13, 2014

I like this reversion of apply in the test, so unapply sounds like the correct name

g = R.unapply(R.apply(f)) === f;

Have a feeling this could be used for #484

@davidchambers
Copy link
Member Author

Are you on board, @buzzdecafe?

@megawac
Copy link
Contributor

megawac commented Nov 13, 2014

I'm +0 for this but it should be called spread to align with ES6

ramda.js Outdated
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be clearer to specify that you will get the function back:

@sig ([*...] -> a) -> (*... -> a)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Updated.

@buzzdecafe
Copy link
Member

ok by me.

not quite as powerful as ES6 spread, e.g. [1, 2, ...nums, 11, 12], for what that's worth.

@davidchambers
Copy link
Member Author

"Spreading" is synonymous with R.apply, no?

f(...iterableObj);

The above is roughly equivalent to:

R.apply(f, iterableObj);

In my view "apply" is better name for this function than "spread". From Wikipedia:

Apply is also the name of a special function in many languages, which takes a function and a list, and uses the list as the function's own argument list, as if the function were called with the elements of the list as the arguments.

There's potential for confusion, of course, as JavaScript functions have an apply method which serves almost the same purpose, but in Ramda code one is rarely concerned with this.

I prefer R.apply+R.unapply to R.spread+R.unspread.

@buzzdecafe
Copy link
Member

ok with the code & docs--thoughts on the name @CrossEye ?

@CrossEye
Copy link
Member

The code is fine... the docs work... either name is definitely better than splat/unsplat. I think our apply is very close to the language's apply; let's keep this inverse named similarly. 👍 for unapply, even if on it's own it makes little sense. (Sounds as though I don't want the job anymore...)

@davidchambers
Copy link
Member Author

One of you could hit the green button. :)

buzzdecafe added a commit that referenced this pull request Nov 13, 2014
@buzzdecafe buzzdecafe merged commit 59937fb into ramda:master Nov 13, 2014
@davidchambers davidchambers deleted the unapply branch November 13, 2014 19:12
@CrossEye
Copy link
Member

Is that what the green button is for? I get it! 😈

@megawac megawac mentioned this pull request Nov 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants