Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Crystal Clean

Crystal Clean

An overview of the Crystal programming language, with a lot of comparisons with Ruby.

Luís Ferreira

January 22, 2015
Tweet

More Decks by Luís Ferreira

Other Decks in Technology

Transcript

  1. def fib(n)! if n < 3! 1! else! fib(n-1) +

    fib(n-2)! end! end! ! fib(35)
  2. RUBY def fib(n)! if n < 3! 1! else! fib(n-1)

    + fib(n-2)! end! end! ! fib(35)
  3. CRYSTAL def fib(n)! if n < 3! 1! else! fib(n-1)

    + fib(n-2)! end! end! ! fib(35)
  4. Calculating ------------------------------------- ruby 1.000 i/100ms crystal 2.000 i/100ms ----------------------------------------------------- ruby

    1.268 (± 0.0%) i/s - 13.000 crystal 26.321 (± 3.8%) i/s - 264.000 ! Comparison: crystal: 26.3 i/s ruby: 1.3 i/s - 20.75x slower
  5. RUBY class Email! attr_reader :subject, :date, :from! ! def initialize(subject,

    date: "", from: "")! @subject = subject! @date = date! @from = from! end! ! def to_s ! “Date: #{date}! From: #{from}! Subject: #{subject}”! end! end
  6. CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,

    options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
  7. CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,

    options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
  8. CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,

    options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
  9. CRYSTAL class Email! getter :subject, :date, :from! ! def initialize(subject,

    options = {} of Symbol => String)! @subject = subject! @date = options.fetch(:date, "")! @from = options.fetch(:from, "")! end! ! def to_s(io: IO)! io << "Date: #{date}\n! ! ! ! ! ! ! ! From: #{from}\n! Subject: #{subject}\n\n"! end! end
  10. RUBY def string_size(str)! puts str.length! end! ! string_size("This is a

    string")! string_size(nil) 16 `string_size': undefined method `length' for nil:NilClass (NoMethodError)
  11. CRYSTAL def string_size(str)! puts str.length! end! ! string_size("This is a

    string")! string_size(nil) instantiating 'string_size(Nil)' undefined method 'length' for Nil
  12. CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
  13. CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
  14. CRYSTAL class Email! getter! :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date Nil assertion failed
  15. CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date? # nil
  16. CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date
  17. CRYSTAL class Email! getter? :subject, :date, :from! ! ...! end!

    ! email = Email.new("Homework this week", { date: nil, from: "Ferdous" }),! ! p email.date undefined method 'date' for Email (did you mean 'date?'?)
  18. CRYSTAL class Object! macro getter(*names)! {% for name in names

    %}! {% name = name.var if name.is_a?(DeclareVar) %}! ! def {{name.id}}! @{{name.id}}! end! {% end %}! end! end
  19. RUBY Not very simple to connect ! Need to write

    C ! Need to know Ruby’s C implementation (struct, methods, etc…)
  20. CRYSTAL lib C! fun atoi(str : UInt8*) : Int32! end!

    ! p C.atoi(“28").class # Int32
  21. CRYSTAL @[Link("readline")]! lib LibReadline! fun readline(prompt : UInt8*) : Pointer(UInt8)!

    end! ! line = LibReadline.readline("What's your name?\n")! p String.new(line) # What’s your name?! # zamith! # ”zamith"
  22. RUBY class Foo! attr_reader :value! ! def initialize(value)! @value =

    value! end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97
  23. CRYSTAL class Foo! getter :value! ! def initialize(@value); end! end!

    ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new(‘a’)! p foo.value.ord # undefined method 'abs' for Char
  24. CRYSTAL class Foo(T)! getter :value! ! def initialize(@value : T);

    end! end! ! foo = Foo.new(1)! p foo.value.abs # 1! ! foo = Foo.new('a')! p foo.value.ord # 97
  25. CRYSTAL class Array(T)! include Enumerable! include Comparable(Array)! ! def initialize(size,

    value : T)! ! ! . . .! end! end arr = [1, 2]! arr.first.abs! ! arr = [1, '2']! arr.first.abs # error!
  26. CRYSTAL require "spec"! ! class MyString! def initialize(@string); end! !

    def size! @string.size! end! end! ! describe "Repo" do! describe "size" do! it "returns the size of my string" do! MyString.new("Hello").size.should eq 5! end! end! end
  27. CRYSTAL RUBY [1,2].map &.to_s.+(“ hello”) [1,2].map do |num| ! !

    num.to_s.+(" hello”)! end # [“1 hello", "2 hello"] # [“1 hello", "2 hello"]
  28. CRYSTAL class Hash(K, V)! ...! def self.new(comp = StandardComparator, &block

    : (Hash(K, V), K -> V))! new block, comp! end! ! def self.new(default_value : V, comp = StandardComparator)! new(comp) { default_value }! end! ! def self.new(comparator)! new nil, comparator! end! ...! end
  29. CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :

    V, comp = StandardComparator)! new(comp) { default_value }! end! ...! end
  30. CRYSTAL class Hash(K, V)! ...! def self.new(default_value = nil :

    V?, comp = StandardComparator)! new(comp) { default_value }! end! ...! end