Skip to content

Commit a935029

Browse files
committed
get rid of the "_sneaky" hack. Wasn't worth it, because to make it work in the empty case required an Effect(Constant()) anyway.
1 parent f2e449a commit a935029

2 files changed

Lines changed: 17 additions & 14 deletions

File tree

effect/fold.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from functools import reduce
22

3-
_sneaky = object()
3+
from effect import Constant, Effect
44

55

66
def fold_effect(f, initial, effects):
@@ -19,15 +19,6 @@ def fold_effect(f, initial, effects):
1919
"""
2020

2121
def folder(acc, element):
22-
# A bit of a hack here. We could just wrap `initial` in a
23-
# Effect(Constant()), but to simplify testing we avoid the additional
24-
# Effect by "sneaking" it in this way.
22+
return acc.on(lambda r: element.on(lambda r2: f(r, r2)))
2523

26-
# This way people can use SequenceDispatcher and not get anything
27-
# unexpected.
28-
if acc is _sneaky:
29-
return element.on(lambda r: f(initial, r))
30-
else:
31-
return acc.on(lambda r: element.on(lambda r2: f(r, r2)))
32-
33-
return reduce(folder, effects, _sneaky)
24+
return reduce(folder, effects, Effect(Constant(initial)))

effect/test_fold.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11

22
import operator
33

4-
from effect import Effect, sync_perform
4+
from effect import ComposedDispatcher, Effect, base_dispatcher, sync_perform
55
from effect.fold import fold_effect
66
from effect.testing import SequenceDispatcher
77

88

99
def test_fold_effect():
10+
"""Behaves like foldM."""
1011
effs = [Effect('a'), Effect('b'), Effect('c')]
1112

1213
dispatcher = SequenceDispatcher([
@@ -17,5 +18,16 @@ def test_fold_effect():
1718
eff = fold_effect(operator.add, 'Nil', effs)
1819

1920
with dispatcher.consume():
20-
result = sync_perform(dispatcher, eff)
21+
result = sync_perform(
22+
ComposedDispatcher([dispatcher, base_dispatcher]),
23+
eff)
2124
assert result == 'NilEiBeeCee'
25+
26+
27+
def test_fold_effect_empty():
28+
"""
29+
Returns an Effect resulting in the initial value when there are no effects.
30+
"""
31+
eff = fold_effect(operator.add, 0, [])
32+
result = sync_perform(base_dispatcher, eff)
33+
assert result == 0

0 commit comments

Comments
 (0)