7

I understand the use of ramda's complement function which inverts the return of a predicate. What I can't get my head around is why "complement" was used as a name.

const invertPredicate = R.complement(R.identity)
invertPredicate(false) //true

Could someone please give some insight into this?

robstarbuck
  • 6,893
  • 2
  • 41
  • 40
  • [Inverting a function](https://en.wikipedia.org/wiki/Inverse_function) actually means something different. The inverse of a function `x -> boolean` would be a function `boolean -> x`. – Karl Bielefeldt Aug 03 '18 at 18:55

2 Answers2

9

“In set theory, the complement of a set A refers to elements not in A”

https://en.m.wikipedia.org/wiki/Complement_(set_theory)

So R.complement will produce a predicate for all values that don’t satisfy the provided predicate.

Scott Christopher
  • 6,458
  • 23
  • 26
7

Adding to the answer from Scott Christopher, Ramda (disclaimer: both Scott Christopher and I are Ramda authors) has gone through several iterations of these basic functions. At one point we had and, or, and not for values as well as andFn, orFn, and notFn for functions. We got rid of the value ones and used the nicer names for combining predicate functions.

But those value versions were popular. We brought them back. And we gave them back the simpler names, as that is how people read the infix operators || and && and the prefix !. It was easy to name the others as both and either, but we struggled a bit before finding complement. Once we find that one, we were happy enough to rename again. The actual explanation of the name is exactly as Scott Christopher says. I still have some regrets, but this was the correct decision for the library.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103