Counter and Theme Toggle - Elixir Typing CST Test
Loading…
Counter and Theme Toggle — Elixir Code
Demonstrates a simple counter with theme toggling using Elixir variables and console output.
defmodule Counter do
def run do
count = 0
is_dark = false
update_ui = fn ->
IO.puts("Counter: #{count}")
IO.puts("Theme: #{if is_dark, do: \"Dark\", else: \"Light\"}")
end
increment = fn ->
count = count + 1
update_ui.()
end
decrement = fn ->
count = count - 1
update_ui.()
end
reset = fn ->
count = 0
update_ui.()
end
toggle_theme = fn ->
is_dark = !is_dark
update_ui.()
end
# Simulate actions
update_ui.()
increment.()
increment.()
toggle_theme.()
decrement.()
reset.()
end
end
Counter.run()Elixir Language Guide
Elixir is a functional, concurrent, fault-tolerant programming language built on the Erlang VM (BEAM). It enables highly scalable, distributed, and resilient applications - ideal for real-time systems, telecom-grade reliability, and web applications via Phoenix.
Primary Use Cases
- ▸Highly concurrent real-time systems
- ▸Scalable web backends (Phoenix)
- ▸Distributed and fault-tolerant applications
- ▸Event-driven architectures
- ▸Messaging systems
- ▸Telecom-level resilience workloads
- ▸IoT and device message brokers
Notable Features
- ▸Actor-based concurrency via BEAM
- ▸Hot code upgrades
- ▸Fault tolerance through supervision trees
- ▸Pattern matching
- ▸Lightweight processes
- ▸Macros & metaprogramming
Origin & Creator
Created by José Valim in 2011 to bring modern tooling, metaprogramming, and extensibility to the Erlang ecosystem while preserving BEAM's reliability.
Industrial Note
Elixir thrives in telecom-grade environments, real-time systems, fintech, IoT backends, distributed messaging, soft real-time applications, scalable web platforms, low-latency systems, and systems requiring uptime measured in years.