Counter and Theme Toggle - Nim Typing CST Test
Loading…
Counter and Theme Toggle — Nim Code
Demonstrates a simple counter with theme toggling using Nim variables and console output.
var count = 0
var isDark = false
proc updateUI() =
echo "Counter: ", count
echo "Theme: ", if isDark: "Dark" else: "Light"
proc increment() =
count += 1
updateUI()
proc decrement() =
count -= 1
updateUI()
proc reset() =
count = 0
updateUI()
proc toggleTheme() =
isDark = not isDark
updateUI()
# Simulate actions
updateUI()
increment()
increment()
toggleTheme()
decrement()
reset()Nim Language Guide
Nim is a statically typed, compiled systems programming language with Python-like syntax. It emphasizes performance, expressiveness, and metaprogramming, making it suitable for system tools, web development, and scientific computing.
Primary Use Cases
- ▸System utilities and command-line tools
- ▸Web backends and frameworks
- ▸Scientific and numerical computing
- ▸Game development and graphics engines
- ▸Metaprogramming and code generation
Notable Features
- ▸Python-like clean syntax
- ▸Compile-time metaprogramming with macros
- ▸Memory safety with manual or garbage-collected management
- ▸Cross-platform compilation to C/C++/JS
- ▸Lightweight concurrency support with async/await
Origin & Creator
Created in 2008 by Andreas Rumpf.
Industrial Note
Nim is used in high-performance tools, embedded systems, game development, and niche web frameworks where low-level control and speed are essential.
Quick Explain
- ▸Nim compiles to C, C++, or JavaScript for portability and speed.
- ▸It combines imperative, object-oriented, and functional programming paradigms.
- ▸Supports metaprogramming and macros for code generation.
Core Features
- ▸Strong static typing with type inference
- ▸Procedures and first-class functions
- ▸Object-oriented programming with classes and inheritance
- ▸Generics and templates for reusable code
- ▸Macros and compile-time execution for metaprogramming
Learning Path
- ▸Learn syntax and basic constructs
- ▸Explore procedures and functions
- ▸Understand sequences, arrays, and tuples
- ▸Study OOP and generics
- ▸Practice macros and async programming
Practical Examples
- ▸Hello world CLI
- ▸Web server with Jester framework
- ▸Generic container library
- ▸Async network client
- ▸Macro to generate repetitive code
Comparisons
- ▸Faster than Python due to compilation
- ▸Simpler syntax than C/C++
- ▸Less mature ecosystem than Rust or Go
- ▸Better metaprogramming than most mainstream languages
- ▸Flexible paradigms compared to single-paradigm languages
Strengths
- ▸High-performance native binaries
- ▸Readable and concise syntax
- ▸Powerful metaprogramming capabilities
- ▸Cross-platform portability
- ▸Flexibility between paradigms (OOP, functional, imperative)
Limitations
- ▸Smaller ecosystem and community compared to mainstream languages
- ▸Limited libraries for some specialized domains
- ▸Less industrial adoption than Rust or Go
- ▸Requires understanding of memory management for optimal performance
- ▸Interfacing with large C++ projects can be complex
When NOT to Use
- ▸Projects requiring massive library ecosystems
- ▸Mobile app development without JS compilation
- ▸High-concurrency backend systems at extreme scale
- ▸GUI-heavy desktop applications
- ▸Rapid prototyping with minimal compilation overhead
Cheat Sheet
- ▸var x: int = 10
- ▸let y = 20 # immutable
- ▸proc add(a, b: int): int = a + b
- ▸import strutils, sequtils
- ▸macro generateCode() = ...
FAQ
- ▸Is Nim still relevant?
- ▸Yes - popular for high-performance and metaprogramming tasks.
- ▸Is Nim compiled or interpreted?
- ▸Compiled to native, C/C++, or JavaScript.
- ▸Is Nim functional or imperative?
- ▸Supports functional, imperative, and OOP paradigms.
- ▸Can Nim be used for web development?
- ▸Yes - via frameworks like Jester and compilation to JS.
30-Day Skill Plan
- ▸Week 1: Syntax, variables, control flow
- ▸Week 2: Procedures, functions, and OOP basics
- ▸Week 3: Generics, sequences, and modules
- ▸Week 4: Macros, async, and performance optimization
Final Summary
- ▸Nim is a modern, statically typed, high-performance language.
- ▸Flexible paradigms: functional, OOP, and imperative.
- ▸Supports metaprogramming and cross-platform compilation.
- ▸Suitable for system tools, web backends, and scientific computing.
Project Structure
- ▸src/ - Nim source files
- ▸tests/ - unit and integration tests
- ▸examples/ - sample projects
- ▸docs/ - documentation
- ▸nimble file for package configuration
Monetization
- ▸CLI tool and system utility development
- ▸Web backend services
- ▸Game engines and high-performance apps
- ▸Scientific computing libraries
- ▸Training and consultancy on Nim development
Productivity Tips
- ▸Use Nimble for dependencies
- ▸Diligent testing and profiling
- ▸Leverage macros for repetitive code
- ▸Organize code with modules
- ▸Use async/await for concurrency
Basic Concepts
- ▸Variables and constants
- ▸Procedures and functions
- ▸Control flow: if, while, for
- ▸Types, arrays, sequences, tuples
- ▸Modules and imports
Official Docs
- ▸Nim Official Documentation
- ▸Nim Language Guide
- ▸Nimble Package Manager Documentation