Skip to content

Commit 2e8974a

Browse files
authored
Merge pull request #29 from matthewleon/Unfoldable1
Unfoldable1 support
2 parents 1d53fbd + 6e20d7e commit 2e8974a

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

bower.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
],
1919
"dependencies": {
2020
"purescript-foldable-traversable": "^3.4.0",
21-
"purescript-prelude": "^3.0.0"
21+
"purescript-prelude": "^3.0.0",
22+
"purescript-unfoldable": "^3.2.0"
2223
},
2324
"devDependencies": {
2425
"purescript-assert": "^3.0.0",

src/Data/NonEmpty.purs

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import Data.Ord (class Ord1, compare1)
2727
import Data.Semigroup.Foldable as F1
2828
import Data.Traversable (class Traversable, traverse, sequence)
2929
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
30+
import Data.Tuple (uncurry)
31+
import Data.Unfoldable (class Unfoldable, unfoldr)
32+
import Data.Unfoldable1 (class Unfoldable1)
3033

3134
-- | A non-empty container of elements of type a.
3235
-- |
@@ -122,3 +125,6 @@ instance traversableWithIndexNonEmpty
122125
instance foldable1NonEmpty :: Foldable f => F1.Foldable1 (NonEmpty f) where
123126
fold1 = foldMap1 id
124127
foldMap1 f (a :| fa) = foldl (\s a1 -> s <> f a1) (f a) fa
128+
129+
instance unfoldable1NonEmpty :: Unfoldable f => Unfoldable1 (NonEmpty f) where
130+
unfoldr1 f b = uncurry (:|) $ unfoldr (map f) <$> f b

test/Main.purs

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Control.Monad.Eff (Eff)
66
import Data.Foldable (fold, foldl)
77
import Data.Maybe (Maybe(..))
88
import Data.NonEmpty (NonEmpty(), (:|), fold1, foldl1, oneOf, head, tail, singleton)
9+
import Data.Unfoldable1 as U1
910
import Test.Assert (ASSERT, assert)
1011

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

0 commit comments

Comments
 (0)