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
Function | Signature | Location |
---|---|---|
alt | m a -> m a -> m a | crocks/pointfree |
ap | m a -> m (a -> b) -> m b | crocks/pointfree |
bichain | (a -> m c d) -> (b -> m c d) -> m a b -> m c d | crocks/pointfree |
bimap | (a -> c) -> (b -> d) -> m a b -> m c d | crocks/pointfree |
both | m (a -> b) -> m (Pair a a -> Pair b b) | crocks/pointfree |
chain | (a -> m b) -> m a -> m b | crocks/pointfree |
coalesce | (a -> c) -> (b -> c) -> m a b -> m _ c | crocks/pointfree |
compareWith | a -> a -> m a -> b | crocks/pointfree |
concat | m a -> m a -> m a | crocks/pointfree |
cons | a -> m a -> m a | crocks/pointfree |
contramap | (b -> a) -> m a -> m b | crocks/pointfree |
either | (a -> c) -> (b -> c) -> m a b -> c | crocks/pointfree |
empty | m -> m | crocks/pointfree |
equals | m -> m -> Boolean | crocks/pointfree |
evalWith | s -> m -> a | crocks/State |
execWith | s -> m -> s | crocks/State |
extend | (m a -> b) -> m a -> m b | crocks/pointfree |
filter | ((a -> Boolean) | Pred a) -> m a -> m a | crocks/pointfree |
first | m (a -> b) -> m (Pair a c -> Pair b c) | crocks/pointfree |
fold | Semigroup s => m s -> s | crocks/pointfree |
foldMap | Semigroup s => (a -> s) -> m a -> s | crocks/pointfree |
fst | m a b -> a | crocks/Pair |
head | m a -> Maybe a | crocks/pointfree |
init | m a -> Maybe (m a) | crocks/pointfree |
last | m a -> Maybe a | crocks/pointfree |
log | m a b -> a | crocks/Writer |
map | (a -> b) -> m a -> m b | crocks/pointfree |
merge | (a -> b -> c) -> m a b -> c | crocks/pointfree |
nmap | Integer -> ...(* -> *) m ...* -> m ...* | crocks/Tuple |
option | a -> m a -> a | crocks/pointfree |
project | Integer -> m ...* -> a | crocks/Tuple |
promap | (c -> a) -> (b -> d) -> m a b -> m c d | crocks/pointfree |
race | m e a -> m e a -> m e a | crocks/Async |
read | m a b -> Pair a b | crocks/Writer |
reduce | (b -> a -> b) -> b -> m a -> b | crocks/pointfree |
reduceRight | (b -> a -> b) -> b -> m a -> b | crocks/pointfree |
reject | ((a -> Boolean) | Pred a) -> m a -> m a | crocks/pointfree |
run | m a -> b | crocks/pointfree |
runWith | a -> m -> b | crocks/pointfree |
second | m (a -> b) -> m (Pair c a -> Pair c b) | crocks/pointfree |
sequence | Applicative TypeRep t, Apply f => (t | (b -> f b)) -> m (f a) -> f (m a) | crocks/pointfree |
snd | m a b -> b | crocks/Pair |
swap | (c -> d) -> (a -> b) -> m c a -> m b d | crocks/pointfree |
tail | m a -> Maybe (m a) | crocks/pointfree |
traverse | Applicative TypeRep t, Apply f => (t | (c -> f c)) -> (a -> f b) -> m (f a) -> f (m b) | crocks/pointfree |
valueOf | m a -> a | crocks/pointfree |
Datatypes
Function | Datatypes |
---|---|
alt | Async , Either , Maybe , Result |
ap | Array , Async , Const , Either , Identity , IO , List , Maybe , Pair , Reader , Result , State , Unit , Writer |
bichain | Async , Either , Maybe , Result |
bimap | Async , Either , Pair , Result |
both | Arrow , Function , Star |
chain | Array , Async , Const , Either , Identity , IO , List , Maybe , Pair , Reader , Result , State , Unit , Writer |
coalesce | Async , Either , Maybe , Result |
compareWith | Equiv |
concat | All , Any , Array , Assign , Const , Either , Endo , Equiv , First , Identity , Last , List , Max , Maybe , Min , Pair , Pred , Prod , Result , String , Sum , Tuple , Unit |
cons | Array , List |
contramap | Arrow , Equiv , Pred , Star |
either | Either , Maybe , Result |
empty | All , Any , Array , Assign , Endo , Equiv , First , Last , List , Max , Min , Object , Pred , Prod , String , Sum , Unit |
equals | All , Any , Array , Assign , Boolean , Const , Either , First , Last , List , Max , Maybe , Min , Number , Object , Pair , Prod , Result , String , Sum , Tuple , Unit , Writer |
evalWith | State |
execWith | State |
extend | Pair |
filter | Array , List , Object |
first | Arrow , Function , Star |
fold | Array , List |
foldMap | Array , List |
fst | Pair |
head | Array , List , String |
init | Array , List , String |
last | Array , List , String |
log | Writer |
map | Async , Array , Arrow , Const , Either , Function , Identity , IO , List , Maybe , Object , Pair , Reader , Result , Star , State , Tuple , Unit , Writer |
merge | Pair , Tuple |
option | First , Last , Maybe |
promap | Arrow , Star |
race | Async |
read | Writer |
reduce | Array , List |
reduceRight | Array , List |
reject | Array , List , Object |
run | IO |
runWith | Arrow , Endo , Pred , Reader , Star , State |
second | Arrow , Function , Star |
sequence | Array , Either , Identity , List , Maybe , Pair , Result |
snd | Pair |
swap | Async , Either , Pair , Result |
tail | Array , List , String |
traverse | Array , Either , Identity , List , Maybe , Pair , Result |
valueOf | All , Any , Assign , Const , Endo , Equiv , First , Identity , Last , Max , Min , Pred , Prod , Sum , Unit , Writer |
Contribute on Github! Edit this section.