Predict the output
Replay the account's events. What's the balance?
events = [("opened", 0), ("deposited", 100), ("withdrew", 30),
("deposited", 5)]
balance = 0
for kind, amount in events:
if kind == "deposited":
balance += amount
elif kind == "withdrew":
balance -= amount
print(balance)Hint 1 of 1
Fold every event in order.
Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 75
Correct. 0 + 100 − 30 + 5.
In event sourcing, current state is a fold over the history: start at 0 and apply each event in order. 100 in, 30 out and 5 in gives 75.
Why not the others?
- B
105. The withdrawal subtracts 30. - C
5. State is the sum of all events, not the last one. - D
135. Withdrawals subtract.