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

Issue-1177 TArray #1191

Merged
merged 2 commits into from Aug 22, 2019
Merged

Issue-1177 TArray #1191

merged 2 commits into from Aug 22, 2019

Conversation

LGLO
Copy link
Contributor

@LGLO LGLO commented Jul 16, 2019

TArray[A] wrapper for Array[TRef[A]]
List of functions (quick draft, for sure some will be added, maybe some will be removed):

  • Tests for each method
  • apply
  • update, updateM
  • fold, foldM
  • map, mapM - copies to new TArray
  • foreach (doesn't make sense for A => Unit)
  • transform, transformM

@LGLO
Copy link
Contributor Author

LGLO commented Jul 16, 2019

@jdegoes I've opened this for quick review. Could you just tell me if I'm generally doing it right or is this totally missed shot?
Moar functions will come, documentation also.

@jdegoes
Copy link
Member

jdegoes commented Jul 17, 2019

@LGLO Yes, generally speaking, this is right on! Nice work. 👍

@LGLO LGLO force-pushed the issue-1177-tarray branch 2 times, most recently from c394a7f to c56f472 Compare July 28, 2019 05:45
@LGLO LGLO requested a review from jdegoes July 28, 2019 14:53
@LGLO LGLO changed the title [WIP] Issue-1177 TArray Issue-1177 TArray Jul 28, 2019
@LGLO
Copy link
Contributor Author

LGLO commented Jul 28, 2019

Should I re-write tests to new framework?

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.

Looking great! Just a few minor tweaks / questions and will be good to ship.

this.foldM(())((_, a) => f(a))

/** Creates [[TArray]] of new [[TRef]]s, mapped with pure function. */
def map[B](f: A => B): STM[Nothing, TArray[B]] =
Copy link
Member

Choose a reason for hiding this comment

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

I do not think I would include map or mapM. If necessary we could include a duplicate method to create a copy of the array, then the user could use transform / transformM on the duplicate.

How does that sound?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't see it working, because duplicate and transform wouldn't/don't change type of TArray[A].
transform (named after Array#transform) updates values inside Refs.
Maybe I should rename transform to updateAll?

STM.foreach(array)(_.get.flatMap(f).flatMap(b => TRef.make(b))).map(l => new TArray(l.toArray))

/** Atomically updates all [[TRef]]s inside this array using pure function. */
def transform(f: A => A): 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.

I'd make all methods final.

}

/** Updates element in the array with given function. None signals index out of bounds. */
def update(index: Int, fn: A => A): STM[Nothing, Option[A]] =
Copy link
Member

Choose a reason for hiding this comment

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

We could also use STM.die for out of bounds. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think it will be better. User is still obliged to take care of it but not necessarily just after calling this method.
👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Umm, why die and not fail?

@LGLO
Copy link
Contributor Author

LGLO commented Aug 2, 2019

@jdegoes please take a look at 97a7d65 and tell me which parts do you like and which you don't like.
After implementing alternative solution, I think that mapping index out of bounds to None in result channel is not that bad. Error channel gets ugly for updateM if I use STM.fail.
STM.die isn't nice IMO.

I see a lot of changes in tests. Should I migrate TArraySpec to new zio tests?

@jdegoes
Copy link
Member

jdegoes commented Aug 2, 2019

@LGLO I would not port tests yet. We can do that later after ZIO Test is further along.

@LGLO
Copy link
Contributor Author

LGLO commented Aug 2, 2019

@jdegoes OK - I'll leave tests refactoring for some later time.
Please choose if I should go with Option, fail or die for apply, update and updateM. I'll make changes this weekend!

@jdegoes
Copy link
Member

jdegoes commented Aug 3, 2019

@LGLO I would prefer to die, because in any case I can imagine, array index out of bounds is a bug, and a user cannot sensibly recover from a bug in their code.

@LGLO
Copy link
Contributor Author

LGLO commented Aug 5, 2019

@jdegoes I've switched to die

@jdegoes jdegoes merged commit 934967a into zio:master Aug 22, 2019
@jdegoes
Copy link
Member

jdegoes commented Aug 22, 2019

@LGLO Looks great! Sorry for the delay in getting this merged.

@ghostdogpr
Copy link
Member

Woops, some methods became deprecated so it doesn't compile on master. Will send a PR shortly.

@ghostdogpr ghostdogpr mentioned this pull request Aug 22, 2019
@LGLO LGLO deleted the issue-1177-tarray branch October 11, 2019 04:23
@adamgfraser adamgfraser mentioned this pull request Oct 18, 2019
10 tasks
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.

None yet

3 participants