Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@mijicd
Copy link
Member

@mijicd mijicd commented Oct 22, 2019

Closes #1178 (finally)

@mijicd mijicd requested a review from jdegoes October 22, 2019 23:02
Copy link
Member

@jdegoes jdegoes left a 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
Copy link
Member

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).

Copy link
Member Author

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]] =
Copy link
Member

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

Copy link
Member Author

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)
Copy link
Member

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

Copy link
Member Author

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.

@mijicd
Copy link
Member Author

mijicd commented Oct 23, 2019

@jdegoes Done with the changes. Note that the PR uses a part of #2019 , therefore that one should be reviewed and (hopefully) merged first.

/**
* Atomically updates all values using effectful function.
*/
final def transformValuesM[E](f: V => STM[E, V]): STM[E, Unit] =
Copy link
Member

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] =
Copy link
Member

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).

Copy link
Member Author

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] =
Copy link
Member

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).

Copy link
Member Author

Choose a reason for hiding this comment

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

Replaced.

@mijicd
Copy link
Member Author

mijicd commented Oct 24, 2019

@jdegoes removed tupled parameters, refactored transform* functions. I've also exposed toList, keys and values.

/**
* Atomically performs side-effect for each binding present in map.
*/
final def foreach[E](f: ((K, V)) => STM[E, Unit]): STM[E, Unit] =
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch, removed.

Copy link
Member

@jdegoes jdegoes left a 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. 🙏

@jdegoes jdegoes merged commit fba9e53 into zio:master Oct 24, 2019
@jdegoes
Copy link
Member

jdegoes commented Oct 24, 2019

Awesome work! 🎉

@mijicd mijicd deleted the tmap branch October 24, 2019 12:39
ghostdogpr pushed a commit to ghostdogpr/scalaz-zio that referenced this pull request Nov 5, 2019
* 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
Twizty pushed a commit to Twizty/zio that referenced this pull request Nov 13, 2019
* 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
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.

Implement TMap

3 participants