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 peek 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);
Copied!

append

1
// append :: String -> String -> String
2
const append = flip(concat);
Copied!

chain

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

concat

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

eq

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

filter

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

flip

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

forEach

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

head

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

intercalate

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

join

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

last

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

map

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

match

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

prop

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

reduce

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

replace

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

reverse

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

safeHead

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

safeLast

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

safeProp

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

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));
Copied!

sortBy

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

split

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

take

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

toLowerCase

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

toString

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

toUpperCase

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

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));
Copied!

unsafePerformIO

1
// unsafePerformIO :: IO a -> a
2
const unsafePerformIO = io => io.unsafePerformIO();
Copied!
Last modified 8mo ago