Skip to content
Merged
Show file tree
Hide file tree
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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# purescript-lists

[![Latest release](http://img.shields.io/bower/v/purescript-lists.svg)](https://github.com/purescript/purescript-lists/releases)
[![Build Status](https://travis-ci.org/purescript/purescript-lists.svg?branch=master)](https://travis-ci.org/purescript/purescript-lists)
[![Dependency Status](https://www.versioneye.com/user/projects/55848c8a363861001d00033b/badge.svg?style=flat)](https://www.versioneye.com/user/projects/55848c8a363861001d00033b)
[![Latest release](http://img.shields.io/github/release/purescript/purescript-lists.svg)](https://github.com/purescript/purescript-lists/releases)
[![Build status](https://travis-ci.org/purescript/purescript-lists.svg?branch=master)](https://travis-ci.org/purescript/purescript-lists)

This library defines strict and lazy linked lists, and associated helper functions and type class instances.

Expand Down
56 changes: 29 additions & 27 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ length :: forall a. List a -> Int
length xs = go (step xs)
where
go Nil = 0
go (Cons _ xs) = 1 + go (step xs)
go (Cons _ xs') = 1 + go (step xs')

--------------------------------------------------------------------------------
-- Extending arrays ------------------------------------------------------------
Expand Down Expand Up @@ -234,11 +234,12 @@ head xs = _.head <$> uncons xs
-- |
-- | Running time: `O(n)`.
last :: forall a. List a -> Maybe a
last xs = go (step xs)
last = go <<< step
where
go (Cons x xs) | null xs = Just x
| otherwise = go (step xs)
go _ = Nothing
go (Cons x xs)
| null xs = Just x
| otherwise = go (step xs)
go _ = Nothing

-- | Get all but the first element of a list, or `Nothing` if the list is empty.
-- |
Expand All @@ -250,21 +251,22 @@ tail xs = _.tail <$> uncons xs
-- |
-- | Running time: `O(n)`
init :: forall a. List a -> Maybe (List a)
init xs = go (step xs)
init = go <<< step
where
go :: Step a -> Maybe (List a)
go (Cons x xs) | null xs = Just nil
| otherwise = cons x <$> go (step xs)
go _ = Nothing
go (Cons x xs)
| null xs = Just nil
| otherwise = cons x <$> go (step xs)
go _ = Nothing

-- | Break a list into its first element, and the remaining elements,
-- | or `Nothing` if the list is empty.
-- |
-- | Running time: `O(1)`
uncons :: forall a. List a -> Maybe { head :: a, tail :: List a }
uncons xs = case step xs of
Nil -> Nothing
Cons x xs -> Just { head: x, tail: xs }
Nil -> Nothing
Cons x xs' -> Just { head: x, tail: xs' }

--------------------------------------------------------------------------------
-- Indexed operations ----------------------------------------------------------
Expand Down Expand Up @@ -332,7 +334,7 @@ deleteAt n xs = List (go n <$> unwrap xs)
where
go _ Nil = Nil
go 0 (Cons y ys) = step ys
go n (Cons y ys) = Cons y (deleteAt (n - 1) ys)
go n' (Cons y ys) = Cons y (deleteAt (n' - 1) ys)

-- | Update the element at the specified index, returning a new
-- | list or `Nothing` if the index is out-of-bounds.
Expand All @@ -346,7 +348,7 @@ updateAt n x xs = List (go n <$> unwrap xs)
where
go _ Nil = Nil
go 0 (Cons _ ys) = Cons x ys
go n (Cons y ys) = Cons y (updateAt (n - 1) x ys)
go n' (Cons y ys) = Cons y (updateAt (n' - 1) x ys)

-- | Update the element at the specified index by applying a function to
-- | the current value, returning a new list or `Nothing` if the index is
Expand Down Expand Up @@ -374,7 +376,7 @@ alterAt n f xs = List (go n <$> unwrap xs)
go 0 (Cons y ys) = case f y of
Nothing -> step ys
Just y' -> Cons y' ys
go n (Cons y ys) = Cons y (alterAt (n - 1) f ys)
go n' (Cons y ys) = Cons y (alterAt (n' - 1) f ys)

--------------------------------------------------------------------------------
-- Transformations -------------------------------------------------------------
Expand All @@ -384,7 +386,7 @@ alterAt n f xs = List (go n <$> unwrap xs)
-- |
-- | Running time: `O(n)`
reverse :: forall a. List a -> List a
reverse xs = go nil (step xs)
reverse = go nil <<< step
where
go acc Nil = acc
go acc (Cons x xs) = go (cons x acc) (step xs)
Expand All @@ -406,7 +408,7 @@ concatMap = flip bind
-- |
-- | Running time: `O(n)`
filter :: forall a. (a -> Boolean) -> List a -> List a
filter p xs = List (go <$> unwrap xs)
filter p = List <<< map go <<< unwrap
where
go Nil = Nil
go (Cons x xs)
Expand Down Expand Up @@ -436,7 +438,7 @@ filterM p list =
-- |
-- | Running time: `O(n)`
mapMaybe :: forall a b. (a -> Maybe b) -> List a -> List b
mapMaybe f xs = List (go <$> unwrap xs)
mapMaybe f = List <<< map go <<< unwrap
where
go Nil = Nil
go (Cons x xs) =
Expand Down Expand Up @@ -465,18 +467,18 @@ slice start end xs = take (end - start) (drop start xs)
-- |
-- | Running time: `O(n)` where `n` is the number of elements to take.
take :: forall a. Int -> List a -> List a
take n xs = List (go n <$> unwrap xs)
take n = List <<< map (go n) <<< unwrap
where
go :: Int -> Step a -> Step a
go i _ | i <= 0 = Nil
go _ Nil = Nil
go n (Cons x xs) = Cons x (take (n - 1) xs)
go n' (Cons x xs) = Cons x (take (n' - 1) xs)

-- | Take those elements from the front of a list which match a predicate.
-- |
-- | Running time (worst case): `O(n)`
takeWhile :: forall a. (a -> Boolean) -> List a -> List a
takeWhile p xs = List (go <$> unwrap xs)
takeWhile p = List <<< map go <<< unwrap
where
go (Cons x xs) | p x = Cons x (takeWhile p xs)
go _ = Nil
Expand All @@ -485,17 +487,17 @@ takeWhile p xs = List (go <$> unwrap xs)
-- |
-- | Running time: `O(n)` where `n` is the number of elements to drop.
drop :: forall a. Int -> List a -> List a
drop n xs = List (go n <$> unwrap xs)
drop n = List <<< map (go n) <<< unwrap
where
go 0 xs = xs
go _ Nil = Nil
go n (Cons x xs) = go (n - 1) (step xs)
go n' (Cons x xs) = go (n' - 1) (step xs)

-- | Drop those elements from the front of a list which match a predicate.
-- |
-- | Running time (worst case): `O(n)`
dropWhile :: forall a. (a -> Boolean) -> List a -> List a
dropWhile p xs = go (step xs)
dropWhile p = go <<< step
where
go (Cons x xs) | p x = go (step xs)
go xs = fromStep xs
Expand Down Expand Up @@ -537,7 +539,7 @@ group = groupBy (==)
-- |
-- | Running time: `O(n)`
groupBy :: forall a. (a -> a -> Boolean) -> List a -> List (NEL.NonEmptyList a)
groupBy eq xs = List (go <$> unwrap xs)
groupBy eq = List <<< map go <<< unwrap
where
go Nil = Nil
go (Cons x xs) =
Expand All @@ -560,7 +562,7 @@ nub = nubBy eq
-- |
-- | Running time: `O(n^2)`
nubBy :: forall a. (a -> a -> Boolean) -> List a -> List a
nubBy eq xs = List (go <$> unwrap xs)
nubBy eq = List <<< map go <<< unwrap
where
go Nil = Nil
go (Cons x xs) = Cons x (nubBy eq (filter (\y -> not (eq x y)) xs))
Expand Down Expand Up @@ -678,8 +680,8 @@ transpose xs =
case uncons h of
Nothing ->
transpose xss
Just { head: x, tail: xs } ->
(x : mapMaybe head xss) : transpose (xs : mapMaybe tail xss)
Just { head: x, tail: xs' } ->
(x : mapMaybe head xss) : transpose (xs' : mapMaybe tail xss)

--------------------------------------------------------------------------------
-- Folding ---------------------------------------------------------------------
Expand Down
24 changes: 12 additions & 12 deletions src/Data/List/Lazy/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,14 @@ instance showList :: Show a => Show (List a) where
show xs = "fromStrict (" <> go (step xs) <> ")"
where
go Nil = "Nil"
go (Cons x xs) = "(Cons " <> show x <> " " <> go (step xs) <> ")"
go (Cons x xs') = "(Cons " <> show x <> " " <> go (step xs') <> ")"

instance eqList :: Eq a => Eq (List a) where
eq xs ys = go (step xs) (step ys)
where
go Nil Nil = true
go (Cons x xs) (Cons y ys)
| x == y = go (step xs) (step ys)
go (Cons x xs') (Cons y ys')
| x == y = go (step xs') (step ys')
go _ _ = false

instance ordList :: Ord a => Ord (List a) where
Expand All @@ -74,9 +74,9 @@ instance ordList :: Ord a => Ord (List a) where
go Nil Nil = EQ
go Nil _ = LT
go _ Nil = GT
go (Cons x xs) (Cons y ys) =
go (Cons x xs') (Cons y ys') =
case compare x y of
EQ -> go (step xs) (step ys)
EQ -> go (step xs') (step ys')
other -> other

instance lazyList :: Z.Lazy (List a) where
Expand All @@ -86,7 +86,7 @@ instance semigroupList :: Semigroup (List a) where
append xs ys = List (go <$> unwrap xs)
where
go Nil = step ys
go (Cons x xs) = Cons x (xs <> ys)
go (Cons x xs') = Cons x (xs' <> ys)

instance monoidList :: Monoid (List a) where
mempty = nil
Expand All @@ -95,7 +95,7 @@ instance functorList :: Functor List where
map f xs = List (go <$> unwrap xs)
where
go Nil = Nil
go (Cons x xs) = Cons (f x) (f <$> xs)
go (Cons x xs') = Cons (f x) (f <$> xs')

instance foldableList :: Foldable List where
foldr o b xs = go (step xs)
Expand All @@ -111,24 +111,24 @@ instance foldableList :: Foldable List where
foldMap f xs = go (step xs)
where
go Nil = mempty
go (Cons x xs) = f x <> foldMap f xs
go (Cons x xs') = f x <> foldMap f xs'

instance unfoldableList :: Unfoldable List where
unfoldr f b = go (f b)
where
go Nothing = nil
go (Just (Tuple a b)) = a : Z.defer \_ -> go (f b)
go (Just (Tuple a b')) = a : Z.defer \_ -> go (f b')

instance traversableList :: Traversable List where
traverse f xs = go (step xs)
where
go Nil = pure nil
go (Cons x xs) = cons <$> f x <*> traverse f xs
go (Cons x xs') = cons <$> f x <*> traverse f xs'

sequence xs = go (step xs)
where
go Nil = pure nil
go (Cons x xs) = cons <$> x <*> sequence xs
go (Cons x xs') = cons <$> x <*> sequence xs'

instance applyList :: Apply List where
apply = ap
Expand All @@ -140,7 +140,7 @@ instance bindList :: Bind List where
bind xs f = List (go <$> unwrap xs)
where
go Nil = Nil
go (Cons x xs) = step (f x <> bind xs f)
go (Cons x xs') = step (f x <> bind xs' f)

instance monadList :: Monad List

Expand Down
10 changes: 5 additions & 5 deletions src/Data/List/Types.purs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ instance eqList :: Eq a => Eq (List a) where
where
go _ _ false = false
go Nil Nil acc = acc
go (x : xs) (y : ys) acc = go xs ys $ acc && (y == x)
go (x : xs') (y : ys') acc = go xs' ys' $ acc && (y == x)
go _ _ _ = false

instance ordList :: Ord a => Ord (List a) where
Expand All @@ -44,9 +44,9 @@ instance ordList :: Ord a => Ord (List a) where
go Nil Nil = EQ
go Nil _ = LT
go _ Nil = GT
go (x : xs) (y : ys) =
go (x : xs') (y : ys') =
case compare x y of
EQ -> go xs ys
EQ -> go xs' ys'
other -> other

instance semigroupList :: Semigroup (List a) where
Expand Down Expand Up @@ -111,8 +111,8 @@ instance extendList :: Extend List where
extend f l@(a : as) =
f l : (foldr go { val: Nil, acc: Nil } as).val
where
go a { val, acc } =
let acc' = a : acc
go a' { val, acc } =
let acc' = a' : acc
in { val: f acc' : val, acc: acc' }

newtype NonEmptyList a = NonEmptyList (NonEmpty List a)
Expand Down