Last active
April 10, 2018 07:01
-
-
Save girishso/8a5be4717e9f0f965bd411caf8874b94 to your computer and use it in GitHub Desktop.
decoders.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
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