aelve / haskell-issues

An unofficial issue tracker for all things Haskell-related
18 stars 0 forks source link

Finally add whenM/unlessM/ifM #15

Open neongreen opened 8 years ago

neongreen commented 8 years ago

The previous discussion (I think it was this one) ended in nothing, even tho most people seemed to be in favor of adding them.

Gurkenglas commented 8 years ago

Beware boolean blindness.

f = whenM (not <$> doesDirectoryExists path) $ do
         putStrLn $ "Creating directory " ++ path
         createDirectory path

ought to look like

directory = runMaybeT $ (MaybeT (getDirectoryMay path) <|>) $ lift $ do
         putStrLn $ "Creating directory " ++ path
         createDirectory path

and any additional combinators should work on making this more readable. Although the viewpoint that this will take years and we should have whenM/unlessM/ifM in the interim is reasonable.

Edit: Hmm...

directory = (ala MaybeT . alaf Alt) foldMap
  [ getDirectoryMay path
  , lift $ do
    putStrLn $ "Creating directory " ++ path
    createDirectory path
  ]
neongreen commented 8 years ago

I would love to be able to write the MaybeT example in a more readable way, but perfect is the enemy of good. Once we find a good set of combinators to make MaybeT-style code look nice, I don't think having whenM would hinder the adoption of mentioned combinators – and since we don't have such combinators yet, whenM would make life simpler for me. (I wish I could have !-notation from Idris instead, but that's a much more drastic change.)

(By the way, what is getDirectoryMay supposed to do / what should its type be? I'm not sure.)

Gurkenglas commented 8 years ago

getDirectoryMay :: FilePath -> IO (Maybe Directory)

http://lpaste.net/150858

directory = runIdentityT $ (MaybeT (getDirectoryMay path) <|!>) $ IdentityT $ do
         putStrLn $ "Creating directory " ++ path
         createDirectory path

Perhaps I should relate MaybeT m to m instead of IdentityT m? That feels hacky, though. Then it would look like

directory = MaybeT (getDirectoryMay path) <|!> do
         putStrLn $ "Creating directory " ++ path
         createDirectory path

Edit: did that.