Skip to content

Commit 16795c7

Browse files
Remove pull array index (unsafe), add uncons and empty. (#475)
* Remove pull array index (unsafe), add uncons. * Add pull array `empty`, add tests
1 parent 4398151 commit 16795c7

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

src/Data/Array/Polarized.hs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@
5555
-- vecfilter vec f = Push.alloc (transfer (loop (Pull.fromVector vec) f))
5656
-- where
5757
-- loop :: Pull.Array a -> (a -> Bool) -> Pull.Array a
58-
-- loop arr f = case Pull.findLength arr of
59-
-- (0,_) -> Pull.fromFunction (error "empty") 0
60-
-- (n,_) -> case Pull.split 1 arr of
61-
-- (head, tail) -> case Pull.index head 0 of
62-
-- (a,_) ->
63-
-- if f a
64-
-- then Pull.append (Pull.singleton a) (loop tail f)
65-
-- else loop tail f
58+
-- loop arr f = case Pull.uncons arr of
59+
-- Nothing -> Pull.empty
60+
-- Just (a, as) ->
61+
-- if f a
62+
-- then Pull.append (Pull.singleton a) (loop as f)
63+
-- else loop as f
6664
-- @
6765
--
6866
--

src/Data/Array/Polarized/Pull.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module Data.Array.Polarized.Pull
1313
fromVector,
1414
make,
1515
singleton,
16+
empty,
1617

1718
-- * Consumption
1819
toVector,
@@ -27,7 +28,7 @@ module Data.Array.Polarized.Pull
2728
findLength,
2829
split,
2930
reverse,
30-
index,
31+
uncons,
3132
)
3233
where
3334

@@ -45,7 +46,7 @@ import Data.Array.Polarized.Pull.Internal
4546
import qualified Data.Functor.Linear as Data
4647
import Data.Vector (Vector)
4748
import qualified Data.Vector as Vector
48-
import Prelude.Linear hiding (foldMap, foldr, reverse, zip, zipWith)
49+
import Prelude.Linear hiding (foldMap, foldr, reverse, uncons, zip, zipWith)
4950
import qualified Unsafe.Linear as Unsafe
5051

5152
-- | Convert a pull array into a list.

src/Data/Array/Polarized/Pull/Internal.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ instance Data.Functor Array where
3737
-- is interesting in and of itself: I think this is like an n-ary With), and
3838
-- changing the other arrows makes no difference)
3939

40+
-- | Create an empty pull array
41+
empty :: Array a
42+
empty = fromFunction (\_ -> error "Data.Array.Polarized.Pull.Internal.empty: this should never be called") 0
43+
4044
-- | Produce a pull array of lenght 1 consisting of solely the given element.
4145
singleton :: a %1 -> Array a
4246
singleton = Unsafe.toLinear (\x -> fromFunction (\_ -> x) 1)
@@ -110,6 +114,7 @@ split k (Array f n) = (fromFunction f (min k n), fromFunction (\x -> f (x + k))
110114
reverse :: Array a %1 -> Array a
111115
reverse (Array f n) = Array (\x -> f (n + 1 - x)) n
112116

113-
-- | Index a pull array (without checking bounds)
114-
index :: Array a %1 -> Int -> (a, Array a)
115-
index (Array f n) ix = (f ix, Array f n)
117+
-- | Decompose an array into its head and tail, returns @Nothing@ if the array is empty.
118+
uncons :: Array a %1 -> Maybe (a, Array a)
119+
uncons (Array _ 0) = Nothing
120+
uncons (Array f n) = Just (f 0, fromFunction (\x -> f (x + 1)) (n - 1))

test/Test/Data/Polarized.hs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Test.Data.Polarized (polarizedArrayTests) where
66
import qualified Data.Array.Polarized as Polar
77
import qualified Data.Array.Polarized.Pull as Pull
88
import qualified Data.Array.Polarized.Push as Push
9+
import Data.Functor.Linear (fmap)
910
import qualified Data.Vector as Vector
1011
import Hedgehog
1112
import qualified Hedgehog.Gen as Gen
@@ -34,10 +35,12 @@ polarizedArrayTests =
3435
testPropertyNamed "Push.make ~ Vec.replicate" "pushMake" pushMake,
3536
testPropertyNamed "Pull.append ~ Vec.append" "pullAppend" pullAppend,
3637
testPropertyNamed "Pull.asList . Pull.fromVector ~ id" "pullAsList" pullAsList,
38+
testPropertyNamed "Pull.empty = []" "pullEmpty" pullEmpty,
3739
testPropertyNamed "Pull.singleton x = [x]" "pullSingleton" pullSingleton,
3840
testPropertyNamed "Pull.splitAt ~ splitAt" "pullSplitAt" pullSplitAt,
3941
testPropertyNamed "Pull.make ~ Vec.replicate" "pullMake" pullMake,
40-
testPropertyNamed "Pull.zip ~ zip" "pullZip" pullZip
42+
testPropertyNamed "Pull.zip ~ zip" "pullZip" pullZip,
43+
testPropertyNamed "Pull.uncons ~ uncons" "pullUncons" pullUncons
4144
]
4245

4346
list :: Gen [Int]
@@ -88,6 +91,10 @@ pullAsList = property Prelude.$ do
8891
xs <- forAll list
8992
xs === Pull.asList (Pull.fromVector (Vector.fromList xs))
9093

94+
pullEmpty :: Property
95+
pullEmpty = property Prelude.$ do
96+
([] :: [Int]) === Pull.asList Pull.empty
97+
9198
pullSingleton :: Property
9299
pullSingleton = property Prelude.$ do
93100
x <- forAll randInt
@@ -115,3 +122,8 @@ pullZip = property Prelude.$ do
115122
let xs' = Pull.fromVector (Vector.fromList xs)
116123
let ys' = Pull.fromVector (Vector.fromList ys)
117124
zip xs ys === Pull.asList (Pull.zip xs' ys')
125+
126+
pullUncons :: Property
127+
pullUncons = property Prelude.$ do
128+
xs <- forAll list
129+
uncons xs === fmap (fmap Pull.asList) (Pull.uncons (Pull.fromVector (Vector.fromList xs)))

0 commit comments

Comments
 (0)