TL;DR
In the previous post, we learned about the Elm application state so users could interact with our Picshare application. Today we explain Elm Record Type, an Elm implementation of key/value data pairs. This post is part of the functional language series, and it is based on a remarkable book Programming Elm Build Safe and Maintainable Front-End Applications by Jeremy Fairbank.
Elm Record
Let’s create a comedian record in elm repl
:
Note that elm repl
always return data type, in comedian case, all values are Strings
. With .
notation, we can get key value. But as Elm is immutable, we can not add to comedian new key or assign to the existing key new value (explore Elm Record image above).
Create Record
Let’s create a function that will have as input
and output
parameter record type
. The function will use input
record to create a new instance of output
record. The function adds to comedian wealth fixed income of 1 000 000.
The second definition of income
the function is using update notatation
using |
operator. With this syntax, we write less boilerplate code.
Benefits Of Immutability
There is a high chance that you will need to edit somebody else code. You will use debugging or unit tests to grasp how a function works.
With immutability, your job is much easier because each function returns new data. You can forget about input data and concentrate on function output data.
Remember
- Elm Record Syntax
- How To Create Record
- How To Update Record
- Why Record Is Immutable