Skip to content

Commit 7d98fc1

Browse files
committed
Add newtype Salign
1 parent 0a6cb8c commit 7d98fc1

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Data/Align.hs

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
module Data.Align (
99
Align(..)
1010
-- * Specialized aligns
11-
, malign, salign, padZip, padZipWith
11+
, malign, salign, Salign (..), padZip, padZipWith
1212
, lpadZip, lpadZipWith
1313
, rpadZip, rpadZipWith
1414
, alignVectorWith
@@ -285,6 +285,16 @@ malign = alignWith (mergeThese mappend)
285285
salign :: (Align f, Semigroup a) => f a -> f a -> f a
286286
salign = alignWith (mergeThese (<>))
287287

288+
-- | Monoid under 'salign' and 'nil'.
289+
newtype Salign f a = Salign (f a)
290+
291+
instance (Align f, Semigroup a) => Semigroup (Salign f a) where
292+
Salign x <> Salign y = Salign (salign x y)
293+
294+
instance (Align f, Semigroup a) => Monoid (Salign f a) where
295+
mappend = (<>)
296+
mempty = Salign nil
297+
288298
-- | Align two structures as in 'zip', but filling in blanks with 'Nothing'.
289299
padZip :: (Align f) => f a -> f b -> f (Maybe a, Maybe b)
290300
padZip = alignWith (fromThese Nothing Nothing . bimap Just Just)

test/Tests.hs

+7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
{-# LANGUAGE DeriveFoldable #-}
33
{-# LANGUAGE DeriveFunctor #-}
44
{-# LANGUAGE FlexibleContexts #-}
5+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
56
{-# LANGUAGE KindSignatures #-}
67
{-# LANGUAGE MonoLocalBinds #-}
78
{-# LANGUAGE ScopedTypeVariables #-}
9+
{-# LANGUAGE StandaloneDeriving #-}
810
{-# LANGUAGE TupleSections #-}
911
module Main (main) where
1012

@@ -63,6 +65,7 @@ tests = testGroup "Tests"
6365
[ semigroupLaws "These" (These "x" "y")
6466
, semigroupLaws "SearchResult" (ScannedAndFound "x" "y")
6567
, monoidLaws "List" "x" -- to disallow
68+
, monoidLaws "Salign" (Salign ["x"])
6669
]
6770
]
6871

@@ -444,3 +447,7 @@ instance Monoid a => Monoid (SearchResult a b) where
444447
mappend = (<>)
445448
mempty = Scanned mempty
446449
-}
450+
451+
deriving instance Eq (f a) => Eq (Salign f a)
452+
deriving instance Show (f a) => Show (Salign f a)
453+
deriving instance Arbitrary (f a) => Arbitrary (Salign f a)

0 commit comments

Comments
 (0)