How to partially update a purescript record
Asked Answered
K

1

6

everyone!

I'm a Purescript beginner and have trouble with working on records.

I have one record type:

type Employee =
 { firstName :: String
 , lastName :: String
 , address :: String
 , height :: Number
 , weight :: Number
 ...
 }

And I want to update just a portion of this record. Let's say I want to only update height like the following typescript code.

let a: Employee = {
 ...a,
 height: 180
}

How can I achieve this in Purescript? Thank you.

Kimberykimble answered 30/12, 2019 at 14:45 Comment(0)
H
8

Syntax for record update in PureScript is the following:

r2 = r1 { x = 42, y = "foo" }

Where:

  • r1 is the original record
  • r2 is the new, updated record
  • x and y are record fields (not necessarily ALL fields)

The above snippet is equivalent to the following JavaScript code:

r2 = { ...r1, x: 42, y: "foo" }
Hyacinthe answered 30/12, 2019 at 14:53 Comment(2)
Can you explain to me that to update only x? @Fyodor SoikinKimberykimble
Just don't include yHyacinthe

© 2022 - 2024 — McMap. All rights reserved.