<commit_before>{-# LANGUAGE DeriveFunctor #-}
module Text.Regex.Applicative.Priorities
    ( Priority
    , PrNum
    , withPriority
    , priorityToMaybe
    , isOK
    ) where
import Control.Applicative
import qualified Data.Sequence as Sequence

-- | An applicative functor similar to Maybe, but it's '<|>' method honors
-- priority.
data Priority a = Priority { priority :: !PrSeq, pValue :: a } | Fail
    deriving (Functor, Show)
type PrSeq = Sequence.Seq PrNum
type PrNum = Int

instance Applicative Priority where
    pure x = Priority Sequence.empty x
    Priority p1 f <*> Priority p2 x = Priority (p1 Sequence.>< p2) (f x)
    _ <*> _ = Fail

instance Alternative Priority where
    empty = Fail
    p@Priority {} <|> Fail = p
    Fail <|> p@Priority {} = p
    Fail <|> Fail = Fail
    p1@Priority {} <|> p2@Priority {} = {-# SCC "compare_priorities" #-}
        case compare (priority p1) (priority p2) of
            LT -> p2
            GT -> p1
            EQ -> error $
                "Two priorities are the same! Should not happen.\n" ++ show (priority p1)

-- | Adds priority to the end
withPriority :: PrNum -> Priority a -> Priority a
withPriority p (Priority ps x) = Priority (ps Sequence.|> p) x
withPriority _ Fail = Fail

-- | Discards priority information
priorityToMaybe :: Priority a -> Maybe a
priorityToMaybe p =
    case p of
        Priority { pValue = x } -> Just x
        Fail -> Nothing

isOK p =
    case p of
        Fail -> False
        Priority {} -> True<commit_msg>Use lists instead of sequences<commit_after>{-# LANGUAGE DeriveFunctor #-}
module Text.Regex.Applicative.Priorities
    ( Priority
    , PrNum
    , withPriority
    , priorityToMaybe
    , isOK
    ) where
import Control.Applicative

-- | An applicative functor similar to Maybe, but it's '<|>' method honors
-- priority.
data Priority a = Priority { priority :: PrSeq, pValue :: a } | Fail
    deriving (Functor, Show)
type PrSeq = [PrNum]
type PrNum = Int

instance Applicative Priority where
    pure x = Priority [] x
    Priority p1 f <*> Priority p2 x = Priority ({-# SCC "concat_priorities" #-} p1 ++ p2) (f x)
    _ <*> _ = Fail

instance Alternative Priority where
    empty = Fail
    p@Priority {} <|> Fail = p
    Fail <|> p@Priority {} = p
    Fail <|> Fail = Fail
    p1@Priority {} <|> p2@Priority {} = {-# SCC "compare_priorities" #-}
        case compare (priority p1) (priority p2) of
            LT -> p2
            GT -> p1
            EQ -> error $
                "Two priorities are the same! Should not happen.\n" ++ show (priority p1)

-- | Adds priority to the end
withPriority :: PrNum -> Priority a -> Priority a
withPriority p (Priority ps x) = Priority ({-# SCC "snoc_priorities" #-}ps ++ [p]) x
withPriority _ Fail = Fail

-- | Discards priority information
priorityToMaybe :: Priority a -> Maybe a
priorityToMaybe p =
    case p of
        Priority { pValue = x } -> Just x
        Fail -> Nothing

isOK p =
    case p of
        Fail -> False
        Priority {} -> True
{'fraction_non_alphanumeric': 0.11343283582089553, 'fraction_numerical': 0.007296849087893864, 'mean_word_length': 2.462686567164179, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5546443', 'n_tokens_mistral': 963, 'n_tokens_neox': 906, 'n_words': 330}