Skip to content

Instantly share code, notes, and snippets.

@anilanar
Created December 25, 2017 03:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anilanar/3530f9bbb2314490c23c27f391c071b8 to your computer and use it in GitHub Desktop.
Save anilanar/3530f9bbb2314490c23c27f391c071b8 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