Skip to content

Commit

Permalink
add operations
Browse files Browse the repository at this point in the history
  • Loading branch information
mamantoha committed Apr 30, 2024
1 parent 594a437 commit 2531804
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions spec/haversine/distance_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ describe Haversine::Distance do
end
end
end

describe "operations" do
dist1 = Haversine::Distance.new(2)
dist2 = Haversine::Distance.new(1)

it { (dist1 + dist2).distance.should eq(3) }
it { (dist1 - dist2).distance.should eq(1) }
end
end
10 changes: 10 additions & 0 deletions src/haversine/distance.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,15 @@ module Haversine
def <=>(other : Haversine::Distance)
distance <=> other.distance
end

# Adds the value of `self` to *other*.
def +(other : Haversine::Distance) : Haversine::Distance
Haversine::Distance.new(distance + other.distance)
end

# Removes the value of *other* from `self`.
def -(other : Haversine::Distance) : Haversine::Distance
Haversine::Distance.new(distance - other.distance)
end
end
end

0 comments on commit 2531804

Please sign in to comment.