├── .gitignore ├── hello-world.cr ├── http-server.cr ├── http-client.cr ├── bubble-sort.cr └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .crystal 2 | *.swp 3 | 4 | -------------------------------------------------------------------------------- /hello-world.cr: -------------------------------------------------------------------------------- 1 | puts "Hello world!" 2 | -------------------------------------------------------------------------------- /http-server.cr: -------------------------------------------------------------------------------- 1 | require "http/server" 2 | 3 | server = HTTP::Server.new(8080) do |request| 4 | #puts "[#{Time.now}] Received request #{request}" 5 | HTTP::Response.ok "text/plain", "Hello world! The time is #{Time.now}" 6 | end 7 | 8 | puts "Listening on http://0.0.0.0:8080" 9 | server.listen 10 | 11 | -------------------------------------------------------------------------------- /http-client.cr: -------------------------------------------------------------------------------- 1 | require "http" 2 | 3 | client = HTTP::Client.new("0.0.0.0", 8080) 4 | 5 | 1_000_000.times do |i| 6 | # puts "[#{Time.now}] Sending #{i} request" 7 | # response = 8 | client.get("/") 9 | # puts "[#{Time.now}] Got response #{response.body}" 10 | end 11 | 12 | puts "Done 1M requests" 13 | -------------------------------------------------------------------------------- /bubble-sort.cr: -------------------------------------------------------------------------------- 1 | a = [] of Int32 2 | 3 | 10.times do |i| 4 | prng = Random.new(i) 5 | a << prng.rand(1000 + i) 6 | end 7 | 8 | def sort(a) 9 | a.each_with_index do |e1, i1| 10 | a.each_with_index do |e2, i2| 11 | if i1 < i2 12 | if e1 > e2 13 | tmp = e2 14 | a[i2] = e1 15 | a[i1] = tmp 16 | end 17 | end 18 | end 19 | end 20 | end 21 | 22 | puts sort(a).join(" ") 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Crystal language playground 2 | 3 | Trying out some stuff in [Crystal](http://crystal-lang.org/) 4 | 5 | `git clone git@github.com:rilian/crystal-playground.git && cd crystal-playground` 6 | 7 | Bubble sort 8 | ----------- 9 | 10 | `crystal bubble-sort.cr` 11 | 12 | or 13 | 14 | `crystal build bubble-sort.cr && ./bubble-sort` 15 | 16 | Hello world 17 | ----------- 18 | 19 | `crystal hello-world.cr` 20 | 21 | or 22 | 23 | `crystal build hello-world.cr && ./hello-world` 24 | 25 | HTTP Server and Client 26 | ---------------------- 27 | 28 | In one terminal tab `crystal http-server.cr` 29 | In another tab `crystal http-client.cr` 30 | 31 | or 32 | 33 | In one terminal tab `crystal build http-server.cr && ./http-server` 34 | In another tab `crystal build http-client.cr && ./http-client` 35 | 36 | --------------------------------------------------------------------------------