Skip to content

Instantly share code, notes, and snippets.

@robertberry-zz
Created January 8, 2014 14:37
Show Gist options
  • Save robertberry-zz/8317748 to your computer and use it in GitHub Desktop.
Save robertberry-zz/8317748 to your computer and use it in GitHub Desktop.
ugh
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