packages feed

venzone-1.0.0.0: src/NonEmptyZipper.hs

-- todo [refactor] Un giorno risponderà...
-- https://gitlab.com/fresheyeball/non-empty-zipper/merge_requests/2

module NonEmptyZipper where

type NonEmptyZipper a = (Int, [a])

-- ~pure
wrap :: a -> NonEmptyZipper a
wrap x = (0, [x])

-- Make a NonEmptyZipper from a focus item (which will be the head)
-- and ADDITIONAL options.
(|:) :: a -> [a] -> NonEmptyZipper a
x |: xs = (0, x:xs)

-- Move the current element backward by one. If the current is the first
-- element in the list, we loop to the last element.
nextMod, previousMod :: NonEmptyZipper a -> NonEmptyZipper a
nextMod z = moveMod z 1
previousMod z = moveMod z (-1)

-- todo elimina quando ti accettano la patch [refactor]
--     https://gitlab.com/fresheyeball/non-empty-zipper/issues
current :: NonEmptyZipper a -> a
current (i, xs) = xs !! i

toList :: NonEmptyZipper a -> [a]
toList (_, xs) = xs

getPosition :: NonEmptyZipper a -> Int
getPosition (i, _) = i


-- ANCILLARIES --

moveMod :: NonEmptyZipper a -> Int -> NonEmptyZipper a
moveMod (i, xs) m =
            let i' = i + m
                -- fixme [refactor] non valido per tutti i wrap!
                ib | i' < 0          = length xs - 1
                   | i' >= length xs = 0
                   | otherwise       = i'
            in (ib, xs)