Convert Figma logo to code with AI

origamitower logofolktale

[not actively maintained!] A standard library for functional programming in JavaScript

2,040
102
2,040
30

Top Related Projects

Specification for interoperability of common algebraic structures in JavaScript

:see_no_evil: Refuge from unsafe JavaScript

23,757

:ram: Practical functional Javascript

59,602

A modern JavaScript utility library delivering modularity, performance, & extras.

2,480

🦋 Fantasy Land compliant (monadic) alternative to Promises

Quick Overview

Folktale is a standard library for functional programming in JavaScript. It provides a collection of tools for working with common algebraic structures, handling errors and asynchronous computations, and composing functions. The library aims to improve code reliability and expressiveness in JavaScript applications.

Pros

  • Comprehensive set of functional programming tools
  • Well-documented with clear examples
  • Type-safe with TypeScript support
  • Modular design allowing for tree-shaking

Cons

  • Learning curve for developers new to functional programming
  • May introduce additional complexity in simpler applications
  • Less popular compared to other functional libraries like Ramda or Lodash/FP

Code Examples

Example 1: Using Maybe for handling nullable values

import { Maybe } from 'folktale/maybe';

const getName = (user) => Maybe.fromNullable(user).map(u => u.name);

const user1 = { name: 'Alice' };
const user2 = null;

console.log(getName(user1).getOrElse('Unknown')); // Output: Alice
console.log(getName(user2).getOrElse('Unknown')); // Output: Unknown

Example 2: Using Result for error handling

import { Result } from 'folktale/result';

const divide = (a, b) =>
  b === 0 ? Result.Error('Division by zero') : Result.Ok(a / b);

console.log(divide(10, 2).getOrElse('Error')); // Output: 5
console.log(divide(10, 0).getOrElse('Error')); // Output: Error

Example 3: Using Task for asynchronous operations

import { task } from 'folktale/concurrency/task';

const fetchUser = (id) =>
  task((resolver) => {
    fetch(`https://api.example.com/users/${id}`)
      .then(response => response.json())
      .then(user => resolver.resolve(user))
      .catch(error => resolver.reject(error));
  });

fetchUser(1)
  .run()
  .listen({
    onResolved: user => console.log('User:', user),
    onRejected: error => console.error('Error:', error)
  });

Getting Started

To start using Folktale in your project:

  1. Install the library:

    npm install folktale
    
  2. Import the modules you need in your JavaScript files:

    import { Maybe } from 'folktale/maybe';
    import { Result } from 'folktale/result';
    import { task } from 'folktale/concurrency/task';
    
  3. Start using Folktale's functional programming tools in your code as shown in the examples above.

Competitor Comparisons

Specification for interoperability of common algebraic structures in JavaScript

Pros of Fantasy Land

  • Provides a specification for algebraic structures in JavaScript
  • Widely adopted by the functional programming community
  • Allows for interoperability between different libraries

Cons of Fantasy Land

  • More abstract and theoretical, which can be challenging for beginners
  • Lacks concrete implementations, focusing primarily on specifications
  • Requires additional libraries or custom implementations for practical use

Code Comparison

Fantasy Land (specification):

// Example of a Functor specification
const myFunctor = {
  map: (f) => myFunctor
};

Folktale (implementation):

const { task } = require('folktale');

const myTask = task((resolver) => {
  resolver.resolve(42);
});

Summary

Fantasy Land focuses on providing specifications for algebraic structures, while Folktale offers concrete implementations of functional programming concepts. Fantasy Land is more abstract and theoretical, making it suitable for library authors and advanced functional programmers. Folktale, on the other hand, provides ready-to-use implementations that are more accessible to developers looking to apply functional programming concepts in their projects.

:see_no_evil: Refuge from unsafe JavaScript

Pros of Sanctuary

  • More comprehensive documentation and examples
  • Stricter type checking and runtime type safety
  • Larger ecosystem with additional libraries and extensions

Cons of Sanctuary

  • Steeper learning curve due to its more rigorous approach
  • Potentially slower performance in some cases due to runtime type checks

Code Comparison

Sanctuary:

S.map(S.add(1), [1, 2, 3])
// => [2, 3, 4]

Folktale:

F.map([1, 2, 3], x => x + 1)
// => [2, 3, 4]

Both libraries provide functional programming utilities, but Sanctuary emphasizes type safety and mathematical correctness, while Folktale focuses on practical, everyday use cases. Sanctuary's approach may lead to more robust code but can be more challenging for beginners. Folktale offers a gentler learning curve and may be more suitable for projects where absolute type safety is less critical.

Sanctuary's stricter typing can catch more errors at runtime, potentially improving code quality. However, this comes at the cost of some performance overhead. Folktale, being more flexible, may offer better performance in certain scenarios but with fewer guarantees about type correctness.

The code comparison shows that both libraries provide similar functionality, but Sanctuary's syntax is more aligned with mathematical notation, while Folktale's is more reminiscent of traditional JavaScript methods.

23,757

:ram: Practical functional Javascript

Pros of Ramda

  • More comprehensive library with a wider range of utility functions
  • Better performance for large-scale applications
  • Stronger focus on functional programming paradigms

Cons of Ramda

  • Steeper learning curve for developers new to functional programming
  • Less emphasis on algebraic data types and monads
  • Larger bundle size, which may impact load times in web applications

Code Comparison

Ramda:

const numbers = [1, 2, 3, 4, 5];
const doubleEvens = R.pipe(
  R.filter(R.both(R.isEven, R.lt(R.__, 10))),
  R.map(R.multiply(2))
);
console.log(doubleEvens(numbers)); // [4, 8]

Folktale:

const { Maybe } = require('folktale/maybe');

const safeDiv = (a, b) =>
  b === 0 ? Maybe.Nothing() : Maybe.Just(a / b);

console.log(safeDiv(10, 2).getOrElse(0)); // 5
console.log(safeDiv(10, 0).getOrElse(0)); // 0

Both libraries offer functional programming tools, but Ramda focuses more on utility functions and data transformation, while Folktale emphasizes algebraic data types and monadic operations. Ramda's approach may be more familiar to JavaScript developers, while Folktale introduces concepts from languages like Haskell.

59,602

A modern JavaScript utility library delivering modularity, performance, & extras.

Pros of Lodash

  • Larger ecosystem and community support
  • More comprehensive set of utility functions
  • Better performance for many operations

Cons of Lodash

  • Larger bundle size, which can impact load times
  • Less focus on functional programming paradigms
  • Some functions may be redundant with modern JavaScript features

Code Comparison

Folktale (Maybe monad):

const { Maybe } = require('folktale');

const result = Maybe.fromNullable(someValue)
  .map(x => x * 2)
  .getOrElse(0);

Lodash (Similar functionality):

const _ = require('lodash');

const result = _.isNil(someValue) ? 0 : someValue * 2;

Folktale emphasizes a more functional approach with monads, while Lodash provides a more imperative style with utility functions. Folktale's code is more expressive for handling nullable values, but Lodash's approach may be more familiar to developers used to imperative programming.

Both libraries have their strengths, with Lodash offering a wider range of utility functions and Folktale focusing on functional programming concepts. The choice between them depends on the project's needs and the team's preferred programming paradigm.

2,480

🦋 Fantasy Land compliant (monadic) alternative to Promises

Pros of Fluture

  • More focused on asynchronous programming and futures
  • Provides a wider range of utility functions for working with futures
  • Better performance in handling large numbers of asynchronous operations

Cons of Fluture

  • Steeper learning curve due to its more specialized nature
  • Less comprehensive documentation compared to Folktale
  • Narrower scope, focusing primarily on futures and not other functional programming concepts

Code Comparison

Fluture:

const future = Future((reject, resolve) => {
  setTimeout(() => resolve('Hello'), 1000);
});

future.fork(console.error, console.log);

Folktale:

const task = Task.task(resolver => {
  setTimeout(() => resolver.resolve('Hello'), 1000);
});

task.run().listen({
  onResolved: console.log,
  onRejected: console.error
});

Both libraries provide ways to handle asynchronous operations, but Fluture's API is more focused on futures, while Folktale offers a broader range of functional programming tools. Fluture's approach may be more intuitive for developers working primarily with asynchronous code, while Folktale provides a more comprehensive toolkit for functional programming in JavaScript.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Folktale

Chat on Gitter NPM version Licence

NOTE: This package is not actively maintained anymore, because the maintainer stopped working with JavaScript. If you use this library and would like to take over maintaining it, reach out to @robotlolita on Twitter.

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:

  • Android 4.4+
  • iOS 8.3+

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.

NPM DownloadsLast 30 Days