Skip to content

Commit 7599f20

Browse files
committed
make state usable if reducer or its upstream is unactionable
1 parent b76a2e9 commit 7599f20

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

computation_graph/composers/memory.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,13 @@ def lag(state, current):
5353

5454
@gamla.curry
5555
def with_state(key: str, default, f: Callable) -> base_types.GraphType:
56-
return composers.make_compose_future(f, f, key, default)
56+
# Include a bypass for last state if reducer (or its upstream) in current state is unactionable
57+
def passthrough(x):
58+
return x
59+
60+
f_with_bypass = composers.make_first(f, passthrough)
61+
62+
return base_types.merge_graphs(
63+
composers.make_compose_future(f, f_with_bypass, key, default),
64+
composers.make_compose_future(passthrough, f_with_bypass, None, default),
65+
)

computation_graph/graph_runners.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,11 @@ def variadic_with_state_and_expectations(g, sink):
8080
def inner(turns):
8181
prev = {}
8282
for turn, expectation in turns:
83-
prev = f(prev, turn)
83+
new = f(prev, turn)
8484
assert (
85-
prev[graph.make_computation_node(sink)] == expectation
86-
), f"actual={prev[graph.make_computation_node(sink)]}\n expected: {expectation}"
85+
new[graph.make_computation_node(sink)] == expectation
86+
), f"actual={new[graph.make_computation_node(sink)]}\n expected: {expectation}"
87+
prev = new
8788

8889
return inner
8990

computation_graph/graph_test.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -611,15 +611,12 @@ def output_node(x):
611611
)
612612
graph_runners.unary_with_state_and_expectations(
613613
composers.compose_left(
614-
composers.make_first(
615-
composers.make_compose(
616-
composers.compose_left(
617-
skip_or_passthrough, remember_first, key="upstream"
618-
),
619-
input_node,
620-
key="input",
614+
composers.compose_left(
615+
input_node,
616+
composers.compose_left(
617+
skip_or_passthrough, remember_first, key="upstream"
621618
),
622-
lambda: "state skipped",
619+
key="input",
623620
),
624621
output_node,
625622
),
@@ -628,7 +625,7 @@ def output_node(x):
628625
)(
629626
[
630627
["remember this", "remember this"],
631-
["skip state", "state skipped"],
628+
["skip state", "remember this"],
632629
["recall", "remember this"],
633630
]
634631
)

0 commit comments

Comments
 (0)