@@ -7,11 +7,15 @@ import Data.Eq (class Eq1)
77import Data.Foldable (class Foldable )
88import Data.FoldableWithIndex (class FoldableWithIndex )
99import Data.FunctorWithIndex (class FunctorWithIndex )
10+ import Data.Maybe (Maybe , fromJust , isNothing )
1011import Data.Ord (class Ord1 )
1112import Data.Semigroup.Foldable (class Foldable1 , foldMap1Default )
1213import Data.Semigroup.Traversable (class Traversable1 , sequence1Default )
1314import Data.Traversable (class Traversable )
1415import Data.TraversableWithIndex (class TraversableWithIndex )
16+ import Data.Tuple (Tuple , fst , snd )
17+ import Data.Unfoldable1 (class Unfoldable1 )
18+ import Partial.Unsafe (unsafePartial )
1519
1620newtype NonEmptyArray a = NonEmptyArray (Array a )
1721
@@ -24,6 +28,8 @@ derive newtype instance eq1NonEmptyArray :: Eq1 NonEmptyArray
2428derive newtype instance ordNonEmptyArray :: Ord a => Ord (NonEmptyArray a )
2529derive newtype instance ord1NonEmptyArray :: Ord1 NonEmptyArray
2630
31+ derive newtype instance semigroupNonEmptyArray :: Semigroup (NonEmptyArray a )
32+
2733derive newtype instance functorNonEmptyArray :: Functor NonEmptyArray
2834derive newtype instance functorWithIndexNonEmptyArray :: FunctorWithIndex Int NonEmptyArray
2935
@@ -34,10 +40,13 @@ instance foldable1NonEmptyArray :: Foldable1 NonEmptyArray where
3440 foldMap1 = foldMap1Default
3541 fold1 = fold1Impl (<>)
3642
43+ instance unfoldable1NonEmptyArray :: Unfoldable1 NonEmptyArray where
44+ unfoldr1 = unfoldr1Impl isNothing (unsafePartial fromJust) fst snd
45+
3746derive newtype instance traversableNonEmptyArray :: Traversable NonEmptyArray
3847derive newtype instance traversableWithIndexNonEmptyArray :: TraversableWithIndex Int NonEmptyArray
3948
40- instance traversable1NonEmptyArray :: Traversable1 NonEmptyArray where
49+ instance traversable1NonEmptyArray :: Traversable1 NonEmptyArray where
4150 traverse1 = traverse1Impl apply map
4251 sequence1 = sequence1Default
4352
@@ -51,9 +60,19 @@ derive newtype instance monadNonEmptyArray :: Monad NonEmptyArray
5160
5261derive newtype instance altNonEmptyArray :: Alt NonEmptyArray
5362
54- -- we use FFI here to avoid the unnecessary copy created by `tail`
63+ -- we use FFI here to avoid the unncessary copy created by `tail`
5564foreign import fold1Impl :: forall a . (a -> a -> a ) -> NonEmptyArray a -> a
5665
66+ foreign import unfoldr1Impl
67+ :: forall a b
68+ . (forall x . Maybe x -> Boolean )
69+ -> (forall x . Maybe x -> x )
70+ -> (forall x y . Tuple x y -> x )
71+ -> (forall x y . Tuple x y -> y )
72+ -> (b -> Tuple a (Maybe b ))
73+ -> b
74+ -> NonEmptyArray a
75+
5776foreign import traverse1Impl
5877 :: forall m a b
5978 . (forall a' b' . (m (a' -> b' ) -> m a' -> m b' ))
0 commit comments