Fizzbuzz fun in Scala: A straightforward implementation
I write about: Software architecture and engineering, Better software better. DDD, Scala and Rust
Search for a command to run...
I write about: Software architecture and engineering, Better software better. DDD, Scala and Rust
No comments yet. Be the first to comment.
Some fun and exploring of FizzBuzz implementations in functional programming style and Scala 3.
Every implementation of FizzBuzz in this series, at its core, has relied on an infinitely counting lazy list. This modelling is logical, as the game can theoretically be played indefinitely. In this post, we will explore the possibility of defining a...
In Claude Code, a skill is a modular function-like component that can process requests — like /code-review or /commit. As of version 2.1.1, these skills can be directly invoked through slash commands. So a code-review skill can also be invoked direct...
I have been meaning to share more about my LLM workflows and tooling for a while, partly to have a reference for conversations, but mostly to learn in public. Agent Chisels is where I will be sharing the custom artefacts (primarily skills, with comma...
This year I bit the bullet and moved mail, calendar and drive from Google to Proton. Knowing that my family is not being profiled when, for example, I plan a doctor's appointment feels liberating. Sure, there were fun and useful technical improvement...
This is a short post to share a positive experience I had using an LLM agent to quickly add a feature to an existing personal CLI time-tracking application. Below, I describe how I added it using Zed, Opencode and Claude. To start, I wasn't even sure...
I have been writing "Today I Learned" posts (TILs) for a few weeks now and I started writing them as a complement to long form blogging. But I found myself spending 45-60 minutes, sometimes longer, per TIL instead of 15-25. This defeated the purpose ...
In previous articles of this series, I examined various implementations and meanwhile experimented with others at different levels of abstraction. However, before delving into those, I wanted to present what I believe to be the most straightforward approach, which allows for easy reading and extension.
FizzBuzz.scala and run it like: scala-cli run FizzBuzz.scala.The below example makes use of simple functions:
// Commenting the Scala 3 version / Scala CLI directive:
//> using scala 3.3.1
@main def fizzbuzz(): Unit =
def fizzbuzz(n: Int): List[String] =
// a lazy list's elements or only evaluated when needed
LazyList
// create a lazy list counting infinitely from 1
.from(1)
// map to a tuple where first element will hold the result
.map(i => ("", i))
// implement FizzBuzz the logic using easy to read functions
.map((s, i) => if i % 3 == 0 then (s + "Fizz", i) else (s, i))
.map((s, i) => if i % 5 == 0 then (s + "Buzz", i) else (s, i))
// easy to extend:
// .map((s, i) => if i % 2 == 0 then (s + "Bazz", i) else (s,i))
// convert the tuples to a list of Strings
// empty strings are turned into the index number
.map((s, i) => if s.isBlank then i.toString else s)
// take the request number of elements
// remove this line and the next line will never terminate
.take(n)
// here we turn the lazy list into an eager, evaluated list
.toList
end fizzbuzz
fizzbuzz(20).foreach(fb => print(fb + ","))
end fizzbuzz
Note how much easier it is to extend than if you would these `if-else` statements:
def shout(i: Int): String =
if (i % 15 == 0) "FizzBuzz" else
if (i % 3 == 0) "Fizz" else
if (i % 5 == 0) "Buzz" else
i.toString