lifebalance
lifebalance

Reputation: 1927

How to write a generic function that updates fields of a Record

Consider

type alias Rec = { a: Int, b: Int, c:Int }
updateRec r aVal            = { r|a = aVal } 
updateRec2 r aVal bVal      = { r|a = aVal, b= bVal } 
updateRec3 r aVal bVal cVal = ...

How to generalize updateRec and updateRec2... into one function?

Upvotes: 1

Views: 177

Answers (3)

Søren Debois
Søren Debois

Reputation: 5688

Here's a better way of doing the same thing:

updateA : Int -> Rec -> Rec
updateA x rec = { rec | a = x }

-- Similarly for b, c

Now you can do the following, supposing you already have a value rec : Rec you want to update:

myUpdatedRec : Rec
myUpdatedRec =
  rec 
  |> updateA 7
  |> updateB 19

You can now update an arbitrary number of fields by stringing together |> updateX ....

Upvotes: 2

Chad Gilbert
Chad Gilbert

Reputation: 36385

Elm's record update syntax seems to be exactly what you are looking for. You "pass in" the record that you want updated, r below, and you can return a record with whatever fields you want changed without having to specify every field:

ex1 = { r|a = 1 }
ex2 = { r|b = 2, c = 3 }

You don't need to create a set of additional functions for updating only certain fields at certain times, because Elm's record update syntax is that generalized function.

Upvotes: 1

Søren Debois
Søren Debois

Reputation: 5688

You want to write a function that has a variable number of differently-typed parameters. That's common in dynamically typed languages (Javascript, Python, Ruby) but usually not allowed in typed languages. Elm doesn't allow it.

You can emulate a variable number of differently-typed parameterswith the Maybe type, understanding Nothing as "missing argument":

updateRec : Rec -> Maybe Int -> Maybe Int -> Maybe Int -> Rec
updateRec r a b c = 
  { r
  | a = a |> Maybe.withDefault r.a
  , b = b |> Maybe.withDefault r.b
  , c = c |> Maybe.withDefault r.c
  } 

If the record fields are all of the same type (here Int), you could accept a List Int instead:

updateRec : Rec -> List Int -> Rec 
updateRec r fields = 
  case fields of 
    [a] -> { r | a = a }
    [a,b] -> { r | a = a, b = b }
    [a,b,c] -> { r | a = a, b = b, c = c }
    _ -> r

I don't like this solution because you it'll fail silently if you accidentally supply a list with 0 or 4+ elements. If this function is helpful to you anyway, perhaps it would be better to use List Int instead of Rec in the first place.

Upvotes: 2

Related Questions