Skip to content

Instantly share code, notes, and snippets.

@girishso
Last active April 10, 2018 07:01
Show Gist options
  • Save girishso/8a5be4717e9f0f965bd411caf8874b94 to your computer and use it in GitHub Desktop.
Save girishso/8a5be4717e9f0f965bd411caf8874b94 to your computer and use it in GitHub Desktop.
decoders.elm
boardDecoder : Decode.Decoder (Dict Position Cell)
boardDecoder =
let
asTuple : CellWrapper -> ( Position, Cell )
asTuple cw =
( cw.pos, cw.cell )
toDict : List CellWrapper -> Dict Position Cell
toDict wrappers =
wrappers |> List.map asTuple |> Dict.fromList
in
(Decode.list decodeCellWrapper)
|> Decode.map toDict
decodeCellWrapper : Decode.Decoder CellWrapper
decodeCellWrapper =
Decode.map2 CellWrapper
(field "pos" positionDecoder)
(field "cell" cellDecoder)
boardEncoder : Dict.Dict Position Cell -> Encode.Value
boardEncoder board =
board
|> Dict.toList
|> List.map (\( pos, cell ) -> { pos = pos, cell = cell })
|> List.map cellWrapperEncoder
|> Encode.list   
cellWrapperEncoder : CellWrapper -> Encode.Value
cellWrapperEncoder v =
Encode.object
[ ( "pos", positionEncoder v.pos )
, ( "cell", cellEncoder v.cell )
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment