Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
"dependencies": {
"purescript-console": "^0.1.0",
"purescript-foldable-traversable": "^0.4.0"
},
"dev-dependencies": {
"purescript-assert": "^0.1.0"
}
}
18 changes: 12 additions & 6 deletions docs/Data/NonEmpty.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,30 @@ nonEmptyList = 0 :| empty
(Traversable f) => Traversable (NonEmpty f)
```

#### `singleton`
#### `nonEmpty`

``` purescript
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
nonEmpty :: forall f a. a -> f a -> NonEmpty f a
```

Create a non-empty structure with a single value.

#### `(:|)`

``` purescript
(:|) :: forall f a. a -> f a -> NonEmpty f a
infixr 5 nonEmpty as :|
```

_non-associative / precedence 5_
_right-associative / precedence 5_

An infix synonym for `NonEmpty`.

#### `singleton`

``` purescript
singleton :: forall f a. (Plus f) => a -> NonEmpty f a
```

Create a non-empty structure with a single value.

#### `foldl1`

``` purescript
Expand Down
19 changes: 10 additions & 9 deletions src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
module Data.NonEmpty
( NonEmpty(..)
, singleton
, nonEmpty
, (:|)
, foldl1
, foldMap1
Expand All @@ -17,11 +18,11 @@ module Data.NonEmpty
import Prelude

import Control.Alt ((<|>))
import Control.Alternative (Alternative)
import Control.Plus (Plus, empty)
import Control.Alternative (class Alternative)
import Control.Plus (class Plus, empty)

import Data.Foldable (Foldable, foldl, foldr, foldMap)
import Data.Traversable (Traversable, traverse, sequence)
import Data.Foldable (class Foldable, foldl, foldr, foldMap)
import Data.Traversable (class Traversable, traverse, sequence)

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

infix 5 :|
nonEmpty :: forall f a. a -> f a -> NonEmpty f a
nonEmpty = NonEmpty

-- | An infix synonym for `NonEmpty`.
infixr 5 nonEmpty as :|

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

-- | An infix synonym for `NonEmpty`.
(:|) :: forall f a. a -> f a -> NonEmpty f a
(:|) = NonEmpty

-- | Fold a non-empty structure, collecting results using a binary operation.
foldl1 :: forall f a. (Foldable f) => (a -> a -> a) -> NonEmpty f a -> a
foldl1 f (NonEmpty a fa) = foldl f a fa
Expand Down
24 changes: 18 additions & 6 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ module Test.Main where

import Prelude

import Data.Maybe
import Data.NonEmpty
import Control.Monad.Eff (Eff)
import Data.Foldable (fold, foldl)
import Data.Maybe (Maybe(..))
import Data.NonEmpty (NonEmpty(), (:|), fold1, foldl1, oneOf, head, tail, singleton)
import Test.Assert (ASSERT, assert)

import Control.Monad.Eff.Console
type AtLeastTwo f a = NonEmpty (NonEmpty f) a

second :: forall f a. AtLeastTwo f a -> a
second = tail >>> head

main :: Eff (assert :: ASSERT) Unit
main = do
print $ fold1 ("Hello" :| [" ", "World"])
print $ 0 :| Nothing
print (0 :| Nothing == 0 :| Just 1)
assert $ singleton 0 == 0 :| []
assert $ 0 :| Nothing /= 0 :| Just 1
assert $ foldl1 (+) (1 :| [2, 3]) == 6
assert $ foldl (+) 0 (1 :| [2, 3]) == 6
assert $ fold1 ("Hello" :| [" ", "World"]) == "Hello World"
assert $ fold ("Hello" :| [" ", "World"]) == "Hello World"
assert $ oneOf (0 :| Nothing) == oneOf (0 :| Just 1)
assert $ second (1 :| 2 :| [3, 4]) == 2