Skip to content

Commit 58f3597

Browse files
committed
Simplify zipWith3 and zipWith4 to reduce code size
The performance impact isn't worth the code blowup. Also, fix a bug in `fromFunction`.
1 parent 41cc152 commit 58f3597

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

Data/Sequence.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ fromFunction len f | len < 0 = error "Data.Sequence.fromFunction called with neg
13821382
3 -> Deep (3*s) (Two (b i) (b (i+s))) Empty (One (b (i+2*s)))
13831383
4 -> Deep (4*s) (Two (b i) (b (i+s))) Empty (Two (b (i+2*s)) (b (i+3*s)))
13841384
5 -> Deep (5*s) (Three (b i) (b (i+s)) (b (i+2*s))) Empty (Two (b (i+3*s)) (b (i+4*s)))
1385-
6 -> Deep (5*s) (Three (b i) (b (i+s)) (b (i+2*s))) Empty (Three (b (i+3*s)) (b (i+4*s)) (b (i+5*s)))
1385+
6 -> Deep (6*s) (Three (b i) (b (i+s)) (b (i+2*s))) Empty (Three (b (i+3*s)) (b (i+4*s)) (b (i+5*s)))
13861386
_ -> case trees `quotRem` 3 of
13871387
(trees',1) -> Deep (trees*s) (Two (b i) (b (i+s)))
13881388
(create (\j -> Node3 (3*s) (b j) (b (j+s)) (b (j+2*s))) (3*s) (i+2*s) (trees'-1))
@@ -1937,12 +1937,16 @@ zip = zipWith (,)
19371937
-- For example, @zipWith (+)@ is applied to two sequences to take the
19381938
-- sequence of corresponding sums.
19391939
zipWith :: (a -> b -> c) -> Seq a -> Seq b -> Seq c
1940-
zipWith f s1 s2 = splitMap splitAt' (\s a -> f a (getSingleton s)) s2' s1'
1940+
zipWith f s1 s2 = zipWith' f s1' s2'
19411941
where
19421942
minLen = min (length s1) (length s2)
19431943
s1' = take minLen s1
19441944
s2' = take minLen s2
19451945

1946+
-- | A version of zipWith that assumes the sequences have the same length.
1947+
zipWith' :: (a -> b -> c) -> Seq a -> Seq b -> Seq c
1948+
zipWith' f s1 s2 = splitMap splitAt' (\s a -> f a (getSingleton s)) s2 s1
1949+
19461950
-- | /O(min(n1,n2,n3))/. 'zip3' takes three sequences and returns a
19471951
-- sequence of triples, analogous to 'zip'.
19481952
zip3 :: Seq a -> Seq b -> Seq c -> Seq (a,b,c)
@@ -1952,14 +1956,16 @@ zip3 = zipWith3 (,,)
19521956
-- three elements, as well as three sequences and returns a sequence of
19531957
-- their point-wise combinations, analogous to 'zipWith'.
19541958
zipWith3 :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d
1955-
zipWith3 f s1 s2 s3 = splitMap (\i (s,t) -> case (splitAt' i s, splitAt' i t) of ((s', s''), (t', t'')) -> ((s',t'),(s'',t'')))
1956-
(\(b,c) a -> f a (getSingleton b) (getSingleton c)) (s2',s3') s1'
1959+
zipWith3 f s1 s2 s3 = zipWith' ($) (zipWith' f s1' s2') s3'
19571960
where
19581961
minLen = minimum [length s1, length s2, length s3]
19591962
s1' = take minLen s1
19601963
s2' = take minLen s2
19611964
s3' = take minLen s3
19621965

1966+
zipWith3' :: (a -> b -> c -> d) -> Seq a -> Seq b -> Seq c -> Seq d
1967+
zipWith3' f s1 s2 s3 = zipWith' ($) (zipWith' f s1 s2) s3
1968+
19631969
-- | /O(min(n1,n2,n3,n4))/. 'zip4' takes four sequences and returns a
19641970
-- sequence of quadruples, analogous to 'zip'.
19651971
zip4 :: Seq a -> Seq b -> Seq c -> Seq d -> Seq (a,b,c,d)
@@ -1969,8 +1975,7 @@ zip4 = zipWith4 (,,,)
19691975
-- four elements, as well as four sequences and returns a sequence of
19701976
-- their point-wise combinations, analogous to 'zipWith'.
19711977
zipWith4 :: (a -> b -> c -> d -> e) -> Seq a -> Seq b -> Seq c -> Seq d -> Seq e
1972-
zipWith4 f s1 s2 s3 s4 = splitMap (\i (s,t,u) -> case (splitAt' i s, splitAt' i t, splitAt' i u) of ((s',s''),(t',t''),(u',u'')) -> ((s',t',u'),(s'',t'',u'')))
1973-
(\(b, c, d) a -> f a (getSingleton b) (getSingleton c) (getSingleton d)) (s2',s3',s4') s1'
1978+
zipWith4 f s1 s2 s3 s4 = zipWith' ($) (zipWith3' f s1' s2' s3') s4'
19741979
where
19751980
minLen = minimum [length s1, length s2, length s3, length s4]
19761981
s1' = take minLen s1

0 commit comments

Comments
 (0)