-->

Good names for flipped versions of `lt`, `lte`, `g

2019-01-22 09:19发布

问题:

I've been working for some time on a Javascript FP library called Ramda, and I'm having a slight problem with naming things. (You've heard the old line, right? "There are only two hard problems in Computer Science: cache invalidation, naming things, and off-by-one errors.")

In this library, (almost) every function of more than one parameter is automatically curried. And this works well for most use-cases. But there are some issues with a few functions which are non-commutative binary operators. The issue is that the English names often tend to imply something different than what happens when currying is applied. For example,

var div10 = divide(10);

sounds like it should be a function that divides its parameter by 10. But in fact it divides its parameter into 10, which is pretty clear if you look at the definition:

var divide = curry(function(a, b) {
    return a / b;
});

So instead the expected:

div10(50); //=> 5 // NO!!

In fact, you get

div10(50); //=> 0.2 // Correct, but surprising!

We handle this by documenting the difference from people's possible expectations, and creating divideBy, which is just flip(divide) and subtractN, which is flip(subtract). But we haven't found a good equivalent for functions such as lt:

R.lt = curry(function(a, b) { 
    return a < b;
});

or its cousins lte, gt, and gte.

My own intuition would be that

map(lt(5), [8, 6, 7, 5, 3, 0, 9]); 
//=> [false, false, false, false, true, true, false]

But of course, it actually returns

//=> [true, true, true, false, false, false, true]

So I'd like to do the same document-and-point-to-alternate-name routine for lt and its ilk. But I haven't been able to find a good name. The only real candidate has been ltVal and that doesn't really work when called with both arguments. We did discuss this issue, but had no good conclusions.

Have others dealt with this and come up with good solutions? Or even if not, any good suggestions for a name for the flipped versions of these functions?


Update

Someone suggested that this be closed because 'unclear what you were asking', and I guess the question really was lost a bit in the explanation. The simple question is:

What would be a good, intuitive name for a flipped version of lt?

回答1:

First of all, kudos on the functional programming library that you are maintaining. I've always wanted to write one myself, but I've never found the time to do so.

Considering the fact that you are writing a functional programming library I'm going to assume that you know about Haskell. In Haskell we have functions and operators. Functions are always prefix. Operators are always infix.

Functions in Haskell can be converted into operators using backticks. For example div 6 3 can be written as 6 `div` 3. Similarly operators can be converted into functions using parentheses. For example 2 < 3 can be written as (<) 2 3.

Operators can also be partially applied using sections. There are two types of sections: left sections (e.g. (2 <) and (6 `div`)) and right sections (e.g. (< 3) and (`div` 3)). Left sections are translated as follows: (2 <) becomes (<) 2. Right sections: (< 3) becomes flip (<) 3.


In JavaScript we only have functions. There is no “good” way to create operators in JavaScript. You can write code like (2).lt(3), but in my humble opinion it is uncouth and I would strongly advise against writing code like that.

So trivially we can write normal functions and operators as functions:

div(6, 3) // normal function: div 6 3
lt(2, 3) // operator as a function: (<) 2 3

Writing and implementing infix operators in JavaScript is a pain. Hence we won't have the following:

(6).div(3) // function as an operator: 6 `div` 3
(2).lt(3) // normal operator: 2 < 3

However sections are important. Let's start with the right section:

div(3) // right section: (`div` 3)
lt(3) // right section: (< 3)

When I see div(3) I would expect it to be a right section (i.e. it should behave as (`div` 3)). Hence, according to the principle of least astonishment, this is the way it should be implemented.

Now comes the question of left sections. If div(3) is a right section then what should a left section look like? In my humble opinion it should look like this:

div(6, _) // left section: (6 `div`)
lt(2, _) // left section: (2 <)

To me this reads as “divide 6 by something” and “is 2 lesser than something?” I prefer this way because it is explicit. According to The Zen of Python, “Explicit is better than implicit.”


So how does this affect existing code? For example, consider the filter function. To filter the odd numbers in a list we would write filter(odd, list). For such a function does currying work as expected? For example, how would we write a filterOdd function?

var filterOdd = filter(odd);    // expected solution
var filterOdd = filter(odd, _); // left section, astonished?

According to the principle of least astonishment it should simply be filter(odd). The filter function is not meant to be used as an operator. Hence the programmer should not be forced to use it as a left section. There should be a clear distinction between functions and “function operators”.

Fortunately distinguishing between functions and function operators is pretty intuitive. For example, the filter function is clearly not a function operator:

filter odd list -- filter the odd numbers from the list; makes sense
odd `filter` list -- odd filter of list? huh?

On the other hand the elem function is clearly a function operator:

list `elem` n -- element n of the list; makes sense
elem list n -- element list, n? huh?

It's important to note that this distinction is only possible because functions and function operators are mutually exclusive. It stands to reason that given a function it may either be a normal function or else a function operator, but not both.


It's interesting to note that given a binary function if you flip its arguments then it becomes a binary operator and vice versa. For example consider the flipped variants of filter and elem:

list `filter` odd -- now filter makes sense an an operator
elem n list -- now elem makes sense as a function

In fact this could be generalized for any n-arity function were n is greater than 1. You see, every function has a primary argument. Trivially, for unary functions this distinction is irrelevant. However for non-unary functions this distinction is important.

  1. If the primary argument of the function comes at the end of the argument list then the function is a normal function (e.g. filter odd list where list is the primary argument). Having the primary argument at the end of the list is necessary for function composition.
  2. If the primary argument of the function comes at the beginning of the argument list then the function is a function operator (e.g. list `elem` n where list is the primary argument).
  3. Operators are analogous to methods in OOP and the primary argument is analogous to the object of the method. For example list `elem` n would be written as list.elem(n) in OOP. Chaining methods in OOP is analogous to function composition chains in FP[1].
  4. The primary argument of the function may only be either at the beginning or at the end of the argument list. It wouldn't make sense for it to be anywhere else. This property is vacuously true for binary functions. Hence flipping binary functions makes them operators and vice-versa.
  5. The rest of the arguments along with the function form an indivisible atom called the stem of the argument list. For example in filter odd list the stem is filter odd. In list `elem` n the stem is (`elem` n).
  6. The order and the elements of the stem must remain unchanged for the expression to make sense. This is why odd `filter` list and elem list n don't make any sense. However list `filter` odd and elem n list make sense because the stem is unchanged.

Coming back to the main topic, since functions and function operators are mutually exclusive you could simply treat function operators differently than the way you treat normal functions.

We want operators to have the following behavior:

div(6, 3) // normal operator: 6 `div` 3
div(6, _) // left section: (6 `div`)
div(3)    // right section: (`div` 3)

We want to define operators as follows:

var div = op(function (a, b) {
    return a / b;
});

The definition of the op function is simple:

function op(f) {
    var length = f.length, _; // we want underscore to be undefined
    if (length < 2) throw new Error("Expected binary function.");
    var left = R.curry(f), right = R.curry(R.flip(f));

    return function (a, b) {
        switch (arguments.length) {
        case 0: throw new Error("No arguments.");
        case 1: return right(a);
        case 2: if (b === _) return left(a);
        default: return left.apply(null, arguments);
        }
    };
}

The op function is similar to using backticks to convert a function into a operator in Haskell. Hence you could add it as a standard library function for Ramda. Also mention in the docs that the primary argument of an operator should be the first argument (i.e. it should look like OOP, not FP).


[1] On a side note it would be awesome if Ramda allowed you to compose functions as though it was chaining methods in regular JavaScript (e.g. foo(a, b).bar(c) instead of compose(bar(c), foo(a, b))). It's difficult, but doable.



回答2:

We all know that naming in programming is serious business, particularly when it comes to functions in curried form. It is a valid solution to handle this issue with a programmatic approach as Aadit did in his response. However, I see two issues with his implementation:

  • it introduces function operators with left/right sections in Javascript, which are not part of the language
  • it requires a hideous placeholder or undefined hack to achieve that

Javascript has no curried operators and thus no left or right sections. An idiomatic Javascript solution should consider that.

The cause of the problem

Curried functions don't have a notion of arity, because every function invocation requires exactly one argument. You can either partially or completely apply curried functions without any helpers:

const add = y => x => x + y;

const add2 = add(2); // partial application

add(2)(3); // complete application

Usually the last argument of a function is its primarily one, because it is passed through function compositions (similar to objects that allow method chaining). Consequently when you partially apply a function, you want to pass its initial arguments:

const comp = f => g => x => f(g(x));

const map = f => xs => xs.map(x => f(x));

const inc = x => x + 1;

const sqr = x => x * x;


comp(map(inc)) (map(sqr)) ([1,2,3]); // [2,5,10]

Operator functions are special in this regard. They are binary functions that reduce their two arguments to a single return value. Since not every operator is commutative (a - b !== b - a) the argument order matters. For this reason operator functions don't have a primarily argument. But people are accustomed to read expressions with them in a certain way depending on the type of application:

const concat = y => xs => xs.concat(y);

const sub = y => x => x - y;


// partial application:

const concat4 = concat(4);

const sub4 = sub(4);

concat4([1,2,3]); // [1,2,3,4] - OK

sub4(3); // -1 - OK


// complete application:

concat([1,2,3]) (4); // [4,1,2,3] - ouch!

sub(4) (3); // -1 - ouch!

We defined concat and sub with flipped arguments, so that partial application works as expected. This evidently doesn't apply to complete application though.

A manual solution

const flip = f => y => x => f(x) (y);

const concat_ = flip(concat);

const sub_ = flip(sub);


concat_(xs) (4); // [1,2,3,4] - OK

sub_(4) (3); // 1 - OK

concat_ and sub_ correspond to left sections in Haskell. Please note that function operators like add or lt don't need a left section version because the former are commutative and the latter are predicates, which have logical counterparts:

const comp2 = f => g => x => y => f(g(x) (y));

const map = f => xs => xs.map(x => f(x));

const flip = f => y => x => f(x) (y);

const not = x => !x;

const notf2 = comp2(not);


const lt = y => x => x < y;

const gt = flip(lt);

const lte = notf2(gt);

const gte = notf2(lt);


map(lt(5)) ([8, 6, 7, 5, 3, 0, 9]);
// [false, false, false, false, true, true, false]

map(gte(5)) ([8, 6, 7, 5, 3, 0, 9]);
// [true, true, true, true, false, false, true]

Conclusion

We should solve this naming issue rather with a naming convention then with a programmatic solution which extends Javascript in a non-idiomatic way. Naming conventions are not ideal...well, just like Javascript.