I am wondering whether the following is possible in Scala:
Given some vector x = (x_1, x_2, ..., x_n)
in R^n
and a function f
that maps R^n
to R
, I would like to replicate this concept in Scala. The idea of Scala's partial function/currying should hold here (i.e. when applying a single value x_i
, return a function that is defined only for a subset of its input domain). For example, when n = 2 define f(x, y) = sin(x + y)
, then trivially, f(2, y) = sin(2 + y)
.
However, the dimension (n > 0) may vary from case to case and may even be provided in input.
Partial application for n = 2 is:
def leftPartialFunction(f: (Double, Double) => Double)(x: Double): Double => Double = f(x, _)
but how can this be generalized for arbitrary n?
For example, how can I apply the function in position i
?
Something like this I assume would not work:
def partialFunction(f: IndexedSeq[Double] => Double)(xi: Double): IndexedSeq[Double] => Double = .... // cannot work well with indexed seq as they are not "disjoint"
leftPartialFunction
looks like currying((Double, Double) => Double) => Double => Double => Double
– Equinox((A1, ..., An) => B) => A1 => ... => An => B
for arbitraryn
? – EquinoxDouble => Double
and yieldsDouble
, and we recursively call this function to lower the dimensions of the N dimensional Trapezoidal Rule, there for going (for example)(Double, Double, Double) => Double
to(Double, Double) => Double
toDouble => Double
... – ThalassographySeq
, which seems unfeasible for this purpose. Another level of complexity is that each recursive call, the functionf
changes type (i.e. goes from(Double, Double, Double) => ...
to(Double, Double) => ...
etc... ) – Thalassography