mirror of
https://github.com/jtdaugherty/brick.git
synced 2024-10-27 17:48:48 +03:00
refactor many applications of 'view listElementsL'
List / GenericList derives Foldable, which means that in many cases where 'length (l ^. listElementsL)' was written, we can instead write 'length l'. Likewise for 'null'. Avoid these unnecessary applications.
This commit is contained in:
parent
a5c64ef21f
commit
3324049c90
@ -418,7 +418,7 @@ listRemove :: (Splittable t, Foldable t, Semigroup (t e))
|
|||||||
-- size)
|
-- size)
|
||||||
-> GenericList n t e
|
-> GenericList n t e
|
||||||
-> GenericList n t e
|
-> GenericList n t e
|
||||||
listRemove pos l | null (l^.listElementsL) = l
|
listRemove pos l | null l = l
|
||||||
| pos /= splitClamp l pos = l
|
| pos /= splitClamp l pos = l
|
||||||
| otherwise =
|
| otherwise =
|
||||||
let newSel = case l^.listSelectedL of
|
let newSel = case l^.listSelectedL of
|
||||||
@ -517,7 +517,7 @@ listMoveBy amt l =
|
|||||||
let target = case l ^. listSelectedL of
|
let target = case l ^. listSelectedL of
|
||||||
Nothing
|
Nothing
|
||||||
| amt > 0 -> 0
|
| amt > 0 -> 0
|
||||||
| otherwise -> length (l ^. listElementsL) - 1
|
| otherwise -> length l - 1
|
||||||
Just i -> max 0 (amt + i) -- don't be negative
|
Just i -> max 0 (amt + i) -- don't be negative
|
||||||
in listMoveTo target l
|
in listMoveTo target l
|
||||||
|
|
||||||
@ -541,12 +541,10 @@ listMoveTo :: (Foldable t, Splittable t)
|
|||||||
-> GenericList n t e
|
-> GenericList n t e
|
||||||
-> GenericList n t e
|
-> GenericList n t e
|
||||||
listMoveTo pos l =
|
listMoveTo pos l =
|
||||||
let len = length (l ^. listElementsL)
|
let len = length l
|
||||||
i = if pos < 0 then len - pos else pos
|
i = if pos < 0 then len - pos else pos
|
||||||
newSel = splitClamp l i
|
newSel = splitClamp l i
|
||||||
in l & listSelectedL .~ if not (null (l ^. listElementsL))
|
in l & listSelectedL .~ if null l then Nothing else Just newSel
|
||||||
then Just newSel
|
|
||||||
else Nothing
|
|
||||||
|
|
||||||
-- | Split-based clamp that avoids evaluating 'length' of the structure
|
-- | Split-based clamp that avoids evaluating 'length' of the structure
|
||||||
-- (unless the structure is already fully evaluated).
|
-- (unless the structure is already fully evaluated).
|
||||||
@ -560,7 +558,7 @@ splitClamp l i =
|
|||||||
--
|
--
|
||||||
-- Otherwise if tail is not empty, then we already know that i
|
-- Otherwise if tail is not empty, then we already know that i
|
||||||
-- is in the list, so we don't need to know the length
|
-- is in the list, so we don't need to know the length
|
||||||
clamp 0 (if null t then length (l ^. listElementsL) - 1 else i) i
|
clamp 0 (if null t then length l - 1 else i) i
|
||||||
|
|
||||||
-- | Set the selected index for a list to the index of the first
|
-- | Set the selected index for a list to the index of the first
|
||||||
-- occurence of the specified element if it is in the list, or leave
|
-- occurence of the specified element if it is in the list, or leave
|
||||||
@ -627,7 +625,7 @@ listReverse :: (Reversible t, Foldable t)
|
|||||||
-> GenericList n t e
|
-> GenericList n t e
|
||||||
listReverse l =
|
listReverse l =
|
||||||
l & listElementsL %~ reverse
|
l & listElementsL %~ reverse
|
||||||
& listSelectedL %~ fmap (length (l ^. listElementsL) - 1 -)
|
& listSelectedL %~ fmap (length l - 1 -)
|
||||||
|
|
||||||
-- | Apply a function to the selected element. If no element is selected
|
-- | Apply a function to the selected element. If no element is selected
|
||||||
-- the list is not modified.
|
-- the list is not modified.
|
||||||
|
@ -106,9 +106,9 @@ prop_listOpsMaintainSelectedValid ops l =
|
|||||||
in
|
in
|
||||||
case l' ^. listSelectedL of
|
case l' ^. listSelectedL of
|
||||||
-- either there is no selection and list is empty
|
-- either there is no selection and list is empty
|
||||||
Nothing -> null (l' ^. listElementsL)
|
Nothing -> null l'
|
||||||
-- or the selected index is valid
|
-- or the selected index is valid
|
||||||
Just i -> i >= 0 && i < length (l' ^. listElementsL)
|
Just i -> i >= 0 && i < length l'
|
||||||
|
|
||||||
-- reversing a list keeps the selected element the same
|
-- reversing a list keeps the selected element the same
|
||||||
prop_reverseMaintainsSelectedElement
|
prop_reverseMaintainsSelectedElement
|
||||||
@ -124,7 +124,7 @@ prop_reverseMaintainsSelectedElement ops l =
|
|||||||
-- reversing maintains size of list
|
-- reversing maintains size of list
|
||||||
prop_reverseMaintainsSizeOfList :: List n a -> Bool
|
prop_reverseMaintainsSizeOfList :: List n a -> Bool
|
||||||
prop_reverseMaintainsSizeOfList l =
|
prop_reverseMaintainsSizeOfList l =
|
||||||
length (l ^. listElementsL) == length (listReverse l ^. listElementsL)
|
length l == length (listReverse l)
|
||||||
|
|
||||||
-- an inserted element may always be found at the given index
|
-- an inserted element may always be found at the given index
|
||||||
-- (when target index is clamped to 0 <= n <= len)
|
-- (when target index is clamped to 0 <= n <= len)
|
||||||
@ -132,7 +132,7 @@ prop_insert :: (Eq a) => Int -> a -> List n a -> Bool
|
|||||||
prop_insert i a l =
|
prop_insert i a l =
|
||||||
let
|
let
|
||||||
l' = listInsert i a l
|
l' = listInsert i a l
|
||||||
i' = clamp 0 (length (l ^. listElementsL)) i
|
i' = clamp 0 (length l) i
|
||||||
in
|
in
|
||||||
listSelectedElement (listMoveTo i' l') == Just (i', a)
|
listSelectedElement (listMoveTo i' l') == Just (i', a)
|
||||||
|
|
||||||
@ -142,7 +142,7 @@ prop_insertSize i a l =
|
|||||||
let
|
let
|
||||||
l' = listInsert i a l
|
l' = listInsert i a l
|
||||||
in
|
in
|
||||||
length (l' ^. listElementsL) == length (l ^. listElementsL) + 1
|
length l' == length l + 1
|
||||||
|
|
||||||
-- inserting an element and moving to it always succeeds and
|
-- inserting an element and moving to it always succeeds and
|
||||||
-- the selected element is the one we inserted.
|
-- the selected element is the one we inserted.
|
||||||
@ -169,7 +169,7 @@ prop_insertFindBy ops l i a =
|
|||||||
l' = applyListOps op ops l
|
l' = applyListOps op ops l
|
||||||
l'' = set listSelectedL Nothing . listInsert i a $ l'
|
l'' = set listSelectedL Nothing . listInsert i a $ l'
|
||||||
seeks = converging ((==) `on` (^. listSelectedL)) (listFindBy (== a)) l''
|
seeks = converging ((==) `on` (^. listSelectedL)) (listFindBy (== a)) l''
|
||||||
i' = clamp 0 (length (l' ^. listElementsL)) i -- we can't have inserted past len
|
i' = clamp 0 (length l') i -- we can't have inserted past len
|
||||||
in
|
in
|
||||||
(find ((== Just i') . (^. listSelectedL)) seeks >>= listSelectedElement)
|
(find ((== Just i') . (^. listSelectedL)) seeks >>= listSelectedElement)
|
||||||
== Just (i', a)
|
== Just (i', a)
|
||||||
@ -188,7 +188,7 @@ prop_findByNonDecreasing ops l a =
|
|||||||
prop_insertRemove :: (Eq a) => Int -> a -> List n a -> Bool
|
prop_insertRemove :: (Eq a) => Int -> a -> List n a -> Bool
|
||||||
prop_insertRemove i a l =
|
prop_insertRemove i a l =
|
||||||
let
|
let
|
||||||
i' = clamp 0 (length (l ^. listElementsL)) i
|
i' = clamp 0 (length l) i
|
||||||
l' = listInsert i' a l -- pre-clamped
|
l' = listInsert i' a l -- pre-clamped
|
||||||
l'' = listRemove i' l'
|
l'' = listRemove i' l'
|
||||||
in
|
in
|
||||||
@ -199,13 +199,13 @@ prop_insertRemove i a l =
|
|||||||
prop_remove :: Int -> List n a -> Bool
|
prop_remove :: Int -> List n a -> Bool
|
||||||
prop_remove i l =
|
prop_remove i l =
|
||||||
let
|
let
|
||||||
len = length (l ^. listElementsL)
|
len = length l
|
||||||
i' = clamp 0 (len - 1) i
|
i' = clamp 0 (len - 1) i
|
||||||
test
|
test
|
||||||
| len > 0 && i == i' = (== len - 1) -- i is in bounds
|
| len > 0 && i == i' = (== len - 1) -- i is in bounds
|
||||||
| otherwise = (== len) -- i is out of bounds
|
| otherwise = (== len) -- i is out of bounds
|
||||||
in
|
in
|
||||||
test (length (listRemove i l ^. listElementsL))
|
test (length (listRemove i l))
|
||||||
|
|
||||||
-- deleting an element and re-inserting it at same position
|
-- deleting an element and re-inserting it at same position
|
||||||
-- gives the original list elements
|
-- gives the original list elements
|
||||||
@ -234,7 +234,7 @@ prop_moveUp ops l =
|
|||||||
let
|
let
|
||||||
l' = applyListOps op ops l
|
l' = applyListOps op ops l
|
||||||
l'' = converge ((==) `on` (^. listSelectedL)) listMoveUp l'
|
l'' = converge ((==) `on` (^. listSelectedL)) listMoveUp l'
|
||||||
len = length (l'' ^. listElementsL)
|
len = length l''
|
||||||
in
|
in
|
||||||
maybe (len == 0) (== 0) (l'' ^. listSelectedL)
|
maybe (len == 0) (== 0) (l'' ^. listSelectedL)
|
||||||
|
|
||||||
@ -244,7 +244,7 @@ prop_moveDown ops l =
|
|||||||
let
|
let
|
||||||
l' = applyListOps op ops l
|
l' = applyListOps op ops l
|
||||||
l'' = converge ((==) `on` (^. listSelectedL)) listMoveDown l'
|
l'' = converge ((==) `on` (^. listSelectedL)) listMoveDown l'
|
||||||
len = length (l'' ^. listElementsL)
|
len = length l''
|
||||||
in
|
in
|
||||||
maybe (len == 0) (== len - 1) (l'' ^. listSelectedL)
|
maybe (len == 0) (== len - 1) (l'' ^. listSelectedL)
|
||||||
|
|
||||||
@ -292,7 +292,7 @@ prop_moveByWhenNoSelection :: List n a -> Int -> Property
|
|||||||
prop_moveByWhenNoSelection l amt =
|
prop_moveByWhenNoSelection l amt =
|
||||||
let
|
let
|
||||||
l' = l & listSelectedL .~ Nothing
|
l' = l & listSelectedL .~ Nothing
|
||||||
len = length (l ^. listElementsL)
|
len = length l
|
||||||
expected = if amt > 0 then 0 else len - 1
|
expected = if amt > 0 then 0 else len - 1
|
||||||
in
|
in
|
||||||
len > 0 ==> listMoveBy amt l' ^. listSelectedL == Just expected
|
len > 0 ==> listMoveBy amt l' ^. listSelectedL == Just expected
|
||||||
|
Loading…
Reference in New Issue
Block a user