What is folktale?
Folktale is a standard library for functional programming in JavaScript. It provides a set of tools to help developers write more predictable and maintainable code by using functional programming patterns.
What are folktale's main functionalities?
Data.Task
Data.Task is used for handling asynchronous operations in a functional way. It allows you to compose tasks and handle success and failure cases explicitly.
const { task } = require('folktale/concurrency/task');
const readFile = (filename) => task(resolver => {
require('fs').readFile(filename, 'utf8', (err, data) => {
if (err) resolver.reject(err);
else resolver.resolve(data);
});
});
readFile('example.txt').run().listen({
onRejected: (error) => console.error('Error:', error),
onResolved: (data) => console.log('File content:', data)
});
Data.Maybe
Data.Maybe is used to represent optional values. It helps in avoiding null or undefined values by providing a way to handle the presence or absence of a value explicitly.
const { Just, Nothing } = require('folktale/maybe');
const findUser = (username) => {
const users = { alice: 'Alice', bob: 'Bob' };
return users[username] ? Just(users[username]) : Nothing();
};
const user = findUser('alice');
user.matchWith({
Just: ({ value }) => console.log('User found:', value),
Nothing: () => console.log('User not found')
});
Data.Either
Data.Either is used for computations that can fail. It represents a value that can be either a success (Right) or a failure (Left), allowing you to handle both cases explicitly.
const { Left, Right } = require('folktale/either');
const parseJSON = (jsonString) => {
try {
return Right(JSON.parse(jsonString));
} catch (e) {
return Left(e.message);
}
};
const result = parseJSON('{ "name": "Alice" }');
result.matchWith({
Right: ({ value }) => console.log('Parsed JSON:', value),
Left: ({ value }) => console.log('Error:', value)
});
Other packages similar to folktale
ramda
Ramda is a practical functional library for JavaScript programmers. It focuses on immutability and side-effect-free functions, similar to Folktale. However, Ramda is more focused on providing utility functions for data transformation and manipulation, whereas Folktale provides a more comprehensive set of tools for functional programming patterns.
lodash-fp
Lodash-fp is a version of Lodash with its methods wrapped to produce immutable, auto-curried, iteratee-first, and data-last methods. It offers functional programming utilities similar to Folktale, but it is more focused on providing a functional interface to Lodash's utility functions rather than a complete functional programming toolkit.
monet
Monet is a library that provides a set of functional programming constructs such as Maybe, Either, and IO. It is similar to Folktale in that it offers algebraic data types and functional patterns. However, Monet is more focused on providing a set of monadic structures, while Folktale offers a broader range of functional programming tools.
Folktale
Folktale is a standard library for functional programming in JavaScript.
Installing
Folktale can be installed through npm:
$ npm install folktale
Not using Node.js? Check out our guide to running Folktale in other environments!
Documentation
If you find any functionality that's not documented, or whose documentation
is confusing or hard to understand, please report a bug on our issue tracker
Supported platforms
Folktale is written for ECMAScript 2015 platforms, but it uses a subset of features that can
be safely backported to platforms as old as ECMAScript 3. If you're running your program in
an older platform, you'll need es5-shim and es6-shim.
Browsers:
- Chrome 36+
- Firefox 21+
- Safari 6+
- Internet Explorer 9+
- Edge 13+
- Opera 12.12+
Mobile browsers:
Support
Note that all interactions in this project are subject to Origami Tower's
Code of Conduct.
Having trouble using Folktale?
Think Folktale should do something it doesn't yet?
By submitting a Pull Request you agree with releasing your code under the MIT licence.
Folktale discussion channels and information
Licence
Folktale is copyright (c) Quildreen Motta 2015-2017, and released under the MIT licence.
See the LICENCE
file in this repository for detailed information.