Skip to content

Instantly share code, notes, and snippets.

@ShahOdin
Created June 25, 2018 16:10
Show Gist options
  • Save ShahOdin/82650040b9793b74b115b73158175725 to your computer and use it in GitHub Desktop.
Save ShahOdin/82650040b9793b74b115b73158175725 to your computer and use it in GitHub Desktop.
trait A
trait B
trait C
trait D
class E(a:A,b:B,c:C,d:D)
////////////////////////////////////////////////////////
//(I)function declaration.
////////////////////////////////////////////////////////
trait fooDefinition {
def foo1(a: A, b: B, c: C, d: D): E
val foo2: (A, B, C, D) => E
def foo3(a: A)(b: B)(c: C)(d: D): E
val foo4: A => B => C => D => E
}
// Some have named parameters, some don't.
// There are pros and cons to both approaches.
////////////////////////////////////////////////////////
//(II) function Implementation
////////////////////////////////////////////////////////
trait directImplementation extends fooDefinition {
override def foo1(a: A, b: B, c: C, d: D): E = new E(a,b,c,d)
override val foo2: (A, B, C, D) => E = {
(a:A, b:B, c:C, d:D) => new E(a,b,c,d)
}
override def foo3(a: A)(b: B)(c: C)(d: D): E = new E(a,b,c,d)
override val foo4: A => B => C => D => E = {
a =>
b =>
c =>
d =>
new E(a, b, c, d)
}
}
//foo1 and foo3 are neatest.
////////////////////////////////////////////////////////
//(III)//the case where foo is just a proxy to another call.
////////////////////////////////////////////////////////
trait barFunctions {
def bar1(a: A, b: B, c: C, d: D): E
val bar2: (A, B, C, D) => E
def bar3(a: A)(b: B)(c: C)(d: D): E
val bar4: A => B => C => D => E
}
object proxiedImplementation extends barFunctions with directImplementation {
override def bar1(a: A, b: B, c: C, d: D): E = foo1(a,b,c,d)
override val bar2: (A, B, C, D) => E = foo2
override def bar3(a: A)(b: B)(c: C)(d: D): E = foo3(a)(b)(c)(d)
override val bar4: A => B => C => D => E = foo4
}
//bar2,bar4 are neatest
////////////////////////////////////////////////////////
//(IV)//the case where foo is some calculation + proxy to another call.
////////////////////////////////////////////////////////
trait calculation {
def calculate[A](a:A): A = identity(a)
}
object CalculationAndThenProxyImplementation extends barFunctions
with directImplementation
with calculation {
override def bar1(a: A, b: B, c: C, d: D): E = foo1(calculate(a),b,c,d)
override val bar2: (A, B, C, D) => E = {
(a:A, b:B, c:C, d:D) => foo2(calculate(a),b,c,d)
}
override def bar3(a: A)(b: B)(c: C)(d: D): E = foo3(calculate(a))(b)(c)(d)
override val bar4: A => B => C => D => E = {
a =>
foo4(calculate(a))
}
}
//conclusion:
//???
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment