{-#LANGUAGE FlexibleContexts #-}
{-#LANGUAGE FlexibleInstances #-}
{-#LANGUAGE OverloadedStrings #-}
{-#LANGUAGE TupleSections #-}
{-#LANGUAGE TypeSynonymInstances #-}
{-#LANGUAGE MultiParamTypeClasses #-}
{-#LANGUAGE ScopedTypeVariables #-}
{-#LANGUAGE LambdaCase #-}

-- | Implementations of the default template context functions and values. You
-- will not normally need to import this module, unless you want to provide
-- your own context with things that reuse functionality from the default
-- ones.
module Text.Ginger.Run.Builtins
where

import Prelude ( (.), ($), (==), (/=)
               , (>), (<), (>=), (<=)
               , (+), (-), (*), (/), div, (**), (^)
               , (||), (&&)
               , (++)
               , Show, show
               , undefined, otherwise
               , Maybe (..)
               , Bool (..)
               , Int, Integer, String
               , fromIntegral, floor, round
               , not
               , show
               , uncurry
               , seq
               , fst, snd
               , maybe
               , Either (..)
               , id
               , flip
               , const
               , either
               )
import qualified Prelude
import Control.Monad.Error (runErrorT)
import Data.Maybe (fromMaybe, isJust, isNothing)
import qualified Data.List as List
import Text.Ginger.AST
import Text.Ginger.Html
import Text.Ginger.GVal
import Text.Ginger.Run.Type
import Text.Ginger.Run.FuncUtils
import Text.Ginger.Run.VM
import Text.Printf
import Text.PrintfA

import Data.Text (Text)
import Data.String (fromString)
import qualified Data.Text as Text
import qualified Data.ByteString.UTF8 as UTF8
import qualified Data.ByteString.Lazy as LBS
import Control.Monad
import Control.Monad.Identity
import Control.Monad.Writer
import Control.Monad.Reader
import Control.Monad.State
import Control.Monad.Except (throwError, MonadError)
import Control.Applicative
import qualified Data.HashMap.Strict as HashMap
import Data.HashMap.Strict (HashMap)
import Data.Scientific (Scientific, formatScientific, FPFormat (Fixed) )
import qualified Data.Scientific as Scientific
import Data.Default (def)
import Safe (readMay, lastDef, headMay)
import Network.HTTP.Types (urlEncode)
import Debug.Trace (trace)
import Data.List (lookup, zipWith, unzip, foldl')
import Data.Time ( defaultTimeLocale
                 , formatTime
                 , LocalTime (..)
                 , ZonedTime (..)
                 , utc
                 , utcToZonedTime
                 , zonedTimeToUTC
                 , TimeOfDay (..)
                 , fromGregorian
                 , Day (..)
                 , parseTimeM
                 , TimeLocale (..)
                 , TimeZone (..)
                 )
import Data.Foldable (asum, toList)
import qualified Data.Aeson as JSON
import qualified Data.Aeson.Encode.Pretty as JSON
import qualified Text.Regex.TDFA as RE

tshow :: Show a => a -> Text
tshow :: a -> Text
tshow = String -> Text
Text.pack (String -> Text) -> (a -> String) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show

gfnRawHtml :: Monad m => Function (Run p m h)
gfnRawHtml :: Function (Run p m h)
gfnRawHtml = (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall (m :: * -> *) h p.
Monad m =>
(GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
unaryFunc (Html -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Html -> GVal (Run p m h))
-> (GVal (Run p m h) -> Html)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Html
unsafeRawHtml (Text -> Html)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText)

gfnUrlEncode :: Monad m => Function (Run p m h)
gfnUrlEncode :: Function (Run p m h)
gfnUrlEncode =
    (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall (m :: * -> *) h p.
Monad m =>
(GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
unaryFunc
        ( Text -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal
        (Text -> GVal (Run p m h))
-> (GVal (Run p m h) -> Text)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack
        (String -> Text)
-> (GVal (Run p m h) -> String) -> GVal (Run p m h) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
UTF8.toString
        (ByteString -> String)
-> (GVal (Run p m h) -> ByteString) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> ByteString -> ByteString
urlEncode Bool
True
        (ByteString -> ByteString)
-> (GVal (Run p m h) -> ByteString)
-> GVal (Run p m h)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> ByteString
UTF8.fromString
        (String -> ByteString)
-> (GVal (Run p m h) -> String) -> GVal (Run p m h) -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack
        (Text -> String)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText
        )

gfnDefault :: Monad m => Function m
gfnDefault :: Function m
gfnDefault [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def
gfnDefault ((Maybe Text
_, GVal m
x):[(Maybe Text, GVal m)]
xs)
    | GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal m
x = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
x
    | Bool
otherwise = Function m
forall (m :: * -> *). Monad m => Function m
gfnDefault [(Maybe Text, GVal m)]
xs

gfnEscape :: Monad m => Function m
gfnEscape :: Function m
gfnEscape = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m))
-> ([(Maybe Text, GVal m)] -> GVal m) -> Function m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Html -> GVal m)
-> ([(Maybe Text, GVal m)] -> Html)
-> [(Maybe Text, GVal m)]
-> GVal m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Html
html (Text -> Html)
-> ([(Maybe Text, GVal m)] -> Text)
-> [(Maybe Text, GVal m)]
-> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> Text
forall a. Monoid a => [a] -> a
mconcat ([Text] -> Text)
-> ([(Maybe Text, GVal m)] -> [Text])
-> [(Maybe Text, GVal m)]
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Text, GVal m) -> Text) -> [(Maybe Text, GVal m)] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal m -> Text)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd)

-- Check if all arguments are HTML-\"escaped\" (sic!) NOTE that this doesn't
-- really make much sense considering the way Ginger works - every GVal is, by
-- construction, both escaped in its 'asGVal' representation and not escaped in
-- its 'asText' representation, at the same time.
-- So what we check instead is whether the current 'asHtml'representation of
-- the value is exactly the same as what explicitly HTML-encoding the 'asText'
-- representation would give. In other words, we check whether the HTML
-- representations of @{{ v }}@ and @{{ v|escape }}@ match.
gfnEscaped :: Monad m => Function m
gfnEscaped :: Function m
gfnEscaped = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m))
-> ([(Maybe Text, GVal m)] -> GVal m) -> Function m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> GVal m)
-> ([(Maybe Text, GVal m)] -> Bool)
-> [(Maybe Text, GVal m)]
-> GVal m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Text, GVal m) -> Bool) -> [(Maybe Text, GVal m)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
List.all (GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
isEscaped (GVal m -> Bool)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd)
  where
    isEscaped :: GVal m -> Bool
isEscaped GVal m
v =
      (GVal Any -> Html
forall (m :: * -> *). GVal m -> Html
asHtml (GVal Any -> Html) -> (GVal m -> GVal Any) -> GVal m -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Html -> GVal Any
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Html -> GVal Any) -> (GVal m -> Html) -> GVal m -> GVal Any
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Html
html (Text -> Html) -> (GVal m -> Text) -> GVal m -> Html
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal m -> Html) -> GVal m -> Html
forall a b. (a -> b) -> a -> b
$ GVal m
v) Html -> Html -> Bool
forall a. Eq a => a -> a -> Bool
==
      (GVal m -> Html
forall (m :: * -> *). GVal m -> Html
asHtml GVal m
v)

gfnAny :: Monad m => Function m
gfnAny :: Function m
gfnAny [(Maybe Text, GVal m)]
xs = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ ((Maybe Text, GVal m) -> Bool) -> [(Maybe Text, GVal m)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.any (GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean (GVal m -> Bool)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal m)]
xs

gfnAll :: Monad m => Function m
gfnAll :: Function m
gfnAll [(Maybe Text, GVal m)]
xs = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ ((Maybe Text, GVal m) -> Bool) -> [(Maybe Text, GVal m)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean (GVal m -> Bool)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal m)]
xs

gfnIn :: Monad m => Function m
gfnIn :: Function m
gfnIn [] =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def
gfnIn [(Maybe Text, GVal m)
needle] =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
False
gfnIn [(Maybe Text
_, GVal m
needle),(Maybe Text
_, GVal m
haystack)] =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
inDict GVal m
needle GVal m
haystack Bool -> Bool -> Bool
|| GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
inList GVal m
needle GVal m
haystack

inList :: GVal m -> GVal m -> Bool
inList :: GVal m -> GVal m -> Bool
inList GVal m
needle GVal m
haystack =
  Bool -> ([GVal m] -> Bool) -> Maybe [GVal m] -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
    Bool
False
    ((GVal m -> Bool) -> [GVal m] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
List.any (GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
looseEquals GVal m
needle))
    (GVal m -> Maybe [GVal m]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal m
haystack)

inDict :: GVal m -> GVal m -> Bool
inDict :: GVal m -> GVal m -> Bool
inDict GVal m
needle GVal m
haystack =
  Maybe (GVal m) -> Bool
forall a. Maybe a -> Bool
isJust (Maybe (GVal m) -> Bool) -> Maybe (GVal m) -> Bool
forall a b. (a -> b) -> a -> b
$ Text -> GVal m -> Maybe (GVal m)
forall (m :: * -> *). Text -> GVal m -> Maybe (GVal m)
lookupKey (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
needle) GVal m
haystack

gfnApply :: Monad m => Function (Run p m h)
gfnApply :: Function (Run p m h)
gfnApply [] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
forall a. Default a => a
def
gfnApply [(Maybe Text, GVal (Run p m h))
_] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
forall a. Default a => a
def
gfnApply ((Maybe Text
_, GVal (Run p m h)
fg):(Maybe Text
_,GVal (Run p m h)
a):[(Maybe Text, GVal (Run p m h))]
xs) =
  case GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
fg of
    Maybe (Function (Run p m h))
Nothing -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"apply") Text
"Tried to call something that isn't a function"
    Just Function (Run p m h)
f ->
      let args :: [(Maybe Text, GVal (Run p m h))]
args = ((GVal (Run p m h) -> (Maybe Text, GVal (Run p m h)))
-> [GVal (Run p m h)] -> [(Maybe Text, GVal (Run p m h))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text
forall a. Maybe a
Nothing,) ([GVal (Run p m h)] -> [(Maybe Text, GVal (Run p m h))])
-> (GVal (Run p m h) -> [GVal (Run p m h)])
-> GVal (Run p m h)
-> [(Maybe Text, GVal (Run p m h))]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [GVal (Run p m h)]
-> Maybe [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [GVal (Run p m h)] -> [GVal (Run p m h)])
-> (GVal (Run p m h) -> Maybe [GVal (Run p m h)])
-> GVal (Run p m h)
-> [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList (GVal (Run p m h) -> [(Maybe Text, GVal (Run p m h))])
-> GVal (Run p m h) -> [(Maybe Text, GVal (Run p m h))]
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
a) [(Maybe Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
forall a. [a] -> [a] -> [a]
++ [(Maybe Text, GVal (Run p m h))]
xs
      in Function (Run p m h)
f [(Maybe Text, GVal (Run p m h))]
args

gfnEquals :: Monad m => Function m
gfnEquals :: Function m
gfnEquals [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
True
gfnEquals [(Maybe Text, GVal m)
x] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
True
gfnEquals ((Maybe Text, GVal m)
x:[(Maybe Text, GVal m)]
xs) =
    GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ ((Maybe Text, GVal m) -> Bool) -> [(Maybe Text, GVal m)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (((Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd (Maybe Text, GVal m)
x GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
`looseEquals`) (GVal m -> Bool)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal m)]
xs

gfnNEquals :: Monad m => Function m
gfnNEquals :: Function m
gfnNEquals [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
True
gfnNEquals [(Maybe Text, GVal m)
x] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
True
gfnNEquals ((Maybe Text, GVal m)
x:[(Maybe Text, GVal m)]
xs) =
    GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ ((Maybe Text, GVal m) -> Bool) -> [(Maybe Text, GVal m)] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.any (Bool -> Bool
not (Bool -> Bool)
-> ((Maybe Text, GVal m) -> Bool) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd (Maybe Text, GVal m)
x GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
`looseEquals`) (GVal m -> Bool)
-> ((Maybe Text, GVal m) -> GVal m) -> (Maybe Text, GVal m) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal m)]
xs

gfnContains :: Monad m => Function (Run p m h)
gfnContains :: Function (Run p m h)
gfnContains [] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
False
gfnContains ((Maybe Text, GVal (Run p m h))
list:[(Maybe Text, GVal (Run p m h))]
elems) = do
    [GVal (Run p m h)]
rawList <- RuntimeError p
-> [GVal (Run p m h)]
-> Maybe [GVal (Run p m h)]
-> Run p m h [GVal (Run p m h)]
forall (m :: * -> *) p a h.
Monad m =>
RuntimeError p -> a -> Maybe a -> Run p m h a
warnFromMaybe ([Text] -> Maybe Text -> RuntimeError p
forall p. [Text] -> Maybe Text -> RuntimeError p
TypeError [Text
"list"] (Text -> Maybe Text
forall a. a -> Maybe a
Just (Text -> Maybe Text) -> Text -> Maybe Text
forall a b. (a -> b) -> a -> b
$ (Maybe Text, GVal (Run p m h)) -> Text
forall a. Show a => a -> Text
tshow (Maybe Text, GVal (Run p m h))
list)) [] (Maybe [GVal (Run p m h)] -> Run p m h [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> Maybe [GVal (Run p m h)])
-> (Maybe Text, GVal (Run p m h))
-> Run p m h [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList (GVal (Run p m h) -> Maybe [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> GVal (Run p m h))
-> (Maybe Text, GVal (Run p m h))
-> Maybe [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd ((Maybe Text, GVal (Run p m h)) -> Run p m h [GVal (Run p m h)])
-> (Maybe Text, GVal (Run p m h)) -> Run p m h [GVal (Run p m h)]
forall a b. (a -> b) -> a -> b
$ (Maybe Text, GVal (Run p m h))
list
    let rawElems :: [GVal (Run p m h)]
rawElems = ((Maybe Text, GVal (Run p m h)) -> GVal (Run p m h))
-> [(Maybe Text, GVal (Run p m h))] -> [GVal (Run p m h)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd [(Maybe Text, GVal (Run p m h))]
elems
        GVal m
e isInList :: GVal m -> t (GVal m) -> Bool
`isInList` t (GVal m)
xs = (GVal m -> Bool) -> t (GVal m) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.any (GVal m -> GVal m -> Bool
forall (m :: * -> *). GVal m -> GVal m -> Bool
looseEquals GVal m
e) t (GVal m)
xs
        t (GVal m)
es areInList :: t (GVal m) -> t (GVal m) -> Bool
`areInList` t (GVal m)
xs = (GVal m -> Bool) -> t (GVal m) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (GVal m -> t (GVal m) -> Bool
forall (t :: * -> *) (m :: * -> *).
Foldable t =>
GVal m -> t (GVal m) -> Bool
`isInList` t (GVal m)
xs) t (GVal m)
es
    GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> (Bool -> GVal (Run p m h))
-> Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [GVal (Run p m h)]
rawElems [GVal (Run p m h)] -> [GVal (Run p m h)] -> Bool
forall (t :: * -> *) (t :: * -> *) (m :: * -> *).
(Foldable t, Foldable t) =>
t (GVal m) -> t (GVal m) -> Bool
`areInList` [GVal (Run p m h)]
rawList

gfnConcat :: Monad m => Function m
gfnConcat :: Function m
gfnConcat [] =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
False
gfnConcat [(Maybe Text, GVal m)
x] =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return ((Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd (Maybe Text, GVal m)
x)
gfnConcat ((Maybe Text, GVal m)
x:[(Maybe Text, GVal m)]
xs) =
  GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ (GVal m -> GVal m -> GVal m) -> GVal m -> [GVal m] -> GVal m
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' GVal m -> GVal m -> GVal m
forall (m :: * -> *). GVal m -> GVal m -> GVal m
gappend ((Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd (Maybe Text, GVal m)
x) (((Maybe Text, GVal m) -> GVal m)
-> [(Maybe Text, GVal m)] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd [(Maybe Text, GVal m)]
xs)

looseEquals :: GVal m -> GVal m -> Bool
looseEquals :: GVal m -> GVal m -> Bool
looseEquals GVal m
a GVal m
b
    | Maybe (Function m) -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe (Function m)
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal m
a) Bool -> Bool -> Bool
|| Maybe (Function m) -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe (Function m)
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal m
b) = Bool
False
    | Maybe [GVal m] -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe [GVal m]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal m
a) Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= Maybe [GVal m] -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe [GVal m]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal m
b) = Bool
False
    | Maybe [(Text, GVal m)] -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe [(Text, GVal m)]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal m
a) Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
/= Maybe [(Text, GVal m)] -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe [(Text, GVal m)]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal m
b) = Bool
False
    -- Both numbers: do numeric comparison
    | Maybe Scientific -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a) Bool -> Bool -> Bool
&& Maybe Scientific -> Bool
forall a. Maybe a -> Bool
isJust (GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b) = GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a Maybe Scientific -> Maybe Scientific -> Bool
forall a. Eq a => a -> a -> Bool
== GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b
    -- If either is NULL, the other must be falsy
    | GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
isNull GVal m
a Bool -> Bool -> Bool
|| GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
isNull GVal m
b = GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal m
a Bool -> Bool -> Bool
forall a. Eq a => a -> a -> Bool
== GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal m
b
    | Bool
otherwise = GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
a Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
b

gfnLess :: Monad m => Function m
gfnLess :: Function m
gfnLess [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool
False
gfnLess [(Maybe Text, GVal m)]
xs' =
    let xs :: [GVal m]
xs = ((Maybe Text, GVal m) -> GVal m)
-> [(Maybe Text, GVal m)] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd [(Maybe Text, GVal m)]
xs'
    in GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$
        (Maybe Bool -> Bool) -> [Maybe Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) ((GVal m -> GVal m -> Maybe Bool)
-> [GVal m] -> [GVal m] -> [Maybe Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
Prelude.zipWith GVal m -> GVal m -> Maybe Bool
forall (m :: * -> *). Monad m => GVal m -> GVal m -> Maybe Bool
less [GVal m]
xs ([GVal m] -> [GVal m]
forall a. [a] -> [a]
Prelude.tail [GVal m]
xs))

gfnGreater :: Monad m => Function m
gfnGreater :: Function m
gfnGreater [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool
False
gfnGreater [(Maybe Text, GVal m)]
xs' =
    let xs :: [GVal m]
xs = ((Maybe Text, GVal m) -> GVal m)
-> [(Maybe Text, GVal m)] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd [(Maybe Text, GVal m)]
xs'
    in GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$
        (Maybe Bool -> Bool) -> [Maybe Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) ((GVal m -> GVal m -> Maybe Bool)
-> [GVal m] -> [GVal m] -> [Maybe Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
Prelude.zipWith GVal m -> GVal m -> Maybe Bool
forall (m :: * -> *). Monad m => GVal m -> GVal m -> Maybe Bool
greater [GVal m]
xs ([GVal m] -> [GVal m]
forall a. [a] -> [a]
Prelude.tail [GVal m]
xs))

gfnLessEquals :: Monad m => Function m
gfnLessEquals :: Function m
gfnLessEquals [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool
False
gfnLessEquals [(Maybe Text, GVal m)]
xs' =
    let xs :: [GVal m]
xs = ((Maybe Text, GVal m) -> GVal m)
-> [(Maybe Text, GVal m)] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd [(Maybe Text, GVal m)]
xs'
    in GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$
        (Maybe Bool -> Bool) -> [Maybe Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) ((GVal m -> GVal m -> Maybe Bool)
-> [GVal m] -> [GVal m] -> [Maybe Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
Prelude.zipWith GVal m -> GVal m -> Maybe Bool
forall (m :: * -> *). Monad m => GVal m -> GVal m -> Maybe Bool
lessEq [GVal m]
xs ([GVal m] -> [GVal m]
forall a. [a] -> [a]
Prelude.tail [GVal m]
xs))

gfnGreaterEquals :: Monad m => Function m
gfnGreaterEquals :: Function m
gfnGreaterEquals [] = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Bool
False
gfnGreaterEquals [(Maybe Text, GVal m)]
xs' =
    let xs :: [GVal m]
xs = ((Maybe Text, GVal m) -> GVal m)
-> [(Maybe Text, GVal m)] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd [(Maybe Text, GVal m)]
xs'
    in GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Bool -> GVal m) -> Bool -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> m (GVal m)) -> Bool -> m (GVal m)
forall a b. (a -> b) -> a -> b
$
        (Maybe Bool -> Bool) -> [Maybe Bool] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
Prelude.all (Maybe Bool -> Maybe Bool -> Bool
forall a. Eq a => a -> a -> Bool
== Bool -> Maybe Bool
forall a. a -> Maybe a
Just Bool
True) ((GVal m -> GVal m -> Maybe Bool)
-> [GVal m] -> [GVal m] -> [Maybe Bool]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
Prelude.zipWith GVal m -> GVal m -> Maybe Bool
forall (m :: * -> *). Monad m => GVal m -> GVal m -> Maybe Bool
greaterEq [GVal m]
xs ([GVal m] -> [GVal m]
forall a. [a] -> [a]
Prelude.tail [GVal m]
xs))

less :: Monad m => GVal m -> GVal m -> Maybe Bool
less :: GVal m -> GVal m -> Maybe Bool
less GVal m
a GVal m
b = Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(<) (Scientific -> Scientific -> Bool)
-> Maybe Scientific -> Maybe (Scientific -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a Maybe (Scientific -> Bool) -> Maybe Scientific -> Maybe Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b

greater :: Monad m => GVal m -> GVal m -> Maybe Bool
greater :: GVal m -> GVal m -> Maybe Bool
greater GVal m
a GVal m
b = Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(>) (Scientific -> Scientific -> Bool)
-> Maybe Scientific -> Maybe (Scientific -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a Maybe (Scientific -> Bool) -> Maybe Scientific -> Maybe Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b

lessEq :: Monad m => GVal m -> GVal m -> Maybe Bool
lessEq :: GVal m -> GVal m -> Maybe Bool
lessEq GVal m
a GVal m
b = Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(<=) (Scientific -> Scientific -> Bool)
-> Maybe Scientific -> Maybe (Scientific -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a Maybe (Scientific -> Bool) -> Maybe Scientific -> Maybe Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b

greaterEq :: Monad m => GVal m -> GVal m -> Maybe Bool
greaterEq :: GVal m -> GVal m -> Maybe Bool
greaterEq GVal m
a GVal m
b = Scientific -> Scientific -> Bool
forall a. Ord a => a -> a -> Bool
(>=) (Scientific -> Scientific -> Bool)
-> Maybe Scientific -> Maybe (Scientific -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
a Maybe (Scientific -> Bool) -> Maybe Scientific -> Maybe Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
b

difference :: Prelude.Num a => [a] -> a
difference :: [a] -> a
difference (a
x:[a]
xs) = a
x a -> a -> a
forall a. Num a => a -> a -> a
- [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Prelude.sum [a]
xs
difference [] = a
0

ratio :: (Show a, Prelude.Fractional a, Prelude.Num a) => [a] -> a
ratio :: [a] -> a
ratio (a
x:[a]
xs) = a
x a -> a -> a
forall a. Fractional a => a -> a -> a
/ [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Prelude.product [a]
xs
ratio [] = a
0

intRatio :: (Prelude.Integral a, Prelude.Num a) => [a] -> a
intRatio :: [a] -> a
intRatio (a
x:[a]
xs) = a
x a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.div` [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Prelude.product [a]
xs
intRatio [] = a
0

modulo :: (Prelude.Integral a, Prelude.Num a) => [a] -> a
modulo :: [a] -> a
modulo (a
x:[a]
xs) = a
x a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.mod` [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
Prelude.product [a]
xs
modulo [] = a
0

capitalize :: Text -> Text
capitalize :: Text -> Text
capitalize Text
txt = Text -> Text
Text.toUpper (Int -> Text -> Text
Text.take Int
1 Text
txt) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text -> Text
Text.drop Int
1 Text
txt

gfnCenter :: Monad m => Function m
gfnCenter :: Function m
gfnCenter [] = Function m
forall (m :: * -> *). Monad m => Function m
gfnCenter [(Maybe Text
forall a. Maybe a
Nothing, Text -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text
"" :: Text))]
gfnCenter [(Maybe Text, GVal m)
x] = Function m
forall (m :: * -> *). Monad m => Function m
gfnCenter [(Maybe Text, GVal m)
x, (Maybe Text
forall a. Maybe a
Nothing, Int -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int
80 :: Int))]
gfnCenter [(Maybe Text, GVal m)
x,(Maybe Text, GVal m)
y] = Function m
forall (m :: * -> *). Monad m => Function m
gfnCenter [(Maybe Text, GVal m)
x, (Maybe Text, GVal m)
y, (Maybe Text
forall a. Maybe a
Nothing, Text -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text
" " :: Text))]
gfnCenter ((Maybe Text
_, GVal m
s):(Maybe Text
_, GVal m
w):(Maybe Text
_, GVal m
pad):[(Maybe Text, GVal m)]
_) =
    GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> (Text -> GVal m) -> Text -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text -> m (GVal m)) -> Text -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ Text -> Int -> Text -> Text
center (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
s) (Int -> Maybe Int -> Int
forall a. a -> Maybe a -> a
fromMaybe Int
80 (Maybe Int -> Int) -> Maybe Int -> Int
forall a b. (a -> b) -> a -> b
$ Scientific -> Int
forall a b. (RealFrac a, Integral b) => a -> b
Prelude.truncate (Scientific -> Int) -> Maybe Scientific -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
w) (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
pad)

gfnLength :: Monad m => Function (Run p m h)
gfnLength :: Function (Run p m h)
gfnLength [] =
  GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
forall a. Default a => a
def
gfnLength ((Maybe Text
_,GVal (Run p m h)
x):[(Maybe Text, GVal (Run p m h))]
_) =
  case (GVal (Run p m h) -> Maybe [(Text, GVal (Run p m h))]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal (Run p m h)
x, GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal (Run p m h)
x) of
    (Maybe [(Text, GVal (Run p m h))]
Nothing, Maybe [GVal (Run p m h)]
Nothing) ->
      GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> (GVal (Run p m h) -> GVal (Run p m h))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int -> GVal (Run p m h))
-> (GVal (Run p m h) -> Int)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Int
Text.length (Text -> Int)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
x
    (Just [(Text, GVal (Run p m h))]
items, Maybe [GVal (Run p m h)]
_) ->
      GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> ([(Text, GVal (Run p m h))] -> GVal (Run p m h))
-> [(Text, GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int -> GVal (Run p m h))
-> ([(Text, GVal (Run p m h))] -> Int)
-> [(Text, GVal (Run p m h))]
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, GVal (Run p m h))] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length ([(Text, GVal (Run p m h))]
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> [(Text, GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [(Text, GVal (Run p m h))]
items
    (Maybe [(Text, GVal (Run p m h))]
_, Just [GVal (Run p m h)]
items) ->
      GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> ([GVal (Run p m h)] -> GVal (Run p m h))
-> [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int -> GVal (Run p m h))
-> ([GVal (Run p m h)] -> Int)
-> [GVal (Run p m h)]
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [GVal (Run p m h)] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length ([GVal (Run p m h)]
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [GVal (Run p m h)]
items

gfnSlice :: Monad m => Function (Run p m h)
gfnSlice :: Function (Run p m h)
gfnSlice [(Maybe Text, GVal (Run p m h))]
args =
    let argValues :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues =
            [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
                [ (Text
"slicee", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"start", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"length", GVal (Run p m h)
forall a. Default a => a
def)
                ]
                [(Maybe Text, GVal (Run p m h))]
args
    in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues of
        Right [GVal (Run p m h)
slicee, GVal (Run p m h)
startPos, GVal (Run p m h)
length] -> do
            let startInt :: Int
                startInt :: Int
startInt = Int -> (Scientific -> Int) -> Maybe Scientific -> Int
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Int
0 Scientific -> Int
forall a b. (RealFrac a, Integral b) => a -> b
Prelude.round (Maybe Scientific -> Int)
-> (GVal (Run p m h) -> Maybe Scientific)
-> GVal (Run p m h)
-> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber (GVal (Run p m h) -> Int) -> GVal (Run p m h) -> Int
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
startPos

                lengthInt :: Maybe Int
                lengthInt :: Maybe Int
lengthInt = (Scientific -> Int) -> Maybe Scientific -> Maybe Int
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Scientific -> Int
forall a b. (RealFrac a, Integral b) => a -> b
Prelude.round (Maybe Scientific -> Maybe Int)
-> (GVal (Run p m h) -> Maybe Scientific)
-> GVal (Run p m h)
-> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber (GVal (Run p m h) -> Maybe Int) -> GVal (Run p m h) -> Maybe Int
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
length

                slice :: [a] -> Int -> Maybe Int -> [a]
                slice :: [a] -> Int -> Maybe Int -> [a]
slice [a]
xs Int
start Maybe Int
Nothing
                    | Int
start Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 =
                        Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
Prelude.drop ([a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
Prelude.length [a]
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
start) [a]
xs
                    | Bool
otherwise =
                        Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
Prelude.drop Int
start [a]
xs
                slice [a]
xs Int
start (Just Int
length) =
                    Int -> [a] -> [a]
forall a. Int -> [a] -> [a]
Prelude.take Int
length ([a] -> [a]) -> [a] -> [a]
forall a b. (a -> b) -> a -> b
$ [a] -> Int -> Maybe Int -> [a]
forall a. [a] -> Int -> Maybe Int -> [a]
slice [a]
xs Int
start Maybe Int
forall a. Maybe a
Nothing
            case GVal (Run p m h) -> Maybe [(Text, GVal (Run p m h))]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal (Run p m h)
slicee of
                Just [(Text, GVal (Run p m h))]
items -> do
                    let slicedItems :: [(Text, GVal (Run p m h))]
slicedItems = [(Text, GVal (Run p m h))]
-> Int -> Maybe Int -> [(Text, GVal (Run p m h))]
forall a. [a] -> Int -> Maybe Int -> [a]
slice [(Text, GVal (Run p m h))]
items Int
startInt Maybe Int
lengthInt
                    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [(Text, GVal (Run p m h))] -> GVal (Run p m h)
forall (m :: * -> *). [Pair m] -> GVal m
dict [(Text, GVal (Run p m h))]
slicedItems
                Maybe [(Text, GVal (Run p m h))]
Nothing ->
                    case GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal (Run p m h)
slicee of
                    Just [GVal (Run p m h)]
items ->
                        GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> ([GVal (Run p m h)] -> GVal (Run p m h))
-> [GVal (Run p m h)]
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal (Run p m h)] -> Run p m h (GVal (Run p m h)))
-> [GVal (Run p m h)] -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [GVal (Run p m h)] -> Int -> Maybe Int -> [GVal (Run p m h)]
forall a. [a] -> Int -> Maybe Int -> [a]
slice [GVal (Run p m h)]
items Int
startInt Maybe Int
lengthInt
                    Maybe [GVal (Run p m h)]
Nothing ->
                        GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (String -> GVal (Run p m h))
-> String
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text -> GVal (Run p m h))
-> (String -> Text) -> String -> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack (String -> Run p m h (GVal (Run p m h)))
-> String -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$
                            String -> Int -> Maybe Int -> String
forall a. [a] -> Int -> Maybe Int -> [a]
slice (Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
slicee) Int
startInt Maybe Int
lengthInt
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"slice") Text
"expected: (slicee, start=0, length=null)"

gfnReplace :: Monad m => Function (Run p m h)
gfnReplace :: Function (Run p m h)
gfnReplace [(Maybe Text, GVal (Run p m h))]
args =
    let argValues :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues =
            [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
                [ (Text
"str", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"search", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"replace", GVal (Run p m h)
forall a. Default a => a
def)
                ]
                [(Maybe Text, GVal (Run p m h))]
args
    in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues of
        Right [GVal (Run p m h)
strG, GVal (Run p m h)
searchG, GVal (Run p m h)
replaceG] -> do
            let str :: Text
str = GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
strG
                search :: Text
search = GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
searchG
                replace :: Text
replace = GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
replaceG
            GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (Text -> GVal (Run p m h))
-> Text
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text -> Run p m h (GVal (Run p m h)))
-> Text -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Text -> Text -> Text -> Text
Text.replace Text
search Text
replace Text
str
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"replace") Text
"expected: (str, search, replace)"

gfnSplit :: Monad m => Function (Run p m h)
gfnSplit :: Function (Run p m h)
gfnSplit [(Maybe Text, GVal (Run p m h))]
args =
    let argValues :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues =
            [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
                [ (Text
"str", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"sep", GVal (Run p m h)
forall a. Default a => a
def)
                ]
                [(Maybe Text, GVal (Run p m h))]
args
    in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
argValues of
        Right [GVal (Run p m h)
strG, GVal (Run p m h)
sepG] -> do
            let search :: Text
search = GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
strG
                split :: Text -> [Text]
split = case GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
sepG of
                    Text
"" -> Text -> [Text]
Text.words
                    Text
sep -> Text -> Text -> [Text]
Text.splitOn Text
sep
            GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (Text -> GVal (Run p m h))
-> Text
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Text] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([Text] -> GVal (Run p m h))
-> (Text -> [Text]) -> Text -> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text]
split (Text -> Run p m h (GVal (Run p m h)))
-> Text -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Text
search
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"split") Text
"expected: (str, sep=null)"

gfnCompose :: forall m p h. Monad m => Function (Run p m h)
gfnCompose :: Function (Run p m h)
gfnCompose [] = RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"compose") Text
""
gfnCompose [(Maybe Text
_, GVal (Run p m h)
fG)] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
fG
gfnCompose ((Maybe Text
_, GVal (Run p m h)
fG):[(Maybe Text, GVal (Run p m h))]
xs) = do
  GVal (Run p m h)
gG <- Function (Run p m h)
forall (m :: * -> *) p h. Monad m => Function (Run p m h)
gfnCompose [(Maybe Text, GVal (Run p m h))]
xs
  case (GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
fG, GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
gG) of
    (Maybe (Function (Run p m h))
Nothing, Maybe (Function (Run p m h))
_) -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError RuntimeError p
forall p. RuntimeError p
NotAFunctionError
    (Maybe (Function (Run p m h))
_, Maybe (Function (Run p m h))
Nothing) -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError RuntimeError p
forall p. RuntimeError p
NotAFunctionError
    (Just Function (Run p m h)
f, Just Function (Run p m h)
g) -> do
      let h :: Function (Run p m h)
h [(Maybe Text, GVal (Run p m h))]
args = do
              GVal (Run p m h)
arg' <- Function (Run p m h)
g [(Maybe Text, GVal (Run p m h))]
args
              Function (Run p m h)
f [(Maybe Text
forall a. Maybe a
Nothing, GVal (Run p m h)
arg')]
      GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Function (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). Function m -> GVal m
fromFunction Function (Run p m h)
h

gfnPartial :: forall m p h. Monad m => Function (Run p m h)
gfnPartial :: Function (Run p m h)
gfnPartial [] = RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"partial") Text
""
gfnPartial ((Maybe Text
_, GVal (Run p m h)
fG):[(Maybe Text, GVal (Run p m h))]
args) = do
  case GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
fG of
    Maybe (Function (Run p m h))
Nothing -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"partial") Text
"Argument must be a function"
    Just Function (Run p m h)
f -> do
      let h :: Function (Run p m h)
h [(Maybe Text, GVal (Run p m h))]
args' = do
            Function (Run p m h)
f ([(Maybe Text, GVal (Run p m h))]
args [(Maybe Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
forall a. [a] -> [a] -> [a]
++ [(Maybe Text, GVal (Run p m h))]
args')
      GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Function (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). Function m -> GVal m
fromFunction Function (Run p m h)
h

gfnZip :: forall m p h. Monad m => Function (Run p m h)
gfnZip :: Function (Run p m h)
gfnZip [(Maybe Text, GVal (Run p m h))]
args = do
  [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal (Run p m h)] -> GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
-> Run p m h (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go (((Maybe Text, GVal (Run p m h)) -> [GVal (Run p m h)])
-> [(Maybe Text, GVal (Run p m h))] -> [[GVal (Run p m h)]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([GVal (Run p m h)]
-> Maybe [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [GVal (Run p m h)] -> [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> Maybe [GVal (Run p m h)])
-> (Maybe Text, GVal (Run p m h))
-> [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList (GVal (Run p m h) -> Maybe [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> GVal (Run p m h))
-> (Maybe Text, GVal (Run p m h))
-> Maybe [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal (Run p m h))]
args)
  where
    go :: [[GVal (Run p m h)]] -> Run p m h [GVal (Run p m h)]
    go :: [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go [] = [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    go [[GVal (Run p m h)]]
args = do
      let heads :: [Maybe (GVal (Run p m h))]
heads = ([GVal (Run p m h)] -> Maybe (GVal (Run p m h)))
-> [[GVal (Run p m h)]] -> [Maybe (GVal (Run p m h))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [GVal (Run p m h)] -> Maybe (GVal (Run p m h))
forall a. [a] -> Maybe a
headMay [[GVal (Run p m h)]]
args
          tails :: [[GVal (Run p m h)]]
tails = ([GVal (Run p m h)] -> [GVal (Run p m h)])
-> [[GVal (Run p m h)]] -> [[GVal (Run p m h)]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. Int -> [a] -> [a]
List.drop Int
1) [[GVal (Run p m h)]]
args
      if (Maybe (GVal (Run p m h)) -> Bool)
-> [Maybe (GVal (Run p m h))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
List.any Maybe (GVal (Run p m h)) -> Bool
forall a. Maybe a -> Bool
isNothing [Maybe (GVal (Run p m h))]
heads then
        [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
      else do
        let currentVal :: GVal (Run p m h)
currentVal = [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ((Maybe (GVal (Run p m h)) -> GVal (Run p m h))
-> [Maybe (GVal (Run p m h))] -> [GVal (Run p m h)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GVal (Run p m h) -> Maybe (GVal (Run p m h)) -> GVal (Run p m h)
forall a. a -> Maybe a -> a
fromMaybe GVal (Run p m h)
forall a. Default a => a
def) [Maybe (GVal (Run p m h))]
heads)
        [GVal (Run p m h)]
tailVals <- [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go [[GVal (Run p m h)]]
tails
        [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return ([GVal (Run p m h)]
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      [GVal (Run p m h)])
-> [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
currentVal GVal (Run p m h) -> [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> [a] -> [a]
: [GVal (Run p m h)]
tailVals


gfnZipWith :: forall m p h. Monad m => Function (Run p m h)
gfnZipWith :: Function (Run p m h)
gfnZipWith ((Maybe Text
Nothing, GVal (Run p m h)
gfn):[(Maybe Text, GVal (Run p m h))]
args) = do
  Function (Run p m h)
zipFunction <- case GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
gfn of
    Maybe (Function (Run p m h))
Nothing -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (Function (Run p m h)))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"zipwith") Text
""
    Just Function (Run p m h)
fn -> Function (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return Function (Run p m h)
fn
  [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal (Run p m h)] -> GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
-> Run p m h (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Function (Run p m h)
-> [Maybe Text]
-> [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go Function (Run p m h)
zipFunction (((Maybe Text, GVal (Run p m h)) -> Maybe Text)
-> [(Maybe Text, GVal (Run p m h))] -> [Maybe Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal (Run p m h)) -> Maybe Text
forall a b. (a, b) -> a
fst [(Maybe Text, GVal (Run p m h))]
args) (((Maybe Text, GVal (Run p m h)) -> [GVal (Run p m h)])
-> [(Maybe Text, GVal (Run p m h))] -> [[GVal (Run p m h)]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([GVal (Run p m h)]
-> Maybe [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [GVal (Run p m h)] -> [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> Maybe [GVal (Run p m h)])
-> (Maybe Text, GVal (Run p m h))
-> [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList (GVal (Run p m h) -> Maybe [GVal (Run p m h)])
-> ((Maybe Text, GVal (Run p m h)) -> GVal (Run p m h))
-> (Maybe Text, GVal (Run p m h))
-> Maybe [GVal (Run p m h)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd) [(Maybe Text, GVal (Run p m h))]
args)
  where
    go :: Function (Run p m h) -> [Maybe Text] -> [[GVal (Run p m h)]] -> Run p m h [GVal (Run p m h)]
    go :: Function (Run p m h)
-> [Maybe Text]
-> [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go Function (Run p m h)
f [Maybe Text]
_ [] = [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    go Function (Run p m h)
f [Maybe Text]
argNames [[GVal (Run p m h)]]
args = do
      let heads :: [Maybe (GVal (Run p m h))]
heads = ([GVal (Run p m h)] -> Maybe (GVal (Run p m h)))
-> [[GVal (Run p m h)]] -> [Maybe (GVal (Run p m h))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [GVal (Run p m h)] -> Maybe (GVal (Run p m h))
forall a. [a] -> Maybe a
headMay [[GVal (Run p m h)]]
args
          tails :: [[GVal (Run p m h)]]
tails = ([GVal (Run p m h)] -> [GVal (Run p m h)])
-> [[GVal (Run p m h)]] -> [[GVal (Run p m h)]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. Int -> [a] -> [a]
List.drop Int
1) [[GVal (Run p m h)]]
args
      if (Maybe (GVal (Run p m h)) -> Bool)
-> [Maybe (GVal (Run p m h))] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
List.any Maybe (GVal (Run p m h)) -> Bool
forall a. Maybe a -> Bool
isNothing [Maybe (GVal (Run p m h))]
heads then
        [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return []
      else do
        GVal (Run p m h)
currentVal <- Function (Run p m h)
f ([Maybe Text]
-> [GVal (Run p m h)] -> [(Maybe Text, GVal (Run p m h))]
forall a b. [a] -> [b] -> [(a, b)]
List.zip [Maybe Text]
argNames ((Maybe (GVal (Run p m h)) -> GVal (Run p m h))
-> [Maybe (GVal (Run p m h))] -> [GVal (Run p m h)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (GVal (Run p m h) -> Maybe (GVal (Run p m h)) -> GVal (Run p m h)
forall a. a -> Maybe a -> a
fromMaybe GVal (Run p m h)
forall a. Default a => a
def) [Maybe (GVal (Run p m h))]
heads))
        [GVal (Run p m h)]
tailVals <- Function (Run p m h)
-> [Maybe Text]
-> [[GVal (Run p m h)]]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
go Function (Run p m h)
f [Maybe Text]
argNames [[GVal (Run p m h)]]
tails
        [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a. Monad m => a -> m a
return ([GVal (Run p m h)]
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      [GVal (Run p m h)])
-> [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
currentVal GVal (Run p m h) -> [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> [a] -> [a]
: [GVal (Run p m h)]
tailVals


gfnMap :: (Monad m, MonadError (RuntimeError a) m) => Function m
gfnMap :: Function m
gfnMap [(Maybe Text, GVal m)]
args = do
    let parsedArgs :: Either ([GVal m], HashMap Text (GVal m), [Text]) [GVal m]
parsedArgs = [(Text, GVal m)]
-> [(Maybe Text, GVal m)]
-> Either ([GVal m], HashMap Text (GVal m), [Text]) [GVal m]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
            [ (Text
"collection", GVal m
forall a. Default a => a
def)
            , (Text
"function", GVal m
forall a. Default a => a
def)
            , (Text
"attribute", GVal m
forall a. Default a => a
def)
            ]
            [(Maybe Text, GVal m)]
args
    (Maybe [(Text, GVal m)]
dictMay, Maybe [GVal m]
listMay, Maybe (Function m)
functionMay, Maybe Text
attributeMay) <- case Either ([GVal m], HashMap Text (GVal m), [Text]) [GVal m]
parsedArgs of
        Right [GVal m
collection, GVal m
function, GVal m
attribute] ->
            (Maybe [(Text, GVal m)], Maybe [GVal m], Maybe (Function m),
 Maybe Text)
-> m (Maybe [(Text, GVal m)], Maybe [GVal m], Maybe (Function m),
      Maybe Text)
forall (m :: * -> *) a. Monad m => a -> m a
return ( GVal m -> Maybe [(Text, GVal m)]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal m
collection
                   , GVal m -> Maybe [GVal m]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal m
collection
                   , GVal m -> Maybe (Function m)
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal m
function
                   , Text -> Maybe Text
forall a. a -> Maybe a
Just (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal m
attribute)
                   )
        Either ([GVal m], HashMap Text (GVal m), [Text]) [GVal m]
_ ->
            RuntimeError a
-> m (Maybe [(Text, GVal m)], Maybe [GVal m], Maybe (Function m),
      Maybe Text)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError a
 -> m (Maybe [(Text, GVal m)], Maybe [GVal m], Maybe (Function m),
       Maybe Text))
-> RuntimeError a
-> m (Maybe [(Text, GVal m)], Maybe [GVal m], Maybe (Function m),
      Maybe Text)
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError a
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"map") Text
""
    Function m
mapFunction <- case (Maybe (Function m)
functionMay, Maybe Text
attributeMay) of
        (Just Function m
f, Maybe Text
_) -> Function m -> m (Function m)
forall (m :: * -> *) a. Monad m => a -> m a
return Function m
f
        (Maybe (Function m)
Nothing, Just Text
key) -> Function m -> m (Function m)
forall (m :: * -> *) a. Monad m => a -> m a
return (Function m -> m (Function m)) -> Function m -> m (Function m)
forall a b. (a -> b) -> a -> b
$ \case
            (Maybe Text
_, GVal m
item):[(Maybe Text, GVal m)]
_ ->
                GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m)) -> GVal m -> m (GVal m)
forall a b. (a -> b) -> a -> b
$ GVal m -> GVal m -> GVal m -> GVal m
forall (m :: * -> *). GVal m -> GVal m -> GVal m -> GVal m
lookupLooseDef GVal m
forall a. Default a => a
def (Text -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Text
key) GVal m
item
            [(Maybe Text, GVal m)]
_ ->
                GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def
        (Maybe (Function m), Maybe Text)
_ -> RuntimeError a -> m (Function m)
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError a -> m (Function m))
-> RuntimeError a -> m (Function m)
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError a
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"map") Text
"You have to pass a function or an attribute"
    case (Maybe [(Text, GVal m)]
dictMay, Maybe [GVal m]
listMay) of
        (Just [(Text, GVal m)]
items, Maybe [GVal m]
_) ->
            [(Text, GVal m)] -> GVal m
forall (m :: * -> *). [Pair m] -> GVal m
dict ([(Text, GVal m)] -> GVal m) -> m [(Text, GVal m)] -> m (GVal m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [(Text, GVal m)]
-> ((Text, GVal m) -> m (Text, GVal m)) -> m [(Text, GVal m)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Text, GVal m)]
items
                (\(Text
key, GVal m
value) -> (Text
key,) (GVal m -> (Text, GVal m)) -> m (GVal m) -> m (Text, GVal m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Function m
mapFunction [(Maybe Text
forall a. Maybe a
Nothing, GVal m
value)])
        (Maybe [(Text, GVal m)]
Nothing, Just [GVal m]
items) ->
            [GVal m] -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal m] -> GVal m) -> m [GVal m] -> m (GVal m)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (GVal m -> m (GVal m)) -> [GVal m] -> m [GVal m]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM (Function m
mapFunction Function m
-> (GVal m -> [(Maybe Text, GVal m)]) -> GVal m -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Maybe Text, GVal m)
-> [(Maybe Text, GVal m)] -> [(Maybe Text, GVal m)]
forall a. a -> [a] -> [a]
:[]) ((Maybe Text, GVal m) -> [(Maybe Text, GVal m)])
-> (GVal m -> (Maybe Text, GVal m))
-> GVal m
-> [(Maybe Text, GVal m)]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Maybe Text
forall a. Maybe a
Nothing,)) [GVal m]
items
        (Maybe [(Text, GVal m)]
Nothing, Maybe [GVal m]
Nothing) ->
            GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def


gfnSort :: forall p m h. Monad m => Function (Run p m h)
gfnSort :: Function (Run p m h)
gfnSort [(Maybe Text, GVal (Run p m h))]
args = do
    let parsedArgs :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
parsedArgs = [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
            [ (Text
"sortee", GVal (Run p m h)
forall a. Default a => a
def)
            , (Text
"by", GVal (Run p m h)
forall a. Default a => a
def)
            , (Text
"reverse", Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
False)
            ]
            [(Maybe Text, GVal (Run p m h))]
args
    (GVal (Run p m h)
sortee, GVal (Run p m h)
sortKey, Bool
sortReverse) <- case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
parsedArgs of
        Right [GVal (Run p m h)
sortee, GVal (Run p m h)
sortKey, GVal (Run p m h)
reverseG] ->
            (GVal (Run p m h), GVal (Run p m h), Bool)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h), GVal (Run p m h), Bool)
forall (m :: * -> *) a. Monad m => a -> m a
return ( GVal (Run p m h)
sortee
                   , GVal (Run p m h)
sortKey
                   , GVal (Run p m h) -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal (Run p m h)
reverseG
                   )
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h), GVal (Run p m h), Bool)
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h), GVal (Run p m h), Bool))
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h), GVal (Run p m h), Bool)
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"sort") Text
"expected: (sortee, by, reverse)"
    let extractByFunc :: Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
        extractByFunc :: Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByFunc = do
            Function (Run p m h)
f <- GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
sortKey
            ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (m :: * -> *) a. Monad m => a -> m a
return (((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
 -> Maybe
      ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h))))
-> ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a b. (a -> b) -> a -> b
$ \(Text, GVal (Run p m h))
g ->
                Function (Run p m h)
f [(Maybe Text
forall a. Maybe a
Nothing, (Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd (Text, GVal (Run p m h))
g)]

    let extractByPath :: Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
        extractByPath :: Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByPath = do
            [GVal (Run p m h)]
keys <- GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal (Run p m h)
sortKey
            ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (m :: * -> *) a. Monad m => a -> m a
return (((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
 -> Maybe
      ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h))))
-> ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a b. (a -> b) -> a -> b
$ \(Text, GVal (Run p m h))
g ->
                GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ (GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
-> GVal (Run p m h) -> [GVal (Run p m h)] -> GVal (Run p m h)
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' ((GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
-> GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (GVal (Run p m h)
-> GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). GVal m -> GVal m -> GVal m -> GVal m
lookupLooseDef GVal (Run p m h)
forall a. Default a => a
def)) ((Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd (Text, GVal (Run p m h))
g) [GVal (Run p m h)]
keys

        extractByKey :: Monad m => Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
        extractByKey :: Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
extractByKey =
            if GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
sortKey Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text
"__key"
                then ((Text, a) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
forall a. a -> Maybe a
Just (((Text, a) -> Run p m h (GVal (Run p m h)))
 -> Maybe ((Text, a) -> Run p m h (GVal (Run p m h))))
-> ((Text, a) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> ((Text, a) -> GVal (Run p m h))
-> (Text, a)
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text -> GVal (Run p m h))
-> ((Text, a) -> Text) -> (Text, a) -> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, a) -> Text
forall a b. (a, b) -> a
fst
                else Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
forall a. Maybe a
Nothing

        extractByProp :: Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByProp =
            if GVal (Run p m h) -> Bool
forall (m :: * -> *). GVal m -> Bool
isNull GVal (Run p m h)
sortKey
                then Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a. Maybe a
Nothing
                else ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (m :: * -> *) a. Monad m => a -> m a
return (((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
 -> Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h))))
-> ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> ((a, GVal (Run p m h)) -> GVal (Run p m h))
-> (a, GVal (Run p m h))
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h)
-> GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). GVal m -> GVal m -> GVal m -> GVal m
lookupLooseDef GVal (Run p m h)
forall a. Default a => a
def GVal (Run p m h)
sortKey (GVal (Run p m h) -> GVal (Run p m h))
-> ((a, GVal (Run p m h)) -> GVal (Run p m h))
-> (a, GVal (Run p m h))
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd

        extractFunc :: (Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h))
extractFunc = ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> (Text, GVal (Run p m h))
-> Run p m h (GVal (Run p m h))
forall a. a -> Maybe a -> a
fromMaybe (GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> ((Text, GVal (Run p m h)) -> GVal (Run p m h))
-> (Text, GVal (Run p m h))
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd) (Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
 -> (Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> (Text, GVal (Run p m h))
-> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$
            Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByFunc Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByPath Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a.
Monad m =>
Maybe ((Text, a) -> Run p m h (GVal (Run p m h)))
extractByKey Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
-> Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|>
            Maybe ((Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
forall a.
Maybe ((a, GVal (Run p m h)) -> Run p m h (GVal (Run p m h)))
extractByProp

        ([Pair m] -> GVal m
pack, [(Text, GVal (Run p m h))]
unpacked) =
            case (GVal (Run p m h) -> Maybe [(Text, GVal (Run p m h))]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal (Run p m h)
sortee, GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal (Run p m h)
sortee) of
                (Just [(Text, GVal (Run p m h))]
dictItems, Maybe [GVal (Run p m h)]
_) ->
                    ([Pair m] -> GVal m
forall (m :: * -> *). [Pair m] -> GVal m
orderedDict, [(Text, GVal (Run p m h))]
dictItems)
                (Maybe [(Text, GVal (Run p m h))]
Nothing, Just [GVal (Run p m h)]
listItems) ->
                    ([GVal m] -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal m] -> GVal m)
-> ([Pair m] -> [GVal m]) -> [Pair m] -> GVal m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pair m -> GVal m) -> [Pair m] -> [GVal m]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Pair m -> GVal m
forall a b. (a, b) -> b
snd, [GVal (Run p m h)] -> [(Text, GVal (Run p m h))]
forall a. [a] -> [(Text, a)]
listToIndexedDict [GVal (Run p m h)]
listItems)
                (Maybe [(Text, GVal (Run p m h))]
Nothing, Maybe [GVal (Run p m h)]
Nothing) ->
                    (GVal m -> [Pair m] -> GVal m
forall a b. a -> b -> a
Prelude.const GVal m
forall a. Default a => a
def, [])

    [(Text, (Text, GVal (Run p m h)))]
tagged <- [(Text, GVal (Run p m h))]
-> ((Text, GVal (Run p m h))
    -> ExceptT
         (RuntimeError p)
         (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
         (Text, (Text, GVal (Run p m h))))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [(Text, (Text, GVal (Run p m h)))]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Text, GVal (Run p m h))]
unpacked (((Text, GVal (Run p m h))
  -> ExceptT
       (RuntimeError p)
       (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
       (Text, (Text, GVal (Run p m h))))
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      [(Text, (Text, GVal (Run p m h)))])
-> ((Text, GVal (Run p m h))
    -> ExceptT
         (RuntimeError p)
         (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
         (Text, (Text, GVal (Run p m h))))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [(Text, (Text, GVal (Run p m h)))]
forall a b. (a -> b) -> a -> b
$ \(Text, GVal (Run p m h))
item -> do
        GVal (Run p m h)
extracted <- (Text, GVal (Run p m h)) -> Run p m h (GVal (Run p m h))
extractFunc (Text, GVal (Run p m h))
item
        (Text, (Text, GVal (Run p m h)))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Text, (Text, GVal (Run p m h)))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
extracted, (Text, GVal (Run p m h))
item)
    let compare :: (Text, c) -> (Text, d) -> Ordering
compare =
            (Text -> Text -> Ordering) -> (Text, c) -> (Text, d) -> Ordering
forall a b c d. (a -> a -> b) -> (a, c) -> (a, d) -> b
onFst ((Text -> Text -> Ordering) -> (Text, c) -> (Text, d) -> Ordering)
-> (Text -> Text -> Ordering) -> (Text, c) -> (Text, d) -> Ordering
forall a b. (a -> b) -> a -> b
$ if Bool
sortReverse
                then (Text -> Text -> Ordering) -> Text -> Text -> Ordering
forall a b c. (a -> b -> c) -> b -> a -> c
flip Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
Prelude.compare
                else Text -> Text -> Ordering
forall a. Ord a => a -> a -> Ordering
Prelude.compare

    let sorted :: GVal (Run p m h)
sorted = [(Text, GVal (Run p m h))] -> GVal (Run p m h)
forall (m :: * -> *). [Pair m] -> GVal m
pack ([(Text, GVal (Run p m h))] -> GVal (Run p m h))
-> ([(Text, (Text, GVal (Run p m h)))]
    -> [(Text, GVal (Run p m h))])
-> [(Text, (Text, GVal (Run p m h)))]
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text, (Text, GVal (Run p m h))) -> (Text, GVal (Run p m h)))
-> [(Text, (Text, GVal (Run p m h)))] -> [(Text, GVal (Run p m h))]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Text, (Text, GVal (Run p m h))) -> (Text, GVal (Run p m h))
forall a b. (a, b) -> b
snd ([(Text, (Text, GVal (Run p m h)))] -> GVal (Run p m h))
-> [(Text, (Text, GVal (Run p m h)))] -> GVal (Run p m h)
forall a b. (a -> b) -> a -> b
$ ((Text, (Text, GVal (Run p m h)))
 -> (Text, (Text, GVal (Run p m h))) -> Ordering)
-> [(Text, (Text, GVal (Run p m h)))]
-> [(Text, (Text, GVal (Run p m h)))]
forall a. (a -> a -> Ordering) -> [a] -> [a]
List.sortBy (Text, (Text, GVal (Run p m h)))
-> (Text, (Text, GVal (Run p m h))) -> Ordering
forall c d. (Text, c) -> (Text, d) -> Ordering
compare [(Text, (Text, GVal (Run p m h)))]
tagged

    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
sorted

onFst :: (a -> a -> b) -> (a, c) -> (a, d) -> b
onFst :: (a -> a -> b) -> (a, c) -> (a, d) -> b
onFst a -> a -> b
f (a
x, c
_) (a
y, d
_) = a -> a -> b
f a
x a
y

listToIndexedDict :: [a] -> [(Text, a)]
listToIndexedDict :: [a] -> [(Text, a)]
listToIndexedDict [a]
values =
    let indexes :: [Text]
indexes = (Integer -> Text) -> [Integer] -> [Text]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Integer -> Text
forall a. Show a => a -> Text
tshow) [Integer
0..]
    in [Text] -> [a] -> [(Text, a)]
forall a b. [a] -> [b] -> [(a, b)]
List.zip [Text]
indexes [a]
values

center :: Text -> Prelude.Int -> Text -> Text
center :: Text -> Int -> Text -> Text
center Text
str Int
width Text
pad =
    if Text -> Int
Text.length Text
str Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
Prelude.>= Int
width
        then Text
str
        else Text
paddingL Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
str Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
paddingR
    where
        chars :: Int
chars = Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Text -> Int
Text.length Text
str
        charsL :: Int
charsL = Int
chars Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
        charsR :: Int
charsR = Int
chars Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
charsL
        repsL :: Int
repsL = Int -> Int
forall a. Enum a => a -> a
Prelude.succ Int
charsL Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Text -> Int
Text.length Text
pad
        paddingL :: Text
paddingL = Int -> Text -> Text
Text.take Int
charsL (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
Text.replicate Int
repsL (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text
pad
        repsR :: Int
repsR = Int -> Int
forall a. Enum a => a -> a
Prelude.succ Int
charsR Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Text -> Int
Text.length Text
pad
        paddingR :: Text
paddingR = Int -> Text -> Text
Text.take Int
charsR (Text -> Text) -> (Text -> Text) -> Text -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Text -> Text
Text.replicate Int
repsR (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Text
pad

gfnFileSizeFormat :: Monad m => Function m
gfnFileSizeFormat :: Function m
gfnFileSizeFormat [(Maybe Text
_, GVal m
sizeG)] =
    Function m
forall (m :: * -> *). Monad m => Function m
gfnFileSizeFormat [(Maybe Text
forall a. Maybe a
Nothing, GVal m
sizeG), (Maybe Text
forall a. Maybe a
Nothing, GVal m
forall a. Default a => a
def)]
gfnFileSizeFormat [(Maybe Text
_, GVal m
sizeG), (Maybe Text
_, GVal m
binaryG)] = do
    let sizeM :: Maybe Integer
sizeM = Scientific -> Integer
forall a b. (RealFrac a, Integral b) => a -> b
Prelude.round (Scientific -> Integer) -> Maybe Scientific -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Scientific
forall (m :: * -> *). GVal m -> Maybe Scientific
asNumber GVal m
sizeG
        binary :: Bool
binary = GVal m -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal m
binaryG
    m (GVal m)
-> (Integer -> m (GVal m)) -> Maybe Integer -> m (GVal m)
forall b a. b -> (a -> b) -> Maybe a -> b
Prelude.maybe
        (GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def)
        (GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal m -> m (GVal m))
-> (Integer -> GVal m) -> Integer -> m (GVal m)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (String -> GVal m) -> (Integer -> String) -> Integer -> GVal m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Bool -> Integer -> String
formatFileSize Bool
binary)
        Maybe Integer
sizeM
gfnFileSizeFormat [(Maybe Text, GVal m)]
_ = GVal m -> m (GVal m)
forall (m :: * -> *) a. Monad m => a -> m a
return GVal m
forall a. Default a => a
def

formatFileSize :: Bool -> Integer -> String
formatFileSize :: Bool -> Integer -> String
formatFileSize Bool
binary Integer
size =
    let units :: [(Integer, String)]
units =
            if Bool
binary
                then
                    [ (Integer
1, String
"B")
                    , (Integer
1024, String
"kiB")
                    , (Integer
1024 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
2, String
"MiB")
                    , (Integer
1024 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
3, String
"GiB")
                    , (Integer
1024 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
4, String
"TiB")
                    , (Integer
1024 Integer -> Integer -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ Integer
5, String
"PiB")
                    ]
                else
                    [ (Integer
1, String
"B")
                    , (Integer
1000, String
"kB")
                    , (Integer
1000000, String
"MB")
                    , (Integer
1000000000, String
"GB")
                    , (Integer
1000000000000, String
"TB")
                    , (Integer
1000000000000000, String
"PB")
                    ]
        (Integer
divisor, String
unitName) =
            (Integer, String) -> [(Integer, String)] -> (Integer, String)
forall a. a -> [a] -> a
lastDef (Integer
1, String
"B") [ (Integer
d, String
u) | (Integer
d, String
u) <- [(Integer, String)]
units, Integer
d Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Integer
size ]
        dividedSize :: Scientific
        dividedSize :: Scientific
dividedSize = Integer -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
size Scientific -> Scientific -> Scientific
forall a. Fractional a => a -> a -> a
/ Integer -> Scientific
forall a b. (Integral a, Num b) => a -> b
fromIntegral Integer
divisor
        formattedSize :: String
formattedSize =
            if Scientific -> Bool
Scientific.isInteger Scientific
dividedSize
                then FPFormat -> Maybe Int -> Scientific -> String
formatScientific FPFormat
Fixed (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
0) Scientific
dividedSize
                else FPFormat -> Maybe Int -> Scientific -> String
formatScientific FPFormat
Fixed (Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1) Scientific
dividedSize
    in String
formattedSize String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
unitName

gfnPrintf :: Monad m => Function (Run p m h)
gfnPrintf :: Function (Run p m h)
gfnPrintf [] = do
    RuntimeError p -> Run p m h ()
forall (m :: * -> *) p h. Monad m => RuntimeError p -> Run p m h ()
warn (RuntimeError p -> Run p m h ()) -> RuntimeError p -> Run p m h ()
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"printf") Text
"No format string provided"
    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
forall a. Default a => a
def
gfnPrintf [(Maybe Text
_, GVal (Run p m h)
fmtStrG)] = GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
fmtStrG
gfnPrintf ((Maybe Text
_, GVal (Run p m h)
fmtStrG):[(Maybe Text, GVal (Run p m h))]
args) = do
    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (String -> GVal (Run p m h))
-> String
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (String -> Run p m h (GVal (Run p m h)))
-> String -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ String -> [GVal (Run p m h)] -> String
forall (m :: * -> *). String -> [GVal m] -> String
printfG String
fmtStr (((Maybe Text, GVal (Run p m h)) -> GVal (Run p m h))
-> [(Maybe Text, GVal (Run p m h))] -> [GVal (Run p m h)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Maybe Text, GVal (Run p m h)) -> GVal (Run p m h)
forall a b. (a, b) -> b
snd [(Maybe Text, GVal (Run p m h))]
args)
    where
        fmtStr :: String
fmtStr = Text -> String
Text.unpack (Text -> String) -> Text -> String
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
fmtStrG

gvalToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalToDate TimeZone
tz GVal m
g = TimeZone -> GVal m -> Maybe ZonedTime
forall (m :: * -> *). TimeZone -> GVal m -> Maybe ZonedTime
gvalDictToDate TimeZone
tz GVal m
g
             Maybe ZonedTime -> Maybe ZonedTime -> Maybe ZonedTime
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> TimeZone -> GVal m -> Maybe ZonedTime
forall (m :: * -> *). TimeZone -> GVal m -> Maybe ZonedTime
gvalListToDate TimeZone
tz GVal m
g
             Maybe ZonedTime -> Maybe ZonedTime -> Maybe ZonedTime
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> TimeZone -> GVal m -> Maybe ZonedTime
forall (m :: * -> *). TimeZone -> GVal m -> Maybe ZonedTime
gvalAutoParseDate TimeZone
tz GVal m
g

gvalDictToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalDictToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalDictToDate TimeZone
defTZ GVal m
g = do
    let datePartMay :: Maybe Day
datePartMay = do
            Integer
year <- (Int -> Integer) -> Maybe Int -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral :: Int -> Integer) (Maybe Int -> Maybe Integer) -> Maybe Int -> Maybe Integer
forall a b. (a -> b) -> a -> b
$ GVal m
g GVal m -> GVal m -> Maybe Int
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"year"
            Int
month <- GVal m
g GVal m -> GVal m -> Maybe Int
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"month"
            Int
day <- GVal m
g GVal m -> GVal m -> Maybe Int
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"day"
            Day -> Maybe Day
forall (m :: * -> *) a. Monad m => a -> m a
return (Day -> Maybe Day) -> Day -> Maybe Day
forall a b. (a -> b) -> a -> b
$ Integer -> Int -> Int -> Day
fromGregorian Integer
year Int
month Int
day
        timePartMay :: Maybe TimeOfDay
timePartMay = do
            Int
hours <- GVal m
g GVal m -> GVal m -> Maybe Int
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"hours"
            Int
minutes <- GVal m
g GVal m -> GVal m -> Maybe Int
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"minutes"
            Pico
seconds <- (Scientific -> Pico) -> Maybe Scientific -> Maybe Pico
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Scientific -> Pico
scientificToPico (Maybe Scientific -> Maybe Pico) -> Maybe Scientific -> Maybe Pico
forall a b. (a -> b) -> a -> b
$ GVal m
g GVal m -> GVal m -> Maybe Scientific
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"seconds"
            TimeOfDay -> Maybe TimeOfDay
forall (m :: * -> *) a. Monad m => a -> m a
return (TimeOfDay -> Maybe TimeOfDay) -> TimeOfDay -> Maybe TimeOfDay
forall a b. (a -> b) -> a -> b
$ Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
hours Int
minutes Pico
seconds
        tzPartMay :: Maybe TimeZone
tzPartMay = GVal m
g GVal m -> GVal m -> Maybe TimeZone
forall (m :: * -> *) v. FromGVal m v => GVal m -> GVal m -> Maybe v
~! GVal m
"tz"
    Bool -> Maybe () -> Maybe ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Maybe Day -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Day
datePartMay Bool -> Bool -> Bool
&& Maybe TimeOfDay -> Bool
forall a. Maybe a -> Bool
isNothing Maybe TimeOfDay
timePartMay) Maybe ()
forall a. Maybe a
Nothing
    let datePart :: Day
datePart = Day -> Maybe Day -> Day
forall a. a -> Maybe a -> a
fromMaybe (Integer -> Int -> Int -> Day
fromGregorian Integer
1970 Int
1 Int
1) Maybe Day
datePartMay
        timePart :: TimeOfDay
timePart = TimeOfDay -> Maybe TimeOfDay -> TimeOfDay
forall a. a -> Maybe a -> a
fromMaybe (Int -> Int -> Pico -> TimeOfDay
TimeOfDay Int
12 Int
0 Pico
0) Maybe TimeOfDay
timePartMay
        tz :: TimeZone
tz = TimeZone -> Maybe TimeZone -> TimeZone
forall a. a -> Maybe a -> a
fromMaybe TimeZone
defTZ Maybe TimeZone
tzPartMay
    ZonedTime -> Maybe ZonedTime
forall (m :: * -> *) a. Monad m => a -> m a
return (ZonedTime -> Maybe ZonedTime) -> ZonedTime -> Maybe ZonedTime
forall a b. (a -> b) -> a -> b
$ LocalTime -> TimeZone -> ZonedTime
ZonedTime (Day -> TimeOfDay -> LocalTime
LocalTime Day
datePart TimeOfDay
timePart) TimeZone
tz

gvalListToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalListToDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalListToDate TimeZone
defTZ GVal m
g = [GVal m] -> Maybe ZonedTime
forall (m :: * -> *). [GVal m] -> Maybe ZonedTime
go ([GVal m] -> Maybe ZonedTime) -> Maybe [GVal m] -> Maybe ZonedTime
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< GVal m -> Maybe [GVal m]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal m
g
    where
        go :: [GVal m] -> Maybe ZonedTime
        go :: [GVal m] -> Maybe ZonedTime
go [GVal m]
parts = case [GVal m]
parts of
            [GVal m
yearG, GVal m
monthG, GVal m
dayG, GVal m
hoursG, GVal m
minutesG, GVal m
secondsG, GVal m
tzG] -> do
                Day
datePart <-
                    Integer -> Int -> Int -> Day
fromGregorian
                        (Integer -> Int -> Int -> Day)
-> Maybe Integer -> Maybe (Int -> Int -> Day)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Integer) -> Maybe Int -> Maybe Integer
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
yearG)
                        Maybe (Int -> Int -> Day) -> Maybe Int -> Maybe (Int -> Day)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
monthG
                        Maybe (Int -> Day) -> Maybe Int -> Maybe Day
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
dayG
                TimeOfDay
timePart <-
                    Int -> Int -> Pico -> TimeOfDay
TimeOfDay
                        (Int -> Int -> Pico -> TimeOfDay)
-> Maybe Int -> Maybe (Int -> Pico -> TimeOfDay)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
hoursG
                        Maybe (Int -> Pico -> TimeOfDay)
-> Maybe Int -> Maybe (Pico -> TimeOfDay)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
minutesG
                        Maybe (Pico -> TimeOfDay) -> Maybe Pico -> Maybe TimeOfDay
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int -> Pico
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pico) -> Maybe Int -> Maybe Pico
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal m -> Maybe Int
forall (m :: * -> *). GVal m -> Maybe Int
toInt GVal m
secondsG)
                TimeZone
tzPart <- GVal m -> Maybe TimeZone
forall (m :: * -> *) a. FromGVal m a => GVal m -> Maybe a
fromGVal GVal m
tzG
                ZonedTime -> Maybe ZonedTime
forall (m :: * -> *) a. Monad m => a -> m a
return (ZonedTime -> Maybe ZonedTime) -> ZonedTime -> Maybe ZonedTime
forall a b. (a -> b) -> a -> b
$ LocalTime -> TimeZone -> ZonedTime
ZonedTime (Day -> TimeOfDay -> LocalTime
LocalTime Day
datePart TimeOfDay
timePart) TimeZone
tzPart
            [GVal m
yearG, GVal m
monthG, GVal m
dayG, GVal m
hoursG, GVal m
minutesG, GVal m
secondsG] ->
                [GVal m] -> Maybe ZonedTime
forall (m :: * -> *). [GVal m] -> Maybe ZonedTime
go [GVal m
yearG, GVal m
monthG, GVal m
dayG, GVal m
hoursG, GVal m
minutesG, GVal m
secondsG, TimeZone -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal TimeZone
defTZ]
            [GVal m
yearG, GVal m
monthG, GVal m
dayG, GVal m
hoursG, GVal m
minutesG] ->
                [GVal m] -> Maybe ZonedTime
forall (m :: * -> *). [GVal m] -> Maybe ZonedTime
go [GVal m
yearG, GVal m
monthG, GVal m
dayG, GVal m
hoursG, GVal m
minutesG, Int -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int
0 :: Int)]
            [GVal m
yearG, GVal m
monthG, GVal m
dayG] ->
                [GVal m] -> Maybe ZonedTime
forall (m :: * -> *). [GVal m] -> Maybe ZonedTime
go [GVal m
yearG, GVal m
monthG, GVal m
dayG, Int -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int
12 :: Int), Int -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Int
0 :: Int)]
            [GVal m]
_ -> Maybe ZonedTime
forall a. Maybe a
Nothing

gvalAutoParseDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalAutoParseDate :: TimeZone -> GVal m -> Maybe ZonedTime
gvalAutoParseDate TimeZone
defTZ = String -> Maybe ZonedTime
go (String -> Maybe ZonedTime)
-> (GVal m -> String) -> GVal m -> Maybe ZonedTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> String) -> (GVal m -> Text) -> GVal m -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText
    where
        go :: String -> Maybe ZonedTime
go String
input = [Maybe ZonedTime] -> Maybe ZonedTime
forall (t :: * -> *) (f :: * -> *) a.
(Foldable t, Alternative f) =>
t (f a) -> f a
asum [ String -> String -> Maybe ZonedTime
parse String
t String
input | (String -> String -> Maybe ZonedTime
parse, String
t) <- [(String -> String -> Maybe ZonedTime, String)]
formats ]
        ztparse :: String -> String -> Maybe ZonedTime
        ztparse :: String -> String -> Maybe ZonedTime
ztparse String
fmt = Bool -> TimeLocale -> String -> String -> Maybe ZonedTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
fmt
        utcparse :: String -> String -> Maybe ZonedTime
        utcparse :: String -> String -> Maybe ZonedTime
utcparse String
fmt String
input = do
            LocalTime
lt <- Bool -> TimeLocale -> String -> String -> Maybe LocalTime
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
fmt String
input
            ZonedTime -> Maybe ZonedTime
forall (m :: * -> *) a. Monad m => a -> m a
return (ZonedTime -> Maybe ZonedTime) -> ZonedTime -> Maybe ZonedTime
forall a b. (a -> b) -> a -> b
$ LocalTime -> TimeZone -> ZonedTime
ZonedTime LocalTime
lt TimeZone
defTZ
        formats :: [(String -> String -> Maybe ZonedTime, String)]
formats =
            [ (String -> String -> Maybe ZonedTime
ztparse, String
"%Y-%m-%dT%H:%M:%S%Q%Z")
            , (String -> String -> Maybe ZonedTime
utcparse, String
"%Y-%m-%d %H:%M:%S%Q")
            , (String -> String -> Maybe ZonedTime
ztparse, String
"%Y-%m-%d %H:%M:%S%Q%z")
            , (String -> String -> Maybe ZonedTime
ztparse, String
"%Y-%m-%d %H:%M:%S%Q%Z")
            , (String -> String -> Maybe ZonedTime
utcparse, String
"%Y-%m-%d")
            ]

gvalToTZ :: GVal m -> Maybe TimeZone
gvalToTZ :: GVal m -> Maybe TimeZone
gvalToTZ GVal m
g =
    GVal m -> Maybe TimeZone
forall (m :: * -> *) a. FromGVal m a => GVal m -> Maybe a
fromGVal GVal m
g Maybe TimeZone -> Maybe TimeZone -> Maybe TimeZone
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (String -> Maybe TimeZone
parseTZ (String -> Maybe TimeZone)
-> (GVal m -> String) -> GVal m -> Maybe TimeZone
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> String
Text.unpack (Text -> String) -> (GVal m -> Text) -> GVal m -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal m -> Maybe TimeZone) -> GVal m -> Maybe TimeZone
forall a b. (a -> b) -> a -> b
$ GVal m
g)

parseTZ :: String -> Maybe TimeZone
parseTZ :: String -> Maybe TimeZone
parseTZ = Bool -> TimeLocale -> String -> String -> Maybe TimeZone
forall (m :: * -> *) t.
(MonadFail m, ParseTime t) =>
Bool -> TimeLocale -> String -> String -> m t
parseTimeM Bool
True TimeLocale
defaultTimeLocale String
"%z"

gfnDateFormat :: Monad m => Function (Run p m h)
gfnDateFormat :: Function (Run p m h)
gfnDateFormat [(Maybe Text, GVal (Run p m h))]
args =
    let extracted :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted =
            [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
                [ (Text
"date", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"format", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"tz", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"locale", GVal (Run p m h)
forall a. Default a => a
def)
                ]
                [(Maybe Text, GVal (Run p m h))]
args
    in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted of
        Right [GVal (Run p m h)
gDate, GVal (Run p m h)
gFormat, GVal (Run p m h)
gTimeZone, GVal (Run p m h)
gLocale] ->
                -- The desired target timezone; Nothing means keep original timezone
            let tzMay :: Maybe TimeZone
tzMay = GVal (Run p m h) -> Maybe TimeZone
forall (m :: * -> *). GVal m -> Maybe TimeZone
gvalToTZ GVal (Run p m h)
gTimeZone
                -- The default timezone used when the input doesn't include timezone
                -- information; if a target timezone is given, then it is also used as
                -- the default, otherwise, UTC is assumed. The underlying assumptions
                -- are:
                --
                -- * If the input does not include timezone information, then it is a
                --   local time; hence, if the user explicitly passes a time zone for
                --   formatting, it is assumed that this means the original local time
                --   is in that time zone.
                -- * If the input does not include timezone information, and no
                --   explicit timezone is given, the only sane time zone to pick is
                --   UTC. In this situation, the incoming dates either originate from
                --   a system that doesn't track timezone information but implicitly
                --   stores everything in UTC (which is fine), or the formatting
                --   doesn't use timezone information anyway (in which case it doesn't
                --   matter), or the originator of the data uses another timezone but
                --   fails to report it (in which case it is impossible to do the right
                --   thing)
                -- * If the input *does* include timezone information, it should be
                --   respected; explicitly passing timezone information in the date()
                --   call means the user wants to represent the same zoned time in a
                --   different time zone, which means time zone conversion is required.
                defTZ :: TimeZone
defTZ = TimeZone -> Maybe TimeZone -> TimeZone
forall a. a -> Maybe a -> a
fromMaybe TimeZone
utc Maybe TimeZone
tzMay
                dateMay :: Maybe ZonedTime
dateMay = TimeZone -> GVal (Run p m h) -> Maybe ZonedTime
forall (m :: * -> *). TimeZone -> GVal m -> Maybe ZonedTime
gvalToDate TimeZone
defTZ GVal (Run p m h)
gDate
                fmtMay :: Maybe String
fmtMay = Text -> String
Text.unpack (Text -> String) -> Maybe Text -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> GVal (Run p m h) -> Maybe Text
forall (m :: * -> *) a. FromGVal m a => GVal m -> Maybe a
fromGVal GVal (Run p m h)
gFormat
            in case Maybe String
fmtMay of
                Just String
fmt -> do
                    TimeLocale
locale <- Run p m h TimeLocale
-> (TimeLocale -> Run p m h TimeLocale)
-> Maybe TimeLocale
-> Run p m h TimeLocale
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
                        (GVal (Run p m h) -> Run p m h TimeLocale
forall (m :: * -> *) p h.
Monad m =>
GVal (Run p m h) -> Run p m h TimeLocale
getTimeLocale GVal (Run p m h)
gLocale)
                        TimeLocale -> Run p m h TimeLocale
forall (m :: * -> *) a. Monad m => a -> m a
return
                        (GVal (Run p m h) -> Maybe TimeLocale
forall (m :: * -> *) a. FromGVal m a => GVal m -> Maybe a
fromGVal GVal (Run p m h)
gLocale)
                    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (Maybe String -> GVal (Run p m h))
-> Maybe String
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe String -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe String -> Run p m h (GVal (Run p m h)))
-> Maybe String -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ TimeLocale -> String -> ZonedTime -> String
forall t. FormatTime t => TimeLocale -> String -> t -> String
formatTime TimeLocale
locale String
fmt (ZonedTime -> String)
-> (ZonedTime -> ZonedTime) -> ZonedTime -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe TimeZone -> ZonedTime -> ZonedTime
convertTZ Maybe TimeZone
tzMay (ZonedTime -> String) -> Maybe ZonedTime -> Maybe String
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ZonedTime
dateMay
                Maybe String
Nothing -> do
                    GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (Maybe ZonedTime -> GVal (Run p m h))
-> Maybe ZonedTime
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe ZonedTime -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe ZonedTime -> Run p m h (GVal (Run p m h)))
-> Maybe ZonedTime -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe TimeZone -> ZonedTime -> ZonedTime
convertTZ Maybe TimeZone
tzMay (ZonedTime -> ZonedTime) -> Maybe ZonedTime -> Maybe ZonedTime
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe ZonedTime
dateMay
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"date") Text
"expected: (date, format, tz=null, locale=null)"
    where
        convertTZ :: Maybe TimeZone -> ZonedTime -> ZonedTime
        convertTZ :: Maybe TimeZone -> ZonedTime -> ZonedTime
convertTZ Maybe TimeZone
Nothing = ZonedTime -> ZonedTime
forall a. a -> a
id
        convertTZ (Just TimeZone
tz) = TimeZone -> UTCTime -> ZonedTime
utcToZonedTime TimeZone
tz (UTCTime -> ZonedTime)
-> (ZonedTime -> UTCTime) -> ZonedTime -> ZonedTime
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ZonedTime -> UTCTime
zonedTimeToUTC

getTimeLocale :: Monad m => GVal (Run p m h) -> Run p m h TimeLocale
getTimeLocale :: GVal (Run p m h) -> Run p m h TimeLocale
getTimeLocale GVal (Run p m h)
localeName = do
    GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
toFunction (GVal (Run p m h) -> Maybe (Function (Run p m h)))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Maybe (Function (Run p m h)))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Text
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) p h.
Monad m =>
Text -> Run p m h (GVal (Run p m h))
getVar Text
"getlocale" ExceptT
  (RuntimeError p)
  (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
  (Maybe (Function (Run p m h)))
-> (Maybe (Function (Run p m h)) -> Run p m h TimeLocale)
-> Run p m h TimeLocale
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
        Maybe (Function (Run p m h))
Nothing ->
            TimeLocale -> Run p m h TimeLocale
forall (m :: * -> *) a. Monad m => a -> m a
return TimeLocale
defaultTimeLocale
        Just Function (Run p m h)
getlocale -> do
            let args :: [(Maybe Text, GVal (Run p m h))]
args = [ (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"category", GVal (Run p m h)
"LC_TIME")
                       , (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"locale", GVal (Run p m h)
localeName)
                       ]
            TimeLocale -> Maybe TimeLocale -> TimeLocale
forall a. a -> Maybe a -> a
fromMaybe TimeLocale
defaultTimeLocale (Maybe TimeLocale -> TimeLocale)
-> (GVal (Run p m h) -> Maybe TimeLocale)
-> GVal (Run p m h)
-> TimeLocale
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe TimeLocale
forall (m :: * -> *) a. FromGVal m a => GVal m -> Maybe a
fromGVal (GVal (Run p m h) -> TimeLocale)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
-> Run p m h TimeLocale
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Function (Run p m h)
getlocale [(Maybe Text, GVal (Run p m h))]
args


gfnFilter :: Monad m => Function (Run p m h)
gfnFilter :: Function (Run p m h)
gfnFilter [] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
forall a. Default a => a
def
gfnFilter [(Maybe Text
_, GVal (Run p m h)
xs)] = GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return GVal (Run p m h)
xs
gfnFilter ((Maybe Text
_, GVal (Run p m h)
xs):(Maybe Text
_, GVal (Run p m h)
p):[(Maybe Text, GVal (Run p m h))]
args) = do
    Function (Run p m h)
pfnG <- ExceptT
  (RuntimeError p)
  (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
  (Function (Run p m h))
-> (Function (Run p m h)
    -> ExceptT
         (RuntimeError p)
         (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
         (Function (Run p m h)))
-> Maybe (Function (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError RuntimeError p
forall p. RuntimeError p
NotAFunctionError) Function (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Function (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Maybe (Function (Run p m h))
forall (m :: * -> *). GVal m -> Maybe (Function m)
asFunction GVal (Run p m h)
p)
    let pfn :: GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
pfn GVal (Run p m h)
x = GVal (Run p m h) -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean (GVal (Run p m h) -> Bool)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Function (Run p m h)
pfnG ((Maybe Text
forall a. Maybe a
Nothing, GVal (Run p m h)
x)(Maybe Text, GVal (Run p m h))
-> [(Maybe Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
forall a. a -> [a] -> [a]
:[(Maybe Text, GVal (Run p m h))]
args)
        xsl :: [GVal (Run p m h)]
xsl = [GVal (Run p m h)]
-> Maybe [GVal (Run p m h)] -> [GVal (Run p m h)]
forall a. a -> Maybe a -> a
fromMaybe [] (GVal (Run p m h) -> Maybe [GVal (Run p m h)]
forall (m :: * -> *). GVal m -> Maybe [GVal m]
asList GVal (Run p m h)
xs)
    [GVal (Run p m h)]
filtered <- (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      Bool)
-> [GVal (Run p m h)]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [GVal (Run p m h)]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
pfn [GVal (Run p m h)]
xsl
    GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h)
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      (GVal (Run p m h)))
-> GVal (Run p m h)
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal [GVal (Run p m h)]
filtered

printfG :: String -> [GVal m] -> String
printfG :: String -> [GVal m] -> String
printfG String
fmt [GVal m]
args = String -> [PrintfArgT] -> String
forall t. PrintfType t => String -> [PrintfArgT] -> t
printfa String
fmt ((GVal m -> PrintfArgT) -> [GVal m] -> [PrintfArgT]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap GVal m -> PrintfArgT
forall a. PrintfArg a => a -> PrintfArgT
P [GVal m]
args)


gfnDictsort :: Monad m => Function (Run p m h)
gfnDictsort :: Function (Run p m h)
gfnDictsort [(Maybe Text, GVal (Run p m h))]
args =
    let extracted :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted =
            [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
                [ (Text
"dict", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"case_sensitive", GVal (Run p m h)
forall a. Default a => a
def)
                , (Text
"by", GVal (Run p m h)
"key")
                ]
                [(Maybe Text, GVal (Run p m h))]
args
    in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted of
        Right [GVal (Run p m h)
gDict, GVal (Run p m h)
gCaseSensitive, GVal (Run p m h)
gSortBy] -> do
            let caseSensitive :: Bool
caseSensitive = GVal (Run p m h) -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal (Run p m h)
gCaseSensitive
            Bool
sortByKey <- case GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText GVal (Run p m h)
gSortBy of
                Text
"key" -> Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
True
                Text
"value" -> Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
                Text
"val" -> Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall (m :: * -> *) a. Monad m => a -> m a
return Bool
False
                Text
x -> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      Bool)
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"dictsort")
                        ( Text
"argument 'by' must be one of 'key', 'value', 'val', " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                          Text
"but found '" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                          Text
x Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"'"
                        )
            let items :: [(Text, GVal (Run p m h))]
items = [(Text, GVal (Run p m h))]
-> Maybe [(Text, GVal (Run p m h))] -> [(Text, GVal (Run p m h))]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [(Text, GVal (Run p m h))] -> [(Text, GVal (Run p m h))])
-> Maybe [(Text, GVal (Run p m h))] -> [(Text, GVal (Run p m h))]
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h) -> Maybe [(Text, GVal (Run p m h))]
forall (m :: * -> *). GVal m -> Maybe [(Text, GVal m)]
asDictItems GVal (Run p m h)
gDict
            let projection :: (Text, GVal m) -> Text
projection =
                    (if Bool
caseSensitive then Text -> Text
forall a. a -> a
id else Text -> Text
Text.toUpper) (Text -> Text)
-> ((Text, GVal m) -> Text) -> (Text, GVal m) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
                    (if Bool
sortByKey then (Text, GVal m) -> Text
forall a b. (a, b) -> a
fst else (GVal m -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal m -> Text)
-> ((Text, GVal m) -> GVal m) -> (Text, GVal m) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Text, GVal m) -> GVal m
forall a b. (a, b) -> b
snd))
            GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> ([(Text, GVal (Run p m h))] -> GVal (Run p m h))
-> [(Text, GVal (Run p m h))]
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(Text, GVal (Run p m h))] -> GVal (Run p m h)
forall (m :: * -> *). [Pair m] -> GVal m
orderedDict ([(Text, GVal (Run p m h))] -> GVal (Run p m h))
-> ([(Text, GVal (Run p m h))] -> [(Text, GVal (Run p m h))])
-> [(Text, GVal (Run p m h))]
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Text, GVal (Run p m h)) -> Text)
-> [(Text, GVal (Run p m h))] -> [(Text, GVal (Run p m h))]
forall b a. Ord b => (a -> b) -> [a] -> [a]
List.sortOn (Text, GVal (Run p m h)) -> Text
forall (m :: * -> *). (Text, GVal m) -> Text
projection ([(Text, GVal (Run p m h))] -> Run p m h (GVal (Run p m h)))
-> [(Text, GVal (Run p m h))] -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ [(Text, GVal (Run p m h))]
items
        Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"dictsort") Text
"expected: (dict, case_sensitive=false, by=null)"

gfnJSON :: Monad m => Function (Run p m h)
gfnJSON :: Function (Run p m h)
gfnJSON [(Maybe Text, GVal (Run p m h))]
args =
  let extracted :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted =
          [(Text, GVal (Run p m h))]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [GVal (Run p m h)]
forall a.
[(Text, a)]
-> [(Maybe Text, a)] -> Either ([a], HashMap Text a, [Text]) [a]
extractArgsDefL
            [ (Text
"val", GVal (Run p m h)
forall a. Default a => a
def)
            , (Text
"pretty", Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal Bool
True)
            ]
            [(Maybe Text, GVal (Run p m h))]
args
  in case Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [GVal (Run p m h)]
extracted of
      Right [ GVal (Run p m h)
gVal, GVal (Run p m h)
gPretty ] -> do
          let encoder :: GVal (Run p m h) -> ByteString
encoder =
                if GVal (Run p m h) -> Bool
forall (m :: * -> *). GVal m -> Bool
asBoolean GVal (Run p m h)
gPretty then
                    GVal (Run p m h) -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encodePretty
                else
                    GVal (Run p m h) -> ByteString
forall a. ToJSON a => a -> ByteString
JSON.encode
          GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) a. Monad m => a -> m a
return (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> (GVal (Run p m h) -> GVal (Run p m h))
-> GVal (Run p m h)
-> Run p m h (GVal (Run p m h))
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            Text -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Text -> GVal (Run p m h))
-> (GVal (Run p m h) -> Text)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            String -> Text
Text.pack (String -> Text)
-> (GVal (Run p m h) -> String) -> GVal (Run p m h) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            ByteString -> String
UTF8.toString (ByteString -> String)
-> (GVal (Run p m h) -> ByteString) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            ByteString -> ByteString
LBS.toStrict (ByteString -> ByteString)
-> (GVal (Run p m h) -> ByteString)
-> GVal (Run p m h)
-> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
            GVal (Run p m h) -> ByteString
encoder (GVal (Run p m h) -> Run p m h (GVal (Run p m h)))
-> GVal (Run p m h) -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$
            GVal (Run p m h)
gVal
      Left ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
_ -> RuntimeError p -> Run p m h (GVal (Run p m h))
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h (GVal (Run p m h)))
-> RuntimeError p -> Run p m h (GVal (Run p m h))
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"json") Text
"expected: one argument"


-- Built-in tests
--gfnDefined :: Monad m => Function (Run p m h)
--gfnDefined = unaryFunc $ toGVal . isJust . fmap getVar . fromGVal

gfnDivisibleBy :: Monad m => Function (Run p m h)
gfnDivisibleBy :: Function (Run p m h)
gfnDivisibleBy = (GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
-> Function (Run p m h)
forall (m :: * -> *) h p.
Monad m =>
(GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
-> Function (Run p m h)
binaryFunc ((GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
 -> Function (Run p m h))
-> (GVal (Run p m h) -> GVal (Run p m h) -> GVal (Run p m h))
-> Function (Run p m h)
forall a b. (a -> b) -> a -> b
$ \GVal (Run p m h)
x GVal (Run p m h)
y -> Maybe Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe Bool -> GVal (Run p m h)) -> Maybe Bool -> GVal (Run p m h)
forall a b. (a -> b) -> a -> b
$ Integer -> Integer -> Bool
forall a. Integral a => a -> a -> Bool
divisibleBy (Integer -> Integer -> Bool)
-> Maybe Integer -> Maybe (Integer -> Bool)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (GVal (Run p m h) -> Maybe Integer
forall (m :: * -> *). GVal m -> Maybe Integer
toInteger GVal (Run p m h)
x) Maybe (Integer -> Bool) -> Maybe Integer -> Maybe Bool
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (GVal (Run p m h) -> Maybe Integer
forall (m :: * -> *). GVal m -> Maybe Integer
toInteger GVal (Run p m h)
y)
  where divisibleBy :: a -> a -> Bool
divisibleBy a
x a
y = a
x a -> a -> a
forall a. Integral a => a -> a -> a
`Prelude.mod` a
y a -> a -> Bool
forall a. Eq a => a -> a -> Bool
== a
0

gfnEven :: Monad m => Function (Run p m h)
gfnEven :: Function (Run p m h)
gfnEven = (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall (m :: * -> *) h p.
Monad m =>
(GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
unaryFunc ((GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h))
-> (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall a b. (a -> b) -> a -> b
$ Maybe Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe Bool -> GVal (Run p m h))
-> (GVal (Run p m h) -> Maybe Bool)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Bool) -> Maybe Integer -> Maybe Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Bool
forall a. Integral a => a -> Bool
Prelude.even (Maybe Integer -> Maybe Bool)
-> (GVal (Run p m h) -> Maybe Integer)
-> GVal (Run p m h)
-> Maybe Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe Integer
forall (m :: * -> *). GVal m -> Maybe Integer
toInteger

gfnOdd :: Monad m => Function (Run p m h)
gfnOdd :: Function (Run p m h)
gfnOdd = (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall (m :: * -> *) h p.
Monad m =>
(GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
unaryFunc ((GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h))
-> (GVal (Run p m h) -> GVal (Run p m h)) -> Function (Run p m h)
forall a b. (a -> b) -> a -> b
$ Maybe Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe Bool -> GVal (Run p m h))
-> (GVal (Run p m h) -> Maybe Bool)
-> GVal (Run p m h)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Bool) -> Maybe Integer -> Maybe Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Integer -> Bool
forall a. Integral a => a -> Bool
Prelude.odd (Maybe Integer -> Maybe Bool)
-> (GVal (Run p m h) -> Maybe Integer)
-> GVal (Run p m h)
-> Maybe Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Maybe Integer
forall (m :: * -> *). GVal m -> Maybe Integer
toInteger

gfoRegex :: Monad m => GVal (Run p m h)
gfoRegex :: GVal (Run p m h)
gfoRegex =
  [Pair (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *). [Pair m] -> GVal m
dict
    [ (Text
"match", Function (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). Function m -> GVal m
fromFunction Function (Run p m h)
forall p (m :: * -> *) h. Monad m => Function (Run p m h)
gfnReMatchOne)
    , (Text
"matches", Function (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). Function m -> GVal m
fromFunction Function (Run p m h)
forall p (m :: * -> *) h. Monad m => Function (Run p m h)
gfnReMatch)
    , (Text
"test", Function (Run p m h) -> GVal (Run p m h)
forall (m :: * -> *). Function m -> GVal m
fromFunction Function (Run p m h)
forall (m :: * -> *) p h. Monad m => Function (Run p m h)
gfnReTest)
    ]

gfnReMatchOne :: forall p m h. Monad m => Function (Run p m h)
gfnReMatchOne :: Function (Run p m h)
gfnReMatchOne [(Maybe Text, GVal (Run p m h))]
args =
  Maybe (GVal (Run p m h)) -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Maybe (GVal (Run p m h)) -> GVal (Run p m h))
-> (Maybe (String, MatchText String, String)
    -> Maybe (GVal (Run p m h)))
-> Maybe (String, MatchText String, String)
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((String, MatchText String, String) -> GVal (Run p m h))
-> Maybe (String, MatchText String, String)
-> Maybe (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\(String
_, MatchText String
m, String
_) -> MatchText String -> GVal (Run p m h)
forall (m :: * -> *). Monad m => MatchText String -> GVal m
matchTextToGVal MatchText String
m :: GVal (Run p m h)) (Maybe (String, MatchText String, String) -> GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Maybe (String, MatchText String, String))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Regex -> String -> Maybe (String, MatchText String, String))
-> [(Maybe Text, GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (Maybe (String, MatchText String, String))
forall (m :: * -> *) a p h.
Monad m =>
(Regex -> String -> a)
-> [(Maybe Text, GVal (Run p m h))] -> Run p m h a
fnReMatch Regex -> String -> Maybe (String, MatchText String, String)
forall regex source.
RegexLike regex source =>
regex -> source -> Maybe (source, MatchText source, source)
RE.matchOnceText [(Maybe Text, GVal (Run p m h))]
args

gfnReMatch :: forall p m h. Monad m => Function (Run p m h)
gfnReMatch :: Function (Run p m h)
gfnReMatch [(Maybe Text, GVal (Run p m h))]
args =
  [GVal (Run p m h)] -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([GVal (Run p m h)] -> GVal (Run p m h))
-> ([MatchText String] -> [GVal (Run p m h)])
-> [MatchText String]
-> GVal (Run p m h)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (MatchText String -> GVal (Run p m h))
-> [MatchText String] -> [GVal (Run p m h)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (MatchText String -> GVal (Run p m h)
forall (m :: * -> *). Monad m => MatchText String -> GVal m
matchTextToGVal :: RE.MatchText String -> GVal (Run p m h)) ([MatchText String] -> GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [MatchText String]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Regex -> String -> [MatchText String])
-> [(Maybe Text, GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     [MatchText String]
forall (m :: * -> *) a p h.
Monad m =>
(Regex -> String -> a)
-> [(Maybe Text, GVal (Run p m h))] -> Run p m h a
fnReMatch Regex -> String -> [MatchText String]
forall regex source.
RegexLike regex source =>
regex -> source -> [MatchText source]
RE.matchAllText [(Maybe Text, GVal (Run p m h))]
args

gfnReTest :: Monad m => Function (Run p m h)
gfnReTest :: Function (Run p m h)
gfnReTest [(Maybe Text, GVal (Run p m h))]
args =
  Bool -> GVal (Run p m h)
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal (Bool -> GVal (Run p m h))
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     (GVal (Run p m h))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Regex -> String -> Bool)
-> [(Maybe Text, GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     Bool
forall (m :: * -> *) a p h.
Monad m =>
(Regex -> String -> a)
-> [(Maybe Text, GVal (Run p m h))] -> Run p m h a
fnReMatch Regex -> String -> Bool
forall regex source.
RegexLike regex source =>
regex -> source -> Bool
RE.matchTest [(Maybe Text, GVal (Run p m h))]
args

matchTextToGVal :: Monad m => RE.MatchText String -> GVal m
matchTextToGVal :: MatchText String -> GVal m
matchTextToGVal MatchText String
matchArr =
  let base :: GVal m
base = [Text] -> GVal m
forall (m :: * -> *) a. ToGVal m a => a -> GVal m
toGVal ([Text] -> GVal m)
-> (MatchText String -> [Text]) -> MatchText String -> GVal m
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array Int Text -> [Text]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (Array Int Text -> [Text])
-> (MatchText String -> Array Int Text)
-> MatchText String
-> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((String, (Int, Int)) -> Text)
-> MatchText String -> Array Int Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
Text.pack (String -> Text)
-> ((String, (Int, Int)) -> String) -> (String, (Int, Int)) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, (Int, Int)) -> String
forall a b. (a, b) -> a
fst) (MatchText String -> GVal m) -> MatchText String -> GVal m
forall a b. (a -> b) -> a -> b
$ MatchText String
matchArr
      textRepr :: Text
textRepr = Text -> Maybe Text -> Text
forall a. a -> Maybe a -> a
fromMaybe Text
"" (Maybe Text -> Text)
-> (MatchText String -> Maybe Text) -> MatchText String -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((String, (Int, Int)) -> Text)
-> Maybe (String, (Int, Int)) -> Maybe Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (String -> Text
Text.pack (String -> Text)
-> ((String, (Int, Int)) -> String) -> (String, (Int, Int)) -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (String, (Int, Int)) -> String
forall a b. (a, b) -> a
fst) (Maybe (String, (Int, Int)) -> Maybe Text)
-> (MatchText String -> Maybe (String, (Int, Int)))
-> MatchText String
-> Maybe Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(String, (Int, Int))] -> Maybe (String, (Int, Int))
forall a. [a] -> Maybe a
headMay ([(String, (Int, Int))] -> Maybe (String, (Int, Int)))
-> (MatchText String -> [(String, (Int, Int))])
-> MatchText String
-> Maybe (String, (Int, Int))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. MatchText String -> [(String, (Int, Int))]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList (MatchText String -> Text) -> MatchText String -> Text
forall a b. (a -> b) -> a -> b
$ MatchText String
matchArr
  in GVal m
forall (m :: * -> *). GVal m
base
      { asText :: Text
asText = Text
textRepr
      , asHtml :: Html
asHtml = Text -> Html
html Text
textRepr
      }

fnReMatch :: (Regex -> String -> a)
-> [(Maybe Text, GVal (Run p m h))] -> Run p m h a
fnReMatch Regex -> String -> a
matchFunc [(Maybe Text, GVal (Run p m h))]
args =
  let gArgs :: Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [Maybe (GVal (Run p m h))]
gArgs = [Text]
-> [(Maybe Text, GVal (Run p m h))]
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [Maybe (GVal (Run p m h))]
forall a.
[Text]
-> [(Maybe Text, a)]
-> Either ([a], HashMap Text a, [Text]) [Maybe a]
extractArgsL [Text
"re", Text
"haystack", Text
"opts"] [(Maybe Text, GVal (Run p m h))]
args
  in (([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
 -> Run p m h a)
-> ([Maybe (GVal (Run p m h))] -> Run p m h a)
-> Either
     ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
     [Maybe (GVal (Run p m h))]
-> Run p m h a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Run p m h a
-> ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
-> Run p m h a
forall a b. a -> b -> a
const Run p m h a
forall p h a. Run p m h a
barf) [Maybe (GVal (Run p m h))] -> Run p m h a
forall p h.
[Maybe (GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
go Either
  ([GVal (Run p m h)], HashMap Text (GVal (Run p m h)), [Text])
  [Maybe (GVal (Run p m h))]
gArgs
  where
    go :: [Maybe (GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
go = \case
      [Maybe (GVal (Run p m h))
r, Maybe (GVal (Run p m h))
h, Maybe (GVal (Run p m h))
Nothing] ->
        [Maybe (GVal (Run p m h))]
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
go [Maybe (GVal (Run p m h))
r, Maybe (GVal (Run p m h))
h, GVal (Run p m h) -> Maybe (GVal (Run p m h))
forall a. a -> Maybe a
Just GVal (Run p m h)
forall a. Default a => a
def]
      [Just GVal (Run p m h)
reG, Just GVal (Run p m h)
haystackG, Just GVal (Run p m h)
optsG] -> do
        CompOption
opts <- GVal (Run p m h) -> Run p m h CompOption
forall (m :: * -> *) p h.
Monad m =>
GVal (Run p m h) -> Run p m h CompOption
parseCompOpts GVal (Run p m h)
optsG
        let reM :: Either String Regex
reM = Identity (Either String Regex) -> Either String Regex
forall a. Identity a -> a
runIdentity (Identity (Either String Regex) -> Either String Regex)
-> (ErrorT String Identity Regex -> Identity (Either String Regex))
-> ErrorT String Identity Regex
-> Either String Regex
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ErrorT String Identity Regex -> Identity (Either String Regex)
forall e (m :: * -> *) a. ErrorT e m a -> m (Either e a)
runErrorT (ErrorT String Identity Regex -> Either String Regex)
-> ErrorT String Identity Regex -> Either String Regex
forall a b. (a -> b) -> a -> b
$
                      CompOption -> ExecOption -> String -> ErrorT String Identity Regex
forall regex compOpt execOpt source (m :: * -> *).
(RegexMaker regex compOpt execOpt source, MonadFail m) =>
compOpt -> execOpt -> source -> m regex
RE.makeRegexOptsM CompOption
opts ExecOption
forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
execOpt
RE.defaultExecOpt (Text -> String
Text.unpack (Text -> String)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal (Run p m h) -> String) -> GVal (Run p m h) -> String
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
reG)
            haystack :: String
haystack = Text -> String
Text.unpack (Text -> String)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal (Run p m h) -> String) -> GVal (Run p m h) -> String
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
haystackG
        case Either String Regex
reM of
            Left String
err ->
                RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      a)
-> RuntimeError p
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"re.match") (Text -> RuntimeError p) -> Text -> RuntimeError p
forall a b. (a -> b) -> a -> b
$ Text
"invalid regex: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
err
            Right Regex
re ->
                a
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
forall (m :: * -> *) a. Monad m => a -> m a
return (a
 -> ExceptT
      (RuntimeError p)
      (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
      a)
-> a
-> ExceptT
     (RuntimeError p)
     (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
     a
forall a b. (a -> b) -> a -> b
$ Regex -> String -> a
matchFunc Regex
re String
haystack
      [Maybe (GVal (Run p m h))]
_ -> ExceptT
  (RuntimeError p)
  (StateT (RunState p m h) (ReaderT (GingerContext p m h) m))
  a
forall p h a. Run p m h a
barf
    barf :: Run p m h a
barf = do
      RuntimeError p -> Run p m h a
forall (m :: * -> *) p h a.
Monad m =>
RuntimeError p -> Run p m h a
throwHere (RuntimeError p -> Run p m h a) -> RuntimeError p -> Run p m h a
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError (Text -> Maybe Text
forall a. a -> Maybe a
Just Text
"re.match") Text
"expected: regex, haystack, [opts]"

parseCompOpts :: Monad m => GVal (Run p m h) -> Run p m h RE.CompOption
parseCompOpts :: GVal (Run p m h) -> Run p m h CompOption
parseCompOpts GVal (Run p m h)
g = do
  let str :: String
str = Text -> String
Text.unpack (Text -> String)
-> (GVal (Run p m h) -> Text) -> GVal (Run p m h) -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. GVal (Run p m h) -> Text
forall (m :: * -> *). GVal m -> Text
asText (GVal (Run p m h) -> String) -> GVal (Run p m h) -> String
forall a b. (a -> b) -> a -> b
$ GVal (Run p m h)
g
  (CompOption -> Char -> Run p m h CompOption)
-> CompOption -> String -> Run p m h CompOption
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m b
foldM
    (\CompOption
x ->
      \case
        Char
'i' -> CompOption -> Run p m h CompOption
forall (m :: * -> *) a. Monad m => a -> m a
return CompOption
x { caseSensitive :: Bool
RE.caseSensitive = Bool
False }
        Char
'm' -> CompOption -> Run p m h CompOption
forall (m :: * -> *) a. Monad m => a -> m a
return CompOption
x { multiline :: Bool
RE.multiline = Bool
True }
        Char
c -> do
          RuntimeError p -> Run p m h ()
forall (m :: * -> *) p h. Monad m => RuntimeError p -> Run p m h ()
warn (RuntimeError p -> Run p m h ()) -> RuntimeError p -> Run p m h ()
forall a b. (a -> b) -> a -> b
$ Maybe Text -> Text -> RuntimeError p
forall p. Maybe Text -> Text -> RuntimeError p
ArgumentsError Maybe Text
forall a. Maybe a
Nothing (Text -> RuntimeError p) -> Text -> RuntimeError p
forall a b. (a -> b) -> a -> b
$ Text
"unexpected regex modifier: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Char -> Text
forall a. Show a => a -> Text
tshow Char
c
          CompOption -> Run p m h CompOption
forall (m :: * -> *) a. Monad m => a -> m a
return CompOption
x
    )
    CompOption
forall regex compOpt execOpt.
RegexOptions regex compOpt execOpt =>
compOpt
RE.blankCompOpt
    String
str

-- vim: sw=4 ts=4 expandtab