module Spark.Core.Internal.Utilities(
LB.HasCallStack,
UnknownType,
pretty,
myGroupBy,
myGroupBy',
CanRename(..),
missing,
failure,
failure',
forceRight,
show',
encodeDeterministicPretty,
strictList,
traceHint,
SF.sh,
(<&>),
(<>)
) where
import Data.Aeson
import Data.Aeson.Encode.Pretty
import qualified Data.ByteString.Lazy.Char8 as Char8
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text as T
import qualified Formatting.ShortFormatters as SF
import Control.Arrow ((&&&))
import Data.List
import Data.Function
import Data.Text(Text)
import Formatting
import Debug.Trace(trace)
import qualified Data.Map.Strict as M
import Data.Monoid((<>))
import qualified Spark.Core.Internal.LocatedBase as LB
(<&>) :: Functor f => f a -> (a -> b) -> f b
(<&>) = flip fmap
data UnknownType
pretty :: Value -> Text
pretty = T.pack . Char8.unpack . encodeDeterministicPretty
encodeDeterministicPretty :: Value -> LBS.ByteString
encodeDeterministicPretty =
encodePretty' (defConfig { confIndent = Spaces 0, confCompare = compare })
myGroupBy' :: (Ord b) => (a -> b) -> [a] -> [(b, [a])]
myGroupBy' f = map (f . head &&& id)
. groupBy ((==) `on` f)
. sortBy (compare `on` f)
myGroupBy :: (Ord a) => [(a, b)] -> M.Map a [b]
myGroupBy l = let
l2 = myGroupBy' fst l in
M.map (snd <$>) $ M.fromList l2
class CanRename a txt where
(@@) :: a -> txt -> a
infixl 1 @@
missing :: (LB.HasCallStack) => Text -> a
missing msg = LB.error $ T.concat ["MISSING IMPLEMENTATION: ", msg]
failure :: (LB.HasCallStack) => Text -> a
failure msg = LB.error (T.concat ["FAILURE in Spark. Hint: ", msg])
failure' :: (LB.HasCallStack) => Format Text (a -> Text) -> a -> c
failure' x = failure . sformat x
forceRight :: (LB.HasCallStack, Show a) => Either a b -> b
forceRight (Right b) = b
forceRight (Left a) = LB.error $
sformat ("Failure from either, got instead a left: "%shown) a
strictList :: (Show a) => [a] -> [a]
strictList [] = []
strictList (h : t) = let !t' = strictList t in (h : t')
traceHint :: (Show a) => Text -> a -> a
traceHint hint x = trace (T.unpack hint ++ show x) x
show' :: (Show a) => a -> Text
show' x = T.pack (show x)