Similar things are grouped together
1. Load points coordinates
2. Compute pairwise distances
3. Update clusters if the rule is satisfied
1. Load points coordinates
2. Compute pairwise distances
3. Update clusters if the rule is satisfied
1. Load points coordinates
2. Compute pairwise distances
3. Update clusters if the rule is satisfied
1. Load points coordinates
2. Compute pairwise distances
3. Update clusters if the rule is satisfied
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
def dist(u, v)
(u[0] - v[0]).abs + (u[1] - v[1]).abs
end
dist_list = []
nodes.each_with_index do |u, u_idx|
nodes[u_idx..-1].each_with_index do |v, v_idx|
dist_list << [u_idx, v_idx, dist(u, v)]
end
end
Expressiveness
Correctness
Performance
nodes = File.readlines("./data/nodes.txt")
.map{|line| line.split(" ").map(&:to_i)}
nodes = File.read_lines("./data/nodes.txt")
.map{|line| line.split(" ").map(&.to_i)}
API differences
flexible shorthand
block syntax
flexible shorthand block syntax
["some", "String", "HeRe"].map(&.downcase.reverse)
# => ["emos", "gnirts", "ereh"]
(-2..2).map(&.abs.+(1).*(2))
# => [6, 4, 2, 4, 6]
dist_list = []
dist_list << [u_idx, v_idx, dist(u,v)]
dist_list = [] of {Int32, Int32, Int32}
dist_list << {u_idx, v_idx, dist(u,v)}
Empty array initialization
dist_list = []
dist_list << [u_idx, v_idx, dist(u,v)]
dist_list = [] of {Int32, Int32, Int32}
dist_list << {u_idx, v_idx, dist(u,v)}
Tuples are encoded as a separate type
Expressiveness
Correctness
Performance
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
p find_similar(user, table)
^~~~~~~~~~~~
in users.cr:13: undefined method '[]' for Nil (compile-time type is (Hash(String, String) | Nil))
users.select{ |other| other["origin"] == user["origin"]}
^
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
p find_similar(user, table)
^~~~~~~~~~~~
in users.cr:13: undefined method '[]' for Nil (compile-time type is (Hash(String, String) | Nil))
users.select{ |other| other["origin"] == user["origin"]}
^
Nil reference check
____
def find_by_id(id, users)
users.find{ |user| user["id"].to_i == id}
end
def find_similar(user, users)
return nil if user.nil?
users.select{ |other| other["origin"] == user["origin"]}
end
user = find_by_id(240, table)
p find_similar(user, table)
_____________
def send(commands)
cmds = commands.is_a?(Array) ?
commands :
[commands]
cmds.each do |cmd|
# do something
end
end
def send(commands)
cmds = commands.respond_to?(:each) ?
commands :
[commands]
cmds.each do |cmd|
# do something
end
end
def send(commands)
cmds = commands.responds_to?(:each) ?
commands :
[commands]
cmds.each do |cmd|
# do something
end
end
_
The English language is safe!
def send(commands : Array)
commands.each_with_index do |cmd, idx|
# do something
end
end
def send(command)
send([command])
end
Method overloading based on
def send(commands : Enumerable)
commands.each_with_index do |cmd, idx|
# do something
end
end
def send(command)
send([command])
end
Method overloading
__________
Expressiveness
Correctness
Performance
Compiled language speed
Low memory footprint
Event loop powered non-blocking I/O
Channel based communication between fibers
Expressiveness
Correctness
Performance
Performance
Correctness
{ name: lorenzo.barasti, works_at: , blog: lbarasti.github.io, twitter: @lbarasti }
Thanks for listening!