Skip to content

Releases: RougeWare/Swift-Basic-Math-Tools

1.3 • Additive Reductions

30 Nov 20:24
a821470
Compare
Choose a tag to compare

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

Full Changelog: 1.2.1...1.3.0

1.2 – Wrapping

30 Aug 18:42
ff4ca8f
Compare
Choose a tag to compare

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?

Gotcha Covered

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:

A chart depicting a sawtooth-style wrapping of numbers, 3.0 through 4.9, repeating left to right

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

17 Oct 21:28
1862065
Compare
Choose a tag to compare

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

04 Aug 03:59
d43e243
Compare
Choose a tag to compare

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!