Skip to content

add Builder.modify #24

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

Merged
merged 1 commit into from
Jan 28, 2018
Merged
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
9 changes: 9 additions & 0 deletions src/Data/Record/Builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ exports.unsafeInsert = function(l) {
};
};

exports.unsafeModify = function(l) {
return function (f) {
return function(rec) {
rec[l] = f(rec[l]);
return rec;
};
};
};

exports.unsafeDelete = function(l) {
return function(rec) {
delete rec[l];
Expand Down
13 changes: 13 additions & 0 deletions src/Data/Record/Builder.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Data.Record.Builder
( Builder
, build
, insert
, modify
, delete
, rename
, merge
Expand All @@ -14,6 +15,7 @@ import Type.Row (class RowLacks)

foreign import copyRecord :: forall r1. Record r1 -> Record r1
foreign import unsafeInsert :: forall a r1 r2. String -> a -> Record r1 -> Record r2
foreign import unsafeModify :: forall a b r1 r2. String -> (a -> b) -> Record r1 -> Record r2
foreign import unsafeDelete :: forall r1 r2. String -> Record r1 -> Record r2
foreign import unsafeRename :: forall r1 r2. String -> String -> Record r1 -> Record r2
foreign import unsafeMerge :: forall r1 r2 r3. Record r1 -> Record r2 -> Record r3
Expand Down Expand Up @@ -49,6 +51,17 @@ insert
-> Builder (Record r1) (Record r2)
insert l a = Builder \r1 -> unsafeInsert (reflectSymbol l) a r1

-- | Build by modifying an existing field.
modify
:: forall l a b r r1 r2
. RowCons l a r r1
=> RowCons l b r r2
=> IsSymbol l
=> SProxy l
-> (a -> b)
-> Builder (Record r1) (Record r2)
modify l f = Builder \r1 -> unsafeModify (reflectSymbol l) f r1

-- | Build by deleting an existing field.
delete
:: forall l a r1 r2
Expand Down
3 changes: 2 additions & 1 deletion test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ main = do
let testBuilder = Builder.build (Builder.insert x 42
>>> Builder.merge { y: true, z: "testing" }
>>> Builder.delete y
>>> Builder.modify x show
>>> Builder.rename z y) {}

assert' "Data.Record.Builder" $
testBuilder.x == 42 && testBuilder.y == "testing"
testBuilder.x == "42" && testBuilder.y == "testing"