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 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
19 changes: 19 additions & 0 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ module Data.List
, foldM

, module Exports

-- additions
, appendFoldable

, cons'
, snoc'

) where

import Prelude
Expand All @@ -117,8 +124,20 @@ 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)
import Prim.TypeError (class Warn, Text)


---------- Additions

appendFoldable :: forall t a. Foldable t => List a -> t a -> List a
appendFoldable _ _ = unsafeCrashWith "todo appendFoldable for Basic List"

cons' :: forall a. a -> NEL.NonEmptyList a -> List a
cons' _ _ = unsafeCrashWith "todo cons' for Basic List"
snoc' :: forall a. NEL.NonEmptyList a -> a -> List a
snoc' _ _ = unsafeCrashWith "todo snoc' for Basic List"

-- | Convert a list into any unfoldable structure.
-- |
-- | Running time: `O(n)`
Expand Down
55 changes: 54 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 @@ -94,6 +96,19 @@ module Data.List.Lazy
, scanlLazy

, module Exports

-- additions
, appendFoldable
, someRec
, sort
, sortBy

, cons'
, dropEnd
, groupAllBy
, snoc'
, manyRec

) where

import Prelude
Expand All @@ -102,6 +117,7 @@ import Control.Alt ((<|>))
import Control.Alternative (class Alternative)
import Control.Lazy as Z
import Control.Monad.Rec.Class as Rec
import Control.Monad.Rec.Class (class MonadRec)
import Data.Foldable (class Foldable, foldr, any, foldl)
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
import Data.Lazy (defer)
Expand All @@ -115,6 +131,29 @@ 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)

-- Additions
appendFoldable :: forall t a. Foldable t => List a -> t a -> List a
appendFoldable _ _ = unsafeCrashWith "todo appendFoldable for Lazy List"
someRec :: forall f a. MonadRec f => Alternative f => f a -> f (List a)
someRec _ = unsafeCrashWith "todo someRec for Lazy List"
sort :: forall a. Ord a => List a -> List a
sort _ = unsafeCrashWith "todo sort for Lazy List"
sortBy :: forall a. (a -> a -> Ordering) -> List a -> List a
sortBy _ _ = unsafeCrashWith "todo sortBy for Lazy List"

cons' :: forall a. a -> NEL.NonEmptyList a -> List a
cons' _ _ = unsafeCrashWith "todo cons' for Lazy List"
dropEnd :: forall a. Int -> List a -> List a
dropEnd _ _ = unsafeCrashWith "todo dropEnd for Lazy List"
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> List a -> List (NEL.NonEmptyList a)
groupAllBy _ _ = unsafeCrashWith "todo groupAllBy for Lazy List"
snoc' :: forall a. NEL.NonEmptyList a -> a -> List a
snoc' _ _ = unsafeCrashWith "todo snoc' for Lazy List"

manyRec :: forall f a. MonadRec f => Alternative f => f a -> f (List a)
manyRec _ = unsafeCrashWith "todo manyRec for Lazy List"

-- | Convert a list into any unfoldable structure.
-- |
Expand Down Expand Up @@ -506,6 +545,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 +566,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 +611,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
Loading