Created
March 18, 2018 23:14
-
-
Save oisdk/44637f464f6c57d7b188f7a0e06fb55f to your computer and use it in GitHub Desktop.
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
{-# LANGUAGE BangPatterns #-} | |
import System.Random | |
import GHC.Base (oneShot) | |
choose :: (Foldable f, RandomGen g) => f a -> g -> (Maybe a, g) | |
choose xs = foldr f (const (,)) xs (0 :: Integer) Nothing | |
where | |
f x a = oneShot (\ !c m g -> case m of | |
Nothing -> a 1 (Just x) g | |
Just y -> case randomR (0,c) g of | |
(0,g') -> a (c+1) (Just x) g' | |
(_,g') -> a (c+1) (Just y) g') | |
{-# INLINE f #-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment