Skip to content

Commit f2368cf

Browse files
committed
Add more flexible variety of fromNonEmpty
1 parent 634471a commit f2368cf

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

docs/Data/NonEmpty.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Fold a non-empty structure, collecting results using a binary operation.
5353
foldMap1 :: forall f a s. (Semigroup s, Foldable f) => (a -> s) -> NonEmpty f a -> s
5454
```
5555

56-
Fold a non-empty structure, collecting results in a `Semigroup`.
56+
Fold a non-empty structure, collecting results in a `Semigroup`.
5757

5858
#### `fold1`
5959

@@ -63,4 +63,16 @@ fold1 :: forall f s. (Semigroup s, Foldable f) => NonEmpty f s -> s
6363

6464
Fold a non-empty structure.
6565

66+
#### `fromNonEmpty`
67+
68+
``` purescript
69+
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
70+
```
71+
72+
#### `fromNonEmpty'`
73+
74+
``` purescript
75+
fromNonEmpty' :: forall f a. (Applicative f, Semigroup (f a)) => NonEmpty f a -> f a
76+
```
77+
6678

src/Data/NonEmpty.purs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ module Data.NonEmpty
88
, foldMap1
99
, fold1
1010
, fromNonEmpty
11+
, fromNonEmpty'
1112
) where
1213

1314
import Prelude
@@ -43,8 +44,11 @@ foldMap1 f (NonEmpty a fa) = foldl (\s a1 -> s <> f a1) (f a) fa
4344
fold1 :: forall f s. (Semigroup s, Foldable f) => NonEmpty f s -> s
4445
fold1 = foldMap1 id
4546

46-
fromNonEmpty :: forall f a. (Applicative f, Semigroup (f a)) => NonEmpty f a -> f a
47-
fromNonEmpty (NonEmpty a fa) = pure a <> fa
47+
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
48+
fromNonEmpty f (NonEmpty a fa) = a `f` fa
49+
50+
fromNonEmpty' :: forall f a. (Applicative f, Semigroup (f a)) => NonEmpty f a -> f a
51+
fromNonEmpty' (NonEmpty a fa) = pure a <> fa
4852

4953
instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where
5054
show (NonEmpty a fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"

0 commit comments

Comments
 (0)