Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify some functions in ListZipper #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 19 additions & 28 deletions src/Course/ListZipper.hs
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,11 @@ moveLeftN' ::
Int
-> ListZipper a
-> Either Int (ListZipper a)
moveLeftN' n z =
let moveLeftN'' n' z' q
| n' == 0 = Right z'
| n' < 0 = moveRightN' (negate n') z
| otherwise =
case moveLeft z' of
IsZ zz -> moveLeftN'' (n' - 1) zz (q + 1)
IsNotZ -> Left q
in moveLeftN'' n z 0
moveLeftN' n z
| n < 0 = moveRightN' (negate n) z
| otherwise = case moveLeftN n z of
IsZ zz -> Right zz
IsNotZ -> Left $ length (lefts z)

-- | Move the focus right the given number of positions. If the value is negative, move left instead.
-- If the focus cannot be moved, the given number of times, return the value by which it can be moved instead.
Expand All @@ -484,15 +480,11 @@ moveRightN' ::
Int
-> ListZipper a
-> Either Int (ListZipper a)
moveRightN' n z =
let moveRightN'' n' z' q
| n' == 0 = Right z'
| n' < 0 = moveLeftN' (negate n') z
| otherwise =
case moveRight z' of
IsZ zz -> moveRightN'' (n' - 1) zz (q + 1)
IsNotZ -> Left q
in moveRightN'' n z 0
moveRightN' n z
| n < 0 = moveLeftN' (negate n) z
| otherwise = case moveRightN n z of
IsZ zz -> Right zz
IsNotZ -> Left $ length (rights z)

-- | Move the focus to the given absolute position in the zipper. Traverse the zipper only to the extent required.
--
Expand All @@ -508,14 +500,13 @@ nth ::
Int
-> ListZipper a
-> MaybeListZipper a
nth i z =
if i < 0
then
IsNotZ
else
case moveLeftN' i z of
Left a -> moveRightN (i-a) z
Right (ListZipper l _ _) -> moveLeftN (length l) z
nth n z
| n < 0 = IsNotZ
| v == 0 = IsZ z
| v < 0 = moveLeftN (negate v) z
| otherwise = moveRightN v z
where
v = n - index z

-- | Return the absolute position of the current focus in the zipper.
--
Expand All @@ -526,8 +517,8 @@ nth i z =
index ::
ListZipper a
-> Int
index (ListZipper l _ _) =
length l
index = length . lefts


-- | Move the focus to the end of the zipper.
--
Expand Down