Releases: RougeWare/Swift-Basic-Math-Tools
1.3 • Additive Reductions
This introduces a .reduce(_:)
function to sequences whose elements conform to AdditiveArithmetic
which assumes you're reducing into .zero
:
// Before
bunchaNumbers.reduce(into: 0) { $0 = max($0, $1) }
// After
bunchaNumbers.reduce { $0 = max($0, $1) }
This also adds a convenience function .sum()
, build on this new reducer:
// Before
bunchaNumbers.reduce(into: 0, +=)
// After
bunchaNumbers.sum()
Patch Notes
1.3.0
- #12: Added
.reduce
which assumes reducing into 0 when elements are additive
- #12: Added
Full Changelog: 1.2.1...1.3.0
1.2 – Wrapping
So you want your value to be within a particular range, but instead of clamping when it gets outside that range, you want it to repeat back to the beginning of the range?
Now that's as simple as calling .wrapped(within:)
on any number!
value.wrapped(within: range)
This loops the number within the range, like this:
Here's a code example of how this works:
print(0.wrapped(within: 3..<6)) // 3
print(1.wrapped(within: 3..<6)) // 4
print(2.wrapped(within: 3..<6)) // 5
print(3.wrapped(within: 3..<6)) // 3
print(4.wrapped(within: 3..<6)) // 4
print(5.wrapped(within: 3..<6)) // 5
print(6.wrapped(within: 3..<6)) // 3
print(7.wrapped(within: 3..<6)) // 4
print(8.wrapped(within: 3..<6)) // 5
Patches
1.2.1
• #10 • Fixed compiler error when archiving
1.1 - Clamps
This update introduces some simple clamp functions! These make it easy to clamp a value between two others. Like Swift's min
and max
, these work for any Comparable
type.
Whichever you use simply depends on your preference or needs; they all act identically:
print(clamp(min: 2, value: 0, max: 7)) // Prints 2
print(clamp(min: 2, value: 5, max: 7)) // Prints 5
print(clamp(min: 2, value: 99, max: 7)) // Prints 7
print( 0.clamping(min: 2, max: 7)) // Prints 2
print( 5.clamping(min: 2, max: 7)) // Prints 5
print(99.clamping(min: 2, max: 7)) // Prints 7
print( 0.clamping(within: 2...7)) // Prints 2
print( 5.clamping(within: 2...7)) // Prints 5
print(99.clamping(within: 2...7)) // Prints 7
Patch Updates
- 1.1.1 - Added support for non-x86_64 platforms
1.0.0 - MVP
Kicking this off with one of my favorite little tools - tolerable equality. Basically gets rid of silly little IEEE FP rounding errors, and helps with doing math with big numbers. Hope it helps you too!