Skip to content

Instantly share code, notes, and snippets.

@vrom911
Last active March 1, 2024 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrom911/118c73abff0e25369efc46e4d6c6f290 to your computer and use it in GitHub Desktop.
Save vrom911/118c73abff0e25369efc46e4d6c6f290 to your computer and use it in GitHub Desktop.
Kadena Interview
import Control.Monad
import Data.ByteString(ByteString)
import Control.Monad (ExceptT (..))
import Data.Kind
type Errors = [String]
data HeistConfig (m :: Type -> Type) = HeistConfig
newtype TemplateName = TemplateName String
data HeistState (n :: Type -> Type) = HeistState
data MIMEType
initHeist :: Monad n => HeistConfig n -> n (Either Errors (HeistState n))
initHeist = undefined
renderTemplate :: Monad n => HeistState n -> TemplateName -> Maybe (n Builder, MIMEType)
renderTemplate = undefined
toByteString :: Builder -> ByteString
toByteString = undefined
yourFunc1 :: HeistConfig IO -> TemplateName -> IO (Either Errors ByteString)
yourFunc1 hc nm = do
eitherState <- initHeist hc
case eitherState of
Left errs -> pure $ Left errs
Right heistState -> do
case renderTemplate heistState nm of
Nothing -> pure $ Left
Just (ioBuilder, _mt) -> Right . toByteString <$> ioBuilder
renderTemplateEither :: Monad n => HeistState n -> TemplateName -> Either Errors (n Builder, MIMETYpe)
renderTemplateEither hs tn = case renderTemplate hs tn of
Nothing -> Left ["Can not rennder template"]
Just x -> Right x
yourFunc2 :: HeistConfig IO -> TemplateName -> IO (Either Errors ByteString)
yourFunc2 hc nm = runExceptT $ do
hState <- ExceptT (initHeist hc)
(ioBuilder, _mt) <- ExceptT (pure $ renderTemplateEither state nm)
ExceptT (Right . toByteString <$> ioBuilder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment