Home

Awesome

fops

Functional Operators

When taking the functional approach in JavaScript, and especially when using libraries like Ramda that strongly encourage currying, having functional, curried versions of the langauge operators can be quite convenient.

For operators taking two arguments, and where it makes sense (i.e. not for + or *) there are "flipped" versions which take arguments in the opposite of the expected order. These are have the same names as the unflipped counterparts, preceded by "f" (fin() for the flipped version of the in operator). The equality operators don't have flipped versions for obvious reasons. This library also includes some "impure" functions (f.delete for instance) that should be used very carefully in functional programming. Further, there are a couple language constructs that are included as well, specifically "get" (as in a.b) and "set" (as in a.b = c). get is aliased as . and []; set() is aliased under .= and []=.

Two operators have been excluded because they don't make sense as functions in JavaScript, since arguments are evaluated at call time: void and ,.

Why?

There are other libraries like this one, so why make another? A few reasons:

API

Arithmetic Operators

Bitwise Operators

Bitwise operators are included for completeness. I almost never use them, and further, since they are often used in performance sensitve areas, they are probably a poor fit for functional programming in javascript, since function calls incur a substantial performance overhead relative to bitwise arithmetic.

Comparison Operators

Logical Operators

f.and(1)(true); // true
f.fand(1)(true); // 1
f.or(1)(true); // 1;
f.ffor(1)(true); // true
f.not(1); // false;

Unary Operators

var obj = {a: 1, b: 2};
f.delete(o)("a");
obj; // {b: 2}
var obj = {a: 1, b: 2};
f.delete("a")(o);
obj; // {b: 2}
f.typeof("") // "string"

Relational Operators

f.in("length")([]) // true
f.fin([])("length") // true
f.instanceof([])(Object) // true
f.instanceof(Object)([]) // true

Language Constructs

f.get({a: 1})("a"); // 1
f.fget("a")({a: 1}); // 1
var a = [];
f.set(a)(0)("x");
a; // ["x"]
var a = [];
f.fset(0)(a)("x");
a; // ["x"]

Misc

class F {
  constructor (a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }
}

f.new(F)(1, 2) // F {a: 1, b: 2, c: undefined}