All Projects → fp-works → function-composition-cheatsheet

fp-works / function-composition-cheatsheet

Licence: MIT license
Composition of Functions

Projects that are alternatives of or similar to function-composition-cheatsheet

Bow
🏹 Bow is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 538 (+2141.67%)
Mutual labels:  composition, fp, monad, functor
Crocks
A collection of well known Algebraic Data Types for your utter enjoyment.
Stars: ✭ 1,501 (+6154.17%)
Mutual labels:  composition, monad, functor
functional-structures-refactoring-kata
Starting code and proposed solution for Functional Structures Refactoring Kata
Stars: ✭ 31 (+29.17%)
Mutual labels:  composition, monad, functor
cpsfy
🚀 Tiny goodies for Continuation-Passing-Style functions, fully tested
Stars: ✭ 58 (+141.67%)
Mutual labels:  composition, monad, functor
pyroclastic
Functional dataflow through composable computations
Stars: ✭ 17 (-29.17%)
Mutual labels:  composition, fp
elixir-control
An exploratory look into functors, applicatives, and monads for Elixir
Stars: ✭ 21 (-12.5%)
Mutual labels:  monad, functor
Scriptum
A fool's scriptum on functional programming
Stars: ✭ 346 (+1341.67%)
Mutual labels:  composition, monad
Monio
Async-capable IO monad for JS
Stars: ✭ 311 (+1195.83%)
Mutual labels:  fp, monad
Bugz
🐛 Composable User Agent Detection using Ramda
Stars: ✭ 15 (-37.5%)
Mutual labels:  composition, fp
Promised Pipe
A ramda.pipe-like utility that handles promises internally with zero dependencies
Stars: ✭ 64 (+166.67%)
Mutual labels:  composition, fp
Design-Patterns
Project for learning and discuss about design patterns
Stars: ✭ 16 (-33.33%)
Mutual labels:  fp, monad
UnderstandingLanguageExt
This is a tutorial that aims to demonstrate the practical fundamentals behind using LanguageExt in a fashion though step-by-step tutorials which introduce and then build up on concepts.
Stars: ✭ 73 (+204.17%)
Mutual labels:  composition, monad
Kotlin Result
A multiplatform Result monad for modelling success or failure operations.
Stars: ✭ 369 (+1437.5%)
Mutual labels:  fp, monad
bow-lite
🏹 Bow Lite is a cross-platform library for Typed Functional Programming in Swift
Stars: ✭ 27 (+12.5%)
Mutual labels:  composition, fp
Fun Task
Abstraction for managing asynchronous code in JS
Stars: ✭ 363 (+1412.5%)
Mutual labels:  fp, monad
fnts
λ Minimal Functional Programming Utilities for TypeScript & JavaScript
Stars: ✭ 75 (+212.5%)
Mutual labels:  fp, monad
hkts
Functional programming tools: option, either, task, state, optics, etc.
Stars: ✭ 20 (-16.67%)
Mutual labels:  fp, monad
Swiftz-Validation
A data structure for validations. It implements the applicative functor interface
Stars: ✭ 15 (-37.5%)
Mutual labels:  fp, functor
Functional Promises
Write code like a story w/ a powerful Fluent (function chaining) API
Stars: ✭ 141 (+487.5%)
Mutual labels:  composition, monad
kudojs
A utility library to write code in functional programming style in Javascript
Stars: ✭ 22 (-8.33%)
Mutual labels:  composition, functor

Composition of Functions

Why Another Cheatsheet?

FP is about composition of functions. And it is surprisingly hard to find a cheatsheet just for composition of functions in Haskell, so let's try to start with a simple one and iterate on it over time. PRs more than welcome :)

Change Logs:

Date Contributors Description
2019-09-24 Daniel Deng Initial version, Function as Functor, Applicative and Monad, Kleisli Arrow Composition

Special Thanks To:

  • Shine Li who helped me along the way while I worked on the initial version.

GHC Source Code

Function as Functor, Applicative and Monad
-- | @since 2.01
instance Functor ((->) r) where
    fmap = (.)

-- | @since 2.01
instance Applicative ((->) a) where
    pure = const
    (<*>) f g x = f x (g x)
    liftA2 q f g x = q (f x) (g x)

-- | @since 2.01
instance Monad ((->) r) where
    f >>= k = \ r -> k (f r) r
Function as Semigroup and Monoid
-- | @since 4.9.0.0
instance Semigroup b => Semigroup (a -> b) where
        f <> g = \x -> f x <> g x
        stimes n f e = stimes n (f e)

-- | @since 2.01
instance Monoid b => Monoid (a -> b) where
        mempty _ = mempty
"Komposition" of Functions (Kleisli Arrows)
-- | Left-to-right composition of Kleisli arrows.
(>=>)       :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
f >=> g     = \x -> f x >>= g

-- | Right-to-left composition of Kleisli arrows. @('>=>')@, with the arguments
-- flipped.
--
-- Note how this operator resembles function composition @('.')@:
--
-- > (.)   ::            (b ->   c) -> (a ->   b) -> a ->   c
-- > (<=<) :: Monad m => (b -> m c) -> (a -> m b) -> a -> m c
(<=<)       :: Monad m => (b -> m c) -> (a -> m b) -> (a -> m c)
(<=<)       = flip (>=>)

Cheatsheet

Function as Functor (Function Composition)
f x = f1 (f2 (f3 (fn x)))

-- composition
f = f1 . f2 . f3 . fn

-- GHC.Base.Functor
-- Usually we just use (.) though.
f = f1 `fmap` f2 `fmap` f3 `fmap` fn
f = f1 <$> f2 <$> f3 <$> fn

-- Control.Arrow
f = f1 <<< f2 <<< f3 <<< fn
f = fn >>> f3 >>> f2 >>> f1

-- Flow
f = f1 <. f2 <. f3 <. fn
f = fn .> f3 .> f2 .> f1
-- Functor Laws
id <$> f == f
f <$> id == f
(f <$> g) <$> h = f <$> (g <$> h)
Function as Applicative
f x = f1 x (f2 x)
f = f1 <*> f2
f x = g (f1 x) (f2 x) (f3 x) (fn x)

-- Applicative Style
f = g <$> f1 <*> f2 <*> f3 <*> fn
f = g . f1 <*> f2 <*> f3 <*> fn
f x = g (f1 x) (f2 x) (f3 x) (fn x) x

-- Applicative Style
f = g <$> f1 <*> f2 <*> f3 <*> fn <*> id
f x = g x (f1 x) (f2 x) (f3 x) (fn x)

-- Applicative Style
f = g <$> id <*> f1 <*> f2 <*> f3 <*> fn
f = g <*> f1 <*> f2 <*> f3 <*> fn
Function as Monad
f x = f1 (f2 x) x
f = f1 =<< f2
f = f2 >>= f1
-- Monad Laws
pure a >>= f = f a
f >>= pure = f
(f >>= g) >>= h = f >>= (\x -> g x >>= h)
Function as Semigroup
f x = f1 x <> f2 x
f = f1 <> f2
f x = f1 x <> f2 x <> f3 x <> fn x
f = f1 <> f2 <> f3 <> fn
Function as Monoid
f x = f1 x <> mempty
f = f1 <> mempty
-- Monoid Laws
mempty <> f = f
f <> mempty = f
(f <> g) <> h = f <> (g <> h)
"Komposition" of Functions (Kleisli Arrows)
-- Definition of an Kleisli Arrow:
f :: Monad m => a -> m b
-- `f1` and `f2` must return the same Monad instance type.
-- If `f1` returns `[b]`, `f2` must return `[c]`.
-- If `f1` returns `Maybe b`, `f2` must return `Maybe c`.
-- If `f1` returns `IO b`, `f2` must return `IO c`.
f1 :: Monad m => a -> m b
f2 :: Monad m => b -> m c

-- >>= is the monadic binding
f a = f1 a >>= f2
f = f1 >=> f2

f a = f2 =<< f1 a
f = f2 <=< f1
findUser :: UserID -> Either Err User
getDepartment :: User -> Either Err Department
getManager :: Department -> Either Err User

getManagerByUserID :: UserID -> Either Err User
getManagerByUserID = findUser >=> getDepartment >=> getManager

-- caveat: to use 'fish operator' all monadic functions need to return the same type of monad.
-- In this case above for instance, all functions must return `Either Err <SomeType>`
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].