函数式编程指南中文版附录A
2023-11-22
Appendix A: Essential Functions Support
In this appendix, you'll find some basic JavaScript implementations of various functions described in the book. 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 some functions also refer to algebraic structures defined in the Appendix B
always
// always :: a -> b -> a
;
compose
// compose :: ((y -> z), (x -> y), ..., (a -> b)) -> a -> z
;
curry
// curry :: ((a, b, ...) -> c) -> a -> b -> ... -> c
either
// either :: (a -> c) -> (b -> c) -> Either a b -> c
;
identity
// identity :: x -> x
;
inspect
// inspect :: a -> String
;
left
// left :: a -> Either a b
;
liftA2
// liftA2 :: (Applicative f) => (a1 -> a2 -> b) -> f a1 -> f a2 -> f b
;
liftA3
// liftA3 :: (Applicative f) => (a1 -> a2 -> a3 -> b) -> f a1 -> f a2 -> f a3 -> f b
;
maybe
// maybe :: b -> (a -> b) -> Maybe a -> b
;
nothing
// nothing :: Maybe a
;
reject
// reject :: a -> Task a b
;