Does functional programming avoid state?

前端 未结 4 1699
死守一世寂寞
死守一世寂寞 2021-02-01 20:44

According to wikipedia: functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state an

4条回答
  •  孤独总比滥情好
    2021-02-01 20:56

    Rather than avoids state, think of it like this:

    It avoids changing state. That word "mutable".

    Think in terms of a C# or Java object. Normally you would call a method on the object and you might expect it to modify its internal state as a result of that method call.

    With functional programming, you still have data, but it's just passed through each function, creating output that corresponds to the input and the operation.

    At least in theory. In reality, not everything you do actually works functionally so you frequently end up hiding state to make things work.

    Edit:
    Of course, hiding state also frequently leads to some spectacular bugs, which is why you should only use functional programming languages for purely functional situations. I've found that the best languages are the ones that are object oriented and functional, like Python or C#, giving you the best of both worlds and the freedom to move between each as necessary.

提交回复
热议问题