-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add generalized tupled functions abstraction #6568
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
nicolasstucki
merged 14 commits into
scala:master
from
dotty-staging:add-tupled-functions
May 29, 2019
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c6aaaaf
Add TupledFunction to abstract over function arities
nicolasstucki 9a92ec7
Add auto funtion tupled `andThen` and `compose`
nicolasstucki 24a1d00
Add documentation
nicolasstucki c9e5c2b
Fix typo
nicolasstucki 33d7e96
Use TupledFunction[F, G] as function tupling instead of application
nicolasstucki 5f07a17
Add documentation
nicolasstucki f6eb444
Remove TupledFunction tuppled, apply, andThen and compose from stdlib
nicolasstucki baec08b
Add documentation
nicolasstucki 22162be
Improve documentation
nicolasstucki 3c2005d
Seal TupledFunction to only have instances in internal
nicolasstucki a579756
Rename `TupledFunction.apply` to `TupledFunction.tuple`
nicolasstucki 9f02e64
Add TupledFunction.untuple
nicolasstucki 5e209f5
Update docs
nicolasstucki 9507c1f
Rename TupledFunction.{tuple|untuple} to tupled and untupled
nicolasstucki 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
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
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
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,78 @@ | ||
--- | ||
layout: doc-page | ||
title: "Tupled Function" | ||
--- | ||
|
||
Tupled Function | ||
---------------------- | ||
|
||
With functions bounded to arities up to 22 it was possible to generalize some operation on all function types using overloading. | ||
Now that we have functions and tuples generalized to [arities above 22](https://dotty.epfl.ch/docs/reference/dropped-features/limit22.html) overloading is not an option anymore. | ||
The type class `TupleFunction` provides a way to abstract directly over a function of any arity converting it to an equivalent function that receives all arguments in a single tuple. | ||
|
||
```scala | ||
/** Type class relating a `FunctionN[..., R]` with an equivalent tupled function `Function1[TupleN[...], R]` | ||
* | ||
* @tparam F a function type | ||
* @tparam G a tupled function type (function of arity 1 receiving a tuple as argument) | ||
*/ | ||
@implicitNotFound("${F} cannot be tupled as ${G}") | ||
sealed trait TupledFunction[F, G] { | ||
def tupled(f: F): G | ||
def untupled(g: G): F | ||
} | ||
``` | ||
|
||
The compiler will synthesize an instance of `TupledFunction[F, G]` if: | ||
|
||
* `F` is a function type of arity `N` | ||
* `G` is a function with a single tuple argument of size `N` and it's types are equal to the arguments of `F` | ||
* The return type of `F` is equal to the return type of `G` | ||
* `F` and `G` are the same kind of function (both are `(...) => R` or both are `given (...) => R`) | ||
* If only one of `F` or `G` is instantiated the second one is inferred. | ||
|
||
Examples | ||
-------- | ||
`TupledFunction` can be used to generalize the `Function1.tupled`, ... `Function22.tupled` methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/tests/run/tupled-function-tupled.scala)) | ||
|
||
```scala | ||
/** Creates a tupled version of this function: instead of N arguments, | ||
* it accepts a single [[scala.Tuple]] argument. | ||
* | ||
* @tparam F the function type | ||
* @tparam Args the tuple type with the same types as the function arguments of F | ||
* @tparam R the return type of F | ||
*/ | ||
def (f: F) tupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args => R]): Args => R = tf.tupled(f) | ||
``` | ||
|
||
`TupledFunction` can be used to generalize the `Function.untupled` methods to functions of any arities ([full example](https://github.com/lampepfl/dotty/tests/run/tupled-function-untupled.scala)) | ||
|
||
```scala | ||
/** Creates an untupled version of this function: instead of single [[scala.Tuple]] argument, | ||
* it accepts a N arguments. | ||
* | ||
* This is a generalization of [[scala.Function.untupled]] that work on functions of any arity | ||
* | ||
* @tparam F the function type | ||
* @tparam Args the tuple type with the same types as the function arguments of F | ||
* @tparam R the return type of F | ||
*/ | ||
def (f: Args => R) untupled[F, Args <: Tuple, R] given (tf: TupledFunction[F, Args => R]): F = tf.untupled(f) | ||
``` | ||
|
||
`TupledFunction` can also be used to generalize the [`Tuple1.compose`](https://github.com/lampepfl/dotty/tests/run/tupled-function-compose.scala) and [`Tuple1.andThen`](https://github.com/lampepfl/dotty/tests/run/tupled-function-andThen.scala) methods to compose functions of larger arities and with functions that return tuples. | ||
|
||
```scala | ||
/** Composes two instances of TupledFunctions in a new TupledFunctions, with this function applied last | ||
* | ||
* @tparam F a function type | ||
* @tparam G a function type | ||
* @tparam FArgs the tuple type with the same types as the function arguments of F and return type of G | ||
* @tparam GArgs the tuple type with the same types as the function arguments of G | ||
* @tparam R the return type of F | ||
*/ | ||
def (f: F) compose[F, G, FArgs <: Tuple, GArgs <: Tuple, R](g: G) given (tg: TupledFunction[G, GArgs => FArgs], tf: TupledFunction[F, FArgs => R]): GArgs => R = { | ||
(x: GArgs) => tf.tupled(f)(tg.tupled(g)(x)) | ||
} | ||
``` |
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
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 |
---|---|---|
|
@@ -37,4 +37,5 @@ object DottyPredef { | |
} | ||
|
||
inline def the[T] given (x: T): x.type = x | ||
|
||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.