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!
Here’s a list of shortcuts to the various functions mentioned in this series:
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 |
---|---|---|
>> |
Left-to-right composition | Not part of this series, but discussed here |
<< |
Right-to-left composition | As above |
|> |
Left-to-right piping | As above |
<| |
Right-to-left piping | As above |
<!> |
map |
Discussed here |
<$> |
map |
Haskell operator for map, but not a valid operator in F#, so I’m using <!> in this series. |
<*> |
apply |
Discussed here |
<* |
- | One sided combiner. Discussed here |
*> |
- | One sided combiner. Discussed here |
>>= |
Left-to-right bind |
Discussed here |
=<< |
Right-to-left bind |
As above |
>=> |
Left-to-right Kleisli composition | Discussed here |
<=< |
Right-to-left Kleisli composition | As above |
Alternative tutorials:
For the academically minded:
F# examples:
map
, apply
, liftN
(called “Parallel”), bind
, and other useful extensions for Async
.