Skip to main content
Commonmark migration
Source Link

#Know your monadic functions

Know your monadic functions

1)
simulate monadic functions using mapM.

a lot of times code will have sequence(map f xs), but it can be replaced with mapM f xs. even when just using sequence alone it is longer then mapM id.

2)
combine functions using (>>=) (or (=<<))

the function monad version of (>>=) is defined as so:

(f >>= g) x = g (f x) x 

it can be useful for creating functions which can't be expressed as a pipeline. for example, \x->x==nub x is longer than nub>>=(==), and \t->zip(tail t)t is longer than tail>>=zip.

#Know your monadic functions

1)
simulate monadic functions using mapM.

a lot of times code will have sequence(map f xs), but it can be replaced with mapM f xs. even when just using sequence alone it is longer then mapM id.

2)
combine functions using (>>=) (or (=<<))

the function monad version of (>>=) is defined as so:

(f >>= g) x = g (f x) x 

it can be useful for creating functions which can't be expressed as a pipeline. for example, \x->x==nub x is longer than nub>>=(==), and \t->zip(tail t)t is longer than tail>>=zip.

Know your monadic functions

1)
simulate monadic functions using mapM.

a lot of times code will have sequence(map f xs), but it can be replaced with mapM f xs. even when just using sequence alone it is longer then mapM id.

2)
combine functions using (>>=) (or (=<<))

the function monad version of (>>=) is defined as so:

(f >>= g) x = g (f x) x 

it can be useful for creating functions which can't be expressed as a pipeline. for example, \x->x==nub x is longer than nub>>=(==), and \t->zip(tail t)t is longer than tail>>=zip.

Mod Removes Wiki by Doorknob
Source Link
proud haskeller
  • 6.1k
  • 1
  • 23
  • 37

#Know your monadic functions

1)
simulate monadic functions using mapM.

a lot of times code will have sequence(map f xs), but it can be replaced with mapM f xs. even when just using sequence alone it is longer then mapM id.

2)
combine functions using (>>=) (or (=<<))

the function monad version of (>>=) is defined as so:

(f >>= g) x = g (f x) x 

it can be useful for creating functions which can't be expressed as a pipeline. for example, \x->x==nub x is longer than nub>>=(==), and \t->zip(tail t)t is longer than tail>>=zip.