summaryrefslogtreecommitdiff
path: root/libraries
diff options
context:
space:
mode:
authorKoz Ross <koz.ross@retro-freedom.nz>2021-01-25 14:57:16 +1300
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-01-27 17:45:06 -0500
commit7b0b133dcf7a58097d140c011ce8b83b4d250a88 (patch)
treeb18e898aeaf6d484d889ce652a63fb0e76caa9ee /libraries
parent0da1f19e0d4bfe57706a5f74de8301ae5e106381 (diff)
downloadhaskell-7b0b133dcf7a58097d140c011ce8b83b4d250a88.tar.gz
Implement #18519
Diffstat (limited to 'libraries')
-rw-r--r--libraries/base/Data/List/NonEmpty.hs38
1 files changed, 38 insertions, 0 deletions
diff --git a/libraries/base/Data/List/NonEmpty.hs b/libraries/base/Data/List/NonEmpty.hs
index 84481adb81..45e8c9a8ea 100644
--- a/libraries/base/Data/List/NonEmpty.hs
+++ b/libraries/base/Data/List/NonEmpty.hs
@@ -48,6 +48,9 @@ module Data.List.NonEmpty (
, reverse -- :: NonEmpty a -> NonEmpty a
, inits -- :: Foldable f => f a -> NonEmpty a
, tails -- :: Foldable f => f a -> NonEmpty a
+ , append -- :: NonEmpty a -> NonEmpty a -> NonEmpty a
+ , appendList -- :: NonEmpty a -> [a] -> NonEmpty a
+ , prependList -- :: [a] -> NonEmpty a -> NonEmpty a
-- * Building streams
, iterate -- :: (a -> a) -> a -> NonEmpty a
, repeat -- :: a -> NonEmpty a
@@ -448,3 +451,38 @@ sortBy f = lift (List.sortBy f)
-- > sortBy . comparing
sortWith :: Ord o => (a -> o) -> NonEmpty a -> NonEmpty a
sortWith = sortBy . comparing
+
+-- | A monomorphic version of '<>' for 'NonEmpty'.
+--
+-- >>> append (1 :| []) (2 :| [3])
+-- 1 :| [2,3]
+--
+-- @since 4.16
+append :: NonEmpty a -> NonEmpty a -> NonEmpty a
+append = (<>)
+
+-- | Attach a list at the end of a 'NonEmpty'.
+--
+-- >>> appendList (1 :| [2,3]) []
+-- 1 :| [2,3]
+--
+-- >>> appendList (1 :| [2,3]) [4,5]
+-- 1 :| [2,3,4,5]
+--
+-- @since 4.16
+appendList :: NonEmpty a -> [a] -> NonEmpty a
+appendList (x :| xs) ys = x :| xs <> ys
+
+-- | Attach a list at the beginning of a 'NonEmpty'.
+--
+-- >>> prependList [] (1 :| [2,3])
+-- 1 :| [2,3]
+--
+-- >>> prependList [negate 1, 0] (1 :| [2, 3])
+-- -1 :| [0,1,2,3]
+--
+-- @since 4.16
+prependList :: [a] -> NonEmpty a -> NonEmpty a
+prependList ls ne = case ls of
+ [] -> ne
+ (x : xs) -> x :| xs <> toList ne