-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Added merge
(product) to Arrow
for arrows composition
#2063
Changes from 12 commits
28fe55a
c2e451f
29c878c
4d88b6c
fa6aa46
4f1c522
d7d0d47
5f1e1d6
f80bcb5
87ff6a6
db2d224
2c0bc19
817831a
91dfe26
17c6a9e
c53e368
ef5996f
5bd2889
4caa33e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,4 +45,50 @@ import simulacrum.typeclass | |
@simulacrum.op("***", alias = true) | ||
def split[A, B, C, D](f: F[A, B], g: F[C, D]): F[(A, C), (B, D)] = | ||
andThen(first(f), second(g)) | ||
|
||
/** | ||
* Create a new computation `F` that merge outputs of `f` and `g` both having the same input | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> val addEmpty: Int => Int = _ + 0 | ||
* scala> val multiplyEmpty: Int => Double= _ * 1d | ||
* scala> val f: Int => (Int, Double) = addEmpty &&& multiplyEmpty | ||
* scala> f(1) | ||
* res0: (Int, Double) = (1,1.0) | ||
* }}} | ||
* | ||
* Note that the arrow laws do not guarantee the non-interference between the _effects_ of | ||
* `f` and `g` in the context of F. This means that `f &&& g` may not be equivalent to `g &&& f`. | ||
*/ | ||
@simulacrum.op("&&&", alias = true) | ||
def merge[A, B, C](f: F[A, B], g: F[A, C]): F[A, (B, C)] = { | ||
andThen(lift((x: A) => (x, x)), split(f, g)) | ||
} | ||
|
||
/** | ||
* Create a new computation `F` that apply f andThen biforks the result. | ||
* On one way it is applied to g and on the other it is passed through. | ||
* The final result is a tuple | ||
* <br/><pre> | ||
* /--out1 = f(input) ----\ | ||
* input -->{ }--->(out1,out2) | ||
* \--out2 = g(f(input))--/ | ||
* </pre> | ||
* Example: | ||
* {{{ | ||
* scala> import cats.implicits._ | ||
* scala> val twoTimes: Int => Double = _ * 2d | ||
* scala> val fiveTimes: Double => Double= _ * 5 | ||
* scala> val f: Int => (Double, Double) = twoTimes -< fiveTimes | ||
* scala> f(2) | ||
* res0: (Double, Double) = (4.0,20.0) | ||
* }}} | ||
* | ||
*/ | ||
@simulacrum.op("-<", alias = true) | ||
def combineAndByPass[A, B, C](f: F[A, B], g: F[B, C]): F[A, (B, C)] = { | ||
andThen(lift((x: A) => (x, x)), split(f, andThen(f, g))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One downside of this is that it calculates There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. I think I incline to not adding this one, and the default impl is just an alias to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes you are right. It is also the same of this one: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mmm.... I think that also |
||
} | ||
} |
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.
Could you explain the meaning behind this name? Should it be
Bi
instead ofBy
? It makes me think of the word bypass, but I suspect you weren't going for that based on the camelCasing.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.
Sorry it should be 'bypass' without camelCase. I'm converting the camel to a dromedary ;-) and the I'll commit.