Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Profunctor of Codec can use some generalization. #375

Open
kindaro opened this issue Apr 11, 2021 · 1 comment
Open

Profunctor of Codec can use some generalization. #375

kindaro opened this issue Apr 11, 2021 · 1 comment

Comments

@kindaro
Copy link

kindaro commented Apr 11, 2021

I want to parse values from a TOML configuration file into a map. Going in another direction, it would mean looking for values in a map. But this operation is partial. I cannot find a standard combinator for this case.

(I cannot use functions from Toml.Codec.Combinator.Map because my map's entries live on the top level of the configuration file — so they are not nested under a key, and all functions from that module require a key!)

I already have a codec of type Toml.Codec part part. That is to say, I know how to read and write my values in the context of themselves. Now I need to read and write them in the context of the whole, so that I may have Toml.Codec whole part, from which I could easily assemble the desired Toml.Codec whole whole with some applicative magic.

So, I need to do this:

Toml.Codec part a → Toml.Codec whole a

The problem is, I only have a partial getter of type whole → Maybe part. If I had a total getter of type whole → part, I would have used .=. But here I need something a little more general.

I need this:

lmapMaybe ∷ (whole → Maybe part) → Toml.Codec part a → Toml.Codec whole a
lmapMaybe getter [email protected] {..} = codec {Toml.codecWrite = fixture}
  where
    fixture = Toml.TomlState . fromMaybe (Nowhole, ) . fmap (Toml.unTomlState . codecWrite) . getter

I could also cheaply have this along the way:

dimapMaybe ∷ (whole → Maybe part) → (a → b) → Toml.Codec part a → Toml.Codec whole b
dimapMaybe getter setter = lmapMaybe getter . fmap setter

Are these functions already in the library? If not, can they be added? I can make a pull request.

@kindaro
Copy link
Author

kindaro commented Apr 11, 2021

While at it, I think some if not all of the code in Toml.Codec.Di can work on more general codecs than TomlCodec a.

For example, I am putting this code into my projects:

dimap ∷ (left → left') → (right → right') → Toml.Codec left' right → Toml.Codec left right'
dimap f g Toml.Codec {..} = Toml.Codec
    { codecRead  = fmap g . codecRead
    , codecWrite = fmap g . codecWrite . f
    }

It is essentially the same as your dimap code except for a more general type signature. It would be better for me if such functions should be exported from the library in their full generality. Is it a good idea overall — for you to decide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant