Non-numerical use cases for functional programming?

前端 未结 18 2262
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 21:33

I just finished reading a book on scala. What strikes me is that every single example in the whole book was numerical in some form or another.

Like a lot of programm

18条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 22:07

    The more I use a functional style, the better I like it. Consider this Python fragment from another question:

    >>> testlist
    [1, 2, 3, 5, 3, 1, 2, 1, 6]
    >>> [i for i,x in enumerate(testlist) if x == 1]
    [0, 5, 7]
    

    This is admittedly a more or less mathematical statement, but there are lots of generators in Python. Once you get used to it, using a list comprehension in place of a loop is both easy to understand, and less likely to have a bug (you don't get "off by one" bugs.)

提交回复
热议问题