-
-
Save chrisbuttery/3cd4565007a534b4ae0ed89782ba3e2a to your computer and use it in GitHub Desktop.
List shuffle implementation in Elm
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
import Html exposing (..) | |
import Html.App as Html | |
import Html.Events exposing (..) | |
import Random exposing (Generator) | |
import List | |
users = | |
[ {name = "one", username = "@one"} | |
, {name = "two", username = "@two"} | |
, {name = "three", username = "@three"} | |
, {name = "four", username = "@four"} | |
, {name = "five", username = "@five"} | |
, {name = "six", username = "@six"} | |
, {name = "seven", username = "@seven"} | |
, {name = "eight", username = "@eight"} | |
, {name = "nine", username = "@nine"} | |
, {name = "ten", username = "@ten"} | |
, {name = "eleven", username = "@eleven"} | |
, {name = "twelve", username = "@twelve"} | |
] | |
main = | |
Html.program | |
{ init = init | |
, view = view | |
, update = update | |
, subscriptions = subscriptions | |
} | |
type alias User = { | |
name: String | |
, username: String | |
} | |
type alias Model = { | |
users: List User | |
, limit: Int | |
} | |
initialModel = | |
{ users = users | |
, limit = 2 | |
} | |
init : (Model, Cmd Msg) | |
init = | |
(initialModel, Cmd.none) | |
-- UPDATE | |
type Msg | |
= Set List | |
| Shuffle | |
update msg model = | |
case msg of | |
Set list -> | |
({model | users = list} , Cmd.none) | |
Shuffle -> | |
(model, Random.generate Set (shuffle model.users)) | |
-- MAGIC | |
extractValueHelper : List a -> Int -> List a -> (a, List a) | |
extractValueHelper values index accumulator = | |
case (index, values) of | |
(_, []) -> Debug.crash "Out of values to extract" | |
(0, (head::tail)) -> (head, List.append (List.reverse accumulator) tail) | |
(_, (head::tail)) -> extractValueHelper tail (index - 1) <| head :: accumulator | |
extractValue : List a -> Int -> (a, List a) | |
extractValue values index = extractValueHelper values index [] | |
shuffle : List a -> Generator (List a) | |
shuffle values = | |
case values of | |
[] -> Random.map (\_ -> []) Random.bool | |
values -> | |
let randomIndexGenerator = Random.int 0 <| (List.length values) - 1 | |
extractAndRecurse = \index -> | |
let (randomHead, remainder) = extractValue values index | |
remainderGen = shuffle remainder | |
in Random.map (\randomTail -> randomHead :: randomTail) remainderGen | |
in Random.andThen randomIndexGenerator extractAndRecurse | |
-- SUBSCRIPTIONS | |
subscriptions : Model -> Sub Msg | |
subscriptions model = | |
Sub.none | |
-- VIEW | |
listItem value = | |
li [] [ text value.name ] | |
view model = | |
div [] | |
[ ul [] (List.map listItem model.users) | |
, button [ onClick Shuffle ] [ text "Shuffle" ] | |
] |
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
import Random exposing (Generator) | |
extractValueHelper : List a -> Int -> List a -> (a, List a) | |
extractValueHelper values index accumulator = | |
case (index, values) of | |
(_, []) -> Debug.crash "Out of values to extract" | |
(0, (head::tail)) -> (head, List.append (List.reverse accumulator) tail) | |
(_, (head::tail)) -> extractValueHelper tail (index - 1) <| head :: accumulator | |
extractValue : List a -> Int -> (a, List a) | |
extractValue values index = extractValueHelper values index [] | |
shuffle : List a -> Generator (List a) | |
shuffle values = | |
case values of | |
[] -> Random.map (\_ -> []) Random.bool | |
values -> | |
let randomIndexGenerator = Random.int 0 <| (List.length values) - 1 | |
extractAndRecurse = \index -> | |
let (randomHead, remainder) = extractValue values index | |
remainderGen = shuffle remainder | |
in Random.map (\randomTail -> randomHead :: randomTail) remainderGen | |
in Random.andThen randomIndexGenerator extractAndRecurse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment