From c5744433aae987c05ee2310ce35fbe13a23c4273 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 31 Jan 2016 10:21:27 +0100 Subject: [PATCH 1/2] Right-associative `:|`, requires PureScript 0.8 Make the operator `:|` right-associative in order to enable code like this: ``` purescript 0 :| 1 :| [2, 3] ``` Uses PureScript 0.8 syntax and fixes a couple of warnings. --- docs/Data/NonEmpty.md | 18 ++++++++++++------ src/Data/NonEmpty.purs | 19 ++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/docs/Data/NonEmpty.md b/docs/Data/NonEmpty.md index a22504c..c4ffc25 100644 --- a/docs/Data/NonEmpty.md +++ b/docs/Data/NonEmpty.md @@ -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 diff --git a/src/Data/NonEmpty.purs b/src/Data/NonEmpty.purs index d305d3a..8730508 100644 --- a/src/Data/NonEmpty.purs +++ b/src/Data/NonEmpty.purs @@ -4,6 +4,7 @@ module Data.NonEmpty ( NonEmpty(..) , singleton + , nonEmpty , (:|) , foldl1 , foldMap1 @@ -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. -- | @@ -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 From c208d9433634f7de7b0c4672ded3f45e4faf49c5 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Sun, 31 Jan 2016 10:23:49 +0100 Subject: [PATCH 2/2] Add simple test suite. Previously, the Test.Main module only printed some values to the console. This commit adds a few simple tests using Test.Assert. --- bower.json | 3 +++ test/Main.purs | 24 ++++++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bower.json b/bower.json index 7a6cf4c..b05aaef 100644 --- a/bower.json +++ b/bower.json @@ -13,5 +13,8 @@ "dependencies": { "purescript-console": "^0.1.0", "purescript-foldable-traversable": "^0.4.0" + }, + "dev-dependencies": { + "purescript-assert": "^0.1.0" } } diff --git a/test/Main.purs b/test/Main.purs index d73b1b7..20b2ab2 100644 --- a/test/Main.purs +++ b/test/Main.purs @@ -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