|
1 |
| -module Data.Record where |
| 1 | +module Data.Record |
| 2 | + ( get |
| 3 | + , set |
| 4 | + , modify |
| 5 | + , insert |
| 6 | + , delete |
| 7 | + ) where |
| 8 | + |
| 9 | +import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3) |
| 10 | +import Data.Symbol (class IsSymbol, SProxy, reflectSymbol) |
| 11 | +import Type.Row (class RowLacks) |
| 12 | + |
| 13 | +foreign import unsafeGet :: forall r a. Fn2 String (Record r) a |
| 14 | +foreign import unsafeSet :: forall r1 r2 a. Fn3 String a (Record r1) (Record r2) |
| 15 | +foreign import unsafeDelete :: forall r1 r2. Fn2 String (Record r1) (Record r2) |
| 16 | + |
| 17 | +-- | Get a property for a label which is specified using a value-level proxy for |
| 18 | +-- | a type-level string. |
| 19 | +-- | |
| 20 | +-- | For example: |
| 21 | +-- | |
| 22 | +-- | ```purescript |
| 23 | +-- | get (SProxy :: SProxy "x") :: forall r a. { x :: a | r } -> a |
| 24 | +-- | ``` |
| 25 | +get |
| 26 | + :: forall r r' l a |
| 27 | + . IsSymbol l |
| 28 | + => RowCons l a r' r |
| 29 | + => SProxy l |
| 30 | + -> Record r |
| 31 | + -> a |
| 32 | +get l r = runFn2 unsafeGet (reflectSymbol l) r |
| 33 | + |
| 34 | +-- | Set a property for a label which is specified using a value-level proxy for |
| 35 | +-- | a type-level string. |
| 36 | +-- | |
| 37 | +-- | For example: |
| 38 | +-- | |
| 39 | +-- | ```purescript |
| 40 | +-- | set (SProxy :: SProxy "x") |
| 41 | +-- | :: forall r a b. a -> { x :: b | r } -> { x :: a | r } |
| 42 | +-- | ``` |
| 43 | +set |
| 44 | + :: forall r1 r2 r l a b |
| 45 | + . IsSymbol l |
| 46 | + => RowCons l a r r1 |
| 47 | + => RowCons l b r r2 |
| 48 | + => SProxy l |
| 49 | + -> b |
| 50 | + -> Record r1 |
| 51 | + -> Record r2 |
| 52 | +set l b r = runFn3 unsafeSet (reflectSymbol l) b r |
| 53 | + |
| 54 | +-- | Modify a property for a label which is specified using a value-level proxy for |
| 55 | +-- | a type-level string. |
| 56 | +-- | |
| 57 | +-- | For example: |
| 58 | +-- | |
| 59 | +-- | ```purescript |
| 60 | +-- | modify (SProxy :: SProxy "x") |
| 61 | +-- | :: forall r a b. (a -> b) -> { x :: a | r } -> { x :: b | r } |
| 62 | +-- | ``` |
| 63 | +modify |
| 64 | + :: forall r1 r2 r l a b |
| 65 | + . IsSymbol l |
| 66 | + => RowCons l a r r1 |
| 67 | + => RowCons l b r r2 |
| 68 | + => SProxy l |
| 69 | + -> (a -> b) |
| 70 | + -> Record r1 |
| 71 | + -> Record r2 |
| 72 | +modify l f r = set l (f (get l r)) r |
| 73 | + |
| 74 | +-- | Insert a new property for a label which is specified using a value-level proxy for |
| 75 | +-- | a type-level string. |
| 76 | +-- | |
| 77 | +-- | For example: |
| 78 | +-- | |
| 79 | +-- | ```purescript |
| 80 | +-- | insert (SProxy :: SProxy "x") |
| 81 | +-- | :: forall r a. a -> { | r } -> { x :: a | r } |
| 82 | +-- | ``` |
| 83 | +insert |
| 84 | + :: forall r1 r2 l a |
| 85 | + . IsSymbol l |
| 86 | + => RowLacks l r1 |
| 87 | + => RowCons l a r1 r2 |
| 88 | + => SProxy l |
| 89 | + -> a |
| 90 | + -> Record r1 |
| 91 | + -> Record r2 |
| 92 | +insert l a r = runFn3 unsafeSet (reflectSymbol l) a r |
| 93 | + |
| 94 | +-- | Delete a property for a label which is specified using a value-level proxy for |
| 95 | +-- | a type-level string. |
| 96 | +-- | |
| 97 | +-- | Note that the type of the resulting row must _lack_ the specified property. |
| 98 | +-- | Since duplicate labels are allowed, this is checked with a type class constraint. |
| 99 | +-- | |
| 100 | +-- | For example: |
| 101 | +-- | |
| 102 | +-- | ```purescript |
| 103 | +-- | delete (SProxy :: SProxy "x") |
| 104 | +-- | :: forall r a. RowLacks "x" r => { x :: a | r } -> { | r } |
| 105 | +-- | ``` |
| 106 | +delete |
| 107 | + :: forall r1 r2 l a |
| 108 | + . IsSymbol l |
| 109 | + => RowLacks l r1 |
| 110 | + => RowCons l a r1 r2 |
| 111 | + => SProxy l |
| 112 | + -> Record r2 |
| 113 | + -> Record r1 |
| 114 | +delete l r = runFn2 unsafeDelete (reflectSymbol l) r |
0 commit comments