2828--  |   allowing you to STAI.iterate over an array and accumulate effects.
2929--  |
3030module  Data.Array 
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 
31+   ( fromFoldable 
32+   , toUnfoldable 
33+   , singleton 
34+   , (..), range 
35+   , replicate 
36+   , some 
37+   , many 
38+ 
39+   , null 
40+   , isEmpty 
41+   , length 
42+ 
43+   , (:), cons 
44+   , snoc 
45+   , insert 
46+   , insertBy 
47+ 
48+   , head 
49+   , last 
50+   , tail 
51+   , init 
52+   , uncons 
53+   , unsnoc 
54+ 
55+   , (!!), index 
4956  , elem 
57+   , notElem 
5058  , elemIndex 
5159  , elemLastIndex 
52-   , filter 
53-   , filterA 
5460  , find 
61+   , findMap 
5562  , findIndex 
5663  , 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 
7364  , insertAt 
74-   , insertBy 
75-   , intercalate 
76-   , intersect 
77-   , intersectBy 
78-   , intersperse 
79-   , isEmpty 
80-   , last 
81-   , length 
82-   , many 
83-   , mapMaybe 
84-   , mapWithIndex 
65+   , deleteAt 
66+   , updateAt 
67+   , updateAtIndices 
8568  , modifyAt 
8669  , modifyAtIndices 
87-   , notElem 
88-   , nub 
89-   , nubBy 
90-   , nubByEq 
91-   , nubEq 
92-   , null 
93-   , partition 
94-   , range 
95-   , replicate 
70+   , alterAt 
71+ 
72+   , intersperse 
9673  , reverse 
74+   , concat 
75+   , concatMap 
76+   , filter 
77+   , partition 
78+   , splitAt 
79+   , filterA 
80+   , mapMaybe 
81+   , catMaybes 
82+   , mapWithIndex 
83+   , foldl 
84+   , foldr 
85+   , foldMap 
86+   , fold 
87+   , intercalate 
9788  , scanl 
9889  , scanr 
99-   , singleton 
100-   , slice 
101-   , snoc 
102-   , some 
90+ 
10391  , sort 
10492  , sortBy 
10593  , sortWith 
106-   , span 
107-   , splitAt 
108-   , tail 
94+   , slice 
10995  , take 
11096  , takeEnd 
11197  , takeWhile 
112-   , toUnfoldable 
113-   , uncons 
98+   , drop 
99+   , dropEnd 
100+   , dropWhile 
101+   , span 
102+   , group 
103+   , groupAll 
104+   , groupBy 
105+   , groupAllBy 
106+ 
107+   , nub 
108+   , nubEq 
109+   , nubBy 
110+   , nubByEq 
114111  , union 
115112  , unionBy 
116-   , unsafeIndex 
117-   , unsnoc 
118-   , unzip 
119-   , updateAt 
120-   , updateAtIndices 
121-   , zip 
113+   , delete 
114+   , deleteBy 
115+ 
116+   , (\\), difference 
117+   , intersect 
118+   , intersectBy 
119+ 
122120  , zipWith 
123121  , zipWithA 
122+   , zip 
123+   , unzip 
124+ 
125+   , any 
126+   , all 
127+ 
128+   , foldM 
129+   , foldRecM 
130+ 
131+   , unsafeIndex 
124132  ) where 
125133
126134import  Prelude 
@@ -147,7 +155,7 @@ toUnfoldable xs = unfoldr f 0
147155  where 
148156  len = length xs
149157  f i
150-     | i < len = Just  (Tuple  (unsafePartial (unsafeIndex xs i)) (i +  1 
158+     | i < len    = Just  (Tuple  (unsafePartial (unsafeIndex xs i)) (i+ 1 
151159    | otherwise = Nothing 
152160
153161--  | Convert a `Foldable` structure into an `Array`.
@@ -171,7 +179,7 @@ foreign import fromFoldableImpl
171179--  | singleton 2 = [2]
172180--  | ```
173181singleton  ::  forall  a . a  ->  Array  a 
174- singleton a = [ a  ]
182+ singleton a = [a ]
175183
176184--  | Create an array containing a range of integers, including both endpoints.
177185--  | ```purescript
@@ -244,7 +252,7 @@ foreign import length :: forall a. Array a -> Int
244252--  |
245253--  | Note, the running time of this function is `O(n)`.
246254cons  ::  forall  a . a  ->  Array  a  ->  Array  a 
247- cons x xs = [ x  ] <> xs
255+ cons x xs = [x ] <> xs
248256
249257--  | An infix alias for `cons`.
250258--  |
@@ -284,10 +292,8 @@ insert = insertBy compare
284292--  |
285293insertBy  ::  forall  a . (a  ->  a  ->  Ordering ) ->  a  ->  Array  a  ->  Array  a 
286294insertBy cmp x ys =
287-   let 
288-     i = maybe 0 1 ->  cmp x y == GT ) ys)
289-   in 
290-     unsafePartial (fromJust (insertAt i x ys))
295+   let  i = maybe 0 1 ->  cmp x y == GT ) ys)
296+   in  unsafePartial (fromJust (insertAt i x ys))
291297
292298-- ------------------------------------------------------------------------------
293299--  Non-indexed reads -----------------------------------------------------------
@@ -612,16 +618,15 @@ alterAt i f xs = maybe Nothing go (xs !! i)
612618--  | ```
613619intersperse  ::  forall  a . a  ->  Array  a  ->  Array  a 
614620intersperse a arr = case  length arr of 
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 
621-         ST .for 1 ->  do 
622-           _ <-  STA .push a out
623-           void (STA .push (unsafeGetElem idx) out)
624-         pure out
621+   len | len < 2 ->  arr
622+       | otherwise ->  STA .run do 
623+           let  unsafeGetElem idx = unsafePartial (unsafeIndex arr idx)
624+           out <-  STA .new
625+           _ <-  STA .push (unsafeGetElem 0 
626+           ST .for 1 ->  do 
627+             _ <-  STA .push a out
628+             void (STA .push (unsafeGetElem idx) out)
629+           pure out
625630
626631--  | Reverse an array, creating a new array.
627632--  |
@@ -704,7 +709,7 @@ splitAt i xs = { before: slice 0 i xs, after: slice i (length xs) xs }
704709filterA  ::  forall  a  f . Applicative  f  =>  (a  ->  f  Boolean ) ->  Array  a  ->  f  (Array  a )
705710filterA p =
706711  traverse (\x ->  Tuple  x <$> p x)
707-      >>> map (mapMaybe (\(Tuple  x b) ->  if  b then  Just  x else  Nothing ))
712+   >>> map (mapMaybe (\(Tuple  x b) ->  if  b then  Just  x else  Nothing ))
708713
709714--  | Apply a function to each element in an array, keeping only the results
710715--  | which contain a value, creating a new array.
@@ -1048,16 +1053,16 @@ nubBy :: forall a. (a -> a -> Ordering) -> Array a -> Array a
10481053nubBy comp xs = case  head indexedAndSorted of 
10491054  Nothing  ->  [] 
10501055  Just  x ->  map snd $ sortWith fst $ ST .run do 
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
1056+       --  TODO: use NonEmptyArrays here to avoid partial functions
1057+       result <-  STA .unsafeThaw $ singleton x
1058+       ST .foreach indexedAndSorted \pair@(Tuple  _ x') ->  do 
1059+         lst <-  snd <<< unsafePartial (fromJust <<< last) <$> STA .unsafeFreeze result
1060+         when (comp lst x' /= EQ ) $ void $ STA .push pair result
1061+       STA .unsafeFreeze result
10571062  where 
10581063  indexedAndSorted  ::  Array  (Tuple  Int  a )
10591064  indexedAndSorted = sortBy (\x y ->  comp (snd x) (snd y))
1060-     (mapWithIndex Tuple  xs)
1065+                              (mapWithIndex Tuple  xs)
10611066
10621067--  | Remove the duplicates from an array, where element equality is determined
10631068--  | by the specified equivalence relation, creating a new array.
@@ -1124,7 +1129,7 @@ delete = deleteBy eq
11241129--  | ```
11251130--  |
11261131deleteBy  ::  forall  a . (a  ->  a  ->  Boolean ) ->  a  ->  Array  a  ->  Array  a 
1127- deleteBy _ _ []  = [] 
1132+ deleteBy _   _ []  = [] 
11281133deleteBy eq x ys = maybe ys (\i ->  unsafePartial $ fromJust (deleteAt i ys)) (findIndex (eq x) ys)
11291134
11301135--  | Delete the first occurrence of each element in the second array from the
0 commit comments