“A Scala Code Trick for Converting Type-Method to Curried Function”

fujohnwang

2012-04-06


scala> case class A(x:Int){ def f(y:Int)=x*y}
defined class A

scala> val af = A(2).f _
af: (Int) => Int = <function1>

scala> af(3)
res0: Int = 6

scala> val af = (_: A).f _
af: (A) => (Int) => Int = <function1>

scala> af(A(2))(3)
res4: Int = 6

TWO Points to pay attention to

  1. (_:A) // any instance of Type A?!
  2. .f _ // convert method to function, this I knew before ^_^

I Think This trick should be usedsparingly~


>>>>>> 更多阅读 <<<<<<


「福强私学」来一个?

「福强私学」, 一部沉淀了个人成长、技术与架构、组织与管理以及商业上的方法与心法的百科全书。


开天窗,拉认知,订阅「福报」,即刻拥有自己的全模态人工智能。

订阅「福报」