Created
September 28, 2016 05:39
-
-
Save relrod/00f9429a4381eaa5020bb4983f829f16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main where | |
import Data.Time.Clock.POSIX | |
import System.IO | |
main :: IO () | |
main = do | |
putStrLn "Tap to the beat of a song to determine its BPM." | |
hSetEcho stdin False | |
hSetBuffering stdin NoBuffering | |
loop [1] | |
average :: (Foldable t, Integral a) => t a -> a | |
average n = sum n `div` fromIntegral (length n) | |
takeLast :: Int -> [t] -> [t] | |
takeLast n [] = [] | |
takeLast n l = drop (length l - n) l | |
diff :: Num t => [t] -> [t] | |
diff [] = [] | |
diff ls = zipWith (-) (tail ls) ls | |
loop :: [Integer] -> IO a | |
loop lst = do | |
getChar | |
t <- getPOSIXTime | |
let lastFew = takeLast 4 lst ++ [round (t * 1000)] | |
print (60000 `div` average (diff lastFew)) | |
loop lastFew |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment