FizzBuzz in Smalltalk

ingve3 pts0 comments

FizzBuzz in Smalltalk | MediumSitemapOpen in appSign up<br>Sign in

Medium Logo

Get app<br>Write

Search

Sign up<br>Sign in

FizzBuzz in Smalltalk

Donald Raab

5 min read·<br>1 day ago

Listen

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<br>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.<br>Smalltalk is evolvable.

You can change the system.<br>The Inline FizzBuzz Approach<br>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.<br>Ternary, Predicate, and Pattern Matching for FizzBuzz with Java 26<br>Ternary, Predicate, and Pattern Matching for FizzBuzz with Java 26. Exploring FizzBuzz solutions from Java 8 to Java…

donraab.medium.com

I started with the “simplest” possible solution which only requires inlining code as follows.<br>Press enter or click to view image in full size

FizzBuzz in Smalltalk using inlined codeHere’s the source for the inline approach:<br>(0 to: 20) collect: [ :each |<br>(each isDivisibleBy: 3)<br>ifTrue: [<br>(each isDivisibleBy: 5)<br>ifTrue: [ 'FizzBuzz' ]<br>ifFalse: [ 'Fizz' ]<br>ifFalse: [<br>(each isDivisibleBy: 5)<br>ifTrue: [ 'Buzz' ]<br>ifFalse: [ seach ]<br>]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.<br>Smalltalk Blocks<br>Smalltalk 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.”<br>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.<br>Evolving the Integer Class to Include fizzBuzz<br>Now I’m hoping since you see a method named isDivisibleBy: on Number, you may have had a lightbulb go off in your head.<br>Can we add other methods to numbers? 💡

Yes. Yes we can.<br>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.<br>Here’s the source for the fizzBuzz method.<br>fizzBuzz<br>^(self isDivisibleBy: 3)<br>ifTrue: [<br>(self isDivisibleBy: 5)<br>ifTrue: [ 'FizzBuzz' ]<br>ifFalse: [ 'Fizz' ]<br>ifFalse: [<br>(self isDivisibleBy: 5)<br>ifTrue: [ 'Buzz' ]<br>ifFalse: [ self ]<br>]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.<br>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:<br>|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:<br>|fizzBuzz|

fizzBuzz := (0 to: 20) collect: #fizzBuzzA 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!<br>I thought it would be fun to round out a final Smalltalk blog for June 2026 with...

fizzbuzz smalltalk isdivisibleby method java class

Related Articles