-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Adds tap and pipe methods (available via import scala.util.chaining._)
#7007
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| package scala | ||
| package util | ||
|
|
||
| trait ChainingSyntax { | ||
| implicit final def scalaUtilChainingOps[A](a: A): ChainingOps[A] = new ChainingOps(a) | ||
| } | ||
|
|
||
| /** Adds chaining methods `tap` and `pipe` to every type. | ||
| */ | ||
| final class ChainingOps[A](val self: A) extends AnyVal { | ||
| /** Applies `f` to the value for its side effects, and returns the original value. | ||
| * | ||
| * {{{ | ||
| * val xs = List(1, 2, 3) | ||
| * .tap(ys => println("debug " + ys.toString)) | ||
| * // xs == List(1, 2, 3) | ||
| * }}} | ||
| * | ||
| * @param f the function to apply to the value. | ||
| * @tparam U the result type of the function `f`. | ||
| * @return the original value `self`. | ||
| */ | ||
| def tap[U](f: A => U): self.type = { | ||
| f(self) | ||
| self | ||
| } | ||
|
|
||
| /** Converts the value by applying the function `f`. | ||
| * | ||
| * {{{ | ||
| * val times6 = (_: Int) * 6 | ||
| * val i = (1 - 2 - 3).pipe(times6).pipe(scala.math.abs) | ||
| * // i == 24 | ||
| * }}} | ||
| * | ||
| * Note: `(1 - 2 - 3).pipe(times6)` may have a small amount of overhead at | ||
| * runtime compared to the equivalent `{ val temp = 1 - 2 - 3; times6(temp) }`. | ||
| * | ||
| * @param f the function to apply to the value. | ||
| * @tparam B the result type of the function `f`. | ||
| * @return a new value resulting from applying the given function | ||
| * `f` to this value. | ||
| */ | ||
| def pipe[B](f: A => B): B = f(self) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package scala | ||
|
|
||
| package object util { | ||
| /** | ||
| * Adds chaining methods `tap` and `pipe` to every type. See [[ChainingOps]]. | ||
| */ | ||
| object chaining extends ChainingSyntax | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package scala.util | ||
|
|
||
| import org.junit.Assert._ | ||
| import org.junit.Test | ||
|
|
||
| class ChainingOpsTest { | ||
| import scala.util.chaining._ | ||
|
|
||
| @Test | ||
| def testAnyTap: Unit = { | ||
| var x: Int = 0 | ||
| val result = List(1, 2, 3) | ||
| .tap(xs => x = xs.head) | ||
|
|
||
| assertEquals(1, x) | ||
| assertEquals(List(1, 2, 3), result) | ||
| } | ||
|
|
||
| @Test | ||
| def testAnyPipe: Unit = { | ||
| val times6 = (_: Int) * 6 | ||
| val result = (1 - 2 - 3) | ||
| .pipe(times6) | ||
| .pipe(scala.math.abs) | ||
|
|
||
| assertEquals(24, result) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Other languages name this
|>. Would be great to do so here too.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.
Mentioned here: #6767 (comment)
Uh oh!
There was an error while loading. Please reload this page.
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.
The reasoning is that symbolic operators are bad. Though, as a small datapoint: I have no idea what
<<=,<+=or<++=mean, I only understand>>=if you tell me its name, I always mix up/:and:\, but|>is just obvious to me, and is better at visually separating steps thanpipe. And you have to explicitly import this anyway so having this symbolic alias wouldn't be the worst thing.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.
Ideally if we can ignore backward compat is get rid of
matchas keyword and make it a function namedmatch.|>is basicallymatch.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.
Please add
|>too.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.
Seconding @japgolly . I'm not for symbols in general but this is a very very special ubiquitous one. bash uses something similar, F# has it, PowerShell too.
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.
@ScalaWilliam You can add OCaml & ReasonML to that 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.
Maybe one of you should send a new pull request or contributor thread and get consensus around it.
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.
As advised by @eed3si9n , here's a PR: #7326
fyi @japgolly @RawToast @hepin1989