Simplified adornSuffix; no "Maybe" indirection.
parent
42bc20ae07
commit
c6c5eae311
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue