Hi everyone, I have this idea in my mind, of a record module within the core library. In my mind it would be a big improvement to the language. I’ve not though about if what I’m asking is actually possible, so I’d love to discuss how and why these suggestions can’t or can be done.
So let’s assume for a moment, that we have a module called Record
that can manipulate records using some internal elm magic. Here are some functions that i personally believe would be nice to have:
Record.construct
Record.construct
would be a better version of the existing Record constructors. Essentially making it possible to parse Json files without needing to specify a type alias.
decoder :
D.Decoder
{ name : String
, percent : Float
, per100k : Float
}
decoder =
D.map3 Record.construct
(D.field "name" D.string)
(D.field "percent" D.float)
(D.field "per100k" D.float)
Record.combine
Record.combine
would reintroduce a feature from 1.18: combining records. The difference here would be that both records must have unique field names. This lets us partially create records. When combined with extendable records this makes a lot of sense
{ x = 2
, y = 4
}
|> rotateBy 90
|> Record.combine { z = 0}
Record.map
For Records with the same type for all fields, it should be possible to use map and fold. I’ve seen this in the wild, where I use records for bound lists, but then i need to also introduce a new map function with it.
Record.toList
Again for Records where all types are the same, we can transform the record into a list. Note that in this case the list would be sorted. I need this a lot for using cell automatas. For example in Game of Life, I used a record to represent the neighbours of a cell. Now I need to check if exactly 2 or 3 neighbours are present.
neighbours
|> Record.toList
|> List.filter (\maybe -> maybe /= Nothing)
|> List.length
|> \n -> (n == 3) || (n == 2)
Doing the same without this feature is a lot of boiler plating. (There are 8 neighbours for a given cell)
If you can dream of any additional function that should be added to this imaginary module, i’d like to hear them.