Simplified adornSuffix; no "Maybe" indirection.

master
vi 2014-08-23 23:14:40 +08:00
parent 42bc20ae07
commit c6c5eae311
1 changed files with 4 additions and 9 deletions

View File

@ -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