Skip to content

Instantly share code, notes, and snippets.

@TakashiHarada
Created November 9, 2017 11:18
Show Gist options
  • Save TakashiHarada/71e95aec9b7ba14e8026de7db0a535bc to your computer and use it in GitHub Desktop.
Save TakashiHarada/71e95aec9b7ba14e8026de7db0a535bc to your computer and use it in GitHub Desktop.
-- https://en.wikibooks.org/wiki/Haskell/Monad_transformers
import Data.Char
import Control.Monad
import Control.Monad.Trans
import Control.Monad.Trans.Maybe
-- getPassphrase :: IO (Maybe String)
-- getPassphrase =
-- getLine >>=
-- \s -> if isValid s
-- then return $ Just s
-- else return Nothing
isValid :: String -> Bool
isValid s =
length s >= 8 &&
any isAlpha s &&
any isNumber s &&
any isPunctuation s
-- askPassphrase :: IO ()
-- askPassphrase =
-- putStrLn "Insert your new passphrase:" >>
-- getPassphrase >>=
-- \maybe_value -> case maybe_value of
-- Just value -> putStrLn "Strong in database..."
-- Nothing -> putStrLn "Passphrase invalid."
newtype MaybeT' m a = MaybeT' { runMaybeT' :: m (Maybe a) }
-- MaybeT' $ Just (Just Nothing)
-- MaybeT' $ \c -> (Just Nothing)
-- MaybeT' $ \c -> (Just (c : "hoge"))
-- (runMaybeT' $ MaybeT' $ \c -> (Just (c : "hoge"))) '5'
-- instance MonadTrans MaybeT where
-- lift = MaybeT . (liftM Just)
getPassphrase :: MaybeT IO String
getPassphrase =
lift getLine >>=
\s -> guard (isValid s) >>
return s
askPassphrase :: MaybeT IO ()
askPassphrase =
(lift $ putStrLn "Insert your new passphrase:") >>
(msum $ repeat getPassphrase) >>=
\value -> lift $ putStrLn "Storing in database..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment