Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/samuelschlesinger/merge
Describe merging of data types
https://github.com/samuelschlesinger/merge
Last synced: 22 days ago
JSON representation
Describe merging of data types
- Host: GitHub
- URL: https://github.com/samuelschlesinger/merge
- Owner: SamuelSchlesinger
- License: mit
- Created: 2021-09-16T08:57:33.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-19T00:54:58.000Z (over 3 years ago)
- Last Synced: 2024-10-27T19:10:53.646Z (2 months ago)
- Language: Haskell
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Merge
Often, one finds themselves having multiple sources of knowledge
for some piece of data, and having to merge these together. Perhaps
we have a type representing partial information about a digital friend.```haskell
data Friend = Friend
{ name :: Maybe Text
, email :: Maybe Text
, age :: Max Int
, pubKey :: PublicKey
}
```If we learn some information about a friend from someone, and some
from someone else, we'll want to merge that information to have a
more complete picture. That said, it might not succeed, as we may
have inconsistent information like two different names or different
public keys. We'll want a function of type:```haskell
f :: Friend -> Friend -> Maybe Friend
```That's the pattern that this library encapsulates!
```
mergeFriends :: Merge [String] Friend Friend
mergeFriends =
User
<$> optional name .? ["name"]
<*> optional email .? ["email"]
<*> combine age .? ["age"]
<*> required pubKey .? ["pubKey"]f :: Friend -> Friend -> Validation [String] Friend
f x y = runMerge mergeFriends x y
```We didn't get exactly what we thought we wanted, but this Validation
type is better: it is an Applicative which accumulates the errors for
every field.