packages feed

lhae-0.0.3: src/Model/CellExpression/Evaluator/Common.hs

module Model.CellExpression.Evaluator.Common
    (fLength,withNum,withString,withList,withNumList,getNumList
    ,withBool)
        where

import Model.CellContent (CellValue (..),RuntimeReason(..))

fLength :: Num b => [a] -> b
fLength = fromIntegral . length

withNum :: CellValue -> (Double -> CellValue) -> CellValue
withNum value f = 
    case value of NumberValue a -> f a
                  _ -> Error $ TypeError value

withString :: CellValue -> (String -> CellValue) -> CellValue
withString value f = 
    case value of StringValue a -> f a
                  _ -> Error $ TypeError value

withList :: CellValue -> ([CellValue] -> CellValue) -> CellValue
withList value f =
    case value of ListValue a -> f a
                  _ -> Error $ TypeError value

withNumList :: CellValue -> ([Double] -> CellValue) -> CellValue
withNumList value f =
    case value of
      ListValue a -> either (\_ -> Error $ TypeError value) f $ getNumList a
      _ -> Error $ TypeError value
    
getNumList :: [CellValue] -> Either RuntimeReason [Double]
getNumList =
    foldr (\value accu -> 
               case (value,accu) of
                 (NumberValue n,Right list) -> Right $ n:list
                 (_,Right _) -> Left $ TypeError value
                 (_,Left _) -> accu
          ) (Right [])

withBool :: CellValue -> (Bool -> CellValue) -> CellValue
withBool value f = 
    case value of BoolValue a -> f a
                  _ -> Error $ TypeError value