by DanWaterworth · 12y ago · 0 comments
name.trim.filter{ _.length != 0 }.toUpperCase
becomes: name >>= (return . trim) >>= (guard . ((/= 0) . length)) >>= (return . toUpperCase)
I've purposefully used >>= and return instead of fmap to show that '.' above is almost >>=.EDIT: I've just noticed a type error in the use of guard, but you get the idea.
do
n <- name
let trimmed = trim n
guard (length trimmed /= 0)
return (toUpperCase trimmed) toUpperCase . guard (\x -> length x /= 0) . trim
I think that is how it would be if it was part of the language. mfilter' :: MonadPlus m => (a -> Bool) -> a -> m a
mfilter' f = mfilter f . return
Then it's just: toUpperCase <=< mfilter' (\x -> length x /= 0) . trim
Which is pretty close. No need to change the language.