DEV Community

Discussion on: Functional programming in C++, Python, etc.

Collapse
 
epogrebnyak profile image
Evgeny Pogrebnyak

Check out docs.python.org/3/howto/functional... (as mentioned). For "pure function which prints text on the screen" I think you are stuck, unless you review what is your defintion of fucntional.

If you are to encapsulate your output into IO monad in python you can try github.com/dbrattli/OSlash, but this is just an excercise.

from oslash import put_line, get_line

main = put_line("What is your name?") | (lambda _:
    get_line() | (lambda name:
    put_line("What is your age?") | (lambda _:
    get_line() | (lambda age:
    put_line("Hello " + name + "!") | (lambda _:
    put_line("You are " + age + " years old"))))))

if __name__ == "__main__":
    main()

I thing more fruitful concepts in functional programming are curring, map/filter/reduce and function composition.

Regarding the nice code in gist.github.com/nahiyan/4ea3181b6b... - I think you are just mimicing one behaviour of bind as concat string, while binding can perform different actions on container content. Also there is a difference on class type and an OOP class, as discussed for example here stackoverflow.com/questions/270465....