1
1
mirror of https://github.com/aelve/guide.git synced 2024-11-27 00:14:03 +03:00

Rename 'insertGuaranteed' to 'insertOrAppend'

This commit is contained in:
Artyom 2017-04-26 20:01:44 +03:00
parent 7a6abc0315
commit e0ebf3da64
No known key found for this signature in database
GPG Key ID: B8E35A33FF522710
2 changed files with 13 additions and 9 deletions

View File

@ -556,7 +556,7 @@ restoreCategory catId pos = do
Nothing -> return (Left "category not found in deleted categories") Nothing -> return (Left "category not found in deleted categories")
Just category -> do Just category -> do
categoriesDeleted %= deleteFirst (hasUid catId) categoriesDeleted %= deleteFirst (hasUid catId)
categories %= insertAtGuaranteed pos category categories %= insertOrAppend pos category
return (Right ()) return (Right ())
restoreItem :: Uid Item -> Int -> Acid.Update GlobalState (Either String ()) restoreItem :: Uid Item -> Int -> Acid.Update GlobalState (Either String ())
@ -569,7 +569,7 @@ restoreItem itemId pos = do
let item = fromJust (find (hasUid itemId) (category^.itemsDeleted)) let item = fromJust (find (hasUid itemId) (category^.itemsDeleted))
let category' = category let category' = category
& itemsDeleted %~ deleteFirst (hasUid itemId) & itemsDeleted %~ deleteFirst (hasUid itemId)
& items %~ insertAtGuaranteed pos item & items %~ insertOrAppend pos item
categories . each . filtered ourCategory .= category' categories . each . filtered ourCategory .= category'
categoriesDeleted . each . filtered ourCategory .= category' categoriesDeleted . each . filtered ourCategory .= category'
return (Right ()) return (Right ())
@ -590,7 +590,7 @@ restoreTrait itemId traitId pos = do
(Just trait, _) -> do (Just trait, _) -> do
let item' = item let item' = item
& prosDeleted %~ deleteFirst (hasUid traitId) & prosDeleted %~ deleteFirst (hasUid traitId)
& pros %~ insertAtGuaranteed pos trait & pros %~ insertOrAppend pos trait
let category' = category let category' = category
& items . each . filtered (hasUid itemId) .~ item' & items . each . filtered (hasUid itemId) .~ item'
& itemsDeleted . each . filtered (hasUid itemId) .~ item' & itemsDeleted . each . filtered (hasUid itemId) .~ item'
@ -600,7 +600,7 @@ restoreTrait itemId traitId pos = do
(_, Just trait) -> do (_, Just trait) -> do
let item' = item let item' = item
& consDeleted %~ deleteFirst (hasUid traitId) & consDeleted %~ deleteFirst (hasUid traitId)
& cons %~ insertAtGuaranteed pos trait & cons %~ insertOrAppend pos trait
let category' = category let category' = category
& items . each . filtered (hasUid itemId) .~ item' & items . each . filtered (hasUid itemId) .~ item'
& itemsDeleted . each . filtered (hasUid itemId) .~ item' & itemsDeleted . each . filtered (hasUid itemId) .~ item'

View File

@ -19,7 +19,7 @@ module Guide.Utils
moveUp, moveUp,
moveDown, moveDown,
deleteFirst, deleteFirst,
insertAtGuaranteed, insertOrAppend,
ordNub, ordNub,
-- * 'Eq' -- * 'Eq'
@ -134,10 +134,14 @@ deleteFirst f (x:xs) = if f x then xs else x : deleteFirst f xs
-- | Insert given element into the list, or append it to the list if the -- | Insert given element into the list, or append it to the list if the
-- position is outside the list bounds. -- position is outside the list bounds.
insertAtGuaranteed :: Int -> a -> [a] -> [a] insertOrAppend
insertAtGuaranteed _ a [] = [a] :: Int -- ^ Preferred position
insertAtGuaranteed 0 a xs = a:xs -> a -- ^ Element to insert
insertAtGuaranteed n a (x:xs) = x : insertAtGuaranteed (n-1) a xs -> [a]
-> [a]
insertOrAppend _ a [] = [a]
insertOrAppend 0 a xs = a:xs
insertOrAppend n a (x:xs) = x : insertOrAppend (n-1) a xs
-- | A version of 'works in @O(n log n)@ instead of @O(n^2)@. -- | A version of 'works in @O(n log n)@ instead of @O(n^2)@.
ordNub :: Ord a => [a] -> [a] ordNub :: Ord a => [a] -> [a]