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

More idiomatic and efficient computations #25

Open
dievsky opened this issue Nov 20, 2019 · 0 comments
Open

More idiomatic and efficient computations #25

dievsky opened this issue Nov 20, 2019 · 0 comments
Assignees

Comments

@dievsky
Copy link
Contributor

dievsky commented Nov 20, 2019

Currently it's hard to chain computations efficiently. If you write a.exp() * 2.0 - 1.0, it creates two unnecessary copies. To avoid this, you have to do it either with three lines:

val b = a.exp()
b *= 2.0
b -= 1.0

or with an unreadable mess of apply-s:

val b = a.copy().apply { expInPlace() }.apply { timesAssign(2.0) }.apply { minusAssign(1.0) }

It would be much easier if in-place and assign methods allowed chaining.

This is sadly impossible for the assign operators, since they are required by language to only return Unit, but we can provide equivalent methods with differing names to circumvent this, e.g.

val b = a.exp().timesInPlace(2.0).minusInPlace(1.0)

Another, more difficult but equally more intriguing possibility, is to somehow enable delayed computations, so that the user would describe what needs to be computed and how to compute it, and the library would then choose between in-place and copying methods to satisfy these requirements. I don't currently have a clear idea of how to implement this.

@dievsky dievsky self-assigned this Nov 20, 2019
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

1 participant