mostly-adequate-guide
  • README
  • Chapter 01: What Ever Are We Doing?
    • Introductions
    • A Brief Encounter
  • Chapter 02: First Class Functions
    • A Quick Review
    • Why Favor First Class?
  • Chapter 03: Pure Happiness with Pure Functions
    • Oh to Be Pure Again
    • Side Effects May Include...
    • 8th Grade Math
    • The Case for Purity
    • In Summary
  • Chapter 04: Currying
    • Can't Live If Livin' Is without You
    • More Than a Pun / Special Sauce
    • In Summary
    • Exercises
  • Chapter 05: Coding by Composing
    • Functional Husbandry
    • Pointfree
    • Debugging
    • Category Theory
    • In Summary
    • Exercises
  • Chapter 06: Example Application
    • Declarative Coding
    • A Flickr of Functional Programming
    • A Principled Refactor
    • In Summary
  • Chapter 07: Hindley-Milner and Me
    • What's Your Type?
    • Tales from the Cryptic
    • Narrowing the Possibility
    • Free as in Theorem
    • Constraints
    • In Summary
  • Chapter 08: Tupperware
    • The Mighty Container
    • My First Functor
    • Schrödinger's Maybe
    • Use Cases
    • Releasing the Value
    • Pure Error Handling
    • Old McDonald Had Effects...
    • Asynchronous Tasks
    • A Spot of Theory
    • In Summary
    • Exercises
  • Chapter 09: Monadic Onions
    • Pointy Functor Factory
    • Mixing Metaphors
    • My Chain Hits My Chest
    • Power Trip
    • Theory
    • In Summary
    • Exercises
  • Chapter 10: Applicative Functors
    • Applying Applicatives
    • Ships in Bottles
    • Coordination Motivation
    • Bro, Do You Even Lift?
    • Operators
    • Free Can Openers
    • Laws
    • In Summary
    • Exercises
  • Chapter 11: Transform Again, Naturally
    • Curse This Nest
    • A Situational Comedy
    • All Natural
    • Principled Type Conversions
    • Feature Envy
    • Isomorphic JavaScript
    • A Broader Definition
    • One Nesting Solution
    • In Summary
    • Exercises
  • Chapter 12: Traversing the Stone
    • Types n' Types
    • Type Feng Shui
    • Effect Assortment
    • Waltz of the Types
    • No Law and Order
    • In Summary
    • Exercises
  • Chapter 13: Monoids bring it all together
    • Wild combination
    • Abstracting addition
    • All my favourite functors are semigroups.
    • Monoids for nothing
    • Folding down the house
    • Not quite a monoid
    • Grand unifying theory
    • Group theory or Category theory?
    • In summary
    • Exercises
  • Appendix A: Essential Functions Support
    • always
    • compose
    • curry
    • either
    • identity
    • inspect
    • left
    • liftA2
    • liftA3
    • maybe
    • nothing
    • reject
  • Appendix B: Algebraic Structures Support
    • Compose
    • Either
    • Identity
    • IO
    • List
    • Map
    • Maybe
    • Task
  • Appendix C: Pointfree Utilities
    • add
    • append
    • chain
    • concat
    • eq
    • filter
    • flip
    • forEach
    • head
    • intercalate
    • join
    • last
    • map
    • match
    • prop
    • reduce
    • replace
    • reverse
    • safeHead
    • safeLast
    • safeProp
    • sequence
    • sortBy
    • split
    • take
    • toLowerCase
    • toString
    • toUpperCase
    • traverse
    • unsafePerformIO
Powered by GitBook
On this page
  • always
  • compose
  • curry
  • either
  • identity
  • inspect
  • left
  • liftA2
  • liftA3
  • maybe
  • nothing
  • reject

Was this helpful?

Appendix A: Essential Functions Support

Last updated 4 years ago

Was this helpful?

In this appendix, you'll find some basic JavaScript implementations of various functions described in the book. Keep in mind that these implementations may not be the fastest or the most efficient implementation out there; they solely serve an educational purpose.

In order to find functions that are more production-ready, have a peek at , , or .

Note that some functions also refer to algebraic structures defined in the

always

// always :: a -> b -> a
const always = curry((a, b) => a);

compose

// compose :: ((y -> z), (x -> y),  ..., (a -> b)) -> a -> z
const compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];

curry

// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
function curry(fn) {
  const arity = fn.length;

  return function $curry(...args) {
    if (args.length < arity) {
      return $curry.bind(null, ...args);
    }

    return fn.call(null, ...args);
  };
}

either

// either :: (a -> c) -> (b -> c) -> Either a b -> c
const either = curry((f, g, e) => {
  if (e.isLeft) {
    return f(e.$value);
  }

  return g(e.$value);
});

identity

// identity :: x -> x
const identity = x => x;

inspect

// inspect :: a -> String
const inspect = (x) => {
  if (x && typeof x.inspect === 'function') {
    return x.inspect();
  }

  function inspectFn(f) {
    return f.name ? f.name : f.toString();
  }

  function inspectTerm(t) {
    switch (typeof t) {
      case 'string':
        return `'${t}'`;
      case 'object': {
        const ts = Object.keys(t).map(k => [k, inspect(t[k])]);
        return `{${ts.map(kv => kv.join(': ')).join(', ')}}`;
      }
      default:
        return String(t);
    }
  }

  function inspectArgs(args) {
    return Array.isArray(args) ? `[${args.map(inspect).join(', ')}]` : inspectTerm(args);
  }

  return (typeof x === 'function') ? inspectFn(x) : inspectArgs(x);
};

left

// left :: a -> Either a b
const left = a => new Left(a);

liftA2

// liftA2 :: (Applicative f) => (a1 -> a2 -> b) -> f a1 -> f a2 -> f b
const liftA2 = curry((fn, a1, a2) => a1.map(fn).ap(a2));

liftA3

// liftA3 :: (Applicative f) => (a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
const liftA3 = curry((fn, a1, a2, a3) => a1.map(fn).ap(a2).ap(a3));

maybe

// maybe :: b -> (a -> b) -> Maybe a -> b
const maybe = curry((v, f, m) => {
  if (m.isNothing) {
    return v;
  }

  return f(m.$value);
});

nothing

// nothing :: Maybe a
const nothing = Maybe.of(null);

reject

// reject :: a -> Task a b
const reject = a => Task.rejected(a);
ramda
lodash
folktale
Appendix B