Examples · Groovy
Run Groovy scripts without a project
Groovy is at its best in short scripts, and ByteRunner runs them as they are, with no project and no build tool. Each result you dump() shows as a table or a list.
This one counts words with countBy, groups the distinct words by length, and maps a closure over a list of names.
The query
groovy-scripts.groovy · paste it into a Groovy tab and press F5, or run byterunner run groovy-scripts.groovy in a terminal
// Groovy as a script: closures, GStrings, and the collection methods it's known for.
def words = "the quick brown fox jumps over the lazy dog the end".split(" ").toList()
dump(words.countBy { it }.sort { -it.value }, "Word counts")
def byLength = words.toUnique().groupBy { it.size() }.collectEntries { size, ws -> [size, ws.join(", ")] }
dump(byLength.sort(), "Words by length")
def greet = { name -> "Hello, ${name.capitalize()}!" }
dump(["ada", "grace", "linus"].collect(greet), "Greetings")
What it shows
The output of that run, from byterunner run. In the app each table is a sortable grid.
Word counts
Key Value
----- -----
the 3
quick 1
brown 1
fox 1
jumps 1
over 1
lazy 1
dog 1
end 1
(9 rows)
Words by length
Key Value
--- -------------------
3 the, fox, dog, end
4 over, lazy
5 quick, brown, jumps
(3 rows)
Greetings
Value
-------------
Hello, Ada!
Hello, Grace!
Hello, Linus!
(3 rows)
Groovy's collection methods (countBy, groupBy, collectEntries, inject) are all there. The same scratchpad also runs Kotlin and Java, so a Groovy idea can be checked against its Kotlin version in the next tab.
Download ByteRunner
Free for Windows and Linux. Getting started