This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
target: &Vec<i64> -> &[i64] | |
from : &Vec<([^>]*)> | |
to : &[$1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn a(n) | |
case {n % 3 == 0, n % 5 == 0} | |
when {true, false} | |
"Fizz" | |
when {false, true} | |
"Buzz" | |
when {true, true} | |
"Fizz Buzz" | |
else | |
n.to_s |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro rep(*args, &block) | |
{% for iterator, idx in args %} | |
({{iterator}}).each do |{{block.args[idx].id}}| | |
{% end %} | |
{{ block.body }} | |
{% for i in args %} end {% end %} | |
end | |
# same | |
rep 0...5 do |i| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
val = 0x80_00_00_00.to_s # => "2147483648" | |
#val.to_i # Invalid Int32: 68719476721 | |
val.to_i64 # => 2147483648 | |
val = 0x7f_ff_ff_ff.to_s # => 2147483647 | |
val.to_i # => 2147483647 | |
0b_0111_1111_1111_1111_1111_1111_1111_1111 # => 2,147,483,647 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
hs = { | |
1 => 12, | |
5 => 31, | |
0 => 41, | |
3 => 56, | |
4 => 21 | |
} | |
hs.max_of(&.[1]) # => 56 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
def method : String | |
yield | |
end | |
method do | |
next 10 # => Error in line 6: instantiating 'method()' | |
# in line 2: type must be String, not Int32 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
result = loop do | |
break 100 | |
end | |
p result # => 100 | |
result = while true | |
break 100 | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
def method : String | |
loop do | |
yield | |
end | |
"end" | |
end | |
result = method do | |
break 100 == 100 ? 200 : 200.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
# https://crystal-lang.org/docs/syntax_and_semantics/blocks_and_procs.html | |
arr = [1, 2, 3, 4, 5] | |
def b(arr) | |
loop do | |
arr.each do |x| | |
yield x | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# crystal | |
[1, 2, 3, 4, 5].each do |i| | |
p "before #{i}" | |
next if i == 3 | |
break if i == 4 | |
p "after #{i}" | |
end | |
# => before 1 | |
# => after 1 |
NewerOlder