Writing FizzBuzz in Effect (TS)
July 16th, 2025

Overview

FizzBuzz: From 1 to a given number, print "Fizz" if the number is divisible by 3, "Buzz" if the number is divisible by 5, "FizzBuzz" if it is divisible by both. And if no case matches, then print the number itself.

Below, I'll show two approaches how to write this simple problem using Effect

With a generator function

import { Console, Effect } from "effect" const fizzBuzzFromGen = (n: number) => Effect.gen(function* () { for (let i = 1; i <= n; i++) { let output = "" if (i % 3 === 0) { output += "Fizz" } if (i % 5 === 0) { output += "Buzz" } yield* Console.log(i, output) } }) Effect.runSyncExit(fizzBuzzFromGen(30))

With pipelines

import { Array, Console, Effect, Match } from "effect" const fizzBuzzPipelines= (n: number) => Effect.forEach( Array.range(1, n), (_, i) => Match.value(i+1).pipe( Match.when((n) => n % 15 === 0, () => 'FizzBuzz'), Match.when((n) => n % 3 === 0, () => 'Fizz'), Match.when((n) => n % 5 === 0, () => 'Buzz'), Match.orElse(() => ""), output => Console.log(i+1, output) ) ) Effect.runSyncExit(fizzBuzzPipelines(30))

Conclusion

The generator function is a lot more readable.

The pipelines approach is fun to understand though. It could have been simplied with a helper function, but I wanted to show an example fully using pipelines and Match within a pipeline.