Skip to content

Commit

Permalink
Add moveStart and moveEnd functions
Browse files Browse the repository at this point in the history
  • Loading branch information
paluh committed Oct 7, 2018
1 parent f3ccef3 commit 0a63edc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@ Currently this library provides bunch of instances: `Foldable`, `Foldable1`, `Tr

Additionally it implements following operations:

* `prev` / `next` - O(1)
* `insertLeft` / `insertRight` - O(1)
* `deleteLeft` / `deleteRight` - O(1)
* `replace` - O(1)
* `dropSuffix` - O(1)
* `dropPrefix` - O(1)
* `atEnd` - O(1)
* `atStart` - O(1)
* `prev`, `next`, `insertLeft`, `insertRight`, `deleteLeft`, `deleteRight`, `replace`, `dropPrefix`, `dropSuffix`, `atStart`, `atEnd` - O(1)
* `moveStart`, `moveEnd` - O(n)

## Usage example

Expand Down
21 changes: 20 additions & 1 deletion src/Data/List/Pointed.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import Prelude
import Control.Comonad (class Comonad, class Extend)
import Data.Foldable (class Foldable, foldl, foldMap, foldr)
import Data.List (List(..), reverse, uncons)
import Data.List (fromFoldable) as List
import Data.Maybe (Maybe(..))
import Data.Newtype (class Newtype)
import Data.Semigroup.Foldable (class Foldable1)
Expand Down Expand Up @@ -109,11 +108,13 @@ insertRight a (Pointed { focus, reversedPrefix, suffix }) = Pointed
, suffix
}

-- | Delete current `focus` and move to the left if possible.
deleteLeft a. Pointed a Maybe (Pointed a)
deleteLeft (Pointed { focus, reversedPrefix, suffix }) =
uncons reversedPrefix <#> \{ head, tail } → Pointed
{ focus: head, reversedPrefix: tail, suffix }

-- | Delete current `focus` and move to the right if possible.
deleteRight a. Pointed a Maybe (Pointed a)
deleteRight (Pointed { focus, reversedPrefix, suffix }) =
uncons suffix <#> \{ head, tail } → Pointed
Expand All @@ -127,6 +128,24 @@ dropSuffix ∷ ∀ a. Pointed a → Pointed a
dropSuffix (Pointed { focus, reversedPrefix }) =
Pointed { focus, reversedPrefix, suffix: Nil }

moveStart a. Pointed a Pointed a
moveStart p@(Pointed { focus, reversedPrefix, suffix }) = case uncons (reverse <<< Cons focus $ reversedPrefix) of
Just { head, tail } → Pointed
{ focus: head
, reversedPrefix: Nil
, suffix: tail <> suffix
}
Nothing → p

moveEnd a. Pointed a Pointed a
moveEnd p@(Pointed { focus, reversedPrefix, suffix }) = case uncons (reverse <<< Cons focus $ suffix) of
Just { head, tail } → Pointed
{ focus: head
, reversedPrefix: tail <> reversedPrefix
, suffix: Nil
}
Nothing → p

atStart a. Pointed a Boolean
atStart (Pointed { reversedPrefix: Nil }) = true
atStart _ = false
Expand Down
21 changes: 19 additions & 2 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module Test.Main where
import Prelude

import Control.Comonad (extend, extract)
import Data.Array (fromFoldable, snoc) as Array
import Data.Array (foldl, foldr)
import Data.Array (fromFoldable, snoc) as Array
import Data.Foldable (fold, foldMap, sum)
import Data.List (length) as List
import Data.List.Pointed (Pointed(..), fromFoldable, insertLeft, insertRight, prev)
import Data.List.Pointed (Pointed(..), atEnd, atStart, fromFoldable, insertLeft, insertRight, moveEnd, moveStart, prev)
import Data.List.Pointed (fromFoldable) as Pointed
import Data.Maybe (Maybe(..))
import Data.Monoid.Additive (Additive(..))
Expand All @@ -17,6 +17,7 @@ import Test.Unit (suite, test)
import Test.Unit.Assert (equal)
import Test.Unit.Main (runTest)


main :: Effect Unit
main = runTest $ do
suite "Data.List.Pointed" $ do
Expand Down Expand Up @@ -125,3 +126,19 @@ main = runTest $ do
test "foldMap from the end" do
let r = map (foldMap identity) (fromFoldable ["1", "2", "3", "4", "5"])
equal (Just "12345") r


test "moveEnd" $ do
let
arr = ["1", "2", "3", "4", "5"]
r = prev =<< fromFoldable arr
equal (Just false) (atEnd <$> r)
equal (Just true) (atEnd <<< moveEnd <$> r)
equal (Just arr) (Array.fromFoldable <<< moveEnd <$> r)
test "moveStart" $ do
let
arr = ["1", "2", "3", "4", "5"]
r = prev =<< fromFoldable arr
equal (Just false) (atStart <$> r)
equal (Just true) (atStart <<< moveStart <$> r)
equal (Just arr) (Array.fromFoldable <<< moveStart <$> r)

0 comments on commit 0a63edc

Please sign in to comment.