From c6c5eae31123ee1b30c26142334907ced6ff4569 Mon Sep 17 00:00:00 2001 From: vi Date: Sat, 23 Aug 2014 23:14:40 +0800 Subject: [PATCH] Simplified adornSuffix; no "Maybe" indirection. --- src/Data/Text/ICU/Extras.hs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/src/Data/Text/ICU/Extras.hs b/src/Data/Text/ICU/Extras.hs index 81e920c..3539177 100644 --- a/src/Data/Text/ICU/Extras.hs +++ b/src/Data/Text/ICU/Extras.hs @@ -13,9 +13,8 @@ module Data.Text.ICU.Extras ( ) where import Prelude hiding (span) -import Control.Applicative ((<$>), (*>), (<|>)) +import Control.Applicative ((<$>), (<*>), (*>), (<|>)) import Control.Error (hush) -import Control.Monad (ap) import Data.Attoparsec.Text (Parser, char, digit, takeWhile1, string, many', parseOnly) import Data.Functor.Infix ((<$$>), (<&>)) import Data.Maybe (isJust, fromJust) @@ -53,16 +52,12 @@ parseReplacement :: Text -> Maybe Replacement parseReplacement = hush . parseOnly (many' parseSegment) runReplacement :: [Match] -> Replacement -> Maybe Text -runReplacement matches replacement = mconcat <$$> invert . adornSuffix (safeLast matches) $ do +runReplacement matches replacement = mconcat <$$> invert . adornSuffix matches $ do match <- matches Just (span match) : map (dereference $ flip group match) replacement -safeLast :: [a] -> Maybe a -safeLast = \case { [] -> Nothing; xs -> Just $ last xs } - -adornSuffix :: Maybe Match -> ([Maybe Text] -> [Maybe Text]) -adornSuffix Nothing = id -adornSuffix (Just match) = (++ [ap (flip suffix) groupCount match]) +adornSuffix :: [Match] -> ([Maybe Text] -> [Maybe Text]) +adornSuffix = \case {[] -> id; ms -> (++ [flip suffix <*> groupCount $ last ms])} dereference :: (Int -> Maybe Text) -> Segment -> Maybe Text dereference group = \case