Skip to content

Instantly share code, notes, and snippets.

@mightybyte
Created March 15, 2011 17:25
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 mightybyte/871084 to your computer and use it in GitHub Desktop.
Save mightybyte/871084 to your computer and use it in GitHub Desktop.
mapBind variations
------------------------------------------------------------------------------
-- | Runs the parameter node's children and returns the resulting node list.
-- By itself this function is a simple passthrough splice that makes the
-- spliced node disappear. In combination with locally bound splices, this
-- function makes it easier to pass the desired view into your splices.
runChildren :: Monad m => Splice m
runChildren = runNodeList . X.childNodes =<< getParamNode
------------------------------------------------------------------------------
-- | Binds a list of splices before using the children of the spliced node as
-- a view.
viewWith :: (Monad m)
=> [(Text, Splice m)]
-- ^ List of splices to bind before running the param nodes.
-> Splice m
-- ^ Returns the passed in view.
viewWith splices = localTS (bindSplices splices) runChildren
------------------------------------------------------------------------------
-- | Wrapper around viewWith that applies a transformation function to the
-- second item in each of the tuples before calling viewWith.
viewTrans :: (Monad m)
=> (b -> Splice m)
-- ^ Splice generating function
-> [(Text, b)]
-- ^ List of tuples to be bound
-> Splice m
viewTrans f = viewWith . map (second f)
------------------------------------------------------------------------------
-- | Like viewWith but using constant templates rather than dynamic splices.
viewWithTemplates :: (Monad m) => [(Text, Template)] -> Splice m
viewWithTemplates = viewTrans return
------------------------------------------------------------------------------
-- | Like viewWith but using literal text rather than dynamic splices.
viewWithText :: (Monad m) => [(Text, Text)] -> Splice m
viewWithText = viewTrans (return . (:[]) . X.TextNode)
------------------------------------------------------------------------------
-- | Maps a splice generating function over a list and concatenates the
-- results.
mapSplices :: (Monad m)
=> (a -> Splice m)
-- ^ Function applied to each element of the list to generate splices
-> [a]
-- ^ List of items to generate splices for
-> Splice m
-- ^ The result of all splices concatenated together.
mapSplices f vs = liftM concat $ mapM f vs
-- Here's an example of how the above functions might be used.
postParams :: Monad m => Post -> [(Text, Text)]
postParams p =
[("title", postTitle p), ("body", postBody p)]
allPosts :: Monad m => Splice m
allPosts = do
posts <- lift getAllPosts
mapSplices (viewWithText . postParams) posts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment