diff options
author | Alexander Abushkevich <alex@abushkevi.ch> | 2016-02-23 00:49:17 +1300 |
---|---|---|
committer | Alexander Abushkevich <alex@abushkevi.ch> | 2016-02-23 01:30:31 +1300 |
commit | 746e16d83aaf284f996bcc6d61f0d9ba99c099b2 (patch) | |
tree | 8312b8bdcce9d7ef468abb6e86d0dc3a4d97b5e4 /lib/ur/list.ur | |
parent | 5705d3b510277f033b8a0317b3fe520b0e5cc60a (diff) |
Find longest prefix of elements, which satisfy a predicate; Group a list
Diffstat (limited to 'lib/ur/list.ur')
-rw-r--r-- | lib/ur/list.ur | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/ur/list.ur b/lib/ur/list.ur index 11895884..f3bb0587 100644 --- a/lib/ur/list.ur +++ b/lib/ur/list.ur @@ -434,6 +434,31 @@ fun drop [a] (n : int) (xs : list a) : list a = fun splitAt [a] (n : int) (xs : list a) : list a * list a = (take n xs, drop n xs) + +fun span [a] (f:(a -> bool)) (ls:list a) : list a * list a = + let + fun span' f acc ls = + case ls of + [] => (acc, []) + | x :: xs => if (f x) then span' f (x :: acc) xs else (acc, ls) + in + span' f [] ls + end + +fun groupBy [a] (f:(a -> a -> bool)) (ls:list a) : list (list a) = + let + fun groupBy' f ls = + case ls of + [] => [] :: [] + | x :: xs => + let + val (ys, zs) = span (f x) xs + in + (x :: ys) :: (groupBy' f zs) + end + in + groupBy' f ls + end fun mapXiM [m ::: Type -> Type] (_ : monad m) [a] [ctx ::: {Unit}] (f : int -> a -> m (xml ctx [] [])) : t a -> m (xml ctx [] []) = let |