Created
January 8, 2014 14:37
-
-
Save robertberry-zz/8317748 to your computer and use it in GitHub Desktop.
ugh
This file contains hidden or 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
import Control.Concurrent | |
import Control.Monad | |
import Network.HTTP | |
alphabet = ['a'..'z'] | |
prefixes :: [String] | |
prefixes = [[a, b, c] | a <- alphabet, b <- alphabet, c <- alphabet] | |
searchUrl :: String -> String | |
searchUrl = ("http://localhost:9000/howMany/" ++) | |
getResults :: String -> IO Int | |
getResults query = fmap read $ simpleHTTP (getRequest $ searchUrl query) >>= getResponseBody | |
printResult :: String -> Int -> IO () | |
printResult prefix results = putStrLn $ (show results) ++ " " ++ prefix | |
streamResults :: [String] -> IO () | |
streamResults queries = mapM_ (\p -> getResults p >>= printResult p) queries | |
concurrencyLevel = 10 | |
group :: Int -> [a] -> [[a]] | |
group _ [] = [] | |
group n xs | |
| n > 0 = (take n xs) : (group n (drop n xs)) | |
| otherwise = error "Negative n" | |
chunks :: Int -> [a] -> [[a]] | |
chunks n xs = group chunkSize xs | |
where chunkSize = (length xs) `div` n | |
spin :: IO () | |
spin = forever (threadDelay 1000) | |
main = do | |
mapM_ (forkIO . streamResults) $ chunks concurrencyLevel prefixes | |
spin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment