What is the disadvantage of pure functions regarding I/O? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the disadvantage of pure functions regarding I/O?

I'm going through Python functional programming, and I don't understand the following remake, could you pls explain: The main disadvantage of using only pure functions is that they majorly complicate the otherwise simple task of I/O, since this appears to inherently require side effects. They can also be more difficult to write in some situations.

8th Feb 2017, 1:34 PM
Malik
4 Answers
+ 1
# these will not run in code playground. # I came across this as an alternative to a while loop # when studying functional style programming. # Is it an example of excessive complication caused by # attempts to stick to pure functional style ? Maybe it # is just lack of familiarity with the approach? # Either way you might find it as interesting as I did # and applicable to your question. They are almost, # but not quite equivalent in output def p_print(x): print(' 1 Hello ' + x) return x echo_FP = lambda: p_print(input(" 1 Enter Name "))=='quit' or echo_FP() echo_FP() # compared to. while 1: user = input(' 2 Enter name ') if user =='quit': break print('2 Hello ' + user)
8th Feb 2017, 10:11 PM
richard
+ 1
A pure function always gives the same results based on input. Now, think readline(), the user will type something, so you have no control what you get back. This function cannot be pure.
9th Feb 2017, 1:02 AM
1of3
1of3 - avatar
+ 1
There are different paradigms of programming. Functional and object-oriented programming are two of them. In my opinion, you should prefer object oriented programming rather than functional programming for some reasons. Firstly, it saves a lot of time. You don't have to repeat lines of code again and again. You can liken it to Plato's theory of forms. Our brain perceives the world and it's contents by classifying them. Objects (or classes in Python) are like forms of the computer universe. You can give them attributes and methods (parts of its identity and things that it can do). Secondly, it is easier to read and codes cover less space. Those are the reasons why you should prefer object oriented programming that I know; there can be found more.
10th Feb 2017, 12:24 PM
Göktuğ Aşcı
Göktuğ Aşcı - avatar
0
Objects and functional style work nicely together. Functional style is also usually shorter. If you don't believe me, try some Scala.
10th Feb 2017, 7:15 PM
1of3
1of3 - avatar