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
  • 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

Was this helpful?

Appendix C: Pointfree Utilities

Last updated 4 years ago

Was this helpful?

In this appendix, you'll find pointfree versions of rather classic JavaScript functions described in the book. All of the following functions are seemingly available in exercises, as part of the global context. 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 functions refer to the curry & compose functions defined in

add

// add :: Number -> Number -> Number
const add = curry((a, b) => a + b);

append

// append :: String -> String -> String
const append = flip(concat);

chain

// chain :: Monad m => (a -> m b) -> m a -> m b
const chain = curry((fn, m) => m.chain(fn));

concat

// concat :: String -> String -> String
const concat = curry((a, b) => a.concat(b));

eq

// eq :: Eq a => a -> a -> Boolean
const eq = curry((a, b) => a === b);

filter

// filter :: (a -> Boolean) -> [a] -> [a]
const filter = curry((fn, xs) => xs.filter(fn));

flip

// flip :: (a -> b -> c) -> b -> a -> c
const flip = curry((fn, a, b) => fn(b, a));

forEach

// forEach :: (a -> ()) -> [a] -> ()
const forEach = curry((fn, xs) => xs.forEach(fn));

head

// head :: [a] -> a
const head = xs => xs[0];

intercalate

// intercalate :: String -> [String] -> String
const intercalate = curry((str, xs) => xs.join(str));

join

// join :: Monad m => m (m a) -> m a
const join = m => m.join();

last

// last :: [a] -> a
const last = xs => xs[xs.length - 1];

map

// map :: Functor f => (a -> b) -> f a -> f b
const map = curry((fn, f) => f.map(fn));

match

// match :: RegExp -> String -> Boolean
const match = curry((re, str) => re.test(str));

prop

// prop :: String -> Object -> a
const prop = curry((p, obj) => obj[p]);

reduce

// reduce :: (b -> a -> b) -> b -> [a] -> b
const reduce = curry((fn, zero, xs) => xs.reduce(fn, zero));

replace

// replace :: RegExp -> String -> String -> String
const replace = curry((re, rpl, str) => str.replace(re, rpl));

reverse

// reverse :: [a] -> [a]
const reverse = x => (Array.isArray(x) ? x.reverse() : x.split('').reverse().join(''));

safeHead

// safeHead :: [a] -> Maybe a
const safeHead = compose(Maybe.of, head);

safeLast

// safeLast :: [a] -> Maybe a
const safeLast = compose(Maybe.of, last);

safeProp

// safeProp :: String -> Object -> Maybe a
const safeProp = curry((p, obj) => compose(Maybe.of, prop(p))(obj));

sequence

// sequence :: (Applicative f, Traversable t) => (a -> f a) -> t (f a) -> f (t a)
const sequence = curry((of, f) => f.sequence(of));

sortBy

// sortBy :: Ord b => (a -> b) -> [a] -> [a]
const sortBy = curry((fn, xs) => xs.sort((a, b) => {
  if (fn(a) === fn(b)) {
    return 0;
  }

  return fn(a) > fn(b) ? 1 : -1;
}));

split

// split :: String -> String -> [String]
const split = curry((sep, str) => str.split(sep));

take

// take :: Number -> [a] -> [a]
const take = curry((n, xs) => xs.slice(0, n));

toLowerCase

// toLowerCase :: String -> String
const toLowerCase = s => s.toLowerCase();

toString

// toString :: a -> String
const toString = String;

toUpperCase

// toUpperCase :: String -> String
const toUpperCase = s => s.toUpperCase();

traverse

// traverse :: (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b)
const traverse = curry((of, fn, f) => f.traverse(of, fn));

unsafePerformIO

// unsafePerformIO :: IO a -> a
const unsafePerformIO = io => io.unsafePerformIO();
ramda
lodash
folktale
Appendix A