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: 2 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
],
"dependencies": {
"purescript-foldable-traversable": "^3.4.0",
"purescript-prelude": "^3.0.0"
"purescript-prelude": "^3.0.0",
"purescript-unfoldable": "^3.2.0"
},
"devDependencies": {
"purescript-assert": "^3.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/Data/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import Data.Ord (class Ord1, compare1)
import Data.Semigroup.Foldable as F1
import Data.Traversable (class Traversable, traverse, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Tuple (uncurry)
import Data.Unfoldable (class Unfoldable, unfoldr)
import Data.Unfoldable1 (class Unfoldable1)

-- | A non-empty container of elements of type a.
-- |
Expand Down Expand Up @@ -122,3 +125,6 @@ instance traversableWithIndexNonEmpty
instance foldable1NonEmpty :: Foldable f => F1.Foldable1 (NonEmpty f) where
fold1 = foldMap1 id
foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa

instance unfoldable1NonEmpty :: Unfoldable f => Unfoldable1 (NonEmpty f) where
unfoldr1 f b = uncurry (:|) $ unfoldr (map f) <$> f b
2 changes: 2 additions & 0 deletions test/Main.purs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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 Data.Unfoldable1 as U1
import Test.Assert (ASSERT, assert)

type AtLeastTwo f a = NonEmpty (NonEmpty f) a
Expand All @@ -23,3 +24,4 @@ main = do
assert $ fold ("Hello" :| [" ", "World"]) == "Hello World"
assert $ oneOf (0 :| Nothing) == oneOf (0 :| Just 1)
assert $ second (1 :| 2 :| [3, 4]) == 2
assert $ U1.range 0 9 == (0 :| [1, 2, 3, 4, 5, 6, 7, 8, 9])