I’ve created a gist with one approach you can take. A couple of thoughts:
- I find it clearer to parse data into a structured form first, then add meaning to it
- Working out a good structure for the data helps develop a more robust framework for later analysis. In this example, a graph data structure would be good since the vertices would be the bodies of interest and the edges would be the weight (distance) between them.
Using the iinked module, the following examples reflect this thinking:
# Parse the data into a canonical form. This form happens
# to be compatible with the edge definition in the `lib graph`
# library.
iex> parsed = Distance.parse
[
{"Sun", "Mercury", [weight: 57909227]},
{"Earth", "Moon", [weight: 384400]},
{"Sun", "Earth", [weight: 149598262]},
{"Sun", "Moon", [weight: nil]},
{"Deimos", "Moon", [weight: nil]},
{"Deimos", nil, [weight: nil]},
{"Deimos", "Phobos", [weight: nil]},
{"Moon", nil, [weight: nil]},
{"LROrbiter", nil, [weight: nil]}
]
# Now we can convert this list of parsed data into
# a graph using `libgraph`.
iex> g = Distance.build_graph(parsed)
#Graph<type: directed, vertices: ["Sun", "Mercury", "Earth", "Moon"], edges: ["Sun" -> "Mercury", "Sun" -> "Earth", "Earth" -> "Moon"]>
# Now we can ask questions of the data
iex> Distance.distances_from g, "Earth"
[%Graph.Edge{label: nil, v1: "Earth", v2: "Moon", weight: 384400}]
iex> Distance.distances_from g, "Sun"
[
%Graph.Edge{label: nil, v1: "Sun", v2: "Mercury", weight: 57909227},
%Graph.Edge{label: nil, v1: "Sun", v2: "Earth", weight: 149598262}
]
# We can also filter the parsed data to get only those
# elements that have a distance
iex> Distance.with_distance parsed
[
{"Sun", "Mercury", [weight: 57909227]},
{"Earth", "Moon", [weight: 384400]},
{"Sun", "Earth", [weight: 149598262]}
]
# Or don't have a distance...
iex> Distance.with_no_distance parsed
[
{"Sun", "Moon", [weight: nil]},
{"Deimos", "Moon", [weight: nil]},
{"Deimos", nil, [weight: nil]},
{"Deimos", "Phobos", [weight: nil]},
{"Moon", nil, [weight: nil]},
{"LROrbiter", nil, [weight: nil]}
]