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

Added Bernoulli sample #3554

Conversation

MarquisdeGeek
Copy link

No description provided.

@luislavena
Copy link
Contributor

Hello @MarquisdeGeek, thank you for your contribution.

In order to solve the formating error reported by Travis, can you reformat your code and push it again?

Simply run crystal tool format which will ensure all Crystal files are formatted correctly.

See more details in CONTRIBUTING.md and the docs

end


def calculateBernoulli(bern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crystal uses snake_case convention, so calculate_bernoulli would fit better.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -67,7 +60,6 @@ end

# Int32's are only big enough to calculate to 17 Bernoulli numbers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use Int64?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hadn't seen the syntax for Int64 literals. Now found and fixed.

end

def reduce
gcd = gcd(@numerator, @denominator)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same variable name and method name?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What alternative name would you suggest?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I think it's fine, its clear what's a variable and what's a method call because its not an argless call.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that there's already Int#gcd

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RX14 is probably correct. What is 'best practices' for Crystal? If avoiding the same name for a method and a local variable is desireable [my preference] then, maybe use something like 'div', 'divisor', 'deno', or 'denominator' for the variable since it is internal to the 'Fraction#reduce' method? The '#gcd' method has different params than 'Int#gcd', but the purpose is basically the same, so the method name is probably fine as-is.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asterite, were you suggesting that the method name is fine or were you suggesting a conflict w/ Int#gcd? What is your recommendation?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No conflict, bu we already have a gcd method for the Int` type, that you could use, instead of reimplementing it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep using the alternative gcd impl for examples sake. Make a note that Int#gcd

@straight-shoota
Copy link
Member

This is not a good example of Crystal code because it does not properly use Crystal idioms such as getter, ivar assignment from attributes, custom implementation of stlib method Int#gcd and method names minus multiply instead of operator symbols - * instead of.

@asterite
Copy link
Member

Closing because even though the sample is nice, it doesn't demonstrate any Crystal feature in particular.

@asterite asterite closed this Sep 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants