Skip to content

Commit

Permalink
Refactor using structs
Browse files Browse the repository at this point in the history
  • Loading branch information
dsantosmerino committed May 12, 2018
1 parent 2229dad commit 130f2d2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
43 changes: 25 additions & 18 deletions lib/calculator.ex
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
17 changes: 13 additions & 4 deletions lib/odometex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,22 @@ defmodule Odometex do
@compare_default_options %{order: :best_match, limit: 20}

@doc """
Returns a list of distances
Returns a list of results with the number of times of each one
## Examples
iex> Odometex.compare(42195)
[%{ "label": "Marathon", "meters": 42195, "times": 1}]
iex> Odometex.compare(20, limit: 5)
[
%Odometex.Result{label: "Bowling lane", meters: 19, times: 1.052632},
%Odometex.Result{label: "Blue Whale (female)", meters: 25, times: 0.8},
%Odometex.Result{label: "Basketball court", meters: 28, times: 0.714286},
%Odometex.Result{label: "Football pitch", meters: 105, times: 0.190476},
%Odometex.Result{
label: "Passeig de Gràcia, Barcelona",
meters: 1300,
times: 0.015385
}
]
"""
def compare(distance, options \\ []) do
merged_options = Enum.into(options, @compare_default_options)
Expand Down
3 changes: 3 additions & 0 deletions lib/structs/result.ex
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

0 comments on commit 130f2d2

Please sign in to comment.