FizzBuzz in Smalltalk
FizzBuzz in Smalltalk
5 min read
·
1 day ago
\--
Share
Accept the things you can’t change. Change the things you can’t accept.
Press enter or click to view image in full size
Wait, wut?
The Smalltalk Magic Trick
Amidst all of the discussion over basic language syntax and library depth, developers who only program in languages like Java miss the true power of a language like Smalltalk.
Smalltalk is evolvable.
You can change the system.
The Inline FizzBuzz Approach
I decided to sit down and solve the FizzBuzz problem in Smalltalk. I had posted a blog with my Java based solution on StackOverflow from a decade ago.
I started with the “simplest” possible solution which only requires inlining code as follows.
Press enter or click to view image in full size
FizzBuzz in Smalltalk using inlined code
Here’s the source for the inline approach:
(0 to: 20) collect: [ :each | (each isDivisibleBy: 3) ifTrue: [ (each isDivisibleBy: 5) ifTrue: [ 'FizzBuzz' ] ifFalse: [ 'Fizz' ] ] ifFalse: [ (each isDivisibleBy: 5) ifTrue: [ 'Buzz' ] ifFalse: [ seach ] ] ]
The code `(0 to: 20)` creates an Interval. Then I send the `collect:` message, which takes a one argument `BlockClosure`, to the `Interval`. Then we see a bunch of Boolean messages with blocks (i.e. `ifTrue:ifFalse:`). If you read my recent blog on Smalltalk Blocks, you’ll learn that Smalltalk doesn’t have an if-statement. Control structures are achieved in Smalltalk using messages and blocks.
Smalltalk BlocksSmalltalk Blocks. The expression of the square bracket.donraab.medium.com
The message `isDivisibleBy:` is available on `Number` types in Smalltalk. It is a more human readable form of `i % n == 0` in Java, which I read in my brain as “i mod n equals zero.”
We can see the definition of `isDivisibleBy:` on the `Number` class in Smalltalk.
Press enter or click to view image in full size
The method isDivisibleBy: on Number in Smalltalk
This implementation should explain why I used `isDivisibleBy:` instead of `(i \\ n) = 0`. Any developer can read `isDivisibleBy:` but I would have to explain that `\\` is mod in Smalltalk, instead of `%` like in Java. That would lose folks quickly, but now you know.
Evolving the Integer Class to Include fizzBuzz
Now I’m hoping since you see a method named `isDivisibleBy:` on `Number`, you may have had a lightbulb go off in your head.
Can we add other methods to numbers? 💡
Yes. Yes we can.
Let’s add a `fizzBuzz` method to the `Integer` class.
Press enter or click to view image in full size
Adding fizzBuzz to the Integer class in Smalltalk
Now, I just went to the `Integer` class and added a method by typing in an empty method template. Easy! When I used to pogram in Smalltalk, we would use a slightly different approach for extending existing classes via extension methods that we wanted to share in multiple projects. I will leave that to the reader to discover that capability in Pharo Smalltalk. For my purposes here, it is enough to show that we can change existing library classes.
Here’s the source for the `fizzBuzz` method.
fizzBuzz ^(self isDivisibleBy: 3) ifTrue: [ (self isDivisibleBy: 5) ifTrue: [ 'FizzBuzz' ] ifFalse: [ 'Fizz' ] ] ifFalse: [ (self isDivisibleBy: 5) ifTrue: [ 'Buzz' ] ifFalse: [ self ] ]
This code looks almost identical to the code I used inline. The name `fizzBuzz` at the top is the method signature. The `^` is the return character in Smalltalk. The reserved word `self` in this context refers to the instance of the `Integer` class.
Here’s a test for the `fizzBuzz` method.
Press enter or click to view image in full size
FizzBuzzTest
Now that we have a `fizzBuzz` method on Integer we can write the code as follows, and never have to write `fizzBuzz` algorithm by hand again.
Press enter or click to view image in full size
Using fizzBuzz on the Integer class with an Interval
The source is as follows:
|fizzBuzz| fizzBuzz := (0 to: 20) collect: [:each | each fizzBuzz]
The |fizzBuz| sets up a local variable. The `:=` assigns the result of code on the right to the variable on the left. I used a one argument `BlockClosure` with `collect:` here. I can simplify this to use a `Symbol` as follows:
|fizzBuzz| fizzBuzz := (0 to: 20) collect: #fizzBuzz
A `Symbol` in Smalltalk is a unique `String` that can be used as a key, or as a replacement for a Block as I’ve used it here. Why does this work? Let’s look at the `value:` method on `Symbol` which is the equivalent method that would be called on `BlockClosure`.
Press enter or click to view image in full size
The value: method as defined on the Symbol class which allows a Symbol to behave like a BlockClosure.
That’s All Folks!
I thought it would be fun to round out a final Smalltalk blog for June 2026 with the FizzBuzz implementation so folks could compare it to the equivalent in Java. I haven’t been able to figure out yet how to get emojis to display correctly in Pharo Smalltalk. Some quick Googling looks like I need to get some other fonts installed or something. I settled on just switching back to the standard Fizz and Buzz Strings.
Smalltalk is a pure object-oriented programming language. The code in this blog gives a bit of a sampling of what that can feel like for solving problems. Think `object message`. You can accomplish so much with this simple paradigm.
It’s mystery is only exceeded by it’s power.
Thanks for reading!
I am the creator of and committer for the Eclipse Collections OSS project, which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions. I am the author of the book, Eclipse Collections Categorically: Level up your programming game.




