Compose partial functions

前端 未结 2 767
清酒与你
清酒与你 2021-02-05 12:47

I have two PartialFunctions f and g. They have no side effects and are quick to execute. What\'s the best way to compose them into another partial func

2条回答
  •  有刺的猬
    2021-02-05 13:47

    val h = f.mapValues(g)
    

    But that only works for maps. I don't think there is a short way of doing that for any kind of partial function, you'll just have to create a new PartialFunction object manually.

    edit: I see my above code is not what you wanted. But maybe this is better

    val h = f.collect { case (k, v) if(g.contains(v)) => (k, g(v)) }
    

提交回复
热议问题