@@ -56,29 +56,37 @@ def straight_reducer(
5656 action : ActionType ,
5757) -> CountStateType :
5858 if state is None :
59- if isinstance (action , InitAction ):
60- return CountStateType (count = 0 )
61- raise InitializationActionError (action )
62- if isinstance (action , IncrementAction ):
63- return CountStateType (count = state .count + 1 )
64- if isinstance (action , DecrementAction ):
65- return CountStateType (count = state .count - 1 )
66- return state
59+ match action :
60+ case InitAction ():
61+ return CountStateType (count = 0 )
62+ case _:
63+ raise InitializationActionError (action )
64+ match action :
65+ case IncrementAction ():
66+ return CountStateType (count = state .count + 1 )
67+ case DecrementAction ():
68+ return CountStateType (count = state .count - 1 )
69+ case _:
70+ return state
6771
6872
6973def base10_reducer (
7074 state : CountStateType | None ,
7175 action : ActionType ,
7276) -> CountStateType :
7377 if state is None :
74- if isinstance (action , InitAction ):
75- return CountStateType (count = 10 )
76- raise InitializationActionError (action )
77- if isinstance (action , IncrementAction ):
78- return CountStateType (count = state .count + 1 )
79- if isinstance (action , DecrementAction ):
80- return CountStateType (count = state .count - 1 )
81- return state
78+ match action :
79+ case InitAction ():
80+ return CountStateType (count = 10 )
81+ case _:
82+ raise InitializationActionError (action )
83+ match action :
84+ case IncrementAction ():
85+ return CountStateType (count = state .count + 1 )
86+ case DecrementAction ():
87+ return CountStateType (count = state .count - 1 )
88+ case _:
89+ return state
8290
8391
8492class SleepEvent (BaseEvent ):
@@ -90,20 +98,24 @@ def inverse_reducer(
9098 action : ActionType ,
9199) -> ReducerResult [CountStateType , ActionType , SleepEvent ]:
92100 if state is None :
93- if isinstance (action , InitAction ):
94- return CountStateType (count = 0 )
95- raise InitializationActionError (action )
96- if isinstance (action , IncrementAction ):
97- return CountStateType (count = state .count - 1 )
98- if isinstance (action , DecrementAction ):
99- return CountStateType (count = state .count + 1 )
100- if isinstance (action , DoNothingAction ):
101- return CompleteReducerResult (
102- state = state ,
103- actions = [IncrementAction ()],
104- events = [SleepEvent (duration = 3 )],
105- )
106- return state
101+ match action :
102+ case InitAction ():
103+ return CountStateType (count = 0 )
104+ case _:
105+ raise InitializationActionError (action )
106+ match action :
107+ case IncrementAction ():
108+ return CountStateType (count = state .count - 1 )
109+ case DecrementAction ():
110+ return CountStateType (count = state .count + 1 )
111+ case DoNothingAction ():
112+ return CompleteReducerResult (
113+ state = state ,
114+ actions = [IncrementAction ()],
115+ events = [SleepEvent (duration = 3 )],
116+ )
117+ case _:
118+ return state
107119
108120
109121reducer , reducer_id = combine_reducers (
0 commit comments