<commit_before>{-# LANGUAGE BangPatterns #-}
module MathOp(
        plus, 
        minus,
        times,
        divide,
        equals,
        notEquals,
        lessThan,
        lessThanEq,
        greaterThan,
        greaterThanEq, pow, squarert, absfun) where

import qualified TObj as T

data NPair = Ints !Int !Int | Doubles !Double !Double deriving (Eq,Show)

-- TODO: Inline getNumerics manually and get rid of NPair?
getNumerics :: (T.ITObj t) => t -> t -> Maybe NPair
getNumerics !x !y =
   case (T.asInt x, T.asInt y) of
       (Just i1, Just i2) -> return $! Ints i1 i2
       _ -> case (T.asDouble x, T.asDouble y) of
               (Just d1, Just d2) -> return $! Doubles d1 d2
               _ -> fail $ "expected numeric"
{-# INLINE getNumerics #-}

numop name iop dop !x !y = 
   case getNumerics x y of
       Just (Ints i1 i2)  -> return $! (T.fromInt (i1 `iop` i2))
       Just (Doubles d1 d2) -> return $! T.fromDouble (d1 `dop` d2)
       _ -> fail $ "can't use non-numeric string as operand of " ++ show name
{-# INLINE numop #-}

plus, minus, times, divide :: (Monad m, T.ITObj t) => t -> t -> m t
plus = numop "+" (+) (+) 
minus = numop "-" (-) (-)
times = numop "*" (*) (*)
divide = numop "/" div (/)
{-# INLINE plus #-}
{-# INLINE minus #-}

squarert x = do
    case T.asInt x of
      Just i -> return $! T.fromDouble (sqrt (fromIntegral i))
      Nothing -> do
        d1 <- T.asDouble x
	return $! T.fromDouble (sqrt d1)
<commit_msg>Generalized types in Mathops a bit, removed getNumerics<commit_after>{-# LANGUAGE BangPatterns #-}
module MathOp(
        plus, 
        minus,
        times,
        divide,
        equals,
        notEquals,
        lessThan,
        lessThanEq,
        greaterThan,
        greaterThanEq, pow, squarert, absfun) where

import qualified TObj as T

numop name iop dop !x !y = 
   case (T.asInt x, T.asInt y) of
       (Just i1, Just i2) -> return $! T.fromInt (i1 `iop` i2)
       _ -> case (T.asDouble x, T.asDouble y) of
             (Just d1, Just d2) -> return $! T.fromDouble (d1 `dop` d2)
             _ -> fail $ "can't use non-numeric string as operand of " ++ show name
{-# INLINE numop #-}

plus, minus, times, divide :: (Monad m, T.ITObj t, T.ITObj t2, T.ITObj t3) => t -> t2 -> m t3
plus = numop "+" (+) (+) 
minus = numop "-" (-) (-)
times = numop "*" (*) (*)
divide = numop "/" div (/)
{-# INLINE plus #-}
{-# INLINE minus #-}

squarert x = do
    case T.asInt x of
      Just i -> return $! T.fromDouble (sqrt (fromIntegral i))
      Nothing -> do
        d1 <- T.asDouble x
	return $! T.fromDouble (sqrt d1)

{'fraction_non_alphanumeric': 0.14704747201852567, 'fraction_numerical': 0.012350443844075647, 'mean_word_length': 2.1116446578631454, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '6354651', 'n_tokens_mistral': 970, 'n_tokens_neox': 941, 'n_words': 306}