Skip to content

Instantly share code, notes, and snippets.

@chrisdone
Created August 6, 2019 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisdone/b5e03f19d67abb5b1408d84669ceec9c to your computer and use it in GitHub Desktop.
Save chrisdone/b5e03f19d67abb5b1408d84669ceec9c to your computer and use it in GitHub Desktop.
StableShuffle.hs
{-# 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