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

Add merge flipped #83

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Notable changes to this project are documented in this file. The format is based
Breaking changes:

New features:
- Add `mergeFlipped` and infix operator `//`

Bugfixes:

Expand Down
44 changes: 34 additions & 10 deletions src/Record.purs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module Record
( get
, set
, modify
, insert
( (//)
, class EqualFields
, delete
, rename
, disjointUnion
, equal
, equalFields
, get
, insert
, merge
, union
, disjointUnion
, mergeFlipped
, modify
, nub
, class EqualFields
, equalFields
) where
, rename
, set
, union
)
where

import Prelude

Expand Down Expand Up @@ -168,6 +171,27 @@ merge
-> Record r4
merge l r = runFn2 unsafeUnionFn l r

-- | Like `merge` but with its arguments flipped. I.e. merges two records with the seconds record's labels taking precedence in the
-- | case of overlaps.
-- |
-- | For example:
-- |
-- | ```purescript
-- | mergeFlipped { x: 1, y: "y" } { y: 2, z: true }
-- | :: { x :: Int, y :: Int, z :: Boolean }
-- | ```
mergeFlipped
:: forall r1 r2 r3 r4
. Union r1 r2 r3
=> Nub r3 r4
=> Record r2
-> Record r1
-> Record r4
mergeFlipped = flip merge

-- | Operator alias for mergeFlipped (right-associative / precedence 1)
infixr 1 mergeFlipped as //

-- | Merges two records with the first record's labels taking precedence in the
-- | case of overlaps. Unlike `merge`, this does not remove duplicate labels
-- | from the resulting record type. This can result in better inference for
Expand Down
4 changes: 3 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Test.Main where
import Prelude

import Effect (Effect)
import Record (delete, equal, get, insert, merge, modify, rename, set)
import Record (delete, equal, get, insert, merge, modify, rename, set, (//))
import Record.Builder as Builder
import Record.Unsafe (unsafeHas)
import Test.Assert (assert')
Expand Down Expand Up @@ -33,6 +33,8 @@ main = do
not $ equal { a: 1, b: "b", c: true } { a: 1, b: "b", c: false }
assert' "merge" $
equal { x: 1, y: "y" } (merge { y: "y" } { x: 1, y: 2 })
assert' "mergeFlipped" $
equal { x: 1, y: "y", z: true } ({ x: 1, y: 2 } // { y: "y" } // { z: true })
assert' "unsafeHas1" $
unsafeHas "a" { a: 42 }
assert' "unsafeHas2" $
Expand Down