-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2229dad
commit 130f2d2
Showing
3 changed files
with
41 additions
and
22 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,45 @@ | ||
defmodule Odometex.Calculator do | ||
@distances "#{__DIR__}/../config/distances.json" |> File.read! |> Poison.Parser.parse! | ||
alias Odometex.Result | ||
|
||
def distances_with_times(distance, %{ order: :desc } = options) do | ||
distances_with_times(distance) | ||
|> Enum.sort(fn(x, y) -> x["meters"] > y["meters"] end) | ||
|> Enum.take(options[:limit]) | ||
@distances "#{__DIR__}/../config/distances.json" | ||
|> File.read! | ||
|> Poison.decode!(as: [%Result{}]) | ||
|
||
def distances_with_times(distance, %{ order: :desc } = options) do | ||
distances_with_times( | ||
distance, | ||
fn(x, y) -> x.meters > y.meters end, | ||
options.limit | ||
) | ||
end | ||
|
||
def distances_with_times(distance, %{ order: :asc } = options) do | ||
distances_with_times(distance) | ||
|> Enum.sort(fn(x, y) -> x["meters"] < y["meters"] end) | ||
|> Enum.take(options[:limit]) | ||
distances_with_times( | ||
distance, | ||
fn(x, y) -> x.meters < y.meters end, | ||
options.limit | ||
) | ||
end | ||
|
||
def distances_with_times(distance, %{ order: :best_match } = options) do | ||
distances_with_times(distance) | ||
|> Enum.sort( | ||
fn(x, y) -> | ||
abs(x["meters"] - distance) < abs(y["meters"] - distance) | ||
end | ||
distances_with_times( | ||
distance, | ||
fn(x, y) -> abs(x.meters - distance) < abs(y.meters - distance) end, | ||
options.limit | ||
) | ||
|> Enum.take(options[:limit]) | ||
end | ||
|
||
defp distances_with_times(distance) do | ||
defp distances_with_times(distance, sort_func, limit) do | ||
@distances | ||
|> Enum.map( | ||
fn(item) -> item|> Map.put("times", item|> times_value(distance)) end | ||
fn(item) -> %{item | times: times_value(item, distance)} end | ||
) | ||
|> Enum.sort(sort_func) | ||
|> Enum.take(limit) | ||
|
||
end | ||
|
||
defp times_value(item, distance) do | ||
original_distance = item |> Map.fetch!("meters") | ||
distance / original_distance |> Float.round(6) | ||
distance / item.meters |> Float.round(6) | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Odometex.Result do | ||
defstruct label: "", meters: 0, times: 0 | ||
end |