-
Couldn't load subscription status.
- Fork 1.4k
Add TMap #2017
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 TMap #2017
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fantastic? Squeaky clean code. Just a few minor comments
| * Makes a new `TMap` that is initialized with the specified values. | ||
| */ | ||
| final def apply[K, V](data: => List[(K, V)]): STM[Nothing, TMap[K, V]] = { | ||
| val capacity = if (data.isEmpty) DefaultCapacity else 2 * data.size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would change to varargs (like most collections) and add a fromIterable method that takes an Iterable (not List).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
| /** | ||
| * Creates new [[TMap]] by mapping all bindings using pure function. | ||
| */ | ||
| final def map[K2, V2](f: ((K, V)) => (K2, V2)): STM[Nothing, TMap[K2, V2]] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally I find these pure methods confusing. I would delete map and mapM, leaving only transform and transformM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, done.
| buckets <- tBuckets.get | ||
| idx <- indexOf(k) | ||
| _ <- buckets.update(idx, upsert) | ||
| data <- self.fold(List.empty[(K, V)])((acc, kv) => kv :: acc) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this grab the whole contents of the map, even if there is no need to resize? We should aim for maximum laziness to avoid doing unnecessary worm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, stored the size in TRef.
| /** | ||
| * Atomically updates all values using effectful function. | ||
| */ | ||
| final def transformValuesM[E](f: V => STM[E, V]): STM[E, Unit] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto.
| /** | ||
| * Removes bindings matching predicate. | ||
| */ | ||
| final def removeIf(p: ((K, V)) => Boolean): STM[Nothing, Unit] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a tuple ((K, V)) instead of (K, V) (Function2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced.
| /** | ||
| * Retains bindings matching predicate. | ||
| */ | ||
| final def retainIf(p: ((K, V)) => Boolean): STM[Nothing, Unit] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a tuple ((K, V)) instead of (K, V) (Function2).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced.
|
@jdegoes removed tupled parameters, refactored |
| /** | ||
| * Atomically performs side-effect for each binding present in map. | ||
| */ | ||
| final def foreach[E](f: ((K, V)) => STM[E, Unit]): STM[E, Unit] = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nested tuple ((K, V)) instead of 2 param function here will make it harder to use in general.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just found 1 more place with the nested tuple. Other than that, looks good to go! Thank you for your hard work on this. 🙏
|
Awesome work! 🎉 |
* Draft the TMap API * Add test skeleton * Create zio-test template for TMapSpec * Initialize TMap * Reformat sources * Implement map lookups * Implement element insertion * Implement removal * Implement filtering * Rearrange companion's methods * Rename suite * Implement fold * Change internal TMap representation * Reduce code duplication * Implement map * Rename filter/filterNot to retainIf/removeIf * Introduce rehashing into map function * Implement foldM * Implement mapM * Document the API * Make capacity flexible * Remove accessM helper * Implement resizing * Align val names * Add fromIterable factory * Update apply factory to use varargs * Cache the total number of elements * Replace map/mapM with transform/transformM * Update size when needed * Insert element in non-empty bucket * Remove unnecessary vals * Remove unnecessary error parameter * Add transformValues and transformValuesM * Remove error type from contains * Drop tuples in favor of multiple args * Remove unnecessary reversing * Update the existing TArray instead of creating new * Remove self-type * Add toList conversion * Add keys and values lookups * Remove code duplication * Express transform in terms of transformM * Inline unused helper * Remove more duplicates * Align code format * Express fold in terms of foldM * Reorder private methods * Revert "Express fold in terms of foldM" This reverts commit 6124f9a. * Rename collectWith to collectM * Specialize transform and transformM * Remove tupled param from foreach
* Draft the TMap API * Add test skeleton * Create zio-test template for TMapSpec * Initialize TMap * Reformat sources * Implement map lookups * Implement element insertion * Implement removal * Implement filtering * Rearrange companion's methods * Rename suite * Implement fold * Change internal TMap representation * Reduce code duplication * Implement map * Rename filter/filterNot to retainIf/removeIf * Introduce rehashing into map function * Implement foldM * Implement mapM * Document the API * Make capacity flexible * Remove accessM helper * Implement resizing * Align val names * Add fromIterable factory * Update apply factory to use varargs * Cache the total number of elements * Replace map/mapM with transform/transformM * Update size when needed * Insert element in non-empty bucket * Remove unnecessary vals * Remove unnecessary error parameter * Add transformValues and transformValuesM * Remove error type from contains * Drop tuples in favor of multiple args * Remove unnecessary reversing * Update the existing TArray instead of creating new * Remove self-type * Add toList conversion * Add keys and values lookups * Remove code duplication * Express transform in terms of transformM * Inline unused helper * Remove more duplicates * Align code format * Express fold in terms of foldM * Reorder private methods * Revert "Express fold in terms of foldM" This reverts commit 6124f9a. * Rename collectWith to collectM * Specialize transform and transformM * Remove tupled param from foreach
Closes #1178 (finally)