Skip to content

Instantly share code, notes, and snippets.

@vyorkin
Forked from anilanar/DiffRecords.purs
Created April 22, 2018 15:02
Show Gist options
  • Save vyorkin/992d27a31a64c7bb4c5dfc6905239b75 to your computer and use it in GitHub Desktop.
Save vyorkin/992d27a31a64c7bb4c5dfc6905239b75 to your computer and use it in GitHub Desktop.
lodash.without in purescript using RowToList
{-
This is just adventuring with RowToList.
Naive approach, not stack safe, probably too slow.
-}
module DiffRecords
where
import Data.Record (get, insert)
import Data.Symbol (class IsSymbol, SProxy(..))
import Type.Row (class RowLacks, class RowToList, Cons, Nil, RLProxy(RLProxy), kind RowList)
-- Example:
-- :t diffRecords {a: 1, c: 2} {a: 2, b: 3, c: 4}
-- { b :: Int }
diffRecords :: forall xs ys zs lxs lys lzs
. RowToList xs lxs
=> RowToList ys lys
=> RowToList zs lzs
=> DiffRecords lxs lys xs ys zs
=> {|xs}
-> {|ys}
-> {|zs}
diffRecords = diffRecordsImpl (RLProxy :: RLProxy lxs) (RLProxy :: RLProxy lys)
class DiffRecords
(la :: RowList)
(lb :: RowList)
(ra :: # Type)
(rb :: # Type)
(rc :: # Type)
| la -> ra
, lb -> rb
, la lb -> rc
where
diffRecordsImpl :: RLProxy la -> RLProxy lb -> {|ra} -> {|rb} -> {|rc}
instance diffRecordsNilNil :: DiffRecords Nil Nil ra rb () where
diffRecordsImpl _ _ _ _ = {}
instance diffRecordsNilCons ::
( IsSymbol k
, RowCons k a rbt rb
, RowCons k a rct rc
, RowLacks k rct
, DiffRecords Nil tb ra rb rct
) => DiffRecords Nil (Cons k a tb) ra rb rc where
diffRecordsImpl _ _ xs ys = insert name head tail
where
name = SProxy :: SProxy k
head = get name ys
tail = diffRecordsImpl (RLProxy :: RLProxy Nil) (RLProxy :: RLProxy tb) xs ys
instance diffRecordsConsCons ::
( IsSymbol k
, RowCons k a rat ra
, RowLacks k rat
, DiffRecords ta tb ra rb rc
) => DiffRecords (Cons k a ta) (Cons k a tb) ra rb rc where
diffRecordsImpl :: RLProxy (Cons k a ta)
-> RLProxy (Cons k a tb)
-> {|ra}
-> {|rb}
-> {|rc}
diffRecordsImpl _ _ xs ys = tail
where
name = SProxy :: SProxy k
tail = diffRecordsImpl (RLProxy :: RLProxy ta) (RLProxy :: RLProxy tb) xs ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment