often times found myself in need of skipping rest of iteration (like continue
in c) in haskell:
form_ [1..100] $ \ -> <- dosomeio when (not $ isvalid1 a) <skip_rest_of_the_iteration> b <- dosomeotherio when (not $ isvalid2 b) <skip_rest_of_the_iteration> ...
however, failed find easy way so. way aware of trans.maybe
, necessary use monad transform achieve trivial?
remember loops in haskell not magic...they're normal first-class things can write yourself.
for it's worth, don't think it's useful think of maybet
monad transformer. me, maybet
newtype wrapper give alternative implementation of (>>=)
...just how use product
, sum
, first
, and
, etc. give alternative implementations of mappend
, mempty
.
right now, (>>=)
io -> (a -> io b) -> io b
. it'd more useful have (>>=)
here io (maybe a) -> (a -> io (maybe b) -> io (maybe b)
. first action returns nothing
, it's impossible "bind" further. that's maybet
gives you. "custom instance" of guard
, guard :: bool -> io (maybe a)
, instead of guard :: io a
.
form_ [1..100] $ \i -> runmaybet $ <- lift dosomeio guard (isvalid1 a) b <- lift $ dosomeotherio guard (isvalid2 b) ...
and that's :)
maybet
not magic either, , can achieve same effect using nested when
s. it's not necessary, makes things lot simpler , cleaner :)
Comments
Post a Comment