-
Notifications
You must be signed in to change notification settings - Fork 30
Description
So I find myself writing the following quite a bit:
type VariableName = VariableName of string
module VariableName =
let value (VariableName x) = x
module VariableNameCodec =
let ofDomain = VariableName.value
let toDomain = VariableName
let encoder = ofDomain >> JsonEncode.string
let decoder = JsonDecode.string >> Result.map toDomain
let codec = decoder, encoder And then I saw Codec.invmap and assumed that would satisfy me and I could write JsonCodec.string |> Codec.invmap toDomain ofDomain. However invmap works in the other direction, mapping the JsonValue part of the codec rather than the VariableName part of the codec. Or alternatively, the input to the decoder and the output of the encoder.
I think it would be nice to add the following alongside invmap.
let inline contrainvmap f g (decoder, encoder) =
map (map f) decoder, contramap g encoder
Although I'm not sure the name is quite correct because actually on the decoder it's mapping twice (once through the function and then through the result type it returns) and also because I'm not sure if "contrainvmap" is the correct category theory based name for this thing.
Thoughts on adding this (assuming I've not overlooked something that already exists which does this)?