1. Home
  2. /
  3. Crystal
  4. /
  5. Fibonacci Sequence

Fibonacci Sequence - Crystal Typing CST Test

Loading…

Fibonacci Sequence — Crystal Code

Generates first 10 Fibonacci numbers.

def fib(n)
	return n if n < 2
	fib(n - 1) + fib(n - 2)
end

10.times do |i|
	puts fib(i)
end

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.

Quick Explain

  • ▸Crystal compiles directly to native code for high performance.
  • ▸It features static type checking with powerful type inference, reducing boilerplate.
  • ▸Used for web development, CLI tools, system utilities, and high-performance applications.

Core Features

  • ▸Classes, modules, structs, and enums
  • ▸Generics and type inference
  • ▸Error handling with exceptions
  • ▸Compile-time macros
  • ▸Standard library for networking, HTTP, JSON, and more

Learning Path

  • ▸Learn Ruby syntax basics
  • ▸Understand Crystal type system and syntax
  • ▸Write CLI tools and small scripts
  • ▸Learn web frameworks (Kemal/Amber)
  • ▸Master concurrency and fibers

Practical Examples

  • ▸CLI tool parsing arguments
  • ▸REST API backend
  • ▸Concurrent web crawler
  • ▸JSON data processor
  • ▸Simple microservice with fiber-based concurrency

Comparisons

  • ▸Syntax similar to Ruby, but compiled and statically typed
  • ▸Faster than interpreted languages like Ruby or Python
  • ▸Less mature ecosystem than mainstream languages
  • ▸Macro system allows metaprogramming unlike Go
  • ▸Concurrency model simpler than full OS threads

Strengths

  • ▸High performance due to compilation to native code
  • ▸Readable Ruby-like syntax
  • ▸Strong static typing with minimal verbosity
  • ▸Macros for metaprogramming and code reuse
  • ▸Good concurrency model with lightweight fibers

Limitations

  • ▸Smaller ecosystem than Ruby or Python
  • ▸Slower compiler for large projects
  • ▸Limited libraries for certain domains
  • ▸Less community support compared to mainstream languages
  • ▸Still maturing in tooling and IDE support

When NOT to Use

  • ▸GUI-heavy desktop applications
  • ▸Mobile app development
  • ▸Machine learning with limited libraries
  • ▸Enterprise applications requiring large ecosystems
  • ▸Projects needing mature third-party libraries

Cheat Sheet

  • ▸crystal build file.cr # compile
  • ▸crystal run file.cr # compile & run
  • ▸crystal spec # run tests
  • ▸shards install # install dependencies
  • ▸fiber = Fiber.new { ... }

FAQ

  • ▸Is Crystal ready for production?
  • ▸Yes - used in web backends, CLI tools, and microservices.
  • ▸Is Crystal similar to Ruby?
  • ▸Yes - syntax is Ruby-like, but it is compiled and statically typed.
  • ▸Does Crystal have concurrency?
  • ▸Yes - via fibers and channels.
  • ▸Can I use C libraries?
  • ▸Yes - Crystal has native C interop.

30-Day Skill Plan

  • ▸Week 1: Syntax, types, and variables
  • ▸Week 2: Classes, modules, methods, and exceptions
  • ▸Week 3: Web frameworks and HTTP handling
  • ▸Week 4: Fibers, macros, and high-performance patterns

Final Summary

  • ▸Crystal combines Ruby-like syntax with compiled performance and static typing.
  • ▸Ideal for web backends, CLI tools, and concurrent applications.
  • ▸Smaller but growing ecosystem with modern features.
  • ▸Focuses on productivity without sacrificing execution speed.

Project Structure

  • ▸src/ (source code)
  • ▸lib/ (dependencies via Shards)
  • ▸spec/ (unit tests)
  • ▸shard.yml (dependency config)
  • ▸bin/ (compiled binaries)

Monetization

  • ▸Freelance backend development
  • ▸High-performance CLI tools
  • ▸Microservices consulting
  • ▸Web application startups
  • ▸Open-source library development

Productivity Tips

  • ▸Leverage Shards for library management
  • ▸Use macros for repeated code patterns
  • ▸Write tests in parallel with development
  • ▸Use fiber concurrency for efficient IO
  • ▸Keep code clean and Ruby-like for readability

Basic Concepts

  • ▸Crystal syntax and expressions
  • ▸Variables, types, and constants
  • ▸Classes, structs, and modules
  • ▸Methods, blocks, and lambdas
  • ▸Error handling with `raise` and `rescue`

Official Docs

  • ▸Crystal Lang Official Documentation
  • ▸Shards Dependency Manager Guide
  • ▸Kemal Web Framework Documentation

More Crystal Typing Exercises

Crystal Counter and Theme ToggleCrystal Simple AdditionCrystal FactorialCrystal Max of Two NumbersCrystal List SumCrystal Even Numbers FilterCrystal Conditional Counter IncrementCrystal Resettable CounterCrystal Theme Toggle Only

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher