Browse Source

008/104: sleep

master
Maxim Likhachev 5 years ago
parent
commit
c48c006b0b
  1. 23
      src/sleep.hs

23
src/sleep.hs

@ -0,0 +1,23 @@
{-# LANGUAGE NoImplicitPrelude #-}
import Prelude (Int, IO, Maybe (Just, Nothing), String,
(.), (*), (++),
compare, flip, mapM_, putStrLn)
import Control.Concurrent (threadDelay)
import Control.Monad ((>>=), (>>))
import Data.List (sortBy)
import System.Environment (getArgs)
import System.Exit (exitFailure)
import Text.Read (readMaybe)
microseconds :: Int
microseconds = 1000000
sleep :: String -> IO ()
sleep time = wait (readMaybe time)
where wait Nothing = putStrLn ("sleep: invalid time interval ‘" ++ time ++ "") >> exitFailure
wait (Just n) = threadDelay (n * microseconds)
main :: IO ()
main = getArgs >>= mapM_ sleep . sortBy (flip compare) -- sort args to handle non-integer values firstly
Loading…
Cancel
Save