Skip to content

Commit 9f24303

Browse files
committed
Merge branch 'foldE' into sequence
Conflicts: effect/fold.py effect/test_fold.py
2 parents 9fe32ef + a935029 commit 9f24303

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

effect/fold.py

Lines changed: 4 additions & 13 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,18 +19,9 @@ 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.
25-
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)
22+
return acc.on(lambda r: element.on(lambda r2: f(r, r2)))
23+
24+
return reduce(folder, effects, Effect(Constant(initial)))
3425

3526

3627
def sequence(effects):

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, sequence
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,10 +18,21 @@ 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'
2225

2326

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
34+
35+
2436
def test_sequence():
2537
effs = [Effect('a'), Effect('b'), Effect('c')]
2638
dispatcher = SequenceDispatcher([

0 commit comments

Comments
 (0)