- ELM - Discussion
- ELM - Useful Resources
- ELM - Quick Guide
- ELM - Subscriptions
- ELM - Commands
- ELM - Messages
- ELM - Package Manager
- ELM - Architecture
- ELM - Error Handling
- ELM - Records
- ELM - Tuples
- ELM - List
- ELM - String
- ELM - Functions
- ELM - Loop
- ELM - Decision Making
- ELM - Operators
- ELM - Variables
- ELM - Data Types
- ELM - Basic Syntax
- ELM - Environment Setup
- ELM - Introduction
- ELM - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Elm - Records
The record data structure in Elm can be used to represent data as key-value pairs. A record can be used to organize related data to enable easy access and updating data. Elm records are similar to objects in JavaScript. Data elements in a record are known as fields.
Defining a Record
Use the following syntax to define a record −
Syntax
record_name = {fieldname1 = value1, fieldname2 = value2....fieldnameN = valueN}
A record can store data of multiple types. The field names in a record must conform to the general rules for naming an Elm identifier.
Accessing record values
Use the following syntax to access inspanidual fields in a record.
Syntax
record_name.fieldname
OR
.fieldname record_name
Illustration
Try the following in the Elm REPL −
> company = {name="TutorialsPoint",rating=4.5} { name = "TutorialsPoint", rating = 4.5 } : { name : String, rating : Float } > company.name "TutorialsPoint" : String > .rating company 4.5 : Float
Using Record with List
A record can be stored inside a pst. All field values of the record should be of the same type.
Syntax
pst_name = [ {field_name1 = value1},{field_name1 = value2}]
OR
pst_name = [record_name1, record_name2, record_name3....record_nameN]
Illustration
Try the following in Elm REPL −
> [{name = "Mohtashim"},{name = "kannan"}] [{ name = "Mohtashim" },{ name = "kannan" }] : List { name : String } > record1 = {name = "FirstRecord"} { name = "FirstRecord" } : { name : String } > record2 = {name = "SecondRecord"} { name = "SecondRecord" } : { name : String } > recordList = [record1,record2] [{ name = "FirstRecord" },{ name = "SecondRecord" }] : List { name : String }
Update a Record
Records are immutable in Elm. When a record is updated, a new record with updated values is returned. The field can hold value of a different type when updating a record.
Syntax
{record_name | field_name1 = new_value1, field_name2 = new_value2,field_name3 = new_value3....field_nameN = new_valueN}
Illustration
Try the following in Elm REPL −
> record1 = {name="FirstRecord"} { name = "FirstRecord" } : { name : String } > record1_updated = {record1 | name = "FirstRecordUpdate"} { name = "FirstRecordUpdate" } : { name : String } > record1 { name = "FirstRecord" } : { name : String } > record1 == record1_updated False : Bool
Illustration
The following example updates multiple fields of a record. Try the following in Elm REPL −
> record3 = {a = 1,b = 2,c = 3,d = 4,e = 5} { a = 1, b = 2, c = 3, d = 4, e = 5 } : { a : number, b : number1, c : number2, d : number3, e : number4 } > record4 = {record3 | d=400 ,e=500} { a = 1, b = 2, c = 3, d = 400, e = 500 } : { a : number2, b : number3, c : number4, d : number, e : number1 } >
Types apas
Type apas defines a schema for a record. In other words, a type apas defines which fields can the record store and the type of value these fields can store. Therefore, programmer will not make mistake of missing any specific attribute while assigning values.
Syntax
type apas apas_name = {field_name1:data_type,field_name2:data_type,....field_nameN:data_type}
Illustration
Execute the following in Elm REPL −
> type apas Developer = { name:String,location:String,age:Int} > dev1 = Developer "kannan" "Mumbai" 20 { name = "kannan", location = "Mumbai", age = 20 } : Repl.Developer > dev2 = Developer "mohtashim" "hyderabad" 20 { name = "mohtashim", location = "hyderabad", age = 20 } : Repl.Developer >
Now if you forget to type location and age, the statement returns a function, which has input parameters for location and age fields.
> dev3 = Developer "Bhagavati" <function> : String -> Int -> Repl.Developer We can invoke the function as shown below and pass to it the values for location and age fields. > dev3 "Pune" 25 { name = "Bhagavati", location = "Pune", age = 25 } : Repl.DeveloperAdvertisements