Skip to content

Commit f58893b

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

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-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: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -749,12 +749,28 @@ 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+
-- Use this instead of null for compatibility with pre 4.8 base
770+
if foldr (\_ _ -> False) True items
771+
then codeGen defaultChunk
772+
else foldMapM (\item -> putPromise promise item >>
773+
codeGen chunks) items
758774

759775

760776
------------------------------------------------------------------------------
@@ -773,6 +789,23 @@ defer pf n = do
773789
return $ action `mappend` res
774790

775791

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

0 commit comments

Comments
 (0)