pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://github.com/chpatrick/codec/pull/8

el="stylesheet" href="https://github.githubassets.com/assets/code-2d31826944fd3be8.css" /> Generic construction of codecs by MDeiml · Pull Request #8 · chpatrick/codec · GitHub
Skip to content

Generic construction of codecs#8

Open
MDeiml wants to merge 2 commits intochpatrick:masterfrom
MDeiml:master
Open

Generic construction of codecs#8
MDeiml wants to merge 2 commits intochpatrick:masterfrom
MDeiml:master

Conversation

@MDeiml
Copy link
Copy Markdown

@MDeiml MDeiml commented Dec 12, 2018

Using Generic its easy to combine multiple codecs into more complex ones. This is especially useful for sum types, which were previously hard to deal with when constructing codecs. It is for example possible to write functions for lists and Maybe:

many' :: (Alternative r, Applicative w) => Codec r w a -> Codec r w [a]
many' c = match $ pure () `rchoose` (c `combine` many' c)

maybe' :: (Alternative r, Applicative w) => Codec r w () -> Codec r w a -> Codec r w (Maybe a)
maybe' n j = match $ n `rchoose` j

I think sum types are so hard to deal with because you have to "prove" to the compiler, that you covered all possible cases when serializing. These changes solve this problem by using Generic to deconstruct any type into a combination of tuples and Either.

Another solution would be to write a Alternative instance, but this would require w to also have an Alternative instance, which is generally not the case.

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

Successfully merging this pull request may close these issues.

1 participant

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy