Skip to content

Instantly share code, notes, and snippets.

@JoelQ
Created November 21, 2016 21:05
Show Gist options
  • Save JoelQ/723528ba789abc66f439d86eaf876522 to your computer and use it in GitHub Desktop.
Save JoelQ/723528ba789abc66f439d86eaf876522 to your computer and use it in GitHub Desktop.
Mapping a list to a shuffled version of itself
module Main exposing (..)
import Array
import Tuple
import Dict exposing (Dict, get)
import Random exposing (Generator, map)
-- RANDOM
zip : List a -> List b -> List ( a, b )
zip =
List.map2 (,)
constant : a -> Generator a
constant a =
Random.map (\_ -> a) Random.bool
shuffle : List comparable -> Generator (List comparable)
shuffle list =
Random.list (List.length list) (Random.int 1 3)
|> Random.map (zip list)
|> Random.map (List.sortBy Tuple.second)
|> Random.map (List.map Tuple.first)
shuffledMappings : List comparable -> Generator (Dict comparable comparable)
shuffledMappings list =
shuffle list
|> Random.map (zip list)
|> Random.map (Dict.fromList)
draw : List comparable -> Generator (Maybe (Dict comparable comparable))
draw list =
if List.length list < 2 then
constant Nothing
else
shuffledMappings list
|> Random.map Just
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment