28
28
-- | allowing you to STAI.iterate over an array and accumulate effects.
29
29
-- |
30
30
module Data.Array
31
- ( fromFoldable
32
- , toUnfoldable
33
- , singleton
34
- , (..), range
35
- , replicate
36
- , some
37
- , many
38
-
39
- , null
40
- , length
41
-
42
- , (:), cons
43
- , snoc
44
- , insert
45
- , insertBy
46
-
47
- , head
48
- , last
49
- , tail
50
- , init
51
- , uncons
52
- , unsnoc
53
-
54
- , (!!), index
31
+ ( (!!)
32
+ , (..)
33
+ , (:)
34
+ , (\\)
35
+ , all
36
+ , alterAt
37
+ , any
38
+ , catMaybes
39
+ , concat
40
+ , concatMap
41
+ , cons
42
+ , delete
43
+ , deleteAt
44
+ , deleteBy
45
+ , difference
46
+ , drop
47
+ , dropEnd
48
+ , dropWhile
55
49
, elem
56
- , notElem
57
50
, elemIndex
58
51
, elemLastIndex
52
+ , filter
53
+ , filterA
59
54
, find
60
- , findMap
61
55
, findIndex
62
56
, findLastIndex
57
+ , findMap
58
+ , fold
59
+ , foldM
60
+ , foldMap
61
+ , foldRecM
62
+ , foldl
63
+ , foldr
64
+ , fromFoldable
65
+ , group
66
+ , groupAll
67
+ , groupAllBy
68
+ , groupBy
69
+ , head
70
+ , index
71
+ , init
72
+ , insert
63
73
, insertAt
64
- , deleteAt
65
- , updateAt
66
- , updateAtIndices
67
- , modifyAt
68
- , modifyAtIndices
69
- , alterAt
70
-
74
+ , insertBy
75
+ , intercalate
76
+ , intersect
77
+ , intersectBy
71
78
, intersperse
72
- , reverse
73
- , concat
74
- , concatMap
75
- , filter
76
- , partition
77
- , splitAt
78
- , filterA
79
+ , isEmpty
80
+ , last
81
+ , length
82
+ , many
79
83
, mapMaybe
80
- , catMaybes
81
84
, mapWithIndex
82
- , foldl
83
- , foldr
84
- , foldMap
85
- , fold
86
- , intercalate
85
+ , modifyAt
86
+ , modifyAtIndices
87
+ , notElem
88
+ , nub
89
+ , nubBy
90
+ , nubByEq
91
+ , nubEq
92
+ , null
93
+ , partition
94
+ , range
95
+ , replicate
96
+ , reverse
87
97
, scanl
88
98
, scanr
89
-
99
+ , singleton
100
+ , slice
101
+ , snoc
102
+ , some
90
103
, sort
91
104
, sortBy
92
105
, sortWith
93
- , slice
106
+ , span
107
+ , splitAt
108
+ , tail
94
109
, take
95
110
, takeEnd
96
111
, takeWhile
97
- , drop
98
- , dropEnd
99
- , dropWhile
100
- , span
101
- , group
102
- , groupAll
103
- , groupBy
104
- , groupAllBy
105
-
106
- , nub
107
- , nubEq
108
- , nubBy
109
- , nubByEq
112
+ , toUnfoldable
113
+ , uncons
110
114
, union
111
115
, unionBy
112
- , delete
113
- , deleteBy
114
-
115
- , (\\), difference
116
- , intersect
117
- , intersectBy
118
-
116
+ , unsafeIndex
117
+ , unsnoc
118
+ , unzip
119
+ , updateAt
120
+ , updateAtIndices
121
+ , zip
119
122
, zipWith
120
123
, zipWithA
121
- , zip
122
- , unzip
123
-
124
- , any
125
- , all
126
-
127
- , foldM
128
- , foldRecM
129
-
130
- , unsafeIndex
131
124
) where
132
125
133
126
import Prelude
@@ -154,7 +147,7 @@ toUnfoldable xs = unfoldr f 0
154
147
where
155
148
len = length xs
156
149
f i
157
- | i < len = Just (Tuple (unsafePartial (unsafeIndex xs i)) (i+ 1 ))
150
+ | i < len = Just (Tuple (unsafePartial (unsafeIndex xs i)) (i + 1 ))
158
151
| otherwise = Nothing
159
152
160
153
-- | Convert a `Foldable` structure into an `Array`.
@@ -178,7 +171,7 @@ foreign import fromFoldableImpl
178
171
-- | singleton 2 = [2]
179
172
-- | ```
180
173
singleton :: forall a . a -> Array a
181
- singleton a = [a ]
174
+ singleton a = [ a ]
182
175
183
176
-- | Create an array containing a range of integers, including both endpoints.
184
177
-- | ```purescript
@@ -217,13 +210,21 @@ many v = some v <|> pure []
217
210
-- Array size ------------------------------------------------------------------
218
211
-- ------------------------------------------------------------------------------
219
212
220
- -- | Test whether an array is empty.
213
+ -- | Test whether an array is empty. Alias for `isEmpty`.
221
214
-- | ```purescript
222
215
-- | null [] = true
223
216
-- | null [1, 2] = false
224
217
-- | ```
225
218
null :: forall a . Array a -> Boolean
226
- null xs = length xs == 0
219
+ null = isEmpty
220
+
221
+ -- | Test whether an array is empty.
222
+ -- | ```purescript
223
+ -- | isEmpty [] = true
224
+ -- | isEmpty [1, 2] = false
225
+ -- | ```
226
+ isEmpty :: forall a . Array a -> Boolean
227
+ isEmpty xs = length xs == 0
227
228
228
229
-- | Get the number of elements in an array.
229
230
-- | ```purescript
@@ -243,7 +244,7 @@ foreign import length :: forall a. Array a -> Int
243
244
-- |
244
245
-- | Note, the running time of this function is `O(n)`.
245
246
cons :: forall a . a -> Array a -> Array a
246
- cons x xs = [x ] <> xs
247
+ cons x xs = [ x ] <> xs
247
248
248
249
-- | An infix alias for `cons`.
249
250
-- |
@@ -283,8 +284,10 @@ insert = insertBy compare
283
284
-- |
284
285
insertBy :: forall a . (a -> a -> Ordering ) -> a -> Array a -> Array a
285
286
insertBy cmp x ys =
286
- let i = maybe 0 (_ + 1 ) (findLastIndex (\y -> cmp x y == GT ) ys)
287
- in unsafePartial (fromJust (insertAt i x ys))
287
+ let
288
+ i = maybe 0 (_ + 1 ) (findLastIndex (\y -> cmp x y == GT ) ys)
289
+ in
290
+ unsafePartial (fromJust (insertAt i x ys))
288
291
289
292
-- ------------------------------------------------------------------------------
290
293
-- Non-indexed reads -----------------------------------------------------------
@@ -609,15 +612,16 @@ alterAt i f xs = maybe Nothing go (xs !! i)
609
612
-- | ```
610
613
intersperse :: forall a . a -> Array a -> Array a
611
614
intersperse a arr = case length arr of
612
- len | len < 2 -> arr
613
- | otherwise -> STA .run do
614
- let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx)
615
- out <- STA .new
616
- _ <- STA .push (unsafeGetElem 0 ) out
617
- ST .for 1 len \idx -> do
618
- _ <- STA .push a out
619
- void (STA .push (unsafeGetElem idx) out)
620
- pure out
615
+ len
616
+ | len < 2 -> arr
617
+ | otherwise -> STA .run do
618
+ let unsafeGetElem idx = unsafePartial (unsafeIndex arr idx)
619
+ out <- STA .new
620
+ _ <- STA .push (unsafeGetElem 0 ) out
621
+ ST .for 1 len \idx -> do
622
+ _ <- STA .push a out
623
+ void (STA .push (unsafeGetElem idx) out)
624
+ pure out
621
625
622
626
-- | Reverse an array, creating a new array.
623
627
-- |
@@ -700,7 +704,7 @@ splitAt i xs = { before: slice 0 i xs, after: slice i (length xs) xs }
700
704
filterA :: forall a f . Applicative f => (a -> f Boolean ) -> Array a -> f (Array a )
701
705
filterA p =
702
706
traverse (\x -> Tuple x <$> p x)
703
- >>> map (mapMaybe (\(Tuple x b) -> if b then Just x else Nothing ))
707
+ >>> map (mapMaybe (\(Tuple x b) -> if b then Just x else Nothing ))
704
708
705
709
-- | Apply a function to each element in an array, keeping only the results
706
710
-- | which contain a value, creating a new array.
@@ -1044,16 +1048,16 @@ nubBy :: forall a. (a -> a -> Ordering) -> Array a -> Array a
1044
1048
nubBy comp xs = case head indexedAndSorted of
1045
1049
Nothing -> []
1046
1050
Just x -> map snd $ sortWith fst $ ST .run do
1047
- -- TODO: use NonEmptyArrays here to avoid partial functions
1048
- result <- STA .unsafeThaw $ singleton x
1049
- ST .foreach indexedAndSorted \pair@(Tuple _ x') -> do
1050
- lst <- snd <<< unsafePartial (fromJust <<< last) <$> STA .unsafeFreeze result
1051
- when (comp lst x' /= EQ ) $ void $ STA .push pair result
1052
- STA .unsafeFreeze result
1051
+ -- TODO: use NonEmptyArrays here to avoid partial functions
1052
+ result <- STA .unsafeThaw $ singleton x
1053
+ ST .foreach indexedAndSorted \pair@(Tuple _ x') -> do
1054
+ lst <- snd <<< unsafePartial (fromJust <<< last) <$> STA .unsafeFreeze result
1055
+ when (comp lst x' /= EQ ) $ void $ STA .push pair result
1056
+ STA .unsafeFreeze result
1053
1057
where
1054
1058
indexedAndSorted :: Array (Tuple Int a )
1055
1059
indexedAndSorted = sortBy (\x y -> comp (snd x) (snd y))
1056
- (mapWithIndex Tuple xs)
1060
+ (mapWithIndex Tuple xs)
1057
1061
1058
1062
-- | Remove the duplicates from an array, where element equality is determined
1059
1063
-- | by the specified equivalence relation, creating a new array.
@@ -1120,7 +1124,7 @@ delete = deleteBy eq
1120
1124
-- | ```
1121
1125
-- |
1122
1126
deleteBy :: forall a . (a -> a -> Boolean ) -> a -> Array a -> Array a
1123
- deleteBy _ _ [] = []
1127
+ deleteBy _ _ [] = []
1124
1128
deleteBy eq x ys = maybe ys (\i -> unsafePartial $ fromJust (deleteAt i ys)) (findIndex (eq x) ys)
1125
1129
1126
1130
-- | Delete the first occurrence of each element in the second array from the
0 commit comments