This is an automated email from the ASF dual-hosted git repository.
paulk pushed a commit to branch asf-site
in repository https://gitbox.apache.org/repos/asf/groovy-website.git
The following commit(s) were added to refs/heads/asf-site by this push:
new f523182 updated with a scan/cumulative sum example
f523182 is described below
commit f5231827fcd61cce0304a4d5a1b65feee7d81f03
Author: Paul King <[email protected]>
AuthorDate: Thu Jan 18 16:06:36 2024 +1000
updated with a scan/cumulative sum example
---
site/src/site/blog/groovy-gatherers.adoc | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/site/src/site/blog/groovy-gatherers.adoc
b/site/src/site/blog/groovy-gatherers.adoc
index e229451..0faae04 100644
--- a/site/src/site/blog/groovy-gatherers.adoc
+++ b/site/src/site/blog/groovy-gatherers.adoc
@@ -426,8 +426,19 @@ assert (1..5).inject([]) { acc, next ->
} == [1, 3, 6, 10, 15]
----
-This particular operation is deemed useful enough that Java actually
-has a built-in library function for arrays:
+Groovy has a number of alternatives to achieve this functionality.
+Here is one using `inits`:
+
+[source,groovy]
+----
+assert (1..5).inits().grep().reverse()*.sum() == [1, 3, 6, 10, 15]
+----
+
+`inits` is a list processing function which we cover in more detail
+in the next section.
+
+Before examining gatherer equivalents, we should note that this particular
operation
+is deemed useful enough that Java actually has built-in library function for
arrays:
[source,groovy]
----
@@ -436,7 +447,7 @@ Arrays.parallelPrefix(nums, Integer::sum)
assert nums == [1, 3, 6, 10, 15]
----
-This particular operation isn't well suited to traditional streams,
+Cumulative sum isn't well suited to traditional streams,
but now with gatherers, we can use the `scan` built-in gatherer
to have similar functionality when processing streams: