If monads did not exist, we would be forced to invent them
Over the weekend, I started myself a little F# project, a little more complicated than the little orrery clock I wrote at the end of last year -- hence the little flurry of F# unit test related posts.
The project in question is a simple FxCop rule to identify methods with inadequate test coverage, so, if I were writing this in a conventional imperative language, the
override protected ProblemCollection Check(Member member)
method would look roughly like (using the return ASAP
refactoring of the arrow-antipattern)
where each check either identifies the method as a definite pass or fail (the latter mutating the object state that the overall method also returns), or cannot tell.
Translating this sort of construction to a functional language, the use of a monadic construction becomes -- if you forgive the pun -- imperative if we are not just to transcribe the if
driven style literally.
Looked at in functional style, in order to make this process at all pleasant, we immediately see the State monad (for the mutable state), and some crazy variant on the Maybe monad -- a sort of AndAnd monad, perhaps? -- for the short circuiting of further checks when there is definite result. Though I might go for something a bit closer to the Haskell syntax than the F# computation expression.
No comments :
Post a Comment