diff options
author | Moritz Bruder <muesli4@gmail.com> | 2020-03-12 19:45:16 +0100 |
---|---|---|
committer | Marge Bot <ben+marge-bot@smart-cactus.org> | 2020-04-03 06:26:11 -0400 |
commit | a214d2142c1bafa71fe52cb3823351ff9322d336 (patch) | |
tree | 30530c97b339d8e9d0a3ce18b0f5e7cca7cc8c3b | |
parent | 9462452a4843a2c42fe055a0a7e274d5164d1dc0 (diff) | |
download | haskell-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.
-rw-r--r-- | libraries/base/Data/List/NonEmpty.hs | 7 | ||||
-rw-r--r-- | libraries/base/changelog.md | 3 |
2 files changed, 10 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 diff --git a/libraries/base/changelog.md b/libraries/base/changelog.md index 5abb4701b4..c27b4fb1ca 100644 --- a/libraries/base/changelog.md +++ b/libraries/base/changelog.md @@ -9,6 +9,9 @@ * Add `hGetContents'`, `getContents'`, and `readFile'` in `System.IO`: Strict IO variants of `hGetContents`, `getContents`, and `readFile`. + * Add `singleton` function for `Data.List.NonEmpty`. + + ## 4.14.0.0 *TBA* * Bundled with GHC 8.10.1 |