Skip to content

Commit 1897c64

Browse files
committed
Merge pull request #7 from sharkdp/updates
Right-associative :|, proper tests and PureScript 0.8 updates
2 parents b2c8b5d + c208d94 commit 1897c64

File tree

4 files changed

+43
-21
lines changed

4 files changed

+43
-21
lines changed

bower.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"dependencies": {
1414
"purescript-console": "^0.1.0",
1515
"purescript-foldable-traversable": "^0.4.0"
16+
},
17+
"dev-dependencies": {
18+
"purescript-assert": "^0.1.0"
1619
}
1720
}

docs/Data/NonEmpty.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,30 @@ nonEmptyList = 0 :| empty
2929
(Traversable f) => Traversable (NonEmpty f)
3030
```
3131

32-
#### `singleton`
32+
#### `nonEmpty`
3333

3434
``` purescript
35-
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
35+
nonEmpty :: forall f a. a -> f a -> NonEmpty f a
3636
```
3737

38-
Create a non-empty structure with a single value.
39-
4038
#### `(:|)`
4139

4240
``` purescript
43-
(:|) :: forall f a. a -> f a -> NonEmpty f a
41+
infixr 5 nonEmpty as :|
4442
```
4543

46-
_non-associative / precedence 5_
44+
_right-associative / precedence 5_
4745

4846
An infix synonym for `NonEmpty`.
4947

48+
#### `singleton`
49+
50+
``` purescript
51+
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
52+
```
53+
54+
Create a non-empty structure with a single value.
55+
5056
#### `foldl1`
5157

5258
``` purescript

src/Data/NonEmpty.purs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
module Data.NonEmpty
55
( NonEmpty(..)
66
, singleton
7+
, nonEmpty
78
, (:|)
89
, foldl1
910
, foldMap1
@@ -17,11 +18,11 @@ module Data.NonEmpty
1718
import Prelude
1819

1920
import Control.Alt ((<|>))
20-
import Control.Alternative (Alternative)
21-
import Control.Plus (Plus, empty)
21+
import Control.Alternative (class Alternative)
22+
import Control.Plus (class Plus, empty)
2223

23-
import Data.Foldable (Foldable, foldl, foldr, foldMap)
24-
import Data.Traversable (Traversable, traverse, sequence)
24+
import Data.Foldable (class Foldable, foldl, foldr, foldMap)
25+
import Data.Traversable (class Traversable, traverse, sequence)
2526

2627
-- | A non-empty container of elements of type a.
2728
-- |
@@ -33,16 +34,16 @@ import Data.Traversable (Traversable, traverse, sequence)
3334
-- | ```
3435
data NonEmpty f a = NonEmpty a (f a)
3536

36-
infix 5 :|
37+
nonEmpty :: forall f a. a -> f a -> NonEmpty f a
38+
nonEmpty = NonEmpty
39+
40+
-- | An infix synonym for `NonEmpty`.
41+
infixr 5 nonEmpty as :|
3742

3843
-- | Create a non-empty structure with a single value.
3944
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
4045
singleton a = NonEmpty a empty
4146

42-
-- | An infix synonym for `NonEmpty`.
43-
(:|) :: forall f a. a -> f a -> NonEmpty f a
44-
(:|) = NonEmpty
45-
4647
-- | Fold a non-empty structure, collecting results using a binary operation.
4748
foldl1 :: forall f a. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
4849
foldl1 f (NonEmpty a fa) = foldl f a fa

test/Main.purs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@ module Test.Main where
22

33
import Prelude
44

5-
import Data.Maybe
6-
import Data.NonEmpty
5+
import Control.Monad.Eff (Eff)
6+
import Data.Foldable (fold, foldl)
7+
import Data.Maybe (Maybe(..))
8+
import Data.NonEmpty (NonEmpty(), (:|), fold1, foldl1, oneOf, head, tail, singleton)
9+
import Test.Assert (ASSERT, assert)
710

8-
import Control.Monad.Eff.Console
11+
type AtLeastTwo f a = NonEmpty (NonEmpty f) a
912

13+
second :: forall f a. AtLeastTwo f a -> a
14+
second = tail >>> head
15+
16+
main :: Eff (assert :: ASSERT) Unit
1017
main = do
11-
print $ fold1 ("Hello" :| [" ", "World"])
12-
print $ 0 :| Nothing
13-
print (0 :| Nothing == 0 :| Just 1)
18+
assert $ singleton 0 == 0 :| []
19+
assert $ 0 :| Nothing /= 0 :| Just 1
20+
assert $ foldl1 (+) (1 :| [2, 3]) == 6
21+
assert $ foldl (+) 0 (1 :| [2, 3]) == 6
22+
assert $ fold1 ("Hello" :| [" ", "World"]) == "Hello World"
23+
assert $ fold ("Hello" :| [" ", "World"]) == "Hello World"
24+
assert $ oneOf (0 :| Nothing) == oneOf (0 :| Just 1)
25+
assert $ second (1 :| 2 :| [3, 4]) == 2

0 commit comments

Comments
 (0)