Appendix C: Pointfree Utilities
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) -> (b -> a)
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));
last
// last :: [a] -> a
const last = xs => xs[xs.length - 1];
map
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
const reverse = x => Array.isArray(x) ? x.reverse() : x.split('').reverse().join('');
safeHead
// safeHead :: [a] -> Maybe a
const safeHead = compose(Maybe.of, head);
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) => {
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();
// unsafePerformIO :: IO a -> a