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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: node_js
sudo: false
node_js:
- 0.10
- 4
env:
- PATH=$HOME/purescript:$PATH
install:
Expand Down
49 changes: 20 additions & 29 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module Data.List
, unionBy
, delete
, deleteBy
, (\\)
, (\\), difference
, intersect
, intersectBy

Expand All @@ -88,20 +88,20 @@ module Data.List
, fromList
) where

import Prelude
import Prelude (class Monad, class Bind, class Applicative, class Apply, class Functor, class Semigroup, class Ord, class Eq, class Show, Ordering(EQ, GT, LT), append, flip, (<*>), (<$>), (<>), pure, (<<<), ($), compare, (==), (&&), show, (++), (>>=), return, not, eq, (-), otherwise, (/=), id, bind, (+), one, (<), (<=), negate, (>))

import Control.Alt (Alt, (<|>))
import Control.Alternative (Alternative)
import Control.Lazy (Lazy, defer)
import Control.MonadPlus (MonadPlus)
import Control.Plus (Plus)
import Control.Alt (class Alt, (<|>))
import Control.Alternative (class Alternative)
import Control.Lazy (class Lazy, defer)
import Control.MonadPlus (class MonadPlus)
import Control.Plus (class Plus)

import Data.Foldable (Foldable, foldl, foldr, any)
import Data.Foldable (class Foldable, foldl, foldr, any)
import Data.Maybe (Maybe(..))
import Data.Monoid (Monoid, mempty)
import Data.Traversable (Traversable, traverse, sequence)
import Data.Monoid (class Monoid, mempty)
import Data.Traversable (class Traversable, traverse, sequence)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (Unfoldable, unfoldr)
import Data.Unfoldable (class Unfoldable, unfoldr)

-- | A strict linked list.
-- |
Expand Down Expand Up @@ -132,11 +132,8 @@ fromFoldable = foldr Cons Nil
singleton :: forall a. a -> List a
singleton a = Cons a Nil

infix 8 ..

-- | An infix synonym for `range`.
(..) :: Int -> Int -> List Int
(..) = range
infix 8 range as ..

-- | Create a list containing a range of integers, including both endpoints.
range :: Int -> Int -> List Int
Expand Down Expand Up @@ -196,14 +193,11 @@ length = foldl (\acc _ -> acc + 1) 0
-- Extending arrays ------------------------------------------------------------
--------------------------------------------------------------------------------

infixr 6 :

-- | An infix alias for `Cons`; attaches an element to the front of
-- | a list.
-- |
-- | Running time: `O(1)`
(:) :: forall a. a -> List a -> List a
(:) = Cons
infixr 6 Cons as :

-- | Append an element to the end of an array, creating a new array.
-- |
Expand Down Expand Up @@ -285,19 +279,16 @@ index Nil _ = Nothing
index (Cons a _) 0 = Just a
index (Cons _ as) i = index as (i - 1)

infixl 8 !!

-- | An infix synonym for `index`.
(!!) :: forall a. List a -> Int -> Maybe a
(!!) = index
infixl 8 index as !!

-- | Find the index of the first element equal to the specified element.
elemIndex :: forall a. (Eq a) => a -> List a -> Maybe Int
elemIndex x = findIndex (== x)
elemIndex x = findIndex (_ == x)

-- | Find the index of the last element equal to the specified element.
elemLastIndex :: forall a. (Eq a) => a -> List a -> Maybe Int
elemLastIndex x = findLastIndex (== x)
elemLastIndex x = findLastIndex (_ == x)

-- | Find the first index for which a predicate holds.
findIndex :: forall a. (a -> Boolean) -> List a -> Maybe Int
Expand Down Expand Up @@ -377,7 +368,7 @@ reverse = go Nil
-- |
-- | Running time: `O(n)`, where `n` is the total number of elements.
concat :: forall a. List (List a) -> List a
concat = (>>= id)
concat = (_ >>= id)

-- | Apply a function to each element in a list, and flatten the results
-- | into a single, new list.
Expand Down Expand Up @@ -614,13 +605,13 @@ deleteBy _ _ Nil = Nil
deleteBy (==) x (Cons y ys) | x == y = ys
deleteBy (==) x (Cons y ys) = Cons y (deleteBy (==) x ys)

infix 5 \\
infix 5 difference as \\

-- | Delete the first occurrence of each element in the second list from the first list.
-- |
-- | Running time: `O(n^2)`
(\\) :: forall a. (Eq a) => List a -> List a -> List a
(\\) = foldl (flip delete)
difference :: forall a. (Eq a) => List a -> List a -> List a
difference = foldl (flip delete)

-- | Calculate the intersection of two lists.
-- |
Expand Down
44 changes: 18 additions & 26 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module Data.List.Lazy
, unionBy
, delete
, deleteBy
, (\\)
, (\\), difference
, intersect
, intersectBy

Expand All @@ -94,21 +94,21 @@ module Data.List.Lazy
, fromList
) where

import Prelude
import Prelude (class Monad, class Bind, class Applicative, class Apply, class Functor, class Semigroup, class Ord, class Eq, class Show, Ordering(EQ, GT, LT), append, flip, ap, (<*>), (<$>), pure, (<>), (<<<), ($), compare, (==), (++), show, otherwise, not, eq, (-), id, (>>=), (+), negate, (>))

import Control.Alt (Alt)
import Control.Alternative (Alternative)
import Control.MonadPlus (MonadPlus)
import Control.Plus (Plus)
import qualified Control.Lazy as Z
import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.MonadPlus (class MonadPlus)
import Control.Plus (class Plus)
import Control.Lazy as Z

import Data.Foldable (Foldable, foldMap, foldl, foldr, any)
import Data.Foldable (class Foldable, foldMap, foldl, foldr, any)
import Data.Lazy (Lazy(), defer, force)
import Data.Maybe (Maybe(..), isNothing)
import Data.Monoid (Monoid, mempty)
import Data.Traversable (Traversable, traverse, sequence)
import Data.Monoid (class Monoid, mempty)
import Data.Traversable (class Traversable, traverse, sequence)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (Unfoldable, unfoldr)
import Data.Unfoldable (class Unfoldable, unfoldr)


-- | A lazy linked list.
Expand Down Expand Up @@ -159,8 +159,7 @@ singleton :: forall a. a -> List a
singleton a = cons a nil

-- | An infix synonym for `range`.
(..) :: Int -> Int -> List Int
(..) = range
infix 8 range as ..

-- | Create a list containing a range of integers, including both endpoints.
range :: Int -> Int -> List Int
Expand Down Expand Up @@ -211,14 +210,11 @@ length xs = go (step xs)
cons :: forall a. a -> List a -> List a
cons x xs = List $ defer \_ -> Cons x xs

infixr 6 :

-- | An infix alias for `cons`; attaches an element to the front of
-- | a list.
-- |
-- | Running time: `O(1)`
(:) :: forall a. a -> List a -> List a
(:) = cons
infixr 6 cons as :

-- | Insert an element into a sorted list.
-- |
Expand Down Expand Up @@ -299,11 +295,8 @@ index xs = go (step xs)
go (Cons a _) 0 = Just a
go (Cons _ as) i = go (step as) (i - 1)

infixl 8 !!

-- | An infix synonym for `index`.
(!!) :: forall a. List a -> Int -> Maybe a
(!!) = index
infixl 8 index as !!

-- | Insert an element into a list at the specified index, returning a new
-- | list or `Nothing` if the index is out-of-bounds.
Expand Down Expand Up @@ -392,7 +385,7 @@ reverse xs = go nil (step xs)
-- |
-- | Running time: `O(n)`, where `n` is the total number of elements.
concat :: forall a. List (List a) -> List a
concat = (>>= id)
concat = (_ >>= id)

-- | Apply a function to each element in a list, and flatten the results
-- | into a single, new list.
Expand Down Expand Up @@ -574,13 +567,12 @@ deleteBy eq x xs = List (go <$> runList xs)
go (Cons y ys) | eq x y = step ys
| otherwise = Cons y (deleteBy eq x ys)

infix 5 \\

-- | Delete the first occurrence of each element in the second list from the first list.
-- |
-- | Running time: `O(n^2)`
(\\) :: forall a. (Eq a) => List a -> List a -> List a
(\\) = foldl (flip delete)
difference :: forall a. (Eq a) => List a -> List a -> List a
difference = foldl (flip delete)
infix 5 difference as \\

-- | Calculate the intersection of two lists.
-- |
Expand Down
1 change: 0 additions & 1 deletion src/Data/List/Unsafe.purs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module Data.List.Unsafe
, init
) where

import Prelude
import Data.List (List(..))

-- | Get the first element of a non-empty list.
Expand Down
14 changes: 7 additions & 7 deletions src/Data/List/ZipList.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ module Data.List.ZipList
, runZipList
) where

import Prelude
import Prelude (class Applicative, class Apply, class Functor, class Semigroup, class Ord, class Eq, class Show, append, (<<<), ($), map, (<$>), (++), compare, eq, show)

import Control.Alt (Alt)
import Control.Alternative (Alternative)
import Control.Plus (Plus)
import Control.Alt (class Alt)
import Control.Alternative (class Alternative)
import Control.Plus (class Plus)

import Data.Foldable (Foldable, foldMap, foldl, foldr)
import Data.Foldable (class Foldable, foldMap, foldl, foldr)
import Data.List.Lazy (List(), repeat, zipWith)
import Data.Monoid (Monoid, mempty)
import Data.Traversable (Traversable, traverse, sequence)
import Data.Monoid (class Monoid, mempty)
import Data.Traversable (class Traversable, traverse, sequence)

-- | `ZipList` is a newtype around `List` which provides a zippy
-- | `Applicative` instance.
Expand Down
42 changes: 21 additions & 21 deletions test/Test/Data/List.purs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module Test.Data.List (testList) where

import Prelude
import Prelude (Unit, (*), zero, (/=), mod, (+), (==), ($), bind, pure, void, show, (<), (&&), compare, flip, const, (<<<), map, negate)
import Control.Monad.Eff (Eff())
import Control.Monad.Eff.Console (CONSOLE(), log)
import Data.Foldable
import Data.Monoid.Additive
import Data.List
import Data.Foldable (foldMap, foldl)
import Data.Monoid.Additive (Additive(Additive))
import Data.List (List(Nil, Cons), (..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, null, replicateM, replicate, singleton, fromFoldable, transpose, (:))
import Data.Maybe (Maybe(..), isNothing)
import Data.Maybe.Unsafe (fromJust)
import Data.Tuple (Tuple(..))
Expand Down Expand Up @@ -120,12 +120,12 @@ testList = do
assert $ elemLastIndex 4 (l [1, 2, 1]) == Nothing

log "findIndex should return the index of an item that a predicate returns true for in an list"
assert $ findIndex (/= 1) (l [1, 2, 1]) == Just 1
assert $ findIndex (== 3) (l [1, 2, 1]) == Nothing
assert $ findIndex (_ /= 1) (l [1, 2, 1]) == Just 1
assert $ findIndex (_ == 3) (l [1, 2, 1]) == Nothing

log "findLastIndex should return the last index of an item in an list"
assert $ findLastIndex (/= 1) (l [2, 1, 2]) == Just 2
assert $ findLastIndex (== 3) (l [2, 1, 2]) == Nothing
assert $ findLastIndex (_ /= 1) (l [2, 1, 2]) == Just 2
assert $ findLastIndex (_ == 3) (l [2, 1, 2]) == Nothing

log "insertAt should add an item at the specified index"
assert $ (insertAt 0 1 (l [2, 3])) == Just (l [1, 2, 3])
Expand All @@ -150,22 +150,22 @@ testList = do
assert $ (updateAt 1 9 nil) == Nothing

log "modifyAt should update an item at the specified index"
assert $ (modifyAt 0 (+ 1) (l [1, 2, 3])) == Just (l [2, 2, 3])
assert $ (modifyAt 1 (+ 1) (l [1, 2, 3])) == Just (l [1, 3, 3])
assert $ (modifyAt 0 (_ + 1) (l [1, 2, 3])) == Just (l [2, 2, 3])
assert $ (modifyAt 1 (_ + 1) (l [1, 2, 3])) == Just (l [1, 3, 3])

log "modifyAt should return Nothing if the index is out of range"
assert $ (modifyAt 1 (+ 1) nil) == Nothing
assert $ (modifyAt 1 (_ + 1) nil) == Nothing

log "alterAt should update an item at the specified index when the function returns Just"
assert $ (alterAt 0 (Just <<< (+ 1)) (l [1, 2, 3])) == Just (l [2, 2, 3])
assert $ (alterAt 1 (Just <<< (+ 1)) (l [1, 2, 3])) == Just (l [1, 3, 3])
assert $ (alterAt 0 (Just <<< (_ + 1)) (l [1, 2, 3])) == Just (l [2, 2, 3])
assert $ (alterAt 1 (Just <<< (_ + 1)) (l [1, 2, 3])) == Just (l [1, 3, 3])

log "alterAt should drop an item at the specified index when the function returns Nothing"
assert $ (alterAt 0 (const Nothing) (l [1, 2, 3])) == Just (l [2, 3])
assert $ (alterAt 1 (const Nothing) (l [1, 2, 3])) == Just (l [1, 3])

log "alterAt should return Nothing if the index is out of range"
assert $ (alterAt 1 (Just <<< (+ 1)) nil) == Nothing
assert $ (alterAt 1 (Just <<< (_ + 1)) nil) == Nothing

log "reverse should reverse the order of items in an list"
assert $ (reverse (l [1, 2, 3])) == l [3, 2, 1]
Expand Down Expand Up @@ -204,22 +204,22 @@ testList = do
assert $ (take 1 nil) == nil

log "takeWhile should keep all values that match a predicate from the front of an list"
assert $ (takeWhile (/= 2) (l [1, 2, 3])) == l [1]
assert $ (takeWhile (/= 3) (l [1, 2, 3])) == l [1, 2]
assert $ (takeWhile (/= 1) nil) == nil
assert $ (takeWhile (_ /= 2) (l [1, 2, 3])) == l [1]
assert $ (takeWhile (_ /= 3) (l [1, 2, 3])) == l [1, 2]
assert $ (takeWhile (_ /= 1) nil) == nil

log "drop should remove the specified number of items from the front of an list"
assert $ (drop 1 (l [1, 2, 3])) == l [2, 3]
assert $ (drop 2 (l [1, 2, 3])) == l [3]
assert $ (drop 1 nil) == nil

log "dropWhile should remove all values that match a predicate from the front of an list"
assert $ (dropWhile (/= 1) (l [1, 2, 3])) == l [1, 2, 3]
assert $ (dropWhile (/= 2) (l [1, 2, 3])) == l [2, 3]
assert $ (dropWhile (/= 1) nil) == nil
assert $ (dropWhile (_ /= 1) (l [1, 2, 3])) == l [1, 2, 3]
assert $ (dropWhile (_ /= 2) (l [1, 2, 3])) == l [2, 3]
assert $ (dropWhile (_ /= 1) nil) == nil

log "span should split an list in two based on a predicate"
let spanResult = span (< 4) (l [1, 2, 3, 4, 5, 6, 7])
let spanResult = span (_ < 4) (l [1, 2, 3, 4, 5, 6, 7])
assert $ spanResult.init == l [1, 2, 3]
assert $ spanResult.rest == l [4, 5, 6, 7]

Expand Down
Loading