A FizzBuzz implementation using functional programming principles in Java, leveraging streams, predicates, and functional composition.
FizzBuzz is a classic programming problem used in interviews to test basic programming skills. The rules are:
For numbers 1 through 100:
- If the number is divisible by 3, print "Fizz"
- If the number is divisible by 5, print "Buzz"
- If the number is divisible by both 3 and 5 (15), print "FizzBuzz"
- Otherwise, print the number
This implementation demonstrates functional programming principles:
- Immutability: No mutable state or variables
- Pure Functions: Functions without side effects
- Higher-Order Functions: Functions that take or return other functions
- Function Composition: Building complex behavior from simple functions
- Declarative Style: Expressing what to do, not how to do it
- Stream Processing: Data flows through transformation pipelines
- IntPredicate: Functional interfaces for divisibility testing
- IntFunction: Mapping numbers to their FizzBuzz representation
- Method References: Clean, readable function references (
System.out::println) - Lambda Expressions: Concise function definitions
- Stream API: Functional data processing pipeline
- Function Composition: Combining predicates with
.and()operations
// Pure functional predicates
private static final IntPredicate isDivisibleBy3 = n -> n % 3 == 0;
private static final IntPredicate isDivisibleBy5 = n -> n % 5 == 0;
private static final IntPredicate isDivisibleBy15 = isDivisibleBy3.and(isDivisibleBy5);
// Pure mapping function
private static final IntFunction<String> fizzBuzzMapper = n ->
isDivisibleBy15.test(n) ? "FizzBuzz" :
isDivisibleBy3.test(n) ? "Fizz" :
isDivisibleBy5.test(n) ? "Buzz" :
String.valueOf(n);
// Functional pipeline
IntStream.rangeClosed(1, limit)
.mapToObj(fizzBuzzMapper)
.forEach(System.out::println);# Compile
javac FizzBuzz.java
# Run
java FizzBuzz- Composability: Functions can be easily combined and reused
- Testability: Pure functions are easy to test in isolation
- Readability: Declarative code expresses intent clearly
- Parallelization: Immutable functions work well with parallel streams
- Debugging: No hidden state makes reasoning about code easier
This repository is part of a programming paradigm showcase:
- FizzBuzzImperativeEdition - Simple, procedural approach with for loops and if statements
- FizzBuzzFunctionalEdition (this repo) - Functional programming with streams and predicates
- FizzBuzzObjectOrientedEdition - Over-engineered enterprise OOP solution with patterns
- FizzBuzzDeclarativeEdition - Declarative approach using expressions and pattern matching
Each demonstrates different programming paradigms and approaches to solving the same problem.
- Java 8 or higher (required for streams and lambda expressions)
- Understanding of functional programming concepts is helpful but not required