Skip to content

Commit 4c88b00

Browse files
committed
Newtype-derive Unfoldable1 instance for NonEmptyArray
1 parent e5059a4 commit 4c88b00

File tree

2 files changed

+1
-37
lines changed

2 files changed

+1
-37
lines changed

src/Data/Array/NonEmpty/Internal.js

-21
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,6 @@ exports.fold1Impl = function (f) {
1111
};
1212
};
1313

14-
exports.unfoldr1Impl = function (isNothing) {
15-
return function (fromJust) {
16-
return function (fst) {
17-
return function (snd) {
18-
return function (f) {
19-
return function (value) {
20-
var result = [];
21-
while (true) { // eslint-disable-line no-constant-condition
22-
var tuple = f(value);
23-
result.push(fst(tuple));
24-
var maybe = snd(tuple);
25-
if (isNothing(maybe)) return result;
26-
value = fromJust(maybe); // eslint-disable-line no-param-reassign
27-
}
28-
};
29-
};
30-
};
31-
};
32-
};
33-
};
34-
3514
exports.traverse1Impl = function () {
3615
function Cont(fn) {
3716
this.fn = fn;

src/Data/Array/NonEmpty/Internal.purs

+1-16
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,12 @@ import Data.Eq (class Eq1)
77
import Data.Foldable (class Foldable)
88
import Data.FoldableWithIndex (class FoldableWithIndex)
99
import Data.FunctorWithIndex (class FunctorWithIndex)
10-
import Data.Maybe (Maybe, fromJust, isNothing)
1110
import Data.Ord (class Ord1)
1211
import Data.Semigroup.Foldable (class Foldable1, foldMap1Default)
1312
import Data.Semigroup.Traversable (class Traversable1, sequence1Default)
1413
import Data.Traversable (class Traversable)
1514
import Data.TraversableWithIndex (class TraversableWithIndex)
16-
import Data.Tuple (Tuple, fst, snd)
1715
import Data.Unfoldable1 (class Unfoldable1)
18-
import Partial.Unsafe (unsafePartial)
1916

2017
newtype NonEmptyArray a = NonEmptyArray (Array a)
2118

@@ -40,9 +37,7 @@ instance foldable1NonEmptyArray :: Foldable1 NonEmptyArray where
4037
foldMap1 = foldMap1Default
4138
fold1 = fold1Impl (<>)
4239

43-
instance unfoldable1NonEmptyArray :: Unfoldable1 NonEmptyArray where
44-
unfoldr1 = unfoldr1Impl isNothing (unsafePartial fromJust) fst snd
45-
40+
derive newtype instance unfoldable1NonEmptyArray :: Unfoldable1 NonEmptyArray
4641
derive newtype instance traversableNonEmptyArray :: Traversable NonEmptyArray
4742
derive newtype instance traversableWithIndexNonEmptyArray :: TraversableWithIndex Int NonEmptyArray
4843

@@ -63,16 +58,6 @@ derive newtype instance altNonEmptyArray :: Alt NonEmptyArray
6358
-- we use FFI here to avoid the unncessary copy created by `tail`
6459
foreign import fold1Impl :: forall a. (a -> a -> a) -> NonEmptyArray a -> a
6560

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-
7661
foreign import traverse1Impl
7762
:: forall m a b
7863
. (forall a' b'. (m (a' -> b') -> m a' -> m b'))

0 commit comments

Comments
 (0)