ProductUnit constructor

ProductUnit(
  1. Iterable<RationalPower<Unit>> elements
)

Implementation

factory ProductUnit(Iterable<RationalPower<Unit>> elements) {
  var all = elements
      .expand((v) {
        var base = v.base;
        if (base is ProductUnit) {
          // TODO recursive
          return base._elements
              .map((s) => RationalPower(s.base, s.pow.times(v.pow)));
        }
        return [v];
      })
      .where((v) => v.base != Unit.one)
      .toList();

  var map = <Unit, RationalPower<Unit>>{};
  for (var e in all) {
    if (map.containsKey(e.base)) {
      map[e.base] = RationalPower(e.base, map[e.base]!.pow.add(e.pow));
    } else {
      map[e.base] = e;
    }
  }
  return ProductUnit._(map.values.toList());
}