Fairmont
A collection of useful JavaScript functions to support a functional style of programming that takes advantage of ES6 features like iterators, generators, and promises. Fairmont is inspired by Underscore, EssentialJS, and prelude.coffee.
Why Fairmont?
Fairmont offers a combination of features we couldn't find in existing libraries. In particular, we wanted:
-
To use a functional programming style, even when performing asynchronous operations…
-
While coming as close to inline code for performance as possible (read: use lazy evaluation when working with collections)…
-
Taking full-advantage of ES6 features like iterators, generators, and promises, which offer powerful new ways of doing things
For example, here's how we would define a function that takes a path and returns a content-addressable dictionary of the files it contains.
content_map = async (path) ->
paths = collect map (compose resolve, join path), yield readdir path
assoc zip (map (compose md5, read), paths), paths
We've seamlessly integrated asychronous functions with synchronous functions, even when doing composition. Behind the scenes we're using iterators to avoid multiple passes across the data. We make two passes here, even though it appears that we're making five.
Fairmont is also a literate programming project—the documentation, code, examples, and tests are together, making it easy to see what a function does, how it does it, and why it does it that particular way.
List of Functions
Core Functions
identity, wrap, curry, _, partial, flip,
compose, pipe, variadic, unary, binary, ternary
Logical Functions
f_and, f_or, negate, f_eq, f_neq
Numeric Functions
gt, lt, gte, lte, add, sub, mul, div, mod,
even, odd, min, max
Type Functions
deep_equal, type, is_type, instance_of,
is_string, is_function
Array functions
cat, slice, first, second, third, last, rest,
includes, unique_by, unique, uniq, dupes, union, intersection,
remove, shuffle
Iterator Functions
is_iterable, iterator, is_iterator, iterate,
collect, map, fold, foldr, select, reject, any, all, zip, unzip,
assoc, project, flatten, partition, take, leave, skip, sample
Crypto Functions
md5, base64, base64url
File System Functions
exists, stat, read, readdir, read_stream, read_block, lines,
write, chdir, rm, rmdir
Object Functions
include/extend, merge, clone, pluck, property, delegate, bind, detach
String Functions
capitalize, title_case, camel_case, underscored,
dashed, plain_text, html_escape, w
Miscellaneous Functions
times, shell, sleep, timer, memoize, abort
Status
Fairmont is still under heavy development and is alpha
quality, meaning you should probably not use it in your production code.
Roadmap
You can get an idea of what we're planning by looking at the issues list. If you want something that isn't there, and you think it would be a good addition, please open a ticket.
Our overarching goals for the project include:
-
Making the library more comprehensive
-
Improving the tests and documentation
-
Ensuring that we can use an FP style in real-world scenarios
-
Introducing an idiom for supporting lazy evaluation