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
Piping methods on Any similar to scalaz "|>", Ruby "tap" #5324
Comments
Imported From: https://issues.scala-lang.org/browse/SI-5324?orig=1 |
@paulp said: implicit def enrichAnyRefWithTap[T](x: T) = new TapMaker(x)
class TapMaker[T](x: T) {
def tap[U](f: T => U): T = {
f(x)
x
}
} |
@Ichoran said: I don't like convert, because it's too long and things like |
Nikita Volkov (mojojojo) said: Actually I came up with |
Can one (such as myself) PR this? Or should it be rejected and closed? |
seems like an open question whether a PR on this would ultimately succeed. naming-wise, I like |
Fixes scala/bug#5324 This implements two implicit classes `AnyTap` and `AnyPipe`, which enrich every type `A` to inject `tap` and `pipe` method respectively. ```scala scala> val xs = List(1, 2, 3).tap(ys => println("debug " + ys.toString)) debug List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> val s = List(1, 2, 3).pipe(xs => xs.mkString(",")) s: String = 1,2,3 ```
Sent a PR - scala/scala#6767 |
Repeating what I said at scala/scala#6767 (comment) : What's the rationale for this to be in There should be much stronger resistance against putting stuff in |
So it seems like people are ok with this being part of Scala library, but -1 on Any suggestions on what the import would be called? import scala.util.AnyOps ? |
|
Please, you shouldn't even be considering a name like |
Good point. Maybe we should adopt the Cats naming convention and prefix the package name like |
Fixes scala/bug#5324 This implements an opt-in enrichment for any type called tap and pipe: ```scala scala> import scala.util.chainingOps._ import scala.util.chainingOps._ scala> val xs = List(1, 2, 3).tap(ys => println("debug " + ys.toString)) debug List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> val times6 = (_: Int) * 6 times6: Int => Int = $$Lambda$1727/1479800269@10fbbdb scala> (1 + 2 + 3).pipe(times6) res0: Int = 36 scala> (1 - 2 - 3).pipe(times6).pipe(scala.math.abs) res1: Int = 24 ```
Fixes scala/bug#5324 This implements an opt-in enrichment for any type called tap and pipe: ```scala scala> import scala.util.chainingOps._ import scala.util.chainingOps._ scala> val xs = List(1, 2, 3).tap(ys => println("debug " + ys.toString)) debug List(1, 2, 3) xs: List[Int] = List(1, 2, 3) scala> val times6 = (_: Int) * 6 times6: Int => Int = $$Lambda$1727/1479800269@10fbbdb scala> (1 + 2 + 3).pipe(times6) res0: Int = 36 scala> (1 - 2 - 3).pipe(times6).pipe(scala.math.abs) res1: Int = 24 ```
I wonder if we can backport this feature to https://github.com/scala/scala-collection-compat |
Introduction
The piping methods have proven to be extremely useful in other languages:
.with
- Groovy;.tap
- Ruby. It's a known fact that a lot of people get surprised not to meet any similar feature in Scala as well as that because of that a lot of them introduce custom implicit conversions.Examples
Let's consider two examples of how the same purpose of outputting the following text can be achieved with and without the proposed feature:
Example one: with proposed methods
tap
andconvert
Please notice how factored, maintainable and easy on the eye the above code is
Example two: how one has to do it currently
Notice that you have to stop and think 3 times on naming the obviously redundant intermediate variables. Also notice how unreadable, unfactored and harder to manage that code is.
Implementation
Currently for the first example to work you'll need a following implicit conversion. You can also learn the specification of methods from there:
The suggestions:
This suggestion originates from the following discussion on StackOverflow: http://stackoverflow.com/questions/8537992/with-alternative-in-scala/8538277
The text was updated successfully, but these errors were encountered: