Swift BMI


(Gage Morgan) #1

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.
There are two methods called towards the end of file:1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.
2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.
You already get the gist, the bits can be found here:https://gist.github.com/anonymous/9284017644567c29c7f8
If there's a bug please let me know, but it works in IBM's Sandbox. (Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--


(Gage Morgan) #2

And a more polished version:https://gist.github.com/MGageMorgan/c2166174be878e9cea78

···

Sent from Outlook Mobile

On Mon, Dec 14, 2015 at 7:20 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.
There are two methods called towards the end of file:1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.
2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.
You already get the gist, the bits can be found here:https://gist.github.com/anonymous/9284017644567c29c7f8
If there's a bug please let me know, but it works in IBM's Sandbox. (Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--


(Erica Sadun) #3

Leaving aside typos, any discussion about the merits of BMI, and the intemperate language in the code, you do a few things that could be improved upon language-wise. Of these, the most egregious is the final "break the machine" else statement that can never be reached.

The following example does several things:
http://swiftstub.com/358512527

* Adds a precondition to ensure that clients have passed valid arguments
* Uses an enumeration (with a default value) instead of overloading a class function with nearly identical code
* Introduces a multiplier to address the kg/lb difference
* Establishes an upper case name for the class
* Uses a switch statement instead of cascaded ifs
* Returns a String instead of running procedurally

-- E

···

On Dec 14, 2015, at 8:20 PM, Gage Morgan via swift-users <swift-users@swift.org> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Eimantas Vaiciunas) #4

I exchanged a class to an enum since the BMI values are a finite set. This
eliminates a need for valid value check since the switch that is used to
initialise the enum value is exhaustive.

http://swiftstub.com/242291010/

···

On Tue, Dec 15, 2015 at 3:30 PM, Erica Sadun via swift-users < swift-users@swift.org> wrote:

Leaving aside typos, any discussion about the merits of BMI, and the
intemperate language in the code, you do a few things that could be
improved upon language-wise. Of these, the most egregious is the final
"break the machine" else statement that can never be reached.

The following example does several things:
http://swiftstub.com/358512527

* Adds a precondition to ensure that clients have passed valid arguments
* Uses an enumeration (with a default value) instead of overloading a
class function with nearly identical code
* Introduces a multiplier to address the kg/lb difference
* Establishes an upper case name for the class
* Uses a switch statement instead of cascaded ifs
* Returns a String instead of running procedurally

-- E

On Dec 14, 2015, at 8:20 PM, Gage Morgan via swift-users < > swift-users@swift.org> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI
calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if
you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if
you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit
laid back.)
--MGage--

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #5

I understand that mine is a bit more complicated than it needs to be. It works, that's good enough for me. It gives proper values. I knew SOMEONE would have an issue with the "breaking machine," and I realized it could not be reached before typing it. I put it in there as a joke, nothing more (I've done worse in actual C).
As for types and other stuff, I will acknowledge that there could possibly be up to ten different revisions with much simpler code. I prefer to write things out, which is why there's not as many abstract concepts as some would like. I did as a test, just to see if it could be done. And the answer is yes, and that's good enough for me. The next time I plug in a formula I will consider these, but may not use them.
I understand your above points, but there are of course 14000 different ways that you could do it. Don't overthink it. I did it the way I did it because I could understand it, no intentions to be .selfless or take a .selfie whatsoever (THAT WAS A JOKE, FOR THE LOVE OF CHRIST).
There are many ways you can dumb it down. Unfortunately, abstraction and I don't play nice. So it would make sense for me to have written it out step-by-step. Look at the calculations given by the second revision. They are unrounded versions of the CDC website's results, are they not?

···

Sent from Outlook Mobile

On Mon, Dec 14, 2015 at 9:50 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

And a more polished version:https://gist.github.com/MGageMorgan/c2166174be878e9cea78

Sent from Outlook Mobile

On Mon, Dec 14, 2015 at 7:20 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.
There are two methods called towards the end of file:1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.
2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.
You already get the gist, the bits can be found here:https://gist.github.com/anonymous/9284017644567c29c7f8
If there's a bug please let me know, but it works in IBM's Sandbox. (Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--


(Erica Sadun) #6

In fact, there's no really good reason to use any construct, whether struct, class, or enum, for this at all since the BMI isn't a "thing" per se. A global function might be a better fit.

http://swiftstub.com/443052056

-- E

···

On Dec 15, 2015, at 7:10 AM, Eimantas Vaiciunas via swift-users <swift-users@swift.org> wrote:

I exchanged a class to an enum since the BMI values are a finite set. This eliminates a need for valid value check since the switch that is used to initialise the enum value is exhaustive.

http://swiftstub.com/242291010/

On Tue, Dec 15, 2015 at 3:30 PM, Erica Sadun via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Leaving aside typos, any discussion about the merits of BMI, and the intemperate language in the code, you do a few things that could be improved upon language-wise. Of these, the most egregious is the final "break the machine" else statement that can never be reached.

The following example does several things:
http://swiftstub.com/358512527

* Adds a precondition to ensure that clients have passed valid arguments
* Uses an enumeration (with a default value) instead of overloading a class function with nearly identical code
* Introduces a multiplier to address the kg/lb difference
* Establishes an upper case name for the class
* Uses a switch statement instead of cascaded ifs
* Returns a String instead of running procedurally

-- E

On Dec 14, 2015, at 8:20 PM, Gage Morgan via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Erica Sadun) #7

And, of course, this is a perfect opportunity to use Swift tuple returns and eliminate the side-effect of printing the numeric value:

http://swiftstub.com/176972405

-- E, whose kids have a snow day this morning in case you can't tell

···

On Dec 15, 2015, at 7:20 AM, Erica Sadun <erica@ericasadun.com> wrote:

In fact, there's no really good reason to use any construct, whether struct, class, or enum, for this at all since the BMI isn't a "thing" per se. A global function might be a better fit.

http://swiftstub.com/443052056

-- E

On Dec 15, 2015, at 7:10 AM, Eimantas Vaiciunas via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I exchanged a class to an enum since the BMI values are a finite set. This eliminates a need for valid value check since the switch that is used to initialise the enum value is exhaustive.

http://swiftstub.com/242291010/

On Tue, Dec 15, 2015 at 3:30 PM, Erica Sadun via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:
Leaving aside typos, any discussion about the merits of BMI, and the intemperate language in the code, you do a few things that could be improved upon language-wise. Of these, the most egregious is the final "break the machine" else statement that can never be reached.

The following example does several things:
http://swiftstub.com/358512527

* Adds a precondition to ensure that clients have passed valid arguments
* Uses an enumeration (with a default value) instead of overloading a class function with nearly identical code
* Introduces a multiplier to address the kg/lb difference
* Establishes an upper case name for the class
* Uses a switch statement instead of cascaded ifs
* Returns a String instead of running procedurally

-- E

On Dec 14, 2015, at 8:20 PM, Gage Morgan via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #8

Also, another goal of doing it was to see if I could using a class. Which worked. I apologize for the delay, apparently as a Junior our HS does not trust us with EMAIL which of all things kids probably don't use for messaging.

···

Sent from Outlook Mobile

On Tue, Dec 15, 2015 at 12:35 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

I understand that mine is a bit more complicated than it needs to be. It works, that's good enough for me. It gives proper values. I knew SOMEONE would have an issue with the "breaking machine," and I realized it could not be reached before typing it. I put it in there as a joke, nothing more (I've done worse in actual C).
As for types and other stuff, I will acknowledge that there could possibly be up to ten different revisions with much simpler code. I prefer to write things out, which is why there's not as many abstract concepts as some would like. I did as a test, just to see if it could be done. And the answer is yes, and that's good enough for me. The next time I plug in a formula I will consider these, but may not use them.
I understand your above points, but there are of course 14000 different ways that you could do it. Don't overthink it. I did it the way I did it because I could understand it, no intentions to be .selfless or take a .selfie whatsoever (THAT WAS A JOKE, FOR THE LOVE OF CHRIST).
There are many ways you can dumb it down. Unfortunately, abstraction and I don't play nice. So it would make sense for me to have written it out step-by-step. Look at the calculations given by the second revision. They are unrounded versions of the CDC website's results, are they not?

Sent from Outlook Mobile

On Mon, Dec 14, 2015 at 9:50 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

And a more polished version:https://gist.github.com/MGageMorgan/c2166174be878e9cea78

Sent from Outlook Mobile

On Mon, Dec 14, 2015 at 7:20 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.
There are two methods called towards the end of file:1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.
2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.
You already get the gist, the bits can be found here:https://gist.github.com/anonymous/9284017644567c29c7f8
If there's a bug please let me know, but it works in IBM's Sandbox. (Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--


(Kelly Keenan) #9

Hi Gage,

I’d like to invite you to read through our Code of Conduct <https://swift.org/community/#code-of-conduct> for the Swift community. While something may seem like a joke to you, it is not always the case for everyone. Please be mindful of this in the code that you write as well as the comments you make. From the Code of Conduct:

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Thanks,
~ Kelly

Kelly Keenan kkeenan@.com <mailto:kkeenan@apple.com> conduct@swift.org

···

On Dec 15, 2015, at 12:37, Gage Morgan via swift-users <swift-users@swift.org> wrote:

Also, another goal of doing it was to see if I could using a class. Which worked. I apologize for the delay, apparently as a Junior our HS does not trust us with EMAIL which of all things kids probably don't use for messaging.

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Tue, Dec 15, 2015 at 12:35 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I understand that mine is a bit more complicated than it needs to be. It works, that's good enough for me. It gives proper values. I knew SOMEONE would have an issue with the "breaking machine," and I realized it could not be reached before typing it. I put it in there as a joke, nothing more (I've done worse in actual C).

As for types and other stuff, I will acknowledge that there could possibly be up to ten different revisions with much simpler code. I prefer to write things out, which is why there's not as many abstract concepts as some would like. I did as a test, just to see if it could be done. And the answer is yes, and that's good enough for me. The next time I plug in a formula I will consider these, but may not use them.

I understand your above points, but there are of course 14000 different ways that you could do it. Don't overthink it. I did it the way I did it because I could understand it, no intentions to be .selfless or take a .selfie whatsoever (THAT WAS A JOKE, FOR THE LOVE OF CHRIST).

There are many ways you can dumb it down. Unfortunately, abstraction and I don't play nice. So it would make sense for me to have written it out step-by-step. Look at the calculations given by the second revision. They are unrounded versions of the CDC website's results, are they not?

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Mon, Dec 14, 2015 at 9:50 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

And a more polished version:
https://gist.github.com/MGageMorgan/c2166174be878e9cea78

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Mon, Dec 14, 2015 at 7:20 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--

<Mail Attachment.txt><Mail Attachment.txt><Mail Attachment.txt>_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Gage Morgan) #10

Understood.

Hi Gage,

I’d like to invite you to read through our Code of Conduct <https://swift.org/community/#code-of-conduct> for the Swift community. While something may seem like a joke to you, it is not always the case for everyone. Please be mindful of this in the code that you write as well as the comments you make. From the Code of Conduct:

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Thanks,
~ Kelly

Kelly Keenan kkeenan@.com <mailto:kkeenan@apple.com> conduct@swift.org

···

Sent from Outlook Mobile
On Tue, Dec 15, 2015 at 2:19 PM -0800, "Kelly Keenan" <kkeenan@apple.com> wrote:

On Dec 15, 2015, at 12:37, Gage Morgan via swift-users <swift-users@swift.org> wrote:

Also, another goal of doing it was to see if I could using a class. Which worked. I apologize for the delay, apparently as a Junior our HS does not trust us with EMAIL which of all things kids probably don't use for messaging.

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Tue, Dec 15, 2015 at 12:35 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

I understand that mine is a bit more complicated than it needs to be. It works, that's good enough for me. It gives proper values. I knew SOMEONE would have an issue with the "breaking machine," and I realized it could not be reached before typing it. I put it in there as a joke, nothing more (I've done worse in actual C).

As for types and other stuff, I will acknowledge that there could possibly be up to ten different revisions with much simpler code. I prefer to write things out, which is why there's not as many abstract concepts as some would like. I did as a test, just to see if it could be done. And the answer is yes, and that's good enough for me. The next time I plug in a formula I will consider these, but may not use them.

I understand your above points, but there are of course 14000 different ways that you could do it. Don't overthink it. I did it the way I did it because I could understand it, no intentions to be .selfless or take a .selfie whatsoever (THAT WAS A JOKE, FOR THE LOVE OF CHRIST).

There are many ways you can dumb it down. Unfortunately, abstraction and I don't play nice. So it would make sense for me to have written it out step-by-step. Look at the calculations given by the second revision. They are unrounded versions of the CDC website's results, are they not?

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Mon, Dec 14, 2015 at 9:50 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

And a more polished version:
https://gist.github.com/MGageMorgan/c2166174be878e9cea78

Sent from Outlook Mobile <https://aka.ms/sdimjr>

On Mon, Dec 14, 2015 at 7:20 PM -0800, "Gage Morgan via swift-users" <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

For most languages I try out, my own version of "Hello World!" is a BMI calculator. Swift has passed, I encourage you to try it out.

There are two methods called towards the end of file:
1) bmi.pounds(weight, height) - Replace numbers inside with your own if you want to test out in US Customary units.

2) bmi.kilograms(weight, height) - Replace numbers inside with your own if you want to test out in Metric units used everywhere outside the US.

You already get the gist, the bits can be found here:
https://gist.github.com/anonymous/9284017644567c29c7f8

If there's a bug please let me know, but it works in IBM's Sandbox.
(Yes, ALL code was written by me without help. Very close to C, just a bit laid back.)
--MGage--

<Mail Attachment.txt><Mail Attachment.txt><Mail Attachment.txt>_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users