1. Home
  2. /
  3. Groovy
  4. /
  5. Counter and Theme Toggle

Counter and Theme Toggle - Groovy Typing CST Test

Loading…

Counter and Theme Toggle — Groovy Code

Demonstrates a simple counter with theme toggling using Groovy variables and console output.

def count = 0
def isDark = false

def updateUI = {
	println "Counter: $count"
	println "Theme: ${isDark ? 'Dark' : 'Light'}"
}

def increment = { count++; updateUI() }
def decrement = { count--; updateUI() }
def reset = { count = 0; updateUI() }
def toggleTheme = { isDark = !isDark; updateUI() }

// Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()

Groovy Language Guide

Groovy is a powerful, dynamic JVM language that integrates seamlessly with Java, offering concise syntax, scripting capabilities, functional programming patterns, and strong support for automation, DSL creation, and Gradle build scripts. It blends Java familiarity with Python/Ruby-like expressiveness.

Primary Use Cases

  • ▸Gradle build scripts
  • ▸Jenkins declarative & scripted pipelines
  • ▸Automation & scripting on the JVM
  • ▸Rapid backend prototyping
  • ▸Testing frameworks (Spock)
  • ▸Domain-specific language (DSL) creation

Notable Features

  • ▸Java interoperability
  • ▸Closures & functional constructs
  • ▸Dynamic + optional static typing
  • ▸Simplified syntax
  • ▸Metaprogramming & AST transformations

Origin & Creator

Created by James Strachan in 2003; later developed under the Apache Software Foundation.

Industrial Note

Groovy is foundational to Gradle and Jenkins pipelines. It dominates CI/CD automation, DevOps pipelines, JVM scripting, and domain-specific language (DSL) development across enterprises.

More Groovy Typing Exercises

Groovy Simple CalculatorGroovy FactorialGroovy Fibonacci SequenceGroovy List ComprehensionGroovy Map FilteringGroovy Anonymous ClosuresGroovy Reduce ExampleGroovy Zip ListsGroovy Tuple Destructuring

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher