From 7d3f49cf978770c0d57c73910cfd7d093eb6fb96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jyri-Matti=20La=CC=88hteenma=CC=88ki?= Date: Tue, 10 Dec 2024 08:06:42 +0200 Subject: [PATCH] reduce: don't execute function for collections of size 1 --- src/main/java/fi/solita/utils/functional/FunctionalImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/fi/solita/utils/functional/FunctionalImpl.java b/src/main/java/fi/solita/utils/functional/FunctionalImpl.java index c0cc0e7..6f6546b 100644 --- a/src/main/java/fi/solita/utils/functional/FunctionalImpl.java +++ b/src/main/java/fi/solita/utils/functional/FunctionalImpl.java @@ -412,7 +412,7 @@ static final > Option reduce(Iterable xs) } static final T reduce(Monoid monoid, Iterable xs) { - return xs == null ? null : fold(monoid, cons(monoid.zero(), xs)).get(); + return fold(monoid, xs).getOrElse(monoid.zero()); } static final Z fold(Z zero, Apply, Z> f, Iterable xs) {