You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2023/11/14 05:54:58 UTC
(groovy-website) branch asf-site updated: add set operators blog post
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 0c10d60 add set operators blog post
0c10d60 is described below
commit 0c10d60723949a2d785e33623689b5b29dc20187
Author: Paul King <pa...@asert.com.au>
AuthorDate: Tue Nov 14 15:54:49 2023 +1000
add set operators blog post
---
site/src/site/blog/img/SetOperators.png | Bin 0 -> 13351 bytes
site/src/site/blog/set-operations-with-groovy.adoc | 47 +++++++++++++++++++++
2 files changed, 47 insertions(+)
diff --git a/site/src/site/blog/img/SetOperators.png b/site/src/site/blog/img/SetOperators.png
new file mode 100644
index 0000000..dc51ead
Binary files /dev/null and b/site/src/site/blog/img/SetOperators.png differ
diff --git a/site/src/site/blog/set-operations-with-groovy.adoc b/site/src/site/blog/set-operations-with-groovy.adoc
new file mode 100644
index 0000000..b1f9bc6
--- /dev/null
+++ b/site/src/site/blog/set-operations-with-groovy.adoc
@@ -0,0 +1,47 @@
+= Set Operators with Groovy
+Paul King
+:revdate: 2023-11-14T15:22:57+00:00
+:keywords: groovy, emoji, set
+:description: This blog looks at union, intersection, difference & symmetric difference operators in Groovy.
+
+https://twitter.com/TheDonRaab[Donald Raab] posted an article about set operators using Java collections and https://github.com/eclipse/eclipse-collections[Eclipse Collections]. That post focuses
+on the _union_, _intersection_ and _difference_ operators. We'll add in
+_symmetric difference_ since Groovy supports operator overloading for that
+operator also.
+
+First, a quick refresh about the operators for two sets *A* and *B*:
+
+image:img/SetOperators.png[]
+
+We'll use the same emoji sets from Donald's post:
+
+[source,groovy]
+----
+Set OCT = ["🍁", "🍃", "🎃", "🥧", "🍫", "🌝", "👻", "🌽", "🍎", "🪵", "☕️"]
+Set NOV = ["🍂", "🍁", "🥧", "🦃", "🌽", "🍎", "🪵", "☕️"]
+Set DEC = ["🥧", "🦃", "⛄️", "🍎", "🌰", "🛷", "🪵", "🎄", "☕️"]
+----
+
+Groovy lets you write the same code as in Donald's post but also offers
+operator overloading for these operators as follows:
+
+[source,groovy]
+----
+assert (OCT & NOV & DEC) == ["🪵", "🥧", "🍎", "☕️"] as Set
+
+assert (OCT | NOV | DEC) == ["🪵", "🦃", "🌰", "🛷", "🍎", "🎄",
+ "🍫", "🌝", "☕️", "🥧", "⛄️", "🍁",
+ "🍂", "🍃", "👻", "🌽", "🎃"] as Set
+
+assert (OCT - NOV - DEC) == ["🍫", "🌝", "🍃", "👻", "🎃"] as Set
+assert (NOV - OCT - DEC) == ["🍂"] as Set
+assert (DEC - OCT - NOV) == ["🎄", "🌰", "⛄️", "🛷"] as Set
+
+assert (OCT ^ NOV ^ DEC) == ['⛄️', '🌰', '🛷', '🎄', '🍃', '🎃', '🍫', '🌝',
+ '👻', '🍂', '🥧', '🍎', '🪵', '☕️'] as Set
+assert (NOV ^ OCT ^ DEC) == ['🍂', '🍃', '🎃', '🍫', '🌝', '👻', '🥧', '⛄️',
+ '🍎', '🌰', '🛷', '🪵', '🎄', '☕️'] as Set
+----
+
+Since operator overloading is extensible in Groovy, you can have similar
+shorthand notation for your domain objects also. Enjoy!