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 flip() function #36

Closed
tomek-he-him opened this issue Dec 17, 2014 · 9 comments · Fixed by #82
Closed

Add a flip() function #36

tomek-he-him opened this issue Dec 17, 2014 · 9 comments · Fixed by #82

Comments

@tomek-he-him
Copy link
Contributor

I've been reading issue #16 (closed now), and just wanted to bump the idea of having a flip() function.

The original proposition by @jfsiii:

flip (or reverseArguments, etc)

function flip(fn) {
  return function() {
    var args = [].slice.call(arguments).reverse();
    return fn.apply(this, args);
  };
}

It might seem silly to have a function which just reverses argument order, but it's a great help when you want to invoke a existing function which does what you want/need but has arguments in the opposite order.

Why it's great

In addition to the arguments posted already, I see one great use case – currying right-hand-side arguments.

In the JS world this is common: doGreatStuff(target, options). A function with optional arguments or an options object listed on the very right.

I often want to .bind() the optional arguments. Wouldn't it be great to do flip(doGreatStuff).bind(null, {candyFloss: true})?

@tjmehta
Copy link
Owner

tjmehta commented Dec 17, 2014

Nice comments can you think of any other scenarios? When I think of flip I tend to be able to solve it with with array.reduceRight(fn) or using a bindRight (https://github.com/tjmehta/bind-right).

@tomek-he-him
Copy link
Contributor Author

Well, the var pipe = flip(compose) from the original thread seems reasonable.

There are lots of other functions out there which take a variable number of arguments, where the order matters. Say, the native Array.

A simple and performant FILO queue:

var ReverseArray = flip(Array);
var queue = new ReverseArray(1, 2, 3);
queue.pop();  // 1
queue.pop();  // 2
queue.pop();  // 3

@tjmehta
Copy link
Owner

tjmehta commented Dec 17, 2014

Nice, ES5 has array.reverse though


Sent from Mailbox

On Wed, Dec 17, 2014 at 3:18 PM, Tomek Wiszniewski
notifications@github.com wrote:

Well, the var pipe = flip(compose) from the original thread seems reasonable.
There are lots of other functions out there which take a variable number of arguments, where the order matters. Say, the native Array.
A simple and performant FILO queue:

var ReverseArray = flip(Array);
var queue = new ReverseArray(1, 2, 3);
queue.pop();  // 1
queue.pop();  // 2
queue.pop();  // 3

Reply to this email directly or view it on GitHub:
#36 (comment)

@tomek-he-him
Copy link
Contributor Author

Yup, that was just a quick example from up the sleeve. But I guess the one
with compose is still valid.

Anyway, it would be great to have either flip or bindRight here. I think
both are good, but flip is more flexible.

On 17 December 2014 at 22:20, Tejesh Mehta notifications@github.com wrote:

Nice, ES5 has array.reverse though


Sent from Mailbox

On Wed, Dec 17, 2014 at 3:18 PM, Tomek Wiszniewski
notifications@github.com wrote:

Well, the var pipe = flip(compose) from the original thread seems
reasonable.
There are lots of other functions out there which take a variable number
of arguments, where the order matters. Say, the native Array.
A simple and performant FILO queue:

var ReverseArray = flip(Array);
var queue = new ReverseArray(1, 2, 3);
queue.pop(); // 1
queue.pop(); // 2
queue.pop(); // 3

Reply to this email directly or view it on GitHub:
#36 (comment)


Reply to this email directly or view it on GitHub
#36 (comment).

@tomek-he-him
Copy link
Contributor Author

One more concrete use case. flip(assign)(a, b, c) won't overwrite existing properties on a and b.

var a = {1: 'a'};
var b = {1: 'b', 2: 'b'};
var c = {1: 'c', 2: 'c', 3: 'c'};

assign(a, b, c);        // » {1: 'c', 2: 'c', 3: 'c'}
flip(assign)(a, b, c);  // » {1: 'a', 2: 'b', 3: 'c'}

@stoeffel
Copy link
Contributor

👍
flip is also nice with curry

var hasKeypaths = require('./has-keypaths');
var curry = require('./curry');
var flip = require('101/flip');

var hasFooBar = curry(flip(hasKeypaths), 2)(['foo.bar']);

hasFooBar({ foo: { bar : true } }); // true

btw checkout https://github.com/stoeffel/reverse-arguments used in https://github.com/stoeffel/underscore.string.fp
an example

@stoeffel
Copy link
Contributor

stoeffel commented Apr 9, 2015

I will create a PR for this too this weekend. If it's okay with you guys?

@tomek-he-him
Copy link
Contributor Author

👍

1 similar comment
@tjmehta
Copy link
Owner

tjmehta commented Apr 9, 2015

👍

stoeffel added a commit to stoeffel/101 that referenced this issue Apr 10, 2015
stoeffel added a commit to stoeffel/101 that referenced this issue Apr 10, 2015
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 a pull request may close this issue.

3 participants