Skip to content

Instantly share code, notes, and snippets.

@jah2488
Created January 18, 2016 21:24
Show Gist options
  • Select an option

  • Save jah2488/da3ef5c7cf755e9ef4ff to your computer and use it in GitHub Desktop.

Select an option

Save jah2488/da3ef5c7cf755e9ef4ff to your computer and use it in GitHub Desktop.
Playing around with custom sorting on nested structures in Elm.
import Graphics.Element exposing (show)
type State = Ready | Working | Complete
stateSort a =
case a.state of
Ready -> 1
Working -> 2
Complete -> 3
type alias Row =
{ pos : Int
, state : State
}
type alias Col =
{ rows : List Row
, state : State
}
model =
{
cols =
[ { state = Ready, rows = [] },
{ state = Working, rows = [] },
{ state = Complete, rows = [] }],
rows =
[ { pos = 2, state = Ready}
, { pos = 7, state = Working}
, { pos = 3, state = Ready }
, { pos = 5, state = Complete }
, { pos = 6, state = Ready }
, { pos = 4, state = Working }]}
partitionRows x =
List.map (\col ->
{ state = col.state,
rows = (List.sortBy .pos (List.filter (\r -> r.state == col.state) x.rows)) }) x.cols
showPartitionedRows = partitionRows model
|> show
showSortedRows = rows
|> List.sortBy .pos
|> List.sortBy stateSort
|> show
main = showPartitionedRows
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment