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

Imperative methods #29

Open
raviqqe opened this issue Jan 29, 2023 · 0 comments
Open

Imperative methods #29

raviqqe opened this issue Jan 29, 2023 · 0 comments

Comments

@raviqqe
Copy link
Owner

raviqqe commented Jan 29, 2023

Problem

  • We currently use a functional style for all the interfaces of persistent data structures.
    • This is primarily the easiness of writing programs in a functional style.
  • We implicitly use option types and take references of keys and values through pointer references in methods of persistent data structures.
  • It's not optimal for performance.
    • e.g. We can't know if entries are inserted/removed or not at the same time!
    • Go is primarily a system programming language but not a functional programming language!
  • The interface will be inconsistent with methods providing destructive updates, which might be implemented in the future.

Examples

func (m Map[K, V]) Find(k K) *V {
  ...
}

func (m Map[K, V]) Insert(k K, v V) Map[K, V] {
  ...
}

Solution

Examples

func (m Map[K, V]) Find(k K) (V, bool) {
  ...
}

func (m Map[K, V]) Insert(k K, v V) (Map[K, V], bool) {
  ...
}

References

  • The original discussion is in Update the package to use generics #16.
  • im, which is probably the most popular persistent data structure library in Rust adopts imperative interfaces for their implementations.
    • I guess it's because Rust users usually care more about performance than easiness in programming.
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

No branches or pull requests

1 participant