-
Notifications
You must be signed in to change notification settings - Fork 31
classes from @doolse's purescript-records: #7
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
exports.unsafeMerge = function (l) { | ||
return function (r) { | ||
var o = {}; | ||
return Object.assign(o, l, r); | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module Data.Record.Class ( | ||
class Subrow, | ||
class RecordMerge, | ||
class IntersectRow, | ||
merge, | ||
unionMerge | ||
) where | ||
|
||
-- | Proof that row `r` is a subset of row `s` | ||
class Subrow (r :: # Type) (s :: # Type) | ||
instance srInst :: Union r t s => Subrow r s | ||
|
||
-- | Proof of row `i` being the intersection of rows `ri` and `si`, | ||
-- | `r` is `i` subtracted from `ri` and | ||
-- | `s` is `i` subtracted from `si` | ||
class IntersectRow (ri :: # Type) (si :: # Type) (i :: # Type) (r :: # Type) (s :: # Type) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really belongs in |
||
instance irInst :: (Union r i ri, Union i s si) => IntersectRow ri si i r s | ||
|
||
class RecordMerge (o :: # Type) (mr :: # Type) (mo :: # Type) | ||
instance rmInst :: (IntersectRow mo mr m o r, Subrow r o) => RecordMerge o mr mo | ||
|
||
|
||
-- | Merge any two records together unsafely. | ||
-- | Fields common between the two will result in the value from r2 being kept | ||
foreign import unsafeMerge | ||
:: forall r1 r2 r3 | ||
. Record r1 | ||
-> Record r2 | ||
-> Record r3 | ||
|
||
-- | Merge a record `mr` with optional default values `o`, resulting in record `mo`. | ||
-- | | ||
-- | The record `mr` must consist of the common fields from `mo` and `mr` plus a subset | ||
-- | of fields from `o`. | ||
-- | | ||
-- | Examples: | ||
-- | * `merge {a:1,b:"Unspecified"} {a:3,c:"Mandatory"} = {a:3,b:"Unspecified",c:"Mandatory"}` | ||
-- | * `merge {a:1,b:"Unspecified"} {c:"Only mandatory"} = {a:1,b:"Unspecified",c:"Only Mandatory"}` | ||
-- | * `merge {a:1,b:"Unspecified"} {a:"Wrong type"} = wont compile` | ||
merge :: forall o mr mo. RecordMerge o mr mo => Record o -> Record mr -> Record mo | ||
merge = unsafeMerge | ||
|
||
-- | Merge record `a` with `b` resulting in `c`. The `Union` constraint means that `c` | ||
-- | will contain all the fields from both `a` and `b`, with `b`'s appearing first in the | ||
-- | list of types. | ||
-- | | ||
-- | Examples: | ||
-- | * `unionMerge {a:"Default"} {a:1} = {a:1} :: {a::Int,a::String}` | ||
-- | * `unionMerge {a:"Default",b:2} {a:1} = {a:1,b:2} :: {a::Int,a::String,b::Int}` | ||
unionMerge :: forall a b c. Union b a c => Record a -> Record b -> Record c | ||
unionMerge = unsafeMerge |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should just use
Union
directly here.