on develop help sign in
Generalize patchThatChangesAndSortsMapWith
5fbef38Doug Beardsley
src/Reflex/Patch/MapWithMove.hs+8−4
@@ -120,11 +120,15 @@ patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipW
120-- will produce a 'Map' with the same values as the second 'Map' but with the
120-- will produce a 'Map' with the same values as the second 'Map' but with the
121-- values sorted with the given ordering function.
121-- values sorted with the given ordering function.
122patchThatChangesAndSortsMapWith :: forall k v. (Ord k, Ord v) => (v -> v -> Ordering) -> Map k v -> Map k v -> PatchMapWithMove k v
122patchThatChangesAndSortsMapWith :: forall k v. (Ord k, Ord v) => (v -> v -> Ordering) -> Map k v -> Map k v -> PatchMapWithMove k v
123−patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patch
123+patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
124− where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
125− newList = Map.toList newByIndexUnsorted
124+ where newList = Map.toList newByIndexUnsorted
126 newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
125 newByIndex = Map.fromList $ zip (fst <$> newList) $ sortBy cmp $ snd <$> newList
127− insertsAndMoves :: Map k (NodeInfo k v)
126+
127+-- | Create a 'PatchMapWithMove' that, if applied to the first 'Map' provided,
128+-- will produce the second 'Map'.
129+patchThatChangesMap :: (Ord k, Ord v) => Map k v -> Map k v -> PatchMapWithMove k v
130+patchThatChangesMap oldByIndex newByIndex = patch
131+ where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
128 (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
132 (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
129 let f k v = do
133 let f k v = do
130 remainingValues <- get
134 remainingValues <- get