From 068ce7a41c24d2d31609c161e9ade1c4deb60bc2 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Sun, 15 Oct 2017 02:37:37 +1100 Subject: [PATCH] Slightly more efficient implementations of Model.Entry.Shorten --- src/Model/Entry.hs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/Model/Entry.hs b/src/Model/Entry.hs index 97d64ff..f2a4b59 100644 --- a/src/Model/Entry.hs +++ b/src/Model/Entry.hs @@ -9,20 +9,22 @@ import qualified Data.Text as T import qualified Data.Text.Lazy as TL entryTitle :: Entry -> T.Text -entryTitle = fromMaybe <$> TL.toStrict . shorten 30 . unMarkdown . entryContent <*> entryName +entryTitle = fromMaybe <$> TL.toStrict . unMarkdown . shorten 30 . entryContent <*> entryName class Shorten a where - shorten :: Integral n => n -> a -> a + shorten :: Int -> a -> a instance Shorten T.Text where - shorten n t - | T.length t > fromIntegral n = flip T.append "..." . T.take (fromIntegral n - 1) $ t - | otherwise = t + shorten i t + | T.compareLength t n == GT = flip T.append "..." . T.take (n - 1) $ t + | otherwise = t + where n = fromIntegral i instance Shorten TL.Text where - shorten n t - | TL.length t > fromIntegral n = flip TL.append "..." . TL.take (fromIntegral n - 1) $ t - | otherwise = t + shorten i t + | TL.compareLength t n == GT = flip TL.append "..." . TL.take (n - 1) $ t + | otherwise = t + where n = fromIntegral i instance Shorten Markdown where shorten n (Markdown t) = Markdown $ shorten n t