Skip to content
Closed
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

### Type Class Instances

instance altList :: Alt List

instance alternativeList :: Alternative List

instance applicativeList :: Applicative List
Expand All @@ -27,10 +29,14 @@

instance monadList :: Monad List

instance monadPlusList :: MonadPlus List

instance monoidList :: Monoid (List a)

instance ordList :: (Ord a) => Ord (List a)

instance plusList :: Plus List

instance semigroupList :: Semigroup (List a)

instance showList :: (Show a) => Show (List a)
Expand Down Expand Up @@ -76,6 +82,10 @@

insertBy :: forall a. (a -> a -> Ordering) -> a -> List a -> List a

intersect :: forall a. (Eq a) => List a -> List a -> List a

intersectBy :: forall a. (a -> a -> Boolean) -> List a -> List a -> List a

last :: forall a. List a -> Maybe a

length :: forall a. List a -> Number
Expand All @@ -98,6 +108,10 @@

toArray :: forall a. List a -> [a]

union :: forall a. (Eq a) => List a -> List a -> List a

unionBy :: forall a. (a -> a -> Boolean) -> List a -> List a -> List a

zipWith :: forall a b c. (a -> b -> c) -> List a -> List b -> List c


Expand Down
5 changes: 3 additions & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
"package.json"
],
"dependencies": {
"purescript-arrays" : "*",
"purescript-maybe" : "*",
"purescript-arrays" : "~0.2.0",
"purescript-control" : "~0.2.0",
"purescript-maybe" : "~0.2.0",
"purescript-foldable-traversable" : "*",
"purescript-unfoldable" : "*"
},
Expand Down
35 changes: 23 additions & 12 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ import Data.Foldable
import Data.Unfoldable
import Data.Traversable

import Control.Alt
import Control.Alternative
import Control.MonadPlus
import Control.Plus

data List a = Nil | Cons a (List a)

instance showList :: (Show a) => Show (List a) where
Expand All @@ -57,7 +62,7 @@ instance ordList :: (Ord a) => Ord (List a) where
compare Nil Nil = EQ
compare Nil _ = LT
compare _ Nil = GT
compare (Cons x xs) (Cons y ys) =
compare (Cons x xs) (Cons y ys) =
case compare x y of
EQ -> compare xs ys
other -> other
Expand All @@ -82,9 +87,9 @@ instance foldableList :: Foldable List where
foldl _ b Nil = b
foldl o b (Cons a as) = foldl o (b `o` a) as

-- foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
-- foldMap :: forall a m. (Monoid m) => (a -> m) -> f a -> m
foldMap _ Nil = mempty
foldMap f (Cons x xs) = f x <> foldMap f xs
foldMap f (Cons x xs) = f x <> foldMap f xs

instance unfoldableList :: Unfoldable List where
-- unfoldr :: forall a b. (b -> Maybe (Tuple a b)) -> b -> List a
Expand All @@ -98,7 +103,7 @@ instance traversableList :: Traversable List where
traverse _ Nil = pure Nil
traverse f (Cons a as) = Cons <$> f a <*> traverse f as

-- sequence :: forall a m. (Applicative m) => t (m a) -> m (t a)
-- sequence :: forall a m. (Applicative m) => t (m a) -> m (t a)
sequence Nil = pure Nil
sequence (Cons a as) = Cons <$> a <*> sequence as

Expand All @@ -109,15 +114,21 @@ instance applyList :: Apply List where
instance applicativeList :: Applicative List where
pure a = Cons a Nil

instance altList :: Alt List where
(<|>) = (<>)

instance plusList :: Plus List where
empty = Nil

instance alternativeList :: Alternative List

instance bindList :: Bind List where
(>>=) Nil _ = Nil
(>>=) (Cons a as) f = f a <> (as >>= f)

instance monadList :: Monad List

instance alternativeList :: Alternative List where
empty = Nil
(<|>) = (<>)
instance monadPlusList :: MonadPlus List

fromArray :: forall a. [a] -> List a
fromArray = foldr Cons Nil
Expand Down Expand Up @@ -193,7 +204,7 @@ null Nil = true
null _ = false

span :: forall a. (a -> Boolean) -> List a -> Tuple (List a) (List a)
span p (Cons x xs) | p x =
span p (Cons x xs) | p x =
case span p xs of
Tuple ys zs -> Tuple (Cons x ys) zs
span _ xs = Tuple Nil xs
Expand All @@ -203,7 +214,7 @@ group = groupBy (==)

groupBy :: forall a. (a -> a -> Boolean) -> List a -> List (List a)
groupBy _ Nil = Nil
groupBy eq (Cons x xs) =
groupBy eq (Cons x xs) =
case span (eq x) xs of
Tuple ys zs -> Cons (Cons x ys) (groupBy eq zs)

Expand All @@ -221,7 +232,7 @@ insertBy cmp x ys@(Cons y ys') =
case cmp x y of
GT -> Cons y (insertBy cmp x ys')
_ -> Cons x ys

insertAt :: forall a. Number -> a -> List a -> Maybe (List a)
insertAt 0 x xs = Just (Cons x xs)
insertAt n x (Cons y ys) = Cons y <$> insertAt (n - 1) x ys
Expand All @@ -232,9 +243,9 @@ delete = deleteBy (==)

deleteBy :: forall a. (a -> a -> Boolean) -> a -> List a -> List a
deleteBy _ _ Nil = Nil
deleteBy (==) x (Cons y ys) | x == y = ys
deleteBy (==) x (Cons y ys) | x == y = ys
deleteBy (==) x (Cons y ys) = Cons y (deleteBy (==) x ys)

deleteAt :: forall a. Number -> List a -> Maybe (List a)
deleteAt 0 (Cons y ys) = Just ys
deleteAt n (Cons y ys) = Cons y <$> deleteAt (n - 1) ys
Expand Down