Appendix C: Pointfree Utilities

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 peak at
ramda, lodash, or folktale.

Note that functions refer to the curry & compose functions defined in Appendix A

add

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

chain

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

concat

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

eq

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

filter

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

flip

  1. // flip :: (a -> b) -> (b -> a)
  2. const flip = curry((fn, a, b) => fn(b, a));

forEach

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

head

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

intercalate

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

join

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

last

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

map

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

match

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

prop

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

reduce

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

replace

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

reverse

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

safeHead

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

safeLast

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

safeProp

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

sequence

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

sortBy

  1. // sortBy :: Ord b => (a -> b) -> [a] -> [a]
  2. const sortBy = curry((fn, xs) => {
  3. return xs.sort((a, b) => {
  4. if (fn(a) === fn(b)) {
  5. return 0;
  6. }
  7. return fn(a) > fn(b) ? 1 : -1;
  8. });
  9. });

split

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

take

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

toLowerCase

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

toString

  1. // toString :: a -> String
  2. const toString = String;

toUpperCase

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

traverse

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

unsafePerformIO

  1. // unsafePerformIO :: IO a -> a
  2. const unsafePerformIO = io => io.unsafePerformIO();