From 73aa926bdc8a00b5000a678e63e09e573850bf37 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Wed, 9 Mar 2016 04:13:09 -0500 Subject: [PATCH] Generalize adjoinRowsBy to any Applicative with an aligning function. --- src/Alignment.hs | 6 +++--- test/AlignmentSpec.hs | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Alignment.hs b/src/Alignment.hs index 0649cd4b0..7281a76f6 100644 --- a/src/Alignment.hs +++ b/src/Alignment.hs @@ -115,6 +115,6 @@ openRange source range = (at source <$> maybeLastIndex range) /= Just '\n' type Row a = Both (Line a) -- | Merge open lines and prepend closed lines (as determined by a pair of functions) onto a list of rows. -adjoinRowsBy :: Both (a -> Bool) -> Both (Line a) -> [Both (Line a)] -> [Both (Line a)] -adjoinRowsBy _ row [] = [ row ] -adjoinRowsBy f row (nextRow : rows) = zipDefaults mempty (coalesceLinesBy <$> f <*> row <*> nextRow) ++ rows +adjoinRowsBy :: Applicative f => f (a -> Bool) -> (f [Line a] -> [f (Line a)]) -> f (Line a) -> [f (Line a)] -> [f (Line a)] +adjoinRowsBy _ _ row [] = [ row ] +adjoinRowsBy f align row (nextRow : rows) = align (coalesceLinesBy <$> f <*> row <*> nextRow) ++ rows diff --git a/test/AlignmentSpec.hs b/test/AlignmentSpec.hs index 43a54215e..f1c3fdff2 100644 --- a/test/AlignmentSpec.hs +++ b/test/AlignmentSpec.hs @@ -69,20 +69,20 @@ spec = parallel $ do describe "adjoinRowsBy" $ do prop "is identity on top of no rows" $ forAll (arbitrary `suchThat` (not . isEmptyRow)) $ - \ a -> adjoinRowsBy (pure Maybe.isJust) a [] `shouldBe` [ a :: Row (Maybe Bool) ] + \ a -> adjoinRowsBy (pure Maybe.isJust) (zipDefaults mempty) a [] `shouldBe` [ a :: Row (Maybe Bool) ] prop "prunes empty rows" $ - \ a -> adjoinRowsBy (pure Maybe.isJust) (both mempty mempty) [ a ] `shouldBe` [ a :: Row (Maybe Bool) ] + \ a -> adjoinRowsBy (pure Maybe.isJust) (zipDefaults mempty) (both mempty mempty) [ a ] `shouldBe` [ a :: Row (Maybe Bool) ] prop "merges open rows" $ forAll ((arbitrary `suchThat` (and . fmap (isOpenLineBy Maybe.isJust))) >>= \ a -> (,) a <$> arbitrary) $ - \ (a, b) -> adjoinRowsBy (pure Maybe.isJust) a [ b ] `shouldBe` [ mappend <$> a <*> b :: Row (Maybe Bool) ] + \ (a, b) -> adjoinRowsBy (pure Maybe.isJust) (zipDefaults mempty) a [ b ] `shouldBe` [ mappend <$> a <*> b :: Row (Maybe Bool) ] prop "prepends closed rows" $ - \ a -> adjoinRowsBy (pure Maybe.isJust) (both (pure Nothing) (pure Nothing)) [ both (pure a) (pure a) ] `shouldBe` [ (both (pure Nothing) (pure Nothing)), both (pure a) (pure a) :: Row (Maybe Bool) ] + \ a -> adjoinRowsBy (pure Maybe.isJust) (zipDefaults mempty) (both (pure Nothing) (pure Nothing)) [ both (pure a) (pure a) ] `shouldBe` [ (both (pure Nothing) (pure Nothing)), both (pure a) (pure a) :: Row (Maybe Bool) ] it "aligns closed lines" $ - foldr (adjoinRowsBy (pure (/= '\n'))) [] (Prelude.zipWith (both) (pure <$> "[ bar ]\nquux") (pure <$> "[\nbar\n]\nquux")) `shouldBe` + foldr (adjoinRowsBy (pure (/= '\n')) (zipDefaults mempty)) [] (Prelude.zipWith (both) (pure <$> "[ bar ]\nquux") (pure <$> "[\nbar\n]\nquux")) `shouldBe` [ both (Line "[ bar ]\n") (Line "[\n") , both mempty (Line "bar\n") , both mempty (Line "]\n")