Functional design in Java
15 questions in 5 subtopics.
Higher-order functions
Write a higher-order functionretrying(Supplier<T> action, int attempts) that returns a Supplier<T> which retries on RuntimeException. Show how it is used.Your team builds a small pipeline framework where steps are Function<Context, Context>. Discuss the benefits and the problems this design causes in a large codebase.Some teams in your organisation write heavily functional Java with Vavr, monads and point-free style; others write plain imperative Java. Hiring and code moves between teams suffer. What guidance do you set?Functional programming ideas have moved into mainstream Java over twenty years. Which of them have proven their value in large Java codebases, which have not, and what would you encourage or discourage in your organisation's standards?Function composition
Show how to compose functions withFunction.andThen and compose, and predicates with and, or and negate. What is the difference between f.andThen(g) and f.compose(g)?Design a validation component where rules can be combined, for example Rule<Order> = notEmpty.and(maxItems(50)).and(validAddress), and the result lists all failures, not only the first. How would you model it?Immutability and pure functions
What is a pure function? Which of these are pure and why:Math.max, a method that reads LocalDate.now(), a method that adds to a list parameter, String.toUpperCase().You want to make the pricing logic of an order service easier to test and reason about. How would you apply functional core, imperative shell?Java started as an object-oriented language and has added lambdas, records, sealed types and pattern matching. How do you see the balance between object-oriented and functional styles in Java over the next decade, and what do you standardise?Currying in Java
Show how to write a curried function in Java foradd(a, b, c) and how to partially apply a BiFunction. When is this useful in real code?A library author wants to expose curried APIs such as Function<Config, Function<Request, Response>> in a shared framework. What would you advise?Functional error handling
What are the options for handling errors in a functional style in Java, instead of throwing exceptions from inside lambdas?Write a minimalResult<T> type using a sealed interface and records, with map and a way to handle both cases using a switch.Teams disagree on whether service-layer code should use exceptions or Result types for business errors such as insufficient funds. What standard do you choose for a large Spring codebase?Java now has sealed interfaces, records and pattern matching, which make Result-style error handling practical without a library. Over the next decade, how would you guide error handling style across a large codebase that is built on exceptions?No question matches these choices.