Point-free Functions

While it can seem natural to work with all these containers in a fluent fashion, it can get cumbersome and hard to get a lot of reuse out of. A way to really get the most out of re-usability in JavaScript is to take what is called a point-free approach. Below is a small code same to contrast the difference between the two calling styles:

import map from "crocks/pointfree/map";
import compose from "crocks/helpers/compose";
import isInteger from "crocks/predicates/isInteger";
import safe from "crocks/Maybe/safe";
// isEven :: Integer -> Boolean
const isEven = (x) => x % 2 === 0;
// maybeInt :: a -> Maybe Integer
const maybeInt = safe(isInteger);
// fluentIsEven :: a -> Maybe Boolean
const fluentIsEven = (data) => maybeInt(data).map(isEven);
// pointfreeIsEven :: a -> Maybe Boolean
const pointfreeIsEven = compose(map(isEven), maybeInt);
fluentIsEven(5);
//=> Just false
fluentIsEven("number");
//=> Nothing
fluentIsEven(6);
//=> Just true
pointfreeIsEven(5);
//=> Just false
pointfreeIsEven("not even");
//=> Nothing
pointfreeIsEven(6);
//=> Just true

These functions provide a very clean way to build out very simple functions and compose them all together to compose a more complicated flow. Each point-free function provided in crocks is "auto-curried" and follows a "data-last" pattern in the order of how it receives it's arguments. Typically the most stable of the arguments comes first, moving all the way to the least stable argument (which usually is the data flowing through your composition). Below lists the provided functions and the data types they work with (m refers to an accepted Datatype):

Signatures#

FunctionSignatureLocation
altm a -> m a -> m acrocks/pointfree
apm a -> m (a -> b) -> m bcrocks/pointfree
bichain(a -> m c d) -> (b -> m c d) -> m a b -> m c dcrocks/pointfree
bimap(a -> c) -> (b -> d) -> m a b -> m c dcrocks/pointfree
bothm (a -> b) -> m (Pair a a -> Pair b b)crocks/pointfree
chain(a -> m b) -> m a -> m bcrocks/pointfree
coalesce(a -> c) -> (b -> c) -> m a b -> m _ ccrocks/pointfree
compareWitha -> a -> m a -> bcrocks/pointfree
concatm a -> m a -> m acrocks/pointfree
consa -> m a -> m acrocks/pointfree
contramap(b -> a) -> m a -> m bcrocks/pointfree
either(a -> c) -> (b -> c) -> m a b -> ccrocks/pointfree
emptym -> mcrocks/pointfree
equalsm -> m -> Booleancrocks/pointfree
evalWiths -> m -> acrocks/State
execWiths -> m -> scrocks/State
extend(m a -> b) -> m a -> m bcrocks/pointfree
filter((a -> Boolean) | Pred a) -> m a -> m acrocks/pointfree
firstm (a -> b) -> m (Pair a c -> Pair b c)crocks/pointfree
foldSemigroup s => m s -> scrocks/pointfree
foldMapSemigroup s => (a -> s) -> m a -> scrocks/pointfree
fstm a b -> acrocks/Pair
headm a -> Maybe acrocks/pointfree
initm a -> Maybe (m a)crocks/pointfree
lastm a -> Maybe acrocks/pointfree
logm a b -> acrocks/Writer
map(a -> b) -> m a -> m bcrocks/pointfree
merge(a -> b -> c) -> m a b -> ccrocks/pointfree
nmapInteger -> ...(* -> *) m ...* -> m ...*crocks/Tuple
optiona -> m a -> acrocks/pointfree
projectInteger -> m ...* -> acrocks/Tuple
promap(c -> a) -> (b -> d) -> m a b -> m c dcrocks/pointfree
racem e a -> m e a -> m e acrocks/Async
readm a b -> Pair a bcrocks/Writer
reduce(b -> a -> b) -> b -> m a -> bcrocks/pointfree
reduceRight(b -> a -> b) -> b -> m a -> bcrocks/pointfree
reject((a -> Boolean) | Pred a) -> m a -> m acrocks/pointfree
runm a -> bcrocks/pointfree
runWitha -> m -> bcrocks/pointfree
secondm (a -> b) -> m (Pair c a -> Pair c b)crocks/pointfree
sequenceApplicative TypeRep t, Apply f => (t | (b -> f b)) -> m (f a) -> f (m a)crocks/pointfree
sndm a b -> bcrocks/Pair
swap(c -> d) -> (a -> b) -> m c a -> m b dcrocks/pointfree
tailm a -> Maybe (m a)crocks/pointfree
traverseApplicative TypeRep t, Apply f => (t | (c -> f c)) -> (a -> f b) -> m (f a) -> f (m b)crocks/pointfree
valueOfm a -> acrocks/pointfree

Datatypes#

FunctionDatatypes
altAsync, Either, Maybe, Result
apArray, Async, Const, Either, Identity, IO, List, Maybe, Pair, Reader, Result, State, Unit, Writer
bichainAsync, Either, Maybe, Result
bimapAsync, Either, Pair, Result
bothArrow, Function, Star
chainArray, Async, Const, Either, Identity, IO, List, Maybe, Pair, Reader, Result, State, Unit, Writer
coalesceAsync, Either, Maybe, Result
compareWithEquiv
concatAll, Any, Array, Assign, Const, Either, Endo, Equiv, First, Identity, Last, List, Max, Maybe, Min, Pair, Pred, Prod, Result, String, Sum, Tuple, Unit
consArray, List
contramapArrow, Equiv, Pred, Star
eitherEither, Maybe, Result
emptyAll, Any, Array, Assign, Endo, Equiv, First, Last, List, Max, Min, Object, Pred, Prod, String, Sum, Unit
equalsAll, Any, Array, Assign, Boolean, Const, Either, First, Last, List, Max, Maybe, Min, Number, Object, Pair, Prod, Result, String, Sum, Tuple, Unit, Writer
evalWithState
execWithState
extendPair
filterArray, List, Object
firstArrow, Function, Star
foldArray, List
foldMapArray, List
fstPair
headArray, List, String
initArray, List, String
lastArray, List, String
logWriter
mapAsync, Array, Arrow, Const, Either, Function, Identity, IO, List, Maybe, Object, Pair, Reader, Result, Star, State, Tuple, Unit, Writer
mergePair, Tuple
optionFirst, Last, Maybe
promapArrow, Star
raceAsync
readWriter
reduceArray, List
reduceRightArray, List
rejectArray, List, Object
runIO
runWithArrow, Endo, Pred, Reader, Star, State
secondArrow, Function, Star
sequenceArray, Either, Identity, List, Maybe, Pair, Result
sndPair
swapAsync, Either, Pair, Result
tailArray, List, String
traverseArray, Either, Identity, List, Maybe, Pair, Result
valueOfAll, Any, Assign, Const, Endo, Equiv, First, Identity, Last, Max, Min, Pred, Prod, Sum, Unit, Writer