Map and Bind and Apply, a summary
Series summary
Well, this series 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 (source, NuGet). 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 (home page). 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 in this post is already available in ExtCore here.
And if you liked this series, I have posts explaining the State monad in my series on "Dr Frankenfunctor and the Monadster" and the Either monad in my talk "Railway Oriented Programming".
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!
Series contents
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 5: A real-world example that uses all the techniques
Part 6: Designing your own elevated world
Part 7: Summary
Appendix: List of operators mentioned
Unlike OO languages, functional programming languages are known for their strange operators, 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.
Operator
Equivalent function
Discussion
<<
Right-to-left composition
As above
|>
Left-to-right piping
As above
<|
Right-to-left piping
As above
<$>
map
Haskell operator for map, but not a valid operator in F#, so I'm using <!>
in this series.
=<<
Right-to-left bind
As above
<=<
Right-to-left Kleisli composition
As above
Appendix: Further reading
Alternative tutorials:
Kleisli composition ? la Up-Goer Five. I think this one is funny.
For the academically minded:
Monads for Functional Programming (PDF), by Philip Wadler. One of the first monad papers.
Applicative Programming with Effects (PDF), by Conor McBride and Ross Paterson.
The Essence of the Iterator Pattern (PDF), by Jeremy Gibbons and Bruno Oliveira.
F# examples:
F# ExtCore and
FSharpx.Extras have lots of useful code.
FSharpx.Async has
map
,apply
,liftN
(called "Parallel"),bind
, and other useful extensions forAsync
.Applicatives are very well suited for parsing, as explained in these posts:
Last updated
Was this helpful?