Skip to content

Commit d2ee1a7

Browse files
committed
Add fromNonEmpty and oneOf operations
1 parent 6f09b35 commit d2ee1a7

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

bower.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
{
22
"name": "purescript-nonempty",
3-
"version": "1.0.0",
4-
"moduleType": [
5-
"node"
6-
],
73
"ignore": [
84
"**/.*",
95
"node_modules",
106
"bower_components",
117
"output"
128
],
13-
"repository": {
14-
"type": "git",
15-
"url": "git://github.com/paf31/purescript-nonempty.git"
9+
"repository": {
10+
"type": "git",
11+
"url": "git://github.com/paf31/purescript-nonempty.git"
1612
},
1713
"dependencies": {
1814
"purescript-console": "^0.1.0",

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+
#### `oneOf`
73+
74+
``` purescript
75+
oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a
76+
```
77+
6678

src/Data/NonEmpty.purs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
-- | This module defines a generic non-empty data structure, which adds an additional
22
-- | element to any container type.
33

4-
module Data.NonEmpty
4+
module Data.NonEmpty
55
( NonEmpty(..)
66
, (:|)
77
, foldl1
88
, foldMap1
99
, fold1
10+
, fromNonEmpty
11+
, oneOf
1012
) where
1113

1214
import Prelude
1315

14-
import Data.Foldable
15-
import Data.Traversable
16+
import Control.Alternative (Alternative)
17+
import Control.Alt ((<|>))
18+
19+
import Data.Foldable (Foldable, foldl, foldr, foldMap)
20+
import Data.Traversable (Traversable, traverse, sequence)
1621

1722
-- | A non-empty container of elements of type a.
1823
-- |
@@ -34,34 +39,40 @@ infix 5 :|
3439
foldl1 :: forall f a s. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
3540
foldl1 f (NonEmpty a fa) = foldl f a fa
3641

37-
-- | Fold a non-empty structure, collecting results in a `Semigroup`.
42+
-- | Fold a non-empty structure, collecting results in a `Semigroup`.
3843
foldMap1 :: forall f a s. (Semigroup s, Foldable f) => (a -> s) -> NonEmpty f a -> s
3944
foldMap1 f (NonEmpty a fa) = foldl (\s a1 -> s <> f a1) (f a) fa
4045

4146
-- | Fold a non-empty structure.
4247
fold1 :: forall f s. (Semigroup s, Foldable f) => NonEmpty f s -> s
4348
fold1 = foldMap1 id
4449

50+
fromNonEmpty :: forall f a r. (a -> f a -> r) -> NonEmpty f a -> r
51+
fromNonEmpty f (NonEmpty a fa) = a `f` fa
52+
53+
oneOf :: forall f a. (Alternative f) => NonEmpty f a -> f a
54+
oneOf (NonEmpty a fa) = pure a <|> fa
55+
4556
instance showNonEmpty :: (Show a, Show (f a)) => Show (NonEmpty f a) where
4657
show (NonEmpty a fa) = "(NonEmpty " ++ show a ++ " " ++ show fa ++ ")"
4758

4859
instance eqNonEmpty :: (Eq a, Eq (f a)) => Eq (NonEmpty f a) where
4960
eq (NonEmpty a1 fa1) (NonEmpty a2 fa2) = a1 == a2 && fa1 == fa2
5061

5162
instance ordNonEmpty :: (Ord a, Ord (f a)) => Ord (NonEmpty f a) where
52-
compare (NonEmpty a1 fa1) (NonEmpty a2 fa2) =
63+
compare (NonEmpty a1 fa1) (NonEmpty a2 fa2) =
5364
case compare a1 a2 of
5465
EQ -> compare fa1 fa2
5566
other -> other
56-
67+
5768
instance functorNonEmpty :: (Functor f) => Functor (NonEmpty f) where
5869
map f (NonEmpty a fa) = NonEmpty (f a) (map f fa)
59-
70+
6071
instance foldableNonEmpty :: (Foldable f) => Foldable (NonEmpty f) where
6172
foldMap f (NonEmpty a fa) = f a <> foldMap f fa
6273
foldl f b (NonEmpty a fa) = foldl f (f b a) fa
6374
foldr f b (NonEmpty a fa) = f a (foldr f b fa)
64-
75+
6576
instance traversableNonEmpty :: (Traversable f) => Traversable (NonEmpty f) where
6677
sequence (NonEmpty a fa) = NonEmpty <$> a <*> sequence fa
6778
traverse f (NonEmpty a fa) = NonEmpty <$> f a <*> traverse f fa

0 commit comments

Comments
 (0)