summaryrefslogtreecommitdiff
path: root/libraries/base/Data/List
diff options
context:
space:
mode:
authorMoritz Bruder <muesli4@gmail.com>2020-03-12 19:45:16 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-04-03 06:26:11 -0400
commita214d2142c1bafa71fe52cb3823351ff9322d336 (patch)
tree30530c97b339d8e9d0a3ce18b0f5e7cca7cc8c3b /libraries/base/Data/List
parent9462452a4843a2c42fe055a0a7e274d5164d1dc0 (diff)
downloadhaskell-a214d2142c1bafa71fe52cb3823351ff9322d336.tar.gz
Add singleton to NonEmpty in libraries/base
This adds a definition to construct a singleton non-empty list (Data.List.NonEmpty) according to issue #17851.
Diffstat (limited to 'libraries/base/Data/List')
-rw-r--r--libraries/base/Data/List/NonEmpty.hs7
1 files changed, 7 insertions, 0 deletions
diff --git a/libraries/base/Data/List/NonEmpty.hs b/libraries/base/Data/List/NonEmpty.hs
index 61c1f3d414..1b80469003 100644
--- a/libraries/base/Data/List/NonEmpty.hs
+++ b/libraries/base/Data/List/NonEmpty.hs
@@ -42,6 +42,7 @@ module Data.List.NonEmpty (
, tail -- :: NonEmpty a -> [a]
, last -- :: NonEmpty a -> a
, init -- :: NonEmpty a -> [a]
+ , singleton -- :: a -> NonEmpty a
, (<|), cons -- :: a -> NonEmpty a -> NonEmpty a
, uncons -- :: NonEmpty a -> (a, Maybe (NonEmpty a))
, unfoldr -- :: (a -> (b, Maybe a)) -> a -> NonEmpty b
@@ -168,6 +169,12 @@ last ~(a :| as) = List.last (a : as)
init :: NonEmpty a -> [a]
init ~(a :| as) = List.init (a : as)
+-- | Construct a 'NonEmpty' list from a single element.
+--
+-- @since 4.15
+singleton :: a -> NonEmpty a
+singleton a = a :| []
+
-- | Prepend an element to the stream.
(<|) :: a -> NonEmpty a -> NonEmpty a
a <| ~(b :| bs) = a :| b : bs