“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~


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


欢迎加入「福强私学」

跨越2190个日夜,始终坚持“实践 + 原创”打造的715125字专属知识库,囊括了(但不限于)从职场、技术、管理与商业等多个板块的内容。

  • 一个ChatGPT触达不到的地方
  • 一个带你超越AI/人工智能的地方
  • 一个与你一起成长的地方

https://afoo.me/kb.html


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

订阅「福报」