Open Choc13 opened 3 years ago
I also thought about it, but it turns out it's quite easy to convert the isomorphism into a codec. Here's an example of mapping a codec from Map to list:
(Ok << Map.ofList, Map.toList) |> Codec.compose c
As you can see, all we have to do is to add OK <<
to the first function and we have turned an iso-morphism into a codec, so now we can use compose and choose in which order it does compose with other codecs.
But I think your suggestion is still valid, I just wonder if there is a standard name for it.
So I find myself writing the following quite a bit:
And then I saw
Codec.invmap
and assumed that would satisfy me and I could writeJsonCodec.string |> Codec.invmap toDomain ofDomain
. Howeverinvmap
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
.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)?