/
base_unit.ex
305 lines (235 loc) · 7.87 KB
/
base_unit.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
defmodule Cldr.Unit.BaseUnit do
@moduledoc """
Functions to support the base unit calculations
for a unit.
Base unit equality is used to determine whether
a one unit can be converted to another
"""
alias Cldr.Unit.Conversion
alias Cldr.Unit.Parser
alias Cldr.Unit
@per "_per_"
@doc """
Returns the canonical base unit name
for a unit.
The base unit is the common unit through which
conversions are passed.
## Arguments
* `unit_string` is any string representing
a unit such as `light_year_per_week`.
## Returns
* `{:ok, canonical_base_unit}` or
* `{:error, {exception, reason}}`
## Examples
iex> Cldr.Unit.Parser.canonical_base_unit "meter"
{:ok, :meter}
iex> Cldr.Unit.Parser.canonical_base_unit "meter meter"
{:ok, :square_meter}
iex> Cldr.Unit.Parser.canonical_base_unit "meter per kilogram"
{:ok, "meter_per_kilogram"}
iex> Cldr.Unit.Parser.canonical_base_unit "yottagram per mile scandinavian"
{:ok, "kilogram_per_meter"}
"""
def canonical_base_unit(unit) when is_binary(unit) do
with {:ok, parsed} <- Parser.parse_unit(unit) do
canonical_base_unit(parsed)
end
end
# A "per" unit
def canonical_base_unit({numerator, denominator}) do
with numerator <- do_canonical_base_unit(numerator),
denominator <- do_canonical_base_unit(denominator) do
{numerator, denominator}
|> merge_unit_names()
|> sort_base_units()
|> reduce_powers()
|> flatten_and_stringify()
|> Unit.maybe_translatable_unit()
|> wrap(:ok)
end
end
# A list of conversions
def canonical_base_unit(numerator) do
numerator
|> do_canonical_base_unit()
|> flatten_and_stringify()
|> Unit.maybe_translatable_unit()
|> wrap(:ok)
end
def do_canonical_base_unit(numerator) when is_list(numerator) do
numerator
|> Enum.map(&canonical_base_subunit/1)
|> resolve_unit_names()
|> sort_base_units()
|> reduce_powers()
end
defp canonical_base_subunit({_unit_name, %Conversion{base_unit: base_units}}) do
base_units
|> parse_base_units()
|> extract_unit_names()
end
defp parse_base_units([prefix, unit]) do
[[prefix, parse_base_units([unit])]]
end
defp parse_base_units([unit]) do
unit
|> to_string
|> Cldr.Unit.normalize_unit_name()
|> Parser.parse_unit!()
end
# Base units are either
# A {numerator, denominator} tuple
# A list of {unit, base_unit} tuples
defp extract_unit_names({numerator, denominator}) do
{extract_keys(numerator), extract_keys(denominator)}
end
defp extract_unit_names(numerator) do
extract_keys(numerator)
end
# Extract the base units from the conversion
# And simplify base units (ie unwrap them)
defp extract_keys(list) do
Enum.map(list, fn
[prefix, conversion] ->
[prefix, hd(extract_keys(conversion))]
{_unit, conversion} ->
conversion
|> Map.fetch!(:base_unit)
|> case do
[unit] -> unit
[prefix, unit] -> [prefix, unit]
end
end)
end
# Merge all list elements, starting with the first
# two until the end of the list
defp resolve_unit_names([first]) do
first
end
defp resolve_unit_names([first, second | rest]) do
resolve_unit_names([merge_unit_names(first, second) | rest])
end
# Take two list elements and merge them noting that either
# element might be a "per tuple" represnted by a tuple
defp merge_unit_names({numerator_a, denominator_a}, {numerator_b, denominator_b}) do
{merge_unit_names(numerator_a, numerator_b), merge_unit_names(denominator_a, denominator_b)}
end
defp merge_unit_names({numerator_a, denominator_a}, numerator_b) do
{merge_unit_names(numerator_a, numerator_b), denominator_a}
end
defp merge_unit_names(numerator_a, {numerator_b, denominator_b}) do
{merge_unit_names(numerator_a, numerator_b), denominator_b}
end
defp merge_unit_names(numerator_a, numerator_b) do
numerator_a ++ numerator_b
end
# Final pass for "per" base units
defp merge_unit_names({{_numerator_a, _denominator_a}, {_numerator_b, _denominator_b}}) do
raise ArgumentError, "unexpected"
end
defp merge_unit_names({{numerator_a, denominator_a}, numerator_b}) do
{numerator_a, merge_unit_names(numerator_b, denominator_a)}
end
defp merge_unit_names(other) do
other
end
# Sort the units in canonical order
defp sort_base_units({numerator, denominator}) do
{Enum.sort(numerator, &base_unit_sorter/2), Enum.sort(denominator, &base_unit_sorter/2)}
end
defp sort_base_units(numerator) do
Enum.sort(numerator, &base_unit_sorter/2)
end
# Relies on base units only ever being a single unit
# or a list with two elements being a prefix and a unit
defp base_unit_sorter(unit_a, unit_b) when is_atom(unit_a) and is_atom(unit_b) do
Map.fetch!(base_units_in_order(), unit_a) < Map.fetch!(base_units_in_order(), unit_b)
end
defp base_unit_sorter(unit_a, [_prefix, unit_b]) when is_atom(unit_a) do
Map.fetch!(base_units_in_order(), unit_a) < Map.fetch!(base_units_in_order(), unit_b)
end
defp base_unit_sorter([_prefix, unit_a], unit_b) when is_atom(unit_b) do
Map.fetch!(base_units_in_order(), unit_a) < Map.fetch!(base_units_in_order(), unit_b)
end
defp base_unit_sorter([_prefix_a, unit_a], [_prefix_b, unit_b]) do
Map.fetch!(base_units_in_order(), unit_a) < Map.fetch!(base_units_in_order(), unit_b)
end
# Reduce powers to square and cubic
defp reduce_powers({numerator, denominator}) do
{reduce_powers(numerator), reduce_powers(denominator)}
end
defp reduce_powers([first]) do
[first]
end
defp reduce_powers([first, first | rest]) do
reduce_powers([[:square, first] | rest])
end
defp reduce_powers([[:square, first], first | rest]) do
reduce_powers([[:cubic, first] | rest])
end
defp reduce_powers([first, [:square, first] | rest]) do
reduce_powers([[:cubic, first] | rest])
end
defp reduce_powers([first | rest]) do
[first | reduce_powers(rest)]
end
# Flaten the list and turn it into a string
defp flatten_and_stringify({numerator, denominator}) do
flatten_and_stringify(numerator) <> @per <> flatten_and_stringify(denominator)
end
defp flatten_and_stringify(numerator) do
numerator
|> List.flatten()
|> Enum.map(&to_string/1)
|> Enum.join("_")
end
@doc """
Returns the canonical base unit name
for a unit.
The base unit is the common unit through which
conversions are passed.
## Arguments
* `unit_string` is any string representing
a unit such as `light_year_per_week`.
## Returns
* `canonical_base_unit` or
* raises an exception
## Examples
iex> Cldr.Unit.Parser.canonical_base_unit! "meter"
:meter
iex> Cldr.Unit.Parser.canonical_base_unit! "meter meter"
:square_meter
iex> Cldr.Unit.Parser.canonical_base_unit! "meter per kilogram"
"meter_per_kilogram"
iex> Cldr.Unit.Parser.canonical_base_unit! "yottagram per mile scandinavian"
"kilogram_per_meter"
"""
def canonical_base_unit!(unit_string) when is_binary(unit_string) do
case canonical_base_unit(unit_string) do
{:ok, unit_name} -> unit_name
{:eror, {exception, reason}} -> raise exception, reason
end
end
# We wrap in a tuple since a nested list can
# create ambiguous processing in other places
@doc false
def wrap([numerator, denominator], tag) do
{tag, {numerator, denominator}}
end
def wrap([numerator], tag) do
{tag, numerator}
end
def wrap(other, tag) do
{tag, other}
end
@base_units_in_order Cldr.Config.units()
|> Map.get(:base_units)
|> Cldr.Unit.Additional.merge_base_units()
|> Enum.map(&elem(&1, 1))
|> Enum.with_index()
|> Map.new()
@doc false
def base_units_in_order do
@base_units_in_order
end
end