Last active
August 29, 2015 14:03
-
-
Save nessamurmur/dc8a43b8deea0270dd4e to your computer and use it in GitHub Desktop.
Updating Records 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
| data Color = Black | White | |
| data Piece = Pawn | Knight | Bishop | Rook | Queen | King | |
| blackQueen = { color=Black, piece=Queen } | |
| -- { color = Black, piece = Queen } : {color : Color, piece : Piece} | |
| -- instantiating the record creates functions to return it's attributes: | |
| .color blackQueen | |
| -- Black : Color | |
| -- There's also some syntactic sugar to call the function like a method: | |
| blackQueen.color | |
| -- Black : Color | |
| -- Updating the record | |
| whiteQueen = { blackQueen | color <- White } | |
| -- { color = White, piece = Queen } : {piece : Piece, color : Color} | |
| position = { column = "d", row = 1 } | |
| -- { column = "d", row = 1 } : {column : String, row : number} | |
| homeWhiteQueen = { whiteQueen | position = position } | |
| -- { color = White, piece = Queen, position = { column = "d", row = 1 } } : {piece : Piece, color : Color, position : {column : String, row : number}} | |
| colorAndPosition = { homeWhiteQueen - piece } | |
| -- { color = White, position = { column = "d", row = 1 } } : {b | position : {column : String, row : number}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment