[PYTHON] An introduction to functional programming to improve debugging efficiency in 1 minute

highlight

  1. What happens when you learn functional programming
  2. How to practice functional programming
  3. What are the similarities between functional programming and object-oriented programming?

What Happens When You Learn Functional Programming

The following worries are reduced and debugging efficiency is improved.

"This flag should be true, but it's false!" "I don't know where the cause of the bug is." "If you fix it, you may have a problem somewhere again."

How to practice functional programming

Design the following two functions exactly separately, and increase the pure area to reduce the dirty area.

1. Pure function

A function that always returns the same result for the same argument and does not affect the scope outside the function.

2. Dirty function

A function that returns different results with the same arguments or affects the scope outside the function

Tell me more specifically

The following is an example. Anything is fine as long as you can easily and clearly distinguish between pure and dirty, and make the pure area relatively thick, such as separating files by package or by naming.


class Pure:
	"""
A class of pure functions
Make this fat
	"""
	def plus(self,a,b):
		return a + b
	def minus(self,a,b):
		return a - b


class Dirty:
	"""
A class of dirty functions
	=A class with a function that changes the state
Reduce this
	"""
	def __init__(self):
		self.my_number = 0
	def plus(self,a):
		self.my_number = self.my_number + a
		return self.my_number
	def minus(self,b):
		self.my_number = self.my_number - 1
		return self.my_number


Why can you reduce debugging time by thickening the pure area?

There are four reasons.

  1. Pure functions do not make unintended variable changes
  2. You don't have to worry about the impact on others when modifying a pure function (just apply the function correctly)
  3. If it is a pure function, even if one function becomes bloated, if it is kept pure, it can be easily divided and reused.
  4. It is a dirty area where bugs are likely to occur, so if you keep this small, it will be easier for bugs to hit.

What is a functional "language"?

A language that is easy to do functional programming. It is suitable for writing with abundant languages that handle functions or with pure functions. Functional programming itself can be VB or whatever.

What are the similarities and differences between functional programming and object-oriented programming?

The most specific thing in common is to minimize the impact of one area on another. Object orientation seeks to achieve this through encapsulation, polymorphism, and inheritance. Functional programming is trying to fatten a pure function like the one above to minimize the problem of state changes.

The following may be biased in a very personal view Both are not contradictory concepts, but coexisting concepts, but object-orientation tends to be built on the premise of changing the state of many objects, so the above purpose is "a certain area". Minimizing the impact of has on other areas is difficult to achieve. I also feel that object-oriented programming is less likely to produce results than learning and design costs compared to functional programming. So, while keeping in mind the trade-off with time and classifying to some extent, I mostly pay attention to Pure or Dirty.

Summary

The heart of functional programming is to fatten the pure realm. Functional programming is a delicious and versatile technology that is highly effective for its low learning costs.

Recommended Posts

An introduction to functional programming to improve debugging efficiency in 1 minute
An introduction to Python Programming
An introduction to object-oriented programming for beginners by beginners
An introduction to private TensorFlow
An introduction to machine learning
AOJ Introduction to Programming Topic # 7, Topic # 8
I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
AOJ Introduction to Programming Topic # 5, Topic # 6
I want to improve efficiency with Python even in an experimental system (1) Install Anaconda with Chocolatey
An introduction to the modern socket API to learn in C
An introduction to Bayesian optimization
Programming to fight in the world ~ 5-1
Programming to fight in the world ~ 5-5,5-6
Functional programming in Python Project Euler 1
Programming to fight in the world 5-3
An introduction to Mercurial for non-engineers
[Introduction to Python3 Day 1] Programming and Python
Functional programming in Python Project Euler 3
Programming to fight in the world-Chapter 4
Functional programming in Python Project Euler 2
An introduction to Python for non-engineers
An alternative to `pause` in Python
Programming to fight in the world ~ 5-2
[Python Tutorial] An Easy Introduction to Python
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
An introduction to OpenCV for machine learning
Recurrent Neural Networks: An Introduction to RNN
An introduction to Python for machine learning
Try a functional programming pipe in Python
Introduction to docker Create ubuntu environment in ubuntu
Introduction to Vectors: Linear Algebra in Python <1>
Introduction to Effectiveness Verification Chapter 1 in Python
An Introduction to Object-Oriented-Give an object a child.
An introduction to Python for C programmers
Introduction to Deep Learning (1) --Chainer is explained in an easy-to-understand manner for beginners-