Skip to content

Commit

Permalink
Merge pull request #185 from snoyberg/nonNullToNonEmpty
Browse files Browse the repository at this point in the history
Add function to convert from NonNull to NonEmpty
  • Loading branch information
snoyberg authored Dec 24, 2019
2 parents 4ab1d16 + 9c9e039 commit c6faffa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions mono-traversable/ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.15.0
* Added `toNonEmpty` to `Data.NonNull`
[#185](https://github.com/snoyberg/mono-traversable/pull/185)

## 1.0.14.0
* Added `WrappedMono` to `Data.MonoTraversable`
[#182](https://github.com/snoyberg/mono-traversable/pull/182)
Expand Down
2 changes: 1 addition & 1 deletion mono-traversable/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mono-traversable
version: 1.0.14.0
version: 1.0.15.0
synopsis: Type classes for mapping, folding, and traversing monomorphic containers
description: Please see the README at <https://www.stackage.org/package/mono-traversable>
category: Data
Expand Down
7 changes: 7 additions & 0 deletions mono-traversable/src/Data/NonNull.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Data.NonNull (
, nonNull
, toNullable
, fromNonEmpty
, toNonEmpty
, ncons
, nuncons
, splitFirst
Expand Down Expand Up @@ -132,6 +133,12 @@ fromNonEmpty :: IsSequence seq => NE.NonEmpty (Element seq) -> NonNull seq
fromNonEmpty = impureNonNull . fromList . NE.toList
{-# INLINE fromNonEmpty #-}

-- | __Safely__ convert from a 'NonNull' container to a 'NonEmpty' list.
--
-- @since 1.0.15.0
toNonEmpty :: MonoFoldable mono => NonNull mono -> NE.NonEmpty (Element mono)
toNonEmpty = NE.fromList . otoList

-- | Specializes 'fromNonEmpty' to lists only.
toMinList :: NE.NonEmpty a -> NonNull [a]
toMinList = fromNonEmpty
Expand Down
3 changes: 3 additions & 0 deletions mono-traversable/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ main = hspec $ do
describe "fromNonEmpty" $ do
prop "toMinList" $ \(NonEmpty' ne) ->
(NE.toList ne :: [Int]) @?= NN.toNullable (NN.toMinList ne)
describe "toNonEmpty" $ do
it "converts nonnull to nonempty" $ do
NN.toNonEmpty (NN.impureNonNull [1,2,3]) @?= NE.fromList [1,2,3]

describe "mapNonNull" $ do
prop "mapNonNull id == id" $ \x xs ->
Expand Down

0 comments on commit c6faffa

Please sign in to comment.