Skip to content

Instantly share code, notes, and snippets.

@dnedrow
Last active March 3, 2020 13:23
Show Gist options
  • Select an option

  • Save dnedrow/e7346890e6b4bb568fe95228c60bf82c to your computer and use it in GitHub Desktop.

Select an option

Save dnedrow/e7346890e6b4bb568fe95228c60bf82c to your computer and use it in GitHub Desktop.
// Comparable+Extensions.swift
// Attribution 4.0 International (CC 4.0), see bottom for license details.
import Foundation
extension Comparable {
func clamp<T: Comparable>(lower: T, _ upper: T) -> T {
assert(lower <= upper)
return min(max(self as! T, lower), upper)
}
}
// This work is licensed under Attribution 4.0 International (CC 4.0)
// You are free to:
// Share — copy and redistribute the material in any medium or format
// Adapt — remix, transform, and build upon the material
// for any purpose, even commercially.
//
// This license is acceptable for Free Cultural Works.
// The licensor cannot revoke these freedoms as long as you follow the license terms.
// See https://creativecommons.org/licenses/by/4.0/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment