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
11 changes: 11 additions & 0 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ module Data.List
, group
, group'
, groupBy
, partition

, nub
, nubBy
Expand Down Expand Up @@ -561,6 +562,16 @@ groupBy _ Nil = Nil
groupBy eq (x : xs) = case span (eq x) xs of
{ init: ys, rest: zs } -> NEL.NonEmptyList (x :| ys) : groupBy eq zs

-- | Returns a lists of elements which do and do not satisfy a predicate.
-- |
-- | Running time: `O(n)`
partition :: forall a. (a -> Boolean) -> List a -> { yes :: List a, no :: List a }
partition p xs = foldr select { no: Nil, yes: Nil } xs
where
select x { no, yes } = if p x
then { no, yes: x : yes }
else { no: x : no, yes }

--------------------------------------------------------------------------------
-- Set-like operations ---------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions src/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ module Data.List.Lazy
, group
-- , group'
, groupBy
, partition

, nub
, nubBy
Expand Down Expand Up @@ -558,6 +559,16 @@ groupBy eq = List <<< map go <<< unwrap
{ init: ys, rest: zs } ->
Cons (NEL.NonEmptyList (defer \_ -> x :| ys)) (groupBy eq zs)

-- | Returns a tuple of lists of elements which do
-- | and do not satisfy a predicate, respectively.
-- |
-- | Running time: `O(n)`
partition :: forall a. (a -> Boolean) -> List a -> { yes :: List a, no :: List a }
partition f = foldr go {yes: nil, no: nil}
where
go x {yes: ys, no: ns} =
if f x then {yes: x : ys, no: ns} else {yes: ys, no: x : ns}

--------------------------------------------------------------------------------
-- Set-like operations ---------------------------------------------------------
--------------------------------------------------------------------------------
Expand Down
7 changes: 6 additions & 1 deletion test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.List.NonEmpty as NEL
import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)
import Data.Foldable (foldMap, foldl)
import Data.List (List(..), (..), 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, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
import Data.List (List(..), (..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Monoid.Additive (Additive(..))
import Data.NonEmpty ((:|))
Expand Down Expand Up @@ -250,6 +250,11 @@ testList = do
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
assert $ groupBy (\x y -> odd x && odd y) (l [1, 1, 2, 2, 3, 3]) == l [NEL.NonEmptyList (1 :| l [1]), NEL.singleton 2, NEL.singleton 2, NEL.NonEmptyList (3 :| l [3])]

log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
let partitioned = partition (_ > 2) (l [1, 5, 3, 2, 4])
assert $ partitioned.yes == l [5, 3, 4]
assert $ partitioned.no == l [1, 2]

log "nub should remove duplicate elements from the list, keeping the first occurence"
assert $ nub (l [1, 2, 2, 3, 4, 1]) == l [1, 2, 3, 4]

Expand Down
7 changes: 6 additions & 1 deletion test/Test/Data/List/Lazy.purs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Control.Monad.Eff (Eff)
import Control.Monad.Eff.Console (CONSOLE, log)

import Data.Lazy as Z
import Data.List.Lazy (List, nil, cons, foldl, foldr, foldMap, singleton, transpose, take, iterate, filter, uncons, foldM, range, unzip, zip, length, zipWithA, replicate, repeat, zipWith, intersectBy, intersect, deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group, span, dropWhile, drop, takeWhile, slice, catMaybes, mapMaybe, filterM, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, init, tail, last, head, insertBy, insert, snoc, null, replicateM, fromFoldable, (:), (\\), (!!))
import Data.List.Lazy (List, nil, cons, foldl, foldr, foldMap, singleton, transpose, take, iterate, filter, uncons, foldM, range, unzip, zip, length, zipWithA, replicate, repeat, zipWith, intersectBy, intersect, deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group, partition, span, dropWhile, drop, takeWhile, slice, catMaybes, mapMaybe, filterM, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, init, tail, last, head, insertBy, insert, snoc, null, replicateM, fromFoldable, (:), (\\), (!!))
import Data.List.Lazy.NonEmpty as NEL
import Data.Maybe (Maybe(..), isNothing, fromJust)
import Data.Monoid.Additive (Additive(..))
Expand Down Expand Up @@ -270,6 +270,11 @@ testListLazy = do
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
assert $ groupBy (\x y -> odd x && odd y) (l [1, 1, 2, 2, 3, 3]) == l [nel (1 :| l [1]), NEL.singleton 2, NEL.singleton 2, nel (3 :| l [3])]

log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
let partitioned = partition (_ > 2) (l [1, 5, 3, 2, 4])
assert $ partitioned.yes == l [5, 3, 4]
assert $ partitioned.no == l [1, 2]

log "iterate on nonempty lazy list should apply supplied function correctly"
assert $ (take 3 $ NEL.toList $ NEL.iterate (_ + 1) 0) == l [0, 1, 2]

Expand Down