Skip to content

Instantly share code, notes, and snippets.

@nessamurmur
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save nessamurmur/dc8a43b8deea0270dd4e to your computer and use it in GitHub Desktop.

Select an option

Save nessamurmur/dc8a43b8deea0270dd4e to your computer and use it in GitHub Desktop.
Updating Records in Elm
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