@@ -22,21 +22,21 @@ is handled independently on its own
2222struct Eff{Effectful, Fs}
2323 effectful:: Effectful
2424 cont:: Continuation{Fs}
25+ end
26+
27+ function Eff (effectful:: E , cont:: Continuation{Tuple{}} ) where E<: NoEffect
28+ # also run this if isempty(cont) to stop infinite recursion
29+ # (which happens otherwise because empty cont results returns noeffect for convenience)
30+ Eff {E, Tuple{}} (effectful, cont)
31+ end
2532
26- function Eff (effectful:: T , cont:: Continuation{Fs} ) where {T, Fs}
27- if ! isempty (cont) && effectful isa NoEffect
28- # Evaluate NoEffect directly to make sure, we don't have a chain of NoEffect function accumulating
29- # e.g. with ContextManager this could lead to things not being evaluated, while the syntax suggest
30- # everything is evaluated, and hence the ContextManager may finalize resource despite they are still used.
31- cont (effectful. value)
32-
33- else
34- # also run this if isempty(cont) to stop infinite recursion
35- # (which happens otherwise because empty cont results returns noeffect for convenience)
36- new {T, Fs} (effectful, cont)
37- end
38- end
33+ function Eff (effectful:: NoEffect , cont:: Continuation{Fs} ) where Fs
34+ # Evaluate NoEffect directly to make sure, we don't have a chain of NoEffect function accumulating
35+ # e.g. with ContextManager this could lead to things not being evaluated, while the syntax suggest
36+ # everything is evaluated, and hence the ContextManager may finalize resource despite they are still used.
37+ cont (effectful. value)
3938end
39+
4040Eff (effectful) = Eff (effectful, Continuation ())
4141
4242function Base. show (io:: IO , eff:: Eff )
@@ -60,16 +60,17 @@ effect(eff::Eff) = eff # if we find a Eff effect, we just directly use it
6060# Functionalities for Continuation
6161# --------------------------------
6262
63+ function (c:: Continuation{Tuple{}} )(value)
64+ # mapping empty continuation is the same as wrapping into noeffect
65+ noeffect (value)
66+ end
6367function (c:: Continuation )(value)
64- if isempty (c)
65- noeffect (value)
66- else
67- first_func = c. functions[1 ]
68- rest = c. functions[2 : end ]
69- eff = first_func (value)
70- Eff (eff. effectful, Continuation (eff. cont. functions... , rest... ))
71- end
68+ first_func = Base. first (c. functions)
69+ rest = Base. tail (c. functions)
70+ eff = first_func (value)
71+ Eff (eff. effectful, Continuation (eff. cont. functions... , rest... ))
7272end
73+
7374Base. isempty (c:: Continuation ) = Base. isempty (c. functions)
7475Base. map (f, c:: Continuation ) = Continuation (c. functions... , noeffect ∘ f)
7576
0 commit comments