Map and Bind and Apply, a summary
Last updated
Was this helpful?
Last updated
Was this helpful?
Well, turned out to be longer than I originally planned. Thanks for making it to the end!
I hope that this discussion has been helpful in understanding the various function transformations like map
and bind
, and given you some useful techniques for dealing with world-crossing functions -- maybe even demystified the m-word a bit!
If you want to start using these kinds of functions in your own code, I hope that you can see how easy they are to write, but you should also consider using one of the excellent F# utility libraries that contain these and much more:
ExtCore (, ). ExtCore provides extensions to the F# core library (FSharp.Core) and aims to help you build industrial-strength F# applications. These extensions include additional functions for modules such as Array, List, Set, and Map; immutable IntSet, IntMap, LazyList, and Queue collections; a variety of computation expressions (workflows); and "workflow collections" -- collections modules which have been adapted to work seamlessly from within workflows.
FSharpx.Extras (). FSharpx.Extras is part of the FSharpx series of libraries. It implements several standard monads (State, Reader, Writer, Either, Continuation, Distribution), validation with applicative functors, general functions like flip, and some asynchronous programming utilities, and functions to make C# - F# interop easier.
For example, the monadic traverse List.traverseResultM
that I implemented is already available in ExtCore .
And if you liked this series, I have posts explaining the State monad in my series on and the Either monad in my talk .
As I said at the very beginning, writing this up has been a learning process for me too. I am not an expert, so if I have made any errors please do let me know.
Thanks!
Here's a list of shortcuts to the various functions mentioned in this series:
Part 1: Lifting to the elevated world
Part 2: How to compose world-crossing functions
Part 3: Using the core functions in practice
Part 4: Mixing lists and elevated values
Part 5: A real-world example that uses all the techniques
Part 6: Designing your own elevated world
Part 7: Summary
Operator
Equivalent function
Discussion
>>
Left-to-right composition
<<
Right-to-left composition
As above
|>
Left-to-right piping
As above
<|
Right-to-left piping
As above
<!>
map
<$>
map
Haskell operator for map, but not a valid operator in F#, so I'm using <!>
in this series.
<*>
apply
<*
-
*>
-
>>=
Left-to-right bind
=<<
Right-to-left bind
As above
>=>
Left-to-right Kleisli composition
<=<
Right-to-left Kleisli composition
As above
Alternative tutorials:
For the academically minded:
F# examples:
Applicatives are very well suited for parsing, as explained in these posts:
Unlike OO languages, functional programming languages are known for their , so I thought it would be helpful to document the ones that have been used in this series, with links back to the relevant discussion.
Not part of this series, but
One sided combiner.
One sided combiner.
.
.
. I think this one is funny.
.
(PDF), by Philip Wadler. One of the first monad papers.
(PDF), by Conor McBride and Ross Paterson.
(PDF), by Jeremy Gibbons and Bruno Oliveira.
and
have lots of useful code.
has map
, apply
, liftN
(called "Parallel"), bind
, and other useful extensions for Async
.
.
.