Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement average() for more numeric types #36

Closed
knothed opened this issue Aug 29, 2019 · 4 comments
Closed

Implement average() for more numeric types #36

knothed opened this issue Aug 29, 2019 · 4 comments

Comments

@knothed
Copy link
Contributor

knothed commented Aug 29, 2019

Currently, func average() -> Double is defined as an extension on Collection where Element == Double. What about CGFloats?

I suggest to add a protocol Field (based on algebraic fields) providing addition, multiplication and division with Self elements, and also a zero and one element. This would allow both average() and sum() to be implemented more generically. Double, Float and CGFloat could conform to this protocol (with nearly no additional implementation effort).

The integer case would have to be treated specially though.

@Jeehut
Copy link
Member

Jeehut commented Aug 30, 2019

CGFloats are Doubles. It's just a typealias. Just command-click it and you'll be amazed. ;)

Please note that there are many libraries providing such mathematical extensions to Swift. HandySwift is only targeting to add the most common things that we think should be part of Swift in the first place. I don't think we should dive too much into such things and recommend other tools that are already amazing at such things instead.

If you really wanted to add such things, please consider searching through the Swift Forums for existing suggestions or pitching your idea there and discussing a bit before making a bigger pull request here. For now, I consider this a non-bug. I'll reopen if necessary.

@Jeehut Jeehut closed this as completed Aug 30, 2019
@knothed
Copy link
Contributor Author

knothed commented Aug 31, 2019

I'm not quite sure what you mean by CGFloats are Doubles. I know that they are the same, at least on 64-bit systems, but that still doesn't allow me to provide a CGFloat as an argument to a function that has a Double as its parameter.

As a consequence, I cannot calculate the average of 2 CGFloat-values without first converting them to Double and then converting the result back to a CGFloat, resulting in code like this:

let averageXY = CGFloat([Double(view.frame.origin.x), Double(view.frame.origin.y)].average())

Also, I think you quite misunderstood my intent – I just wanted to generalize the sum and average methods to CGFloats and Floats. I do not want to provide any kind of mathematical extensions or further, more complex, mathematical stuff. :)

The natural consequence of generalising average calculation is a protocol providing the required basic arithmetic operations: addition and division. The implementation would be extremely minimal because all targeted Swift types already provide these functions.

@Jeehut
Copy link
Member

Jeehut commented Sep 1, 2019

Ah yeah, sorry, I mixed things up with TimeInterval. Of course CGFloat is its own type. 😅

Okay, I'll reopen this. Could you please just post a PR with the approach you mean? I think then the discussion about the details of the implementation will be much easier. Currently, I'm not entirely sure what your suggested approach is. A PR would make that very clear. :)

@Jeehut Jeehut reopened this Sep 1, 2019
@knothed
Copy link
Contributor Author

knothed commented Sep 1, 2019

Yes, I'll drop a PR. Thank you very much for reconsidering :)

Jeehut added a commit that referenced this issue Sep 3, 2019
@knothed knothed closed this as completed Feb 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants