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

Map aliases #1315

Merged
merged 7 commits into from Aug 7, 2019
Merged

Map aliases #1315

merged 7 commits into from Aug 7, 2019

Conversation

ioleo
Copy link
Member

@ioleo ioleo commented Aug 1, 2019

This proposition consists of two parts:

  1. Adding the as and asError operators.

Similar to *> and <* operators, sometimes we just don't care about the value in the success/error channel and wish to replace it. With these operators, we can express that more clearly.

val answerToLive1 = getStrLn map (_ => "42")
// vs
val answerToLive2 = getStrLn as "42"
  1. Adding symbolic aliases for map, mapError, as and asError.

The symbolic aliases are more readable when chaining lots of operations.

val result1 = foo flatMap bar flatMap baz map wrap
// vs
val result2 = foo >>= bar >>= baz +> wrap
// or even

val result3 = (
  foo 
    >>= bar
    >>= baz
    +> wrap
)

The choice of symbols is somewhat arbitrary. Among many alternatives I propose these (+>, *+>, #>, *#>) follwing this reasoning:

  • the right arrow > indicates the direction of the flow, similar to *> operator
  • the + and # indicates the channel being mapped (respectively success and error)
  • the * prefix indicates the previous value is being ignored, similar to *> operator

I'm open to discuss alternatives.

@jdegoes
Copy link
Member

jdegoes commented Aug 3, 2019

I am not opposed to as. Note that it serves the same function as const, but const is inherited from Haskell; as reads better with OO syntax.

To make this change, however, we would need:

  1. Deprecate all the const methods
  2. Add asUnit, and deprecate unit

Anyone else have thoughts on this?

@jdegoes
Copy link
Member

jdegoes commented Aug 3, 2019

Clarification: I am opposed to the symbolic aliases. Developers outside FP complain loudly about these, and unlike, e.g., >>=, there is no precedent for new operators.

@ghostdogpr
Copy link
Member

ghostdogpr commented Aug 3, 2019

I quite liked .unit. .asUnit is as long as .as(()).

@adamgfraser
Copy link
Contributor

I like as. I agree that asUnit seems a little wordy for a common and simple method, maybe skip or just leave as is? I am thumbs down on the symbolic aliases for the reasons discussed above. If you want to write in that style you could could always do fa *> succeed(b) or fa *> fail(e).

@iravid
Copy link
Member

iravid commented Aug 3, 2019

👍 on as
👎 on asUnit; prefer to leave unit as is
👎 on additional symbolics, especially ones that aren't established

@jdegoes
Copy link
Member

jdegoes commented Aug 4, 2019

All right, so it looks like we're:

  • 👍 to as, but we need deprecations for const, and we need as added everywhere const is used (and that is a lot of places!)
  • 👎 to asUnit, leaving as(()) with the name unit for brevity
  • 👎 to more symbolic names

@ioleo
Copy link
Member Author

ioleo commented Aug 5, 2019

Alright, thanks for the input! I will adjust the PR as soon as I have some spare time :)

@ioleo ioleo force-pushed the map-aliases branch 5 times, most recently from b41047a to 09abf7d Compare August 6, 2019 12:12
@ioleo
Copy link
Member Author

ioleo commented Aug 6, 2019

Also for consistency (principle of least surprise):

  • deprecated ZSchedule.const
  • added ZSchedule.as
  • deprecated ZSink.const
  • added ZSink.as

@ioleo
Copy link
Member Author

ioleo commented Aug 6, 2019

@jdegoes Ready to review. ci/circleci: test_212_jdk8_jvm failure is in BlockingSpec, I believe its a flaky test and the failure is unrelated to this PR.

core/shared/src/main/scala/zio/ZIO.scala Outdated Show resolved Hide resolved
core/shared/src/main/scala/zio/ZIO.scala Outdated Show resolved Hide resolved
Copy link
Member

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

There are some other const functions defined in ZManaged, Gen, Assertion, Exit, Fiber, FunctionIO, Runtime and STM. I think we better change them all at once?

@ioleo
Copy link
Member Author

ioleo commented Aug 6, 2019

@ghostdogpr Added as and asError (where mapError was defined) to ZManaged, Assertion, Exit, Fiber, FunctionIO, Runtime and STM.

Also, reordered combinators as you asked.

Copy link
Member

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

Looks good! Just one remark left in Exit.

core/shared/src/main/scala/zio/Exit.scala Outdated Show resolved Hide resolved
@ioleo
Copy link
Member Author

ioleo commented Aug 7, 2019

@ghostdogpr Ready

@ghostdogpr ghostdogpr merged commit 4811a5f into zio:master Aug 7, 2019
@ioleo ioleo deleted the map-aliases branch August 7, 2019 09:02
@ghostdogpr
Copy link
Member

@ioleo thanks for the suggestion & implementation!

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

5 participants