functionalify

0.0.1 • Public • Published

functionalify

Functional programming tools with close integration with JavaScript.

Sample Usage

var functools = require("./index");

Motivation

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:

  1. 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.
  2. Contributing new, but necessary, data structures (maps, sets, and maybes (options)).
    • Yes, ES6 does define maps and sets. Ours are immutable.
  3. 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:

Finding the value of a query string param

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"); // ""

Grouping values together

const evensAndOdds = (10).times.groupBy(function(val) {
    return val % 2 ? "odd" : "even";
}); // { even: [ 0, 2, 4, 6, 8 ], odd: [ 1, 3, 5, 7, 9 ] }

API

Array

Class Methods

  • from(array:Array-Like)
    • returns array
Array.from(document.querySelectorAll("p")) // Array[HTMLElement]

Package Sidebar

Install

npm i functionalify

Weekly Downloads

2

Version

0.0.1

License

MIT

Last publish

Collaborators

  • kklimuk