Created
August 6, 2019 15:54
-
-
Save chrisdone/b5e03f19d67abb5b1408d84669ceec9c to your computer and use it in GitHub Desktop.
StableShuffle.hs
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 PartialTypeSignatures #-} | |
module StableShuffle where | |
import Control.Monad | |
import Control.Monad.Random.Class | |
import Data.List | |
import qualified Data.Map.Strict as M | |
import Data.Maybe | |
import Data.Ord | |
import System.Random.Shuffle | |
data Stability a = Stable a | Instable a | |
shuffleWithStability :: MonadRandom m => [Stability a] -> m [a] | |
shuffleWithStability ordered = do | |
shuffled <- shuffleM ordered | |
pure | |
(mergeWithStability stables shuffled) | |
where | |
stables = | |
mapMaybe | |
(\e -> | |
case e of | |
Stable a -> Just a | |
_ -> Nothing) | |
ordered | |
mergeWithStability :: [a] -> [Stability a] -> [a] | |
mergeWithStability stables0 shuffled = | |
snd | |
(mapAccumL | |
(\stables element -> | |
case (element, stables) of | |
(Instable a, _) -> (stables, a) | |
(Stable{}, s:stables') -> (stables', s) | |
(Stable a, []) -> ([], a)) | |
stables0 | |
shuffled) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment