Counter and Theme Toggle - Crystal Typing CST Test
Loading…
Counter and Theme Toggle — Crystal Code
Demonstrates a simple counter with theme toggling using Crystal variables and console output.
count = 0
is_dark = false
def update_ui(count, is_dark)
puts "Counter: #{count}"
puts "Theme: #{is_dark ? "Dark" : "Light"}"
end
def increment(count, is_dark)
count += 1
update_ui(count, is_dark)
count
end
def decrement(count, is_dark)
count -= 1
update_ui(count, is_dark)
count
end
def reset(count, is_dark)
count = 0
update_ui(count, is_dark)
count
end
def toggle_theme(is_dark)
is_dark = !is_dark
update_ui(count, is_dark)
is_dark
end
# Simulate actions
update_ui(count, is_dark)
count = increment(count, is_dark)
count = increment(count, is_dark)
is_dark = toggle_theme(is_dark)
count = decrement(count, is_dark)
count = reset(count, is_dark)Crystal Language Guide
Crystal is a modern, statically typed, compiled programming language with syntax heavily inspired by Ruby. It aims to combine the efficiency and speed of compiled languages with the readability and productivity of Ruby, supporting type inference, concurrency, and high-performance applications.
Primary Use Cases
- ▸Web applications (via Kemal, Amber frameworks)
- ▸Command-line tools
- ▸Microservices and APIs
- ▸High-performance backend services
- ▸System utilities and scripting
- ▸Prototyping with production-ready performance
Notable Features
- ▸Ruby-inspired readable syntax
- ▸Static type system with inference
- ▸Compiled to fast native binaries
- ▸Macros and compile-time code generation
- ▸Concurrency via fibers and channels
Origin & Creator
Crystal was created by Ary Borenszweig, Juan Wajnerman, and Brian Cardiff in 2011, inspired by Ruby’s syntax and modern systems programming needs.
Industrial Note
Crystal is used in web backend frameworks, DevOps tooling, microservices, and performance-critical tasks where Ruby-like syntax with compiled efficiency is desirable.