Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Control.Monad.Trans (lift)
import Control.Parallel (par, pseq)
import Data.Text.Lazy (pack)
import System.Random (StdGen, newStdGen, random)
import Web.Scotty (get, scotty, text)
randomWalk :: Int -> Int -> StdGen -> Int
randomWalk s 0 _ = s
randomWalk s i r = randomWalk s' (pred i) g
where (b, g) = random r
s' = (if b then succ else pred) s
main :: IO ()
main = scotty 3000 $ do
a <- lift $ newStdGen
b <- lift $ newStdGen
get "/" $ do
let walk = randomWalk 0 10000000
first = walk a
second = walk b
text . pack . show $ first `par` second `pseq` (first + second)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment