Skip to content

Commit 308cc13

Browse files
committed
Added group, groupBy and span.
1 parent 9393581 commit 308cc13

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343

4444
findLastIndex :: forall a. (a -> Prim.Boolean) -> [a] -> Prim.Number
4545

46+
group :: forall a. (Eq a) => [a] -> [[a]]
47+
48+
groupBy :: forall a. (a -> a -> Prim.Boolean) -> [a] -> [[a]]
49+
4650
head :: forall a. [a] -> Maybe a
4751

4852
init :: forall a. [a] -> Maybe [a]
@@ -75,6 +79,8 @@
7579

7680
sortBy :: forall a. (a -> a -> Ordering) -> [a] -> [a]
7781

82+
span :: forall a. (a -> Prim.Boolean) -> [a] -> { rest :: [a], init :: [a] }
83+
7884
tail :: forall a. [a] -> Maybe [a]
7985

8086
take :: forall a. Prim.Number -> [a] -> [a]

src/Data/Array.purs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ module Data.Array
3030
, nubBy
3131
, sort
3232
, sortBy
33+
, group
34+
, groupBy
35+
, span
3336
) where
3437

3538
import Data.Maybe
@@ -283,6 +286,19 @@ foreign import sortJS
283286
\ };\
284287
\}" :: forall a. (a -> a -> Number) -> [a] -> [a]
285288

289+
group :: forall a. (Eq a) => [a] -> [[a]]
290+
group xs = groupBy (==) xs
291+
292+
groupBy :: forall a. (a -> a -> Boolean) -> [a] -> [[a]]
293+
groupBy _ [] = []
294+
groupBy eq (x:xs) = case span (eq x) xs of
295+
{init = ys, rest = zs} -> (x:ys) : groupBy eq zs
296+
297+
span :: forall a. (a -> Boolean) -> [a] -> { init :: [a], rest :: [a] }
298+
span p (x:xs') | p x = case span p xs' of
299+
{init = ys, rest = zs} -> {init: (x:ys), rest: zs}
300+
span _ xs = {init: [], rest: xs}
301+
286302
instance functorArray :: Functor [] where
287303
(<$>) = map
288304

0 commit comments

Comments
 (0)