Skip to content

Instantly share code, notes, and snippets.

@rokon12
Created June 29, 2025 02:04
Show Gist options
  • Save rokon12/6576354d5b8a65a01e63fed9d67536f7 to your computer and use it in GitHub Desktop.
Save rokon12/6576354d5b8a65a01e63fed9d67536f7 to your computer and use it in GitHub Desktop.
Code snippet from The Coding Café - snippet-3.java
List<Long> runningProduct = values.stream()
.gather(Gatherers.scan(
() -> 1L, // 1️⃣ Start with identity for multiplication
(acc, val) -> acc * val // 2️⃣ Multiply accumulated value by current
))
.toList();
System.out.println("Running product: " + runningProduct);
// Output: [1, 1, 5, 15, 120, 240, 2160, 8640, 60480, 362880]
// ↑ ↑ ↑ ↑ ↑
// init 1×1 1×5 5×3 15×8 (and so on...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment