Skip to content

Commit b92753a

Browse files
authored
Merge pull request #4 from RougeWare/feature/Clamps
2 parents d304508 + 461497d commit b92753a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,25 @@ print(1_000_000.equals(1_000_100, tolerance: 1_000))
3131
```
3232

3333
Feel free to check out the tests for more examples!
34+
35+
36+
37+
# Clamps #
38+
39+
This includes a few functions which make it easy to clamp a value between two others. Like Swift's `min` and `max`, these work for any `Comparable` type.
40+
41+
Whichever you use simply depends on your preference or needs; they all act identically:
42+
43+
```swift
44+
print(clamp(min: 2, value: 0, max: 7)) // Prints 2
45+
print(clamp(min: 2, value: 5, max: 7)) // Prints 5
46+
print(clamp(min: 2, value: 99, max: 7)) // Prints 7
47+
48+
print( 0.clamping(min: 2, max: 7)) // Prints 2
49+
print( 5.clamping(min: 2, max: 7)) // Prints 5
50+
print(99.clamping(min: 2, max: 7)) // Prints 7
51+
52+
print( 0.clamping(within: 2...7)) // Prints 2
53+
print( 5.clamping(within: 2...7)) // Prints 5
54+
print(99.clamping(within: 2...7)) // Prints 7
55+
```

0 commit comments

Comments
 (0)