jsbean / ArithmeticTools

Basic arithmetic types and operations
MIT License
3 stars 0 forks source link

Conform ClosedRangeProtocol to Monoid? #91

Open jsbean opened 7 years ago

jsbean commented 7 years ago

Currently, ClosedRangeProtocol inherits only from Semigroup ( giving us +), but if the Bound value of a ClosedRangeProtocol is also a Monoid (numerical types, Rational types), then we can benefit from the Monoidal qualities.

That being said, I'm not sure if this is quite possible yet:

extension ClosedRangeProtocol: Monoid where Bound == Monoid {
    static var unit: Self { return .init(uncheckedBounds: (lower: .unit, upper: .unit) }
}

We currently get the error: Extension of protocol cannot have an inheritance clause.

It would be nice to be able to sweep over a sequence of ClosedRange<MetricalDuration> with:

let seq: [ClosedRange<MetricalDuration>] = [...]
let unionOfAllRangesInSeq = seq.reduce(.unit, +)