Skip to content

Commit 5a32eed

Browse files
committed
Add deferManyElse and deferEither to Heist.Compiled
1 parent 7b7a09c commit 5a32eed

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

CONTRIBUTORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ Carl Howells <chowells79@gmail.com>
44
Edward Kmett
55
Will Langstroth <will@langstroth.com>
66
Shane O'Brien <shane@duairc.com>
7+
Kari Pahula <kaol@iki.fi>
78
James Sanders <jimmyjazz14@gmail.com>
89
Mark Wright <gienah@gentoo.org>

src/Heist/Compiled.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ module Heist.Compiled
2828
, pureSplice
2929

3030
, deferMany
31+
, deferManyElse
3132
, defer
33+
, deferEither
3234
, deferMap
3335
, mayDeferMap
3436
, bindLater

src/Heist/Compiled/Internal.hs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,27 @@ deferMany :: (Foldable f, Monad n)
749749
=> (RuntimeSplice n a -> Splice n)
750750
-> RuntimeSplice n (f a)
751751
-> Splice n
752-
deferMany f getItems = do
752+
deferMany = deferManyElse $ return mempty
753+
754+
755+
------------------------------------------------------------------------------
756+
-- | A version of 'deferMany' which has a default splice to run in the case
757+
-- when there are no elements in the given list.
758+
deferManyElse :: (Foldable f, Monad n)
759+
=> Splice n
760+
-> (RuntimeSplice n a -> Splice n)
761+
-> RuntimeSplice n (f a)
762+
-> Splice n
763+
deferManyElse def f getItems = do
753764
promise <- newEmptyPromise
754765
chunks <- f $ getPromise promise
766+
defaultChunk <- def
755767
return $ yieldRuntime $ do
756768
items <- getItems
757-
foldMapM (\item -> putPromise promise item >> codeGen chunks) items
769+
if null items
770+
then codeGen defaultChunk
771+
else foldMapM (\item -> putPromise promise item >>
772+
codeGen chunks) items
758773

759774

760775
------------------------------------------------------------------------------
@@ -773,6 +788,23 @@ defer pf n = do
773788
return $ action `mappend` res
774789

775790

791+
------------------------------------------------------------------------------
792+
-- | Much like 'either', takes a runtime computation and branches to the
793+
-- respective splice depending on the runtime value.
794+
deferEither :: Monad n
795+
=> (RuntimeSplice n a -> Splice n)
796+
-> (RuntimeSplice n b -> Splice n)
797+
-> RuntimeSplice n (Either a b) -> Splice n
798+
deferEither pfa pfb n = do
799+
pa <- newEmptyPromise
800+
pb <- newEmptyPromise
801+
failureChunk <- pfa $ getPromise pa
802+
successChunk <- pfb $ getPromise pb
803+
return $ yieldRuntime $ n >>= either
804+
(\x -> putPromise pa x >> codeGen failureChunk)
805+
(\x -> putPromise pb x >> codeGen successChunk)
806+
807+
776808
------------------------------------------------------------------------------
777809
-- | A version of defer which applies a function on the runtime value.
778810
deferMap :: Monad n

0 commit comments

Comments
 (0)