@@ -6,7 +6,7 @@ import Data.Array as A
6
6
import Data.Array.NonEmpty as NEA
7
7
import Data.Const (Const (..))
8
8
import Data.Foldable (for_ , sum , traverse_ )
9
- import Data.FunctorWithIndex ( mapWithIndex )
9
+ import Data.Traversable ( scanl , scanr )
10
10
import Data.Maybe (Maybe (..), fromJust )
11
11
import Data.Monoid.Additive (Additive (..))
12
12
import Data.NonEmpty ((:|))
@@ -102,6 +102,14 @@ testNonEmptyArray = do
102
102
assert $ NEA .index (fromArray [1 , 2 , 3 ]) 6 == Nothing
103
103
assert $ NEA .index (fromArray [1 , 2 , 3 ]) (-1 ) == Nothing
104
104
105
+ log " elem should return true if the array contains the given element at least once"
106
+ assert $ NEA .elem 1 (fromArray [1 , 2 , 1 ]) == true
107
+ assert $ NEA .elem 4 (fromArray [1 , 2 , 1 ]) == false
108
+
109
+ log " notElem should return true if the array does not contain the given element"
110
+ assert $ NEA .notElem 1 (fromArray [1 , 2 , 1 ]) == false
111
+ assert $ NEA .notElem 4 (fromArray [1 , 2 , 1 ]) == true
112
+
105
113
log " elemIndex should return the index of an item that a predicate returns true for in an array"
106
114
assert $ NEA .elemIndex 1 (fromArray [1 , 2 , 1 ]) == Just 0
107
115
assert $ NEA .elemIndex 4 (fromArray [1 , 2 , 1 ]) == Nothing
@@ -110,6 +118,15 @@ testNonEmptyArray = do
110
118
assert $ NEA .elemLastIndex 1 (fromArray [1 , 2 , 1 ]) == Just 2
111
119
assert $ NEA .elemLastIndex 4 (fromArray [1 , 2 , 1 ]) == Nothing
112
120
121
+ log " find should return the first element for which a predicate returns true in an array"
122
+ assert $ NEA .find (_ == 1 ) (fromArray [1 , 2 , 1 ]) == Just 1
123
+ assert $ NEA .find (_ == 3 ) (fromArray [1 , 2 , 1 ]) == Nothing
124
+
125
+ log " findMap should return the mapping of the first element that satisfies the given predicate"
126
+ assert $ NEA .findMap (\x -> if x > 3 then Just x else Nothing ) (fromArray [1 , 2 , 4 ]) == Just 4
127
+ assert $ NEA .findMap (\x -> if x > 3 then Just x else Nothing ) (fromArray [1 , 2 , 1 ]) == Nothing
128
+ assert $ NEA .findMap (\x -> if x > 3 then Just x else Nothing ) (fromArray [4 , 1 , 5 ]) == Just 4
129
+
113
130
log " findIndex should return the index of an item that a predicate returns true for in an array"
114
131
assert $ (NEA .findIndex (_ /= 1 ) (fromArray [1 , 2 , 1 ])) == Just 1
115
132
assert $ (NEA .findIndex (_ == 3 ) (fromArray [1 , 2 , 1 ])) == Nothing
@@ -158,6 +175,13 @@ testNonEmptyArray = do
158
175
log " alterAt should return Nothing if the index is out of NEA.range"
159
176
assert $ NEA .alterAt 1 (Just <<< (_ + 1 )) (NEA .singleton 1 ) == Nothing
160
177
178
+ log " intersperse should return the original array when given an array with one element"
179
+ assert $ NEA .intersperse " " (NEA .singleton " a" ) == NEA .singleton " a"
180
+
181
+ log " intersperse should insert the given element in-between each element in an array with two or more elements"
182
+ assert $ NEA .intersperse " " (fromArray [" a" , " b" ]) == fromArray [" a" , " " , " b" ]
183
+ assert $ NEA .intersperse 0 (fromArray [ 1 , 2 , 3 , 4 , 5 ]) == fromArray [ 1 , 0 , 2 , 0 , 3 , 0 , 4 , 0 , 5 ]
184
+
161
185
log " reverse should reverse the order of items in an array"
162
186
assert $ NEA .reverse (fromArray [1 , 2 , 3 ]) == fromArray [3 , 2 , 1 ]
163
187
assert $ NEA .reverse (NEA .singleton 0 ) == NEA .singleton 0
@@ -193,7 +217,23 @@ testNonEmptyArray = do
193
217
assert $ NEA .catMaybes (fromArray [Nothing , Just 2 , Nothing , Just 4 ]) == [2 , 4 ]
194
218
195
219
log " mapWithIndex applies a function with an index for every element"
196
- assert $ mapWithIndex (\i x -> x - i) (fromArray [9 ,8 ,7 ,6 ,5 ]) == fromArray [9 ,7 ,5 ,3 ,1 ]
220
+ assert $ NEA .mapWithIndex (\i x -> x - i) (fromArray [9 ,8 ,7 ,6 ,5 ]) == fromArray [9 ,7 ,5 ,3 ,1 ]
221
+
222
+ log " scanl should return an array that stores the accumulated value at each step"
223
+ assert $ NEA .scanl (+) 0 (fromArray [1 ,2 ,3 ]) == fromArray [1 , 3 , 6 ]
224
+ assert $ NEA .scanl (-) 10 (fromArray [1 ,2 ,3 ]) == fromArray [9 , 7 , 4 ]
225
+
226
+ log " scanl should return the same results as its Foldable counterpart"
227
+ assert $ NEA .scanl (+) 0 (fromArray [1 ,2 ,3 ]) == scanl (+) 0 (fromArray [1 ,2 ,3 ])
228
+ assert $ NEA .scanl (-) 10 (fromArray [1 ,2 ,3 ]) == scanl (-) 10 (fromArray [1 ,2 ,3 ])
229
+
230
+ log " scanr should return an array that stores the accumulated value at each step"
231
+ assert $ NEA .scanr (+) 0 (fromArray [1 ,2 ,3 ]) == fromArray [6 ,5 ,3 ]
232
+ assert $ NEA .scanr (flip (-)) 10 (fromArray [1 ,2 ,3 ]) == fromArray [4 ,5 ,7 ]
233
+
234
+ log " scanr should return the same results as its Foldable counterpart"
235
+ assert $ NEA .scanr (+) 0 (fromArray [1 ,2 ,3 ]) == scanr (+) 0 (fromArray [1 ,2 ,3 ])
236
+ assert $ NEA .scanr (flip (-)) 10 (fromArray [1 ,2 ,3 ]) == scanr (flip (-)) 10 (fromArray [1 ,2 ,3 ])
197
237
198
238
log " updateAtIndices changes the elements at specified indices"
199
239
assert $ NEA .updateAtIndices
0 commit comments