<div dir="ltr"><div>Here is a clamp implementation I am using in a real project:</div><div><br></div><div>```</div><div><div>extension Comparable {</div><div> func clamped(from lowerBound: Self, to upperBound: Self) -> Self {</div><div> return min(max(self, lowerBound), upperBound)</div><div> }</div><div> </div><div> func clamped(to range: ClosedRange<Self>) -> Self {</div><div> return min(max(self, range.lowerBound), range.upperBound)</div><div> }</div><div>}</div><div><br></div><div>extension Strideable where Self.Stride: SignedInteger {</div><div> func clamped(to range: CountableClosedRange<Self>) -> Self {</div><div> return min(max(self, range.lowerBound), range.upperBound)</div><div> }</div><div>}</div></div><div>```</div><div><br></div><div><a href="https://github.com/fulldecent/FDWaveformView/blob/master/Source/FDWaveformView.swift#L573-L589">https://github.com/fulldecent/FDWaveformView/blob/master/Source/FDWaveformView.swift#L573-L589</a><br></div><div><br></div><div>Open ranges do not have a clear enough answer for me. Especially in the case of an empty range. Therefore the above implementation is the most conservative. </div><div><br></div>- William Entriken<br clear="all"><div><div class="gmail_signature"><div dir="ltr"><div><br></div></div></div></div>
</div>