Skip to content

Commit f4b1f24

Browse files
committed
Cleanups from PR review
1 parent a295889 commit f4b1f24

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

src/Data/Array/Destination.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ module Data.Array.Destination
123123
, mirror
124124
, fromFunction
125125
, fill
126+
, dropEmpty
126127
)
127128
where
128129

@@ -179,6 +180,14 @@ fill = Unsafe.toLinear2 unsafeFill
179180
else
180181
unsafeDupablePerformIO Prelude.$ MVector.write ds 0 a
181182

183+
-- | @dropEmpty dest@ consumes and empty array and fails otherwise.
184+
dropEmpty :: HasCallStack => DArray a %1-> ()
185+
dropEmpty = Unsafe.toLinear unsafeDrop where
186+
unsafeDrop :: DArray a -> ()
187+
unsafeDrop (DArray ds)
188+
| MVector.length ds > 0 = error "Destination.dropEmpty on non-empty array."
189+
| otherwise = ()
190+
182191
-- | @'split' n dest = (destl, destr)@ such as @destl@ has length @n@.
183192
--
184193
-- 'split' is total: if @n@ is larger than the length of @dest@, then

src/Data/Array/Polarized.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ module Data.Array.Polarized
102102
import qualified Data.Array.Polarized.Pull.Internal as Pull
103103
import qualified Data.Array.Polarized.Pull as Pull
104104
import qualified Data.Array.Polarized.Push as Push
105+
import qualified Data.Foldable as NonLinear
105106
import Prelude.Linear
106-
import qualified Prelude
107107
import Data.Vector (Vector)
108108

109109
-- DEVELOPER NOTE:
@@ -130,8 +130,7 @@ import Data.Vector (Vector)
130130
-- NOTE: this does NOT require allocation and can be in-lined.
131131
transfer :: Pull.Array a %1-> Push.Array a
132132
transfer (Pull.Array f n) =
133-
Push.Array
134-
(\k -> Prelude.foldl (\m i -> m <> (k (f i))) mempty [0..(n-1)])
133+
Push.Array (\k -> NonLinear.foldMap' (\x -> k (f x)) [0..(n-1)])
135134

136135
-- | This is a shortcut convenience function
137136
-- for @transfer . Pull.fromVector@.

src/Data/Array/Polarized/Push.hs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import qualified Data.Array.Destination as DArray
2222
import Data.Array.Destination (DArray)
2323
import Data.Vector (Vector)
2424
import qualified Prelude
25-
import qualified Unsafe.Linear as Unsafe
2625
import Prelude.Linear
2726
import GHC.Stack
2827

@@ -64,7 +63,7 @@ alloc (Array k) = allocArrayWriter $ k singletonWriter where
6463
-- element is @x@.
6564
make :: HasCallStack => a -> Int -> Array a
6665
make x n
67-
| n < 0 = error "Making negative length push array"
66+
| n < 0 = error "Making a negative length push array"
6867
| otherwise = Array (\makeA -> mconcat $ Prelude.replicate n (makeA x))
6968

7069

@@ -110,13 +109,8 @@ addWriters (ArrayWriter k1 l1) (ArrayWriter k2 l2) =
110109
(\darr ->
111110
(DArray.split l1 darr) & \(darr1,darr2) -> consume (k1 darr1, k2 darr2))
112111
(l1+l2)
113-
-- Remark. In order for the function above to work, consume must forcibly
114-
-- evaluate both tuples. If it was lazy, then we might not actually perform
115-
-- @k1@ or @k2@ and the unsafe IO won't get done. In general, this makes me
116-
-- think we haven't spelled out the careful rules of what consuming functions
117-
-- must follow so that we can release a safe API.
118112

119113
emptyWriter :: ArrayWriter a
120-
emptyWriter = ArrayWriter (Unsafe.toLinear (const ())) 0
114+
emptyWriter = ArrayWriter DArray.dropEmpty 0
121115
-- Remark. @emptyWriter@ assumes we can split a destination array at 0.
122116

0 commit comments

Comments
 (0)