@@ -2,6 +2,7 @@ module Record.Builder
2
2
( Builder
3
3
, build
4
4
, buildFromScratch
5
+ , flip
5
6
, insert
6
7
, modify
7
8
, delete
@@ -12,8 +13,9 @@ module Record.Builder
12
13
, nub
13
14
) where
14
15
15
- import Prelude
16
+ import Prelude hiding ( flip )
16
17
18
+ import Data.Function (flip ) as Function
17
19
import Data.Function.Uncurried (runFn2 )
18
20
import Data.Symbol (class IsSymbol , reflectSymbol )
19
21
import Prim.Row as Row
@@ -48,7 +50,11 @@ build (Builder b) r1 = b (copyRecord r1)
48
50
49
51
-- | Build a record from scratch.
50
52
buildFromScratch :: forall r . Builder (Record ()) (Record r ) -> Record r
51
- buildFromScratch = flip build {}
53
+ buildFromScratch = Function .flip build {}
54
+
55
+ -- | Flip a function of one argument returning a builder.
56
+ flip :: forall r1 r2 r3 . (Record r1 -> Builder (Record r2 ) (Record r3 )) -> Record r2 -> Builder (Record r1 ) (Record r3 )
57
+ flip f b = Builder \a -> build (f a) b
52
58
53
59
derive newtype instance semigroupoidBuilder :: Semigroupoid Builder
54
60
derive newtype instance categoryBuilder :: Category Builder
@@ -98,25 +104,40 @@ rename :: forall proxy l1 l2 a r1 r2 r3
98
104
-> Builder (Record r1 ) (Record r3 )
99
105
rename l1 l2 = Builder \r1 -> unsafeRename (reflectSymbol l1) (reflectSymbol l2) r1
100
106
101
- -- | Build by merging existing fields from another record.
107
+ -- | Build by merging existing fields from another record, taking precedence
108
+ -- | in the case of overlaps.
109
+ -- |
110
+ -- | For example:
111
+ -- |
112
+ -- | ```purescript
113
+ -- | build (merge { x: 1, y: "y" }) { y: 2, z: true }
114
+ -- | :: { x :: Int, y :: String, z :: Boolean }
115
+ -- | ```
102
116
merge
103
117
:: forall r1 r2 r3 r4
104
118
. Row.Union r1 r2 r3
105
119
=> Row.Nub r3 r4
106
- => Record r2
107
- -> Builder (Record r1 ) (Record r4 )
108
- merge r2 = Builder \r1 -> runFn2 unsafeUnionFn r1 r2
109
-
110
- -- | Build by merging existing fields from another record. Unlike `merge`,
111
- -- | this does not remove duplicate labels from the resulting record type.
112
- -- | This can result in better inference for some pipelines, deferring the
113
- -- | need for a `Nub` constraint.
120
+ => Record r1
121
+ -> Builder (Record r2 ) (Record r4 )
122
+ merge r1 = Builder \r2 -> runFn2 unsafeUnionFn r1 r2
123
+
124
+ -- | Build by merging existing fields from another record, taking precedence
125
+ -- | in the case of overlaps. Unlike `merge`, this does not remove duplicate
126
+ -- | labels from the resulting record type. This can result in better inference
127
+ -- | for some pipelines, deferring the need for a `Nub` constraint.
128
+ -- |
129
+ -- | For example:
130
+ -- |
131
+ -- | ```purescript
132
+ -- | build (union { x: 1, y: "y" }) { y: 2, z: true }
133
+ -- | :: { x :: Int, y :: String, y :: Int, z :: Boolean }
134
+ -- | ```
114
135
union
115
136
:: forall r1 r2 r3
116
137
. Row.Union r1 r2 r3
117
- => Record r2
118
- -> Builder (Record r1 ) (Record r3 )
119
- union r2 = Builder \r1 -> runFn2 unsafeUnionFn r1 r2
138
+ => Record r1
139
+ -> Builder (Record r2 ) (Record r3 )
140
+ union r1 = Builder \r2 -> runFn2 unsafeUnionFn r1 r2
120
141
121
142
-- | Build by merging some disjoint set of fields from another record.
122
143
disjointUnion
0 commit comments