trait PartialApply[F, ParameterName <: String with Singleton] extends Serializable
A dependent type class that bind the specific parameter of ParameterName
to F
Imports
import com.thoughtworks.feature.PartialApply.ops._
This will enable the partialApply method for any functions
Author:
杨博 (Yang Bo) <[email protected]>
- F
The function type to be partially apply
- Source
- PartialApply.scala
Given a function with three parameters.
val f = { (v1: Int, v2: Int, v3: Int) => (v1 + v2) * v3 }
When partially applying the second parameter.
val partiallyApplied = f.partialApply(v2 = 2)
And applying the rest parameters.
val result = partiallyApplied(v3 = 3, v1 = 1)
Then the result should be the same as applying at once.
result should be(f(1, 2, 3))
, A function with refined parameters can partially apply.
val f: ((Int, Double, String) => String) { def apply(i: Int, d: Double, s: String): String } = { (i, d, s) => (i * d) + s } f.partialApply(s = "seconds").apply(i = 60, d = 1.125) should be(f(s = "seconds", i = 60, d = 1.125))
, Partial applying can be chained.
val f = { (v1: Int, v2: Int, v3: Int) => (v1 + v2) * v3 } f.partialApply(v2 = 2).partialApply(v3 = 3).partialApply(v1 = 1).apply() should be(f(1, 2, 3))
, Function objects can partially apply.
object f extends ((Int, Double, String) => String) { def apply(i: Int, d: Double, s: String): String = { (i * d) + s } } f.partialApply(s = "seconds").apply(i = 60, d = 1.125) should be(f(s = "seconds", i = 60, d = 1.125))
, Case class companion can partially apply.
case class MyCaseClass(i: Int, d: Double, s: String) MyCaseClass.partialApply(s = "seconds").apply(i = 60, d = 1.125) should be(MyCaseClass(s = "seconds", i = 60, d = 1.125))
, Call-by-name functions can partially apply.
def foo(v1: => String, v2: Int) = v1 + v2 val callByNameFunction = foo _ PartialApply.materialize[callByNameFunction.type, "v1"].apply(callByNameFunction, "1").apply(v2 = 2) should be(foo(v1 = "1", v2 = 2))
Examples:
Linear Supertypes
Type Hierarchy
Ordering
- Alphabetic
- By Inheritance
Inherited
- PartialApply
- Serializable
- Serializable
- AnyRef
- Any
Implicitly
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
Visibility
- Public
- All
Concrete Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
def
+(other: String): String
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to any2stringadd[PartialApply[F, ParameterName]] performed by method any2stringadd in scala.Predef.
- Definition Classes
- any2stringadd
-
def
->[B](y: B): (PartialApply[F, ParameterName], B)
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to ArrowAssoc[PartialApply[F, ParameterName]] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @inline()
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
ensuring(cond: (PartialApply[F, ParameterName]) ⇒ Boolean, msg: ⇒ Any): PartialApply[F, ParameterName]
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to Ensuring[PartialApply[F, ParameterName]] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: (PartialApply[F, ParameterName]) ⇒ Boolean): PartialApply[F, ParameterName]
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to Ensuring[PartialApply[F, ParameterName]] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: Boolean, msg: ⇒ Any): PartialApply[F, ParameterName]
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to Ensuring[PartialApply[F, ParameterName]] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
def
ensuring(cond: Boolean): PartialApply[F, ParameterName]
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to Ensuring[PartialApply[F, ParameterName]] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to StringFormat[PartialApply[F, ParameterName]] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @inline()
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
- Annotations
- @native()
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
- Annotations
- @native()
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @native() @throws( ... )
-
def
→[B](y: B): (PartialApply[F, ParameterName], B)
- Implicit
- This member is added by an implicit conversion from PartialApply[F, ParameterName] to ArrowAssoc[PartialApply[F, ParameterName]] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc