title: How to write a currying Scala Function trait?
body: <h2>Issue</h2>
  
  <h3>First approach</h3>
  
  <p>If would like to have</p>
  
  <pre><code>trait Distance extends ((SpacePoint, SpacePoint) =&gt; Double)
  
  object EuclideanDistance extends Distance {
    override def apply(sp1: SpacePoint, sp2: SpacePoint): Double = ???
  }
  
  trait Kernel extends (((Distance)(SpacePoint, SpacePoint)) =&gt; Double)
  
  object GaussianKernel extends Kernel {
    override def apply(distance: Distance)(sp1: SpacePoint, sp2: SpacePoint): Double = ???
  }
  </code></pre>
  
  <p>However the <code>apply</code> of <code>object GaussianKernel extends Kernel</code> is not an excepted <code>override</code> to the <code>apply</code> of <code>trait Kernel</code>.</p>
  
  <h3>Second approach - <strong><em>EDIT:</em></strong> turns out this works afterall...</h3>
  
  <p>Alternatively I could write</p>
  
  <pre><code>trait Kernel extends ((Distance) =&gt; ( (SpacePoint, SpacePoint)  =&gt; Double))
  
  object GaussianKernel extends Kernel {
      override def apply(distance: Distance): (SpacePoint, SpacePoint) =&gt; Double =
          (sp1: SpacePoint, sp2: SpacePoint) =&gt;
              math.exp(-math.pow(distance(sp1, sp2), 2) / (2))
  }
  </code></pre>
  
  <p>but am not sure this is currying...</p>
  
  <p><strong><em>EDIT:</em></strong> Turns out that I can use this second approach in a currying fashion. I think it is exactly what the typical currying is, only without the syntactic sugar.</p>
  
  <hr>
  
  <h2>Explanation of the idea</h2>
  
  <p>The idea is this: For my algorithm I need a <a href="https://en.wikipedia.org/wiki/Kernel_method" rel="nofollow">Kernel</a>. This kernel calculates a metric for two vectors in space - here <code>SpacePoint</code>s. For that the Kernel requires a way to calculate the distance between the two <code>SpacePoint</code>s. Both distance and kernel should be exchangeable (<a href="https://en.wikipedia.org/wiki/Open/closed_principle" rel="nofollow">open-closed principle</a>), thus I declare them as traits (in Java I had them declared as interfaces). Here I use the <a href="https://en.wikipedia.org/wiki/Euclidean_distance" rel="nofollow">Euclidean Distance</a> (not shown) and the <a href="https://en.wikipedia.org/wiki/Gaussian_function" rel="nofollow">Gaussian Kernel</a>. Why the currying? Later when using those things, the <code>distance</code> is going to be more or less the same for all measurements, while the <code>SpacePoint</code>s will change all the time. Again, trying to stay true to the open-closed principle. Thus, in a first step I would like the <code>GaussianKernel</code> to be pre-configured (if you will) with a distance and return a <code>Function</code> that can be feed later in the program with the <code>SpacePoint</code>s (I am sure the code is wrong, just to give you an idea what I am aiming at):</p>
  
  <pre><code>val myFirstKernel  = GaussianKernel(EuclideanDistance)
  val mySecondKernel = GaussianKernel(FancyDistance)
  val myThirdKernel  = EpanechnikovKernel(EuclideanDistance)
  // ...  lots lof code ...
  val firstOtherClass  = new OtherClass(myFirstKernel)
  val secondOtherClass = new OtherClass(mySecondKernel)
  val thirdOtherClass  = new OtherClass(myThirdKernel)
  
  // ...  meanwhile in "OtherClass" ...
  class OtherClass(kernel: Kernel) {
      val thisSpacePoint = ??? // ... fancy stuff going on ...
      val thisSpacePoint = ??? // ... fancy stuff going on ...
      val calculatedKernel = kernel(thisSpacePoint, thatSpacePoint)
  }
  </code></pre>
  
  <hr>
  
  <h2>Questions</h2>
  
  <ol>
  <li>How do I build my trait?</li>
  <li>Since <code>distance</code> can be different for different <code>GaussianKernel</code>s - should <code>GaussianKernel</code> be a class instead of an object?</li>
  <li>Should I partially apply <code>GaussianKernel</code> instead of currying?</li>
  <li>Is my approach bad and <code>GaussianKernel</code> should be a class that stores the <code>distance</code> in a field?</li>
  </ol>