I’ve wanted this before as well. Maybe we should throw a newtype at it?
newtype LeftBiased a = LeftBiased [a]
instance Alternative (LeftBiased a) where
empty = []
[] <|> b = b
a <|> _ = a
newtype RightBiased a = RightBiased [a]
instance Alternative (RightBiased a) where
empty = []
a <|> [] = a
_ <|> b = b
This could be generalised to work on any Foldable, actually, if that’s desirable. And of course the bikeshed could be a different colour as for the names.
It’s unfortunate that the instance for Maybe is already biased differently; I wonder if this instance would be useful, or if it’s already available somewhere?
newtype Unbiased a = Unbiased (Maybe a)
instance (Monoid m) => Alternative (Unbiased m) where
empty = Nothing
Just a <|> Just b = Just (a <> b)
_ <|> Just b = Just b
Just a <|> _ = Just a
_ <|> _ = Nothing