Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deduplicate testing code - with typeclasses #199

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 18 additions & 1 deletion src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ module Data.List.Lazy
, stripPrefix
, slice
, take
, takeEnd
, takeWhile
, drop
, dropWhile
, span
, group
-- , group'
, groupAll
, groupBy
, partition

Expand Down Expand Up @@ -115,6 +117,7 @@ import Data.Traversable (scanl, scanr) as Exports
import Data.Traversable (sequence)
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)
import Partial.Unsafe (unsafeCrashWith)

-- | Convert a list into any unfoldable structure.
-- |
Expand Down Expand Up @@ -506,6 +509,12 @@ take n = if n <= 0
go _ Nil = Nil
go n' (Cons x xs) = Cons x (take (n' - 1) xs)

-- | Take the specified number of elements from the end of a list.
-- |
-- | Running time: Todo
takeEnd :: forall a. Int -> List a -> List a
takeEnd _ _ = unsafeCrashWith "todo takeEnd for Lazy List"

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

Expand Down Expand Up @@ -566,6 +575,14 @@ span p xs =
group :: forall a. Eq a => List a -> List (NEL.NonEmptyList a)
group = groupBy (==)

-- | Group equal elements of a list into lists.
-- |
-- | Todo - fix documentation mismatch of above `group` with non-lazy version.
-- | ```
groupAll :: forall a. Ord a => List a -> List (NEL.NonEmptyList a)
groupAll = unsafeCrashWith "todo groupAll for Lazy List"
--groupAll = group <<< sort

-- | Group equal, consecutive elements of a list into lists, using the specified
-- | equivalence relation to determine equality.
-- |
Expand Down
169 changes: 169 additions & 0 deletions src/Data/List/Lazy/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,49 @@ module Data.List.Lazy.NonEmpty
, init
, uncons
, length
, concat
, concatMap
, appendFoldable
-- additions
, catMaybes
, cons
, drop
, dropWhile
, elemIndex
, elemLastIndex
, filter
, filterM
, findIndex
, findLastIndex
, foldM
, group
, groupAll
, groupBy
, index
, insertAt
, intersect
, intersectBy
, mapMaybe
, modifyAt
, nubEq
, nubByEq
, partition
, range
, reverse
, snoc
, span
, take
, takeEnd
, takeWhile
, union
, unionBy
, unzip
, updateAt
, zip
, zipWith
, zipWithA


) where

import Prelude
Expand All @@ -28,6 +69,128 @@ import Data.Maybe (Maybe(..), maybe, fromMaybe)
import Data.NonEmpty ((:|))
import Data.Tuple (Tuple(..))
import Data.Unfoldable (class Unfoldable, unfoldr)
import Partial.Unsafe (unsafeCrashWith)

--- Sorted additions ------

-- | Filter a list of optional values, keeping only the elements which contain
-- | a value.
catMaybes :: forall a. NonEmptyList (Maybe a) -> L.List a
catMaybes _ = unsafeCrashWith "todo catMaybes for Lazy NonEmptyList"
--catMaybes = mapMaybe identity

cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
cons _ _ = unsafeCrashWith "todo cons for Lazy NonEmptyList"

-- | Drop the specified number of elements from the front of a list.
drop :: forall a. Int -> NonEmptyList a -> L.List a
drop _ _ = unsafeCrashWith "todo drop for Lazy NonEmptyList"

dropWhile :: forall a. (a -> Boolean) -> NonEmptyList a -> L.List a
dropWhile _ _ = unsafeCrashWith "todo dropWhile for Lazy NonEmptyList"

elemIndex :: forall a. Eq a => a -> NonEmptyList a -> Maybe Int
elemIndex _ _ = unsafeCrashWith "todo elemIndex for Lazy NonEmptyList"

elemLastIndex :: forall a. Eq a => a -> NonEmptyList a -> Maybe Int
elemLastIndex _ _ = unsafeCrashWith "todo elemLastIndex for Lazy NonEmptyList"

filter :: forall a. (a -> Boolean) -> NonEmptyList a -> L.List a
filter _ _ = unsafeCrashWith "todo filter for Lazy NonEmptyList"

filterM :: forall m a. Monad m => (a -> m Boolean) -> NonEmptyList a -> m (L.List a)
filterM _ _ = unsafeCrashWith "todo filterM for Lazy NonEmptyList"

findIndex :: forall a. (a -> Boolean) -> NonEmptyList a -> Maybe Int
findIndex _ _ = unsafeCrashWith "todo findIndex for Lazy NonEmptyList"

findLastIndex :: forall a. (a -> Boolean) -> NonEmptyList a -> Maybe Int
findLastIndex _ _ = unsafeCrashWith "todo findLastIndex for Lazy NonEmptyList"

foldM :: forall m a b. Monad m => (b -> a -> m b) -> b -> NonEmptyList a -> m b
foldM _ _ _ = unsafeCrashWith "todo foldM for Lazy NonEmptyList"

group :: forall a. Eq a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
group _ = unsafeCrashWith "todo group for Lazy NonEmptyList"

groupAll :: forall a. Ord a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
groupAll _ = unsafeCrashWith "todo groupAll for Lazy NonEmptyList"

groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
groupBy _ _ = unsafeCrashWith "todo groupBy for Lazy NonEmptyList"

index :: forall a. NonEmptyList a -> Int -> Maybe a
index _ _ = unsafeCrashWith "todo index for Lazy NonEmptyList"

insertAt :: forall a. Int -> a -> NonEmptyList a -> NonEmptyList a
insertAt _ _ _ = unsafeCrashWith "todo insertAt for Lazy NonEmptyList"

intersect :: forall a. Eq a => NonEmptyList a -> NonEmptyList a -> NonEmptyList a
intersect _ _ = unsafeCrashWith "todo intersect for Lazy NonEmptyList"

intersectBy :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList a -> NonEmptyList a
intersectBy _ _ _ = unsafeCrashWith "todo intersectBy for Lazy NonEmptyList"

mapMaybe :: forall a b. (a -> Maybe b) -> NonEmptyList a -> L.List b
mapMaybe _ _ = unsafeCrashWith "todo mapMaybe for Lazy NonEmptyList"

modifyAt :: forall a. Int -> (a -> a) -> NonEmptyList a -> NonEmptyList a
modifyAt _ _ _ = unsafeCrashWith "todo modifyAt for Lazy NonEmptyList"

nubEq :: forall a. Eq a => NonEmptyList a -> NonEmptyList a
nubEq _ = unsafeCrashWith "todo nubEq for Lazy NonEmptyList"

nubByEq :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList a
nubByEq _ _ = unsafeCrashWith "todo nubByEq for Lazy NonEmptyList"

partition :: forall a. (a -> Boolean) -> NonEmptyList a -> { yes :: L.List a, no :: L.List a }
partition _ _ = unsafeCrashWith "todo partition for Lazy NonEmptyList"
range :: Int -> Int -> NonEmptyList Int
range _ _ = unsafeCrashWith "todo range for Lazy NonEmptyList"

reverse :: forall a. NonEmptyList a -> NonEmptyList a
reverse _ = unsafeCrashWith "todo reverse for Lazy NonEmptyList"

snoc :: forall a. NonEmptyList a -> a -> NonEmptyList a
snoc _ _ = unsafeCrashWith "todo snoc for Lazy NonEmptyList"

snoc' :: forall a. L.List a -> a -> NonEmptyList a
snoc' _ _ = unsafeCrashWith "todo snoc' for Lazy NonEmptyList"

span :: forall a. (a -> Boolean) -> NonEmptyList a -> { init :: L.List a, rest :: L.List a }
span _ _ = unsafeCrashWith "todo span for Lazy NonEmptyList"

take :: forall a. Int -> NonEmptyList a -> L.List a
take _ _ = unsafeCrashWith "todo take for Lazy NonEmptyList"

takeEnd :: forall a. Int -> NonEmptyList a -> L.List a
takeEnd _ _ = unsafeCrashWith "todo takeEnd for Lazy NonEmptyList"

takeWhile :: forall a. (a -> Boolean) -> NonEmptyList a -> L.List a
takeWhile _ _ = unsafeCrashWith "todo takeWhile for Lazy NonEmptyList"

union :: forall a. Eq a => NonEmptyList a -> NonEmptyList a -> NonEmptyList a
union _ _ = unsafeCrashWith "todo union for Lazy NonEmptyList"

unionBy :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList a -> NonEmptyList a
unionBy _ _ _ = unsafeCrashWith "todo unionBy for Lazy NonEmptyList"

unzip :: forall a b. NonEmptyList (Tuple a b) -> Tuple (NonEmptyList a) (NonEmptyList b)
unzip _ = unsafeCrashWith "todo unzip for Lazy NonEmptyList"

updateAt :: forall a. Int -> a -> NonEmptyList a -> NonEmptyList a
updateAt _ _ _ = unsafeCrashWith "todo updateAt for Lazy NonEmptyList"

zip :: forall a b. NonEmptyList a -> NonEmptyList b -> NonEmptyList (Tuple a b)
zip _ _ = unsafeCrashWith "todo zip for Lazy NonEmptyList"

zipWith :: forall a b c. (a -> b -> c) -> NonEmptyList a -> NonEmptyList b -> NonEmptyList c
zipWith _ _ _ = unsafeCrashWith "todo zipWith for Lazy NonEmptyList"

zipWithA :: forall m a b c. Applicative m => (a -> b -> m c) -> NonEmptyList a -> NonEmptyList b -> m (NonEmptyList c)
zipWithA _ _ _ = unsafeCrashWith "todo zipWithA for Lazy NonEmptyList"

-----------

toUnfoldable :: forall f. Unfoldable f => NonEmptyList ~> f
toUnfoldable =
Expand Down Expand Up @@ -75,6 +238,12 @@ uncons (NonEmptyList nel) = case force nel of x :| xs -> { head: x, tail: xs }
length :: forall a. NonEmptyList a -> Int
length (NonEmptyList nel) = case force nel of _ :| xs -> 1 + L.length xs

-- | Flatten a list of lists.
-- |
-- | Running time: `O(n)`, where `n` is the total number of elements.
concat :: forall a. NonEmptyList (NonEmptyList a) -> NonEmptyList a
concat = (_ >>= identity)

concatMap :: forall a b. (a -> NonEmptyList b) -> NonEmptyList a -> NonEmptyList b
concatMap = flip bind

Expand Down
17 changes: 15 additions & 2 deletions src/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Data.List.NonEmpty
, fromList
, toList
, singleton
, (..), range
, length
, cons
, cons'
Expand Down Expand Up @@ -36,6 +37,7 @@ module Data.List.NonEmpty
, sort
, sortBy
, take
, takeEnd
, takeWhile
, drop
, dropWhile
Expand Down Expand Up @@ -69,13 +71,13 @@ import Data.FunctorWithIndex (mapWithIndex) as FWI
import Data.List ((:))
import Data.List as L
import Data.List.Types (NonEmptyList(..))
import Data.Maybe (Maybe(..), fromMaybe, maybe)
import Data.Maybe (Maybe(..), fromJust, fromMaybe, maybe)
import Data.NonEmpty ((:|))
import Data.NonEmpty as NE
import Data.Semigroup.Traversable (sequence1)
import Data.Tuple (Tuple(..), fst, snd)
import Data.Unfoldable (class Unfoldable, unfoldr)
import Partial.Unsafe (unsafeCrashWith)
import Partial.Unsafe (unsafeCrashWith, unsafePartial)

import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
import Data.Semigroup.Foldable (fold1, foldMap1, for1_, sequence1_, traverse1_) as Exports
Expand Down Expand Up @@ -133,6 +135,14 @@ toList (NonEmptyList (x :| xs)) = x : xs
singleton :: forall a. a -> NonEmptyList a
singleton = NonEmptyList <<< NE.singleton

-- | An infix synonym for `range`.
infix 8 range as ..

-- | Create a list containing a range of integers, including both endpoints.
-- Todo, rewrite this without unsafe workaround (if necessary)
range :: Int -> Int -> NonEmptyList Int
range start end = unsafePartial fromJust $ fromList $ L.range start end

cons :: forall a. a -> NonEmptyList a -> NonEmptyList a
cons y (NonEmptyList (x :| xs)) = NonEmptyList (y :| x : xs)

Expand Down Expand Up @@ -250,6 +260,9 @@ sortBy = wrappedOperation "sortBy" <<< L.sortBy
take :: forall a. Int -> NonEmptyList a -> L.List a
take = lift <<< L.take

takeEnd :: forall a. Int -> NonEmptyList a -> L.List a
takeEnd = lift <<< L.takeEnd

takeWhile :: forall a. (a -> Boolean) -> NonEmptyList a -> L.List a
takeWhile = lift <<< L.takeWhile

Expand Down
Loading