on master help sign in
Add `append`
commit964957244540a2cd4ef5641934599251f05db001 authorSridhar Ratnakumar <srid@srid.ca> dateWed 19 Aug 2020, 19:42 (6 years ago) parent747dfb5
2 files changed+29−2
src/JRec.hs+20−2 test/JRecSpec.hs+9
src/JRec.hs+20−2
@@ -4,10 +4,11 @@
44
55module JRec
66 ( unField,
7− union,
87 (:=) (..),
98 Rec,
109 pattern Rec,
10+ append,
11+ union,
1112 )
1213where
1314
@@ -39,7 +40,24 @@ unField _ (_ R.:= value) = value
3940-- Other operations
4041----------------------------------------------------------------------------
4142
42−-- Append records
43+-- Appends records, without removing duplicates.
44+--
45+-- Left-biased. Does not sort.
46+append ::
47+ forall lhs rhs res.
48+ ( KnownNat (R.RecSize lhs),
49+ KnownNat (R.RecSize rhs),
50+ KnownNat (R.RecSize lhs + R.RecSize rhs),
51+ res ~ R.RecAppend lhs rhs,
52+ R.RecCopy lhs lhs res,
53+ R.RecCopy rhs rhs res
54+ ) =>
55+ Rec lhs ->
56+ Rec rhs ->
57+ Rec res
58+append = R.combine
59+
60+-- Merges records, removing duplicates (TODO)
4361--
4462-- Left-biased. Does not sort.
4563--
test/JRecSpec.hs+9
@@ -39,6 +39,15 @@ spec = do
3939 `shouldBe` (Rec (#u := True, #a := 8))
4040 setA2 (Rec (#u := True, #a := 5, #b := 6))
4141 `shouldBe` (Rec (#u := True, #a := 8, #b := 6))
42+ describe "append" $ do
43+ it "simple append" $ do
44+ Rec (#a := 1) `append` Rec (#b := 2)
45+ `shouldBe` Rec (#a := 1, #b := 2)
46+ it "append with duplicates" $ do
47+ let r1 = Rec (#b := 5, #a := 6)
48+ r2 = Rec (#c := 7, #a := 8)
49+ r1 `union` r2
50+ `shouldBe` Rec (#b := 5, #a := 8, #c := 7, #a := 8)
4251 describe "union" $ do
4352 it "simple union" $ do
4453 Rec (#a := 1) `union` Rec (#b := 2)