Hi,
I would like to write a custom Ord instance for ordering by length instead of prefix characters.
class ShowLength a where
showLength :: a -> Int
instance ShowLength Text where
showLength = T.length
instance ShowLength a => ShowLength [a] where
showLength = sum . fmap showLength
instance (ShowLength a, ShowLength b, ShowLength b) => ShowLength (a,b,c) where
showLength = 1 + showLength a + 1 + showLength b + 1 + showLength c + 1
newtype OrdLen a = OrdLen a
instance ShowLength a => Ord (OrdLen a) where
compare (OrdLen a1) (OrdLen a2) = on compare showLength a1 a2
I wasn’t able to find ShowLength class anywhere.
I spot Data.Foldable.length
, but it is standalone function and there is no Foldable for Text, so there is no way to get efficient instance.