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/10/19 10:13:19 UTC
[groovy-website] branch asf-site updated: draft of new 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 2b3a88f draft of new blog post
2b3a88f is described below
commit 2b3a88fbf36a4e381f441ca4d96708b28eca12de
Author: Paul King <pa...@asert.com.au>
AuthorDate: Thu Oct 19 20:13:09 2023 +1000
draft of new blog post
---
site/src/site/blog/chatgpt-one-liners.adoc | 263 +++++++++++++++++++++++++++++
1 file changed, 263 insertions(+)
diff --git a/site/src/site/blog/chatgpt-one-liners.adoc b/site/src/site/blog/chatgpt-one-liners.adoc
new file mode 100644
index 0000000..be68141
--- /dev/null
+++ b/site/src/site/blog/chatgpt-one-liners.adoc
@@ -0,0 +1,263 @@
+= ChatGPT meets Groovy one-liners
+Paul King
+:revdate: 2023-10-19T18:00:00+00:00
+:draft: true
+:keywords: groovy, chatgpt, one-liners
+:description: I asked ChatGPT if it could represent some short/one-liner programming-related dad jokes as short/one-liner Groovy scripts.
+
+I asked ChatGPT if it could represent some short/one-liner programming-related dad
+jokes as short/one-liner Groovy scripts. It's responses were initially a little
+underwhelming but after some coercing and some "post-production tweaking", here is the result.
+
+'''
+
+[quote]
+****
+_Q: Why do programmers always mix up Christmas and Halloween?_
+
+_A: Because 25 Dec(imal) is the same as 31 Oct(al)._
+****
+
+We can check that with the following Groovy script:
+
+[source,groovy]
+----
+def christmas = 25 // dec
+def halloween = 031 // oct
+assert christmas == halloween
+----
+
+'''
+
+[quote]
+****
+_Q: Why is Groovy such a neat language?_
+
+_A: Because it takes a bit from Go,
+supports data science like R though with low ceremony,
+mixes in a dose of Python and Perl, takes a decent chunk of Java
+with a twist of C#, and finishes with some Ruby._
+****
+
+We can check that with the following Groovy script:
+
+[source,groovy]
+----
+assert 'Groovy' == 'Go'.take(1) + 'R'.toLowerCase() +
+ 'Python'['Perl'.size()] * 2 +
+ 'Java'['C#'.size()] + 'Ruby'[-1]
+----
+
+'''
+
+[quote]
+****
+_Q: Why did the developer go broke?_
+
+_A: Because his money cache, intellisense and null stocks were all empty._
+****
+
+We combine the `Expando` class with the Elvis operator and Groovy truth to check this fact:
+
+[source,groovy]
+----
+def money = intelli = new Expando()
+def rich = money.cache ?: intelli.cents ?: null?.stocks
+assert !rich
+----
+
+'''
+
+[quote]
+****
+_Q: Why did the programmer think using Groovy was sweet?_
+
+_A: Because creating a box of a-sorted chocolates was easy._
+****
+
+We create a "sortable" `Chocolate` record making use of default parameters and declarative augmentation. Then we create a `Box` class which delegates to `List`. We then create a box of chocolates and check it is "a-sorted" using the `toString()` method:
+
+[source,groovy]
+----
+@Sortable @ToString(ignoreNulls = true)
+record Chocolate(int cocoa, String filling = null) {}
+
+class Box { @Delegate List<Chocolate> contents }
+
+var box = new Box(contents: [
+ new Chocolate(80),
+ new Chocolate(50, 'Caramel')
+].sort())
+
+assert box.toString() == '[Chocolate(50, Caramel), Chocolate(80)]'
+----
+
+'''
+
+[quote]
+****
+_If life gives you melons, ..._
+
+_...you may be dyslexic._
+****
+
+Let's take the word 'melons' and jumble it around a bit using Groovy's flexible indexing to see if we get 'lemons':
+
+[source,groovy]
+----
+def dyslexic = 'melons'[2..0,3..-1] == 'lemons'
+assert dyslexic
+----
+
+'''
+
+[quote]
+****
+_6:30 is the best time on a clock, hands down._
+****
+
+Let's create a `LocalTime` of 6:30 and check that the hands are pointing down. The minute hand travels 360 degrees in 60 minutes,
+so 6 degrees per minute. We can check at 6:30 that it is pointing straight down (180°).
+The hour hand travels 360° in 720 minutes (1/2 a day), so we divide the total minutes by 2. The total minutes is 60 times the number of hours plus the minutes for the current hour.
+At 6:30, we know the hour hand will have travelled halfway between the 6 and 7 hour markers (15°) but for our test we'll
+just make sure it is point down (we'll say within 20° of straight down which we'll specify as a range). Here is the result:
+
+[source,groovy]
+----
+def (h, m) = LocalTime.of(6, 30)[HOUR_OF_DAY, MINUTE_OF_HOUR]
+assert 6 * m == 180L
+assert (h * 60 + m).intdiv(2) in 180L..200L
+----
+
+'''
+
+[quote]
+****
+_Q: What's the difference between ignorance and apathy?_
+
+_A: I don’t know and I don’t care._
+****
+
+To show this with Groovy, we should first declare our
+ignorance and apathy. Then we calculate the difference
+and finally check our assertions:
+
+[source,groovy]
+----
+int ignorance, apathy
+int know = care = ignorance - apathy
+assert !know && !care
+----
+
+'''
+
+[quote]
+****
+_Can Groovy help me answer the question: Which came first, the chicken or the egg?_
+****
+
+Sure, run this script:
+
+[source,groovy]
+----
+def (first, second) = ['egg', 'chicken'].shuffled()
+println "Which came first, the $first or the $second? The eternal debate!"
+----
+
+It really just asks the question in two different orders,
+but I'm sure you get the idea.
+
+'''
+
+[quote]
+****
+_Q: Why are death, taxes, and immutability similar?_
+
+_A: Because, in life and programming, they're the only things you can't change!_
+****
+
+Since death and taxes are certain, we should store a quote
+about them in an immutable structure.
+A record offers shallow immutability and works well for this.
+For deep immutability, consider using the `@Immutable` AST transform,
+so here even though our quotes are stored in a mutable ArrayList,
+there is no way to change it.
+Here is an example:
+
+[source,groovy]
+----
+record Quote(String text, String author, int year) { }
+var q1 = new Quote('Perfection is immutable. But for things imperfect, change is the way to perfect them.', 'Owen Feltham', 1840)
+var q2 = new Quote('Nothing is certain except death and taxes.', 'Benjamin Franklin', 1789)
+
+@Immutable
+class FavoriteQuotes { List<Quote> list }
+
+var favorites = new FavoriteQuotes([q1, q2])
+----
+
+'''
+
+[quote]
+****
+_Why are diamonds and dogs a person's best friends?_
+
+_Because diamonds are forever, and dogs are fur-ever!_
+****
+
+Let's use traits to check best friends for diamonds and dogs:
+
+[source,groovy]
+----
+trait HasBestFriend {
+ abstract String friend()
+ boolean isBestFriend(String candidate) {
+ friend() == candidate
+ }
+}
+
+class Diamond implements HasBestFriend {
+ String friend() { 'girl' }
+}
+
+class Dog implements HasBestFriend {
+ String friend() { 'man' }
+}
+
+assert ['man', 'girl'].collect{
+ [new Diamond().isBestFriend(it),
+ new Dog().isBestFriend(it)]
+} == [[false, true], [true, false]]
+----
+
+'''
+
+[quote]
+****
+_Why did the Land Rover car get along so well with the Land Rover Explore smartphone?_
+
+_Because they both knew the importance of "exploring" new territories,
+whether off-road or online!!_
+****
+
+Who knew that Cars and Smartphones had anything to do with each other,
+but the trend seems to be companies want to get involved in both.
+Similarly, if you have two classes that apparently have nothing to
+do with one another, Groovy's duck typing, or in this case
+property handling might allow you to use them together more easily
+than you think.
+
+[source,groovy]
+----
+import groovy.transform.*
+
+record Smartphone(String make, String model, String color, int year) { }
+
+record Car(String make, String model, String color, int year) { }
+
+def s = new Smartphone('Landrover', 'Explore', 'Black', 2018)
+def c = new Car(s.toMap())
+assert c.toString() == 'Car[make=Landrover, model=Explore, color=Black, year=2018]'
+----
+
+'''