How to best handle IO Bool logic?

I see myself using this pattern a lot:

f x = do
  -- predicate :: a -> IO Bool
  b <- predicate x
  if b then 
    rest ()
    return ()
    rest () = do
      -- rest of my IO operations

is there a pattern other people use to remove the if then else where clauses? How do you best work with control flow and IO Bool?

>Solution :

Check out when and unless.

f x = do
  b <- predicate x
  when b $ do
    -- rest of the IO operation

In the extra package there also is whenM.

Leave a Reply