crystal-lang / crystal-website

crystal-lang.org website

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Relaunch] Suggested changes in code snippets

beta-ziliani opened this issue · comments

Syntax:

  1. It was wrong (crashed with empty string)
  2. It was suggesting to press play, but no play button so far

Suggestion:

class String
  def longest_repetition?
    max = chars
            .chunk(&.itself)
            .map(&.last)
            .max_by?(&.size)

    {max[0], max.size} if max
  end
end

puts "aaabb".longest_repetition? # => {'a', 3}

Concurrency

Add some forced sleep to enforce the notion of concurrent events

Suggestion:

channel = Channel(Int32).new

3.times do |i|
  spawn do
    3.times do |j|
      sleep rand(100).milliseconds
      channel.send 10 * (i + 1) + j
    end
  end
end

9.times do
  puts channel.receive
end