on develop help sign in
Merge pull request #225 from reflex-frp/patch-that-changes-map

Generalize patchThatChangesAndSortsMapWith

commitc339a17fd03337e3c180c0d8d6cd91028d55de55 authorRyan Trinkle <ryan.trinkle@gmail.com> dateMon 27 Aug 2018, 13:04 (8 years ago) parent2d671245fbef38
1 file changed+8−4
src/Reflex/Patch/MapWithMove.hs+8−4
src/Reflex/Patch/MapWithMove.hs+8−4
@@ -120,11 +120,15 @@ patchThatSortsMapWith cmp m = PatchMapWithMove $ Map.fromList $ catMaybes $ zipW
120120-- will produce a 'Map' with the same values as the second 'Map' but with the
121121-- values sorted with the given ordering function.
122122patchThatChangesAndSortsMapWith :: 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
124− where oldByValue = Map.fromListWith Set.union $ swap . first Set.singleton <$> Map.toList oldByIndex
125− newList = Map.toList newByIndexUnsorted
123+patchThatChangesAndSortsMapWith cmp oldByIndex newByIndexUnsorted = patchThatChangesMap oldByIndex newByIndex
124+ where newList = Map.toList newByIndexUnsorted
126125 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
128132 (insertsAndMoves, unusedValuesByValue) = flip runState oldByValue $ do
129133 let f k v = do
130134 remainingValues <- get