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


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

「为AI疯狂」星球上,扶墙老师正在和朋友们讨论有趣的AI话题,你要不要⼀起来呀?^-^
这里

  1. 不但有及时新鲜的AI资讯和深度探讨
  2. 还分享AI工具、产品方法和商业机会
  3. 更有体系化精品付费内容等着你,加入星球(https://t.zsxq.com/0dI3ZA0sL) 即可免费领取。(加入之后一定记得看置顶消息呀!)

知识星球二维码

存量的时代,省钱就是赚钱。
在增量的时代,省钱其实是亏钱。
避坑儿是省钱的一种形式,更是真正聪明人的选择!
弯路虽然也是路,但还是能少走就少走,背后都是高昂的试错成本。
订阅「福报」,少踩坑,少走弯路,多走一步,就是不一样的胜率!

订阅「福报Premium订阅」