Simplified adornSuffix; no "Maybe" indirection.
parent
42bc20ae07
commit
c6c5eae311
|
@ -13,9 +13,8 @@ module Data.Text.ICU.Extras (
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Prelude hiding (span)
|
import Prelude hiding (span)
|
||||||
import Control.Applicative ((<$>), (*>), (<|>))
|
import Control.Applicative ((<$>), (<*>), (*>), (<|>))
|
||||||
import Control.Error (hush)
|
import Control.Error (hush)
|
||||||
import Control.Monad (ap)
|
|
||||||
import Data.Attoparsec.Text (Parser, char, digit, takeWhile1, string, many', parseOnly)
|
import Data.Attoparsec.Text (Parser, char, digit, takeWhile1, string, many', parseOnly)
|
||||||
import Data.Functor.Infix ((<$$>), (<&>))
|
import Data.Functor.Infix ((<$$>), (<&>))
|
||||||
import Data.Maybe (isJust, fromJust)
|
import Data.Maybe (isJust, fromJust)
|
||||||
|
@ -53,16 +52,12 @@ parseReplacement :: Text -> Maybe Replacement
|
||||||
parseReplacement = hush . parseOnly (many' parseSegment)
|
parseReplacement = hush . parseOnly (many' parseSegment)
|
||||||
|
|
||||||
runReplacement :: [Match] -> Replacement -> Maybe Text
|
runReplacement :: [Match] -> Replacement -> Maybe Text
|
||||||
runReplacement matches replacement = mconcat <$$> invert . adornSuffix (safeLast matches) $ do
|
runReplacement matches replacement = mconcat <$$> invert . adornSuffix matches $ do
|
||||||
match <- matches
|
match <- matches
|
||||||
Just (span match) : map (dereference $ flip group match) replacement
|
Just (span match) : map (dereference $ flip group match) replacement
|
||||||
|
|
||||||
safeLast :: [a] -> Maybe a
|
adornSuffix :: [Match] -> ([Maybe Text] -> [Maybe Text])
|
||||||
safeLast = \case { [] -> Nothing; xs -> Just $ last xs }
|
adornSuffix = \case {[] -> id; ms -> (++ [flip suffix <*> groupCount $ last ms])}
|
||||||
|
|
||||||
adornSuffix :: Maybe Match -> ([Maybe Text] -> [Maybe Text])
|
|
||||||
adornSuffix Nothing = id
|
|
||||||
adornSuffix (Just match) = (++ [ap (flip suffix) groupCount match])
|
|
||||||
|
|
||||||
dereference :: (Int -> Maybe Text) -> Segment -> Maybe Text
|
dereference :: (Int -> Maybe Text) -> Segment -> Maybe Text
|
||||||
dereference group = \case
|
dereference group = \case
|
||||||
|
|
Loading…
Reference in New Issue