Commits

Tobias Dammers committed 3ceaa17

Added Function type to Variant

  • Participants
  • Parent commits abc9346

Comments (0)

Files changed (1)

File Data/Variant.hs

+{-# LANGUAGE FlexibleInstances #-}
 module Data.Variant
         ( Variant (..)
         , flatten
         , keys
         , values
         , vmap
+        , wrapf, wrapfs
+        , wrapf1, wrapfs1
+        , call, callMaybe, callDef
         )
 where
 
              | Bool Bool
              | List [Variant]
              | AList [(Variant, Variant)]
+             | Function ([Variant] -> Variant)
              deriving (Show, Eq)
 
+instance Show ([Variant] -> Variant)
+    where show _ = "<<function>>"
+
+instance Eq ([Variant] -> Variant)
+    where (==) a b = False
+
 flatten :: Variant -> String
 flatten (String s) = s
 flatten (Integer i) = show i
 
 vmap :: (Variant -> a) -> Variant -> [a]
 vmap f v = map f $ values v
+
+wrapfs :: (Variant -> [Variant] -> Variant) -> Variant -> Variant
+wrapfs f s = Function $ f s
+
+wrapf :: ([Variant] -> Variant) -> Variant
+wrapf = Function
+
+wrapfs1 :: (Variant -> Variant -> Variant) -> Variant -> Variant
+wrapfs1 f s = wrapf (\(a:_) -> f s a)
+
+wrapf1 :: (Variant -> Variant) -> Variant
+wrapf1 f = wrapf (\(a:_) -> f a)
+
+callMaybe :: Variant -> [Variant] -> Maybe Variant
+callMaybe (Function f) args = Just $ f args
+callMaybe _ _ = Nothing
+
+callDef :: Variant -> [Variant] -> Variant -> Variant
+callDef f args def = fromMaybe def $ callMaybe f args
+
+call :: Variant -> [Variant] -> Variant
+call f args = callDef f args Null