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

Add complex-numbers exercise #100

Merged
merged 1 commit into from
Sep 21, 2024
Merged

Conversation

ageron
Copy link
Contributor

@ageron ageron commented Sep 20, 2024

No description provided.

@ageron
Copy link
Contributor Author

ageron commented Sep 20, 2024

Do you think this is too short?

Complex : { re : F64, im : F64 }

Should it be this instead?

Complex : { real : F64, imaginary: F64 }

This would mean functions like:

div : Complex, Complex -> Complex
div = \z1, z2 ->
    denominator = z2.real * z2.real + z2.imaginary * z2.imaginary
    {
        re: (z1.real * z2.real + z1.imaginary * z2.imaginary) / denominator,
        im: (z1.imaginary * z2.real - z1.real * z2.imaginary) / denominator,
    }

instead of this:

div : Complex, Complex -> Complex
div = \z1, z2 ->
    denominator = z2.re * z2.re + z2.im * z2.im
    {
        re: (z1.re * z2.re + z1.im * z2.im) / denominator,
        im: (z1.im * z2.re - z1.re * z2.im) / denominator,
    }

Copy link
Contributor

@isaacvando isaacvando left a comment

Choose a reason for hiding this comment

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

Love it! I like the terse version personally, I think it is plenty clear and very pleasant to parse. Still many more characters than would be used to describe these operations in math notation 😄

@ageron
Copy link
Contributor Author

ageron commented Sep 21, 2024

I also considered destructuring:

div : Complex, Complex -> Complex
div = \{real: a, imaginary: b}, {real: c, imaginary: d} ->
    denominator = c * c + d * d
    {
        real: (a * c + b * d) / denominator,
        imaginary: (b * c - a * d) / denominator,
    }

This might actually be nicer, wdyt?

@ageron
Copy link
Contributor Author

ageron commented Sep 21, 2024

Actually, I've settled on the following, with the terse { re, im } and using destructuring in the solution, so the equations look nice.

Edit: I merged this PR too fast, so the final improvements are in PR #102, sorry about that.

@ageron
Copy link
Contributor Author

ageron commented Sep 21, 2024

Thanks again @isaacvando !

@ageron ageron merged commit ad09ee5 into exercism:main Sep 21, 2024
4 checks passed
@ageron ageron deleted the add-complex-numbers branch September 21, 2024 00:33
@ageron ageron restored the add-complex-numbers branch September 21, 2024 00:33
@ageron ageron deleted the add-complex-numbers branch September 21, 2024 00:34
@ageron ageron mentioned this pull request Sep 21, 2024
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.

2 participants