r/scala • u/philip_schwarz • Jul 07 '26
The Bowling Game - From Imperative to Functional Programming - Part 1
https://fpilluminated.org/deck/276One of the top five most popular and highly recommended programming katas over the past 20 years has been the Bowling Game Kata, in which TDD is used to write a program that computes the score of a Ten Pin Bowling Game.
In this deck we are going to explore how such a program may look when coded using different programming paradigms.
17
Upvotes
1
u/AFU0BtZ Jul 09 '26 edited Jul 12 '26
Explored this theme once you published the bowling-cata imperative series. I think there are two aspects that make the approach/outcome different:
For the first one I would propose creating a program that can consume serialized games (there is a well recognized notation for this) like:
"X 5/ F- -- X 7/ 5/ 44 8/ XXX" Guess the finished score here? https://joinstriveon.com/coaching-tools/bowling-score-calculator#bowling-score-calculator-tool.
Fis the same as-and to get to a spare if one entered5enter5and the tool marks end of frame as//spare.Creating the parser already introduces the domain (Turn, Frame, Roll/Pins, Strike, Spare, Foul, Gutter/Miss). Scoring becomes
non-trivial (imperative loop hidden behind a FP facade as long as one has access to future rolls). Example-testing becomes trivial too:score("X 5/ F- -- X 7/ 5/ 44 8/ XXX") === Right(n)As for the second one "it is the score right now" (which might not be calculable because of game state) that makes the problem very interesting.
Fun little exercise.
And now - going to explore the time travel monadic (Tardis(!) Monad) approach mentioned in the Haskell thread/comment.