How do you curry the 2nd (or 3rd, 4th, …) parameter in F# or any functional language?

前端 未结 5 1537
予麋鹿
予麋鹿 2021-02-19 06:58

I\'m just starting up with F# and see how you can use currying to pre-load the 1st parameter to a function. But how would one do it with the 2nd, 3rd, or whatever other paramet

5条回答
  •  故里飘歌
    2021-02-19 07:46

    In Python, you can use functools.partial, or a lambda. Python has named arguments. functools.partial can be used to specify the first positional arguments as well as any named argument.

    from functools import partial
    
    def foo(a, b, bar=None):
        ...
    
    f = partial(foo, bar='wzzz') # f(1, 2) ~ foo(1, 2, bar='wzzz')
    f2 = partial(foo, 3)         # f2(5) ~ foo(3, 5)
    
    f3 = lambda a: foo(a, 7)     # f3(9) ~ foo(9, 7)
    

提交回复
热议问题