Functional programming tools with close integration with JavaScript.
npm install functionalify
var functionalify = require("functionalify");
JavaScript provides us with first-class functions to build great functional programs. Unfortunately, it also doesn't provide us with either the data structures or the methods to do so. functionalify fills in these holes by:
- Adding immutable, monadic, and other operations and useful to existing data structures (arrays and objects).
- Yes, modifying prototypes is considered a no-no. But now you can use existing APIs in a functional way.
- Contributing new, but necessary, data structures (maps, sets, and maybes (options)).
- Yes, ES6 does define maps and sets. Ours are immutable.
- Making all of the above work with the common frameworks/libraries of today.
- Yes, we realize that some libraries can overwrite our methods. We'll let them.
Below are some motivating examples:
function getParamValue(search, param) {
return search.slice(1).split("&")
.find(function(component) { return component.startswith(param + "=") })
.flatMap(function(item) { return item.split("=").last })
.getOrElse("");
}
const search = "?foo=bar&bar=baz&taco=belle";
getParamValue(search, "foo"); // "bar"
getParamValue(search, "freedom"); // ""
const evensAndOdds = (10).times.groupBy(function(val) {
return val % 2 ? "odd" : "even";
}); // { even: [ 0, 2, 4, 6, 8 ], odd: [ 1, 3, 5, 7, 9 ] }
from(array:Array-Like)
- returns array
Array.from(document.querySelectorAll("p")) // Array[HTMLElement]