C# lambda - curry usecases

后端 未结 6 1738
小蘑菇
小蘑菇 2021-01-31 12:04

I read This article and i found it interesting.

To sum it up for those who don\'t want to read the entire post. The author implements a higher order function named Curry

6条回答
  •  梦谈多话
    2021-01-31 12:47

    Its easier to first consider fn(x,y,z). This could by curried using fn(x,y) giving you a function that only takes one parameter, the z. Whatever needs to be done with x and y alone can be done and stored by a closure that the returned function holds on to.

    Now you call the returned function several times with various values for z without having to recompute the part the required x and y.

    Edit:

    There are effectively two reasons to curry.


    Parameter reduction

    As Cameron says to convert a function that takes say 2 parameters into a function that only takes 1. The result of calling this curried function with a parameter is the same as calling the original with the 2 parameters.

    With Lambdas present in C# this has limited value since these can provide this effect anyway. Although it you are use C# 2 then the Curry function in your question has much greater value.

    Staging computation

    The other reason to curry is as I stated earlier. To allow complex/expensive operations to be staged and re-used several times when the final parameter(s) are supplied to the curried function.

    This type of currying isn't truely possible in C#, it really takes a functional language that can natively curry any of its functions to acheive.


    Conclusion

    Parameter reduction via the Curry you mention is useful in C# 2 but is considerably de-valued in C# 3 due to Lambdas.

提交回复
热议问题