शुद्ध कार्य

From alpha
Jump to navigation Jump to search

कंप्यूटर प्रोग्रामिंग में, एक शुद्ध फ़ंक्शन एक सबरूटीन होता है जिसमें निम्नलिखित गुण होते हैं:[1][2]

  1. फ़ंक्शन वापसी विवरण रिलेशनल ऑपरेटर हैं # किसी फ़ंक्शन के समान तर्क के लिए स्थान समानता बनाम सामग्री समानता (स्थानीय स्थैतिक चर, गैर-स्थानीय चर, परिवर्तनीय मान प्रकार और संदर्भ प्रकार या इनपुट/आउटपुट के साथ कोई भिन्नता नहीं), और
  2. फ़ंक्शन का कोई साइड इफेक्ट नहीं है (कंप्यूटर विज्ञान) (स्थानीय स्थैतिक चर, गैर-स्थानीय चर, परिवर्तनशील संदर्भ तर्क या इनपुट/आउटपुट स्ट्रीम का कोई उत्परिवर्तन नहीं)।

कुछ लेखक, विशेष रूप से अनिवार्य भाषा समुदाय से, उन सभी कार्यों के लिए शुद्ध शब्द का उपयोग करते हैं जिनमें उपरोक्त संपत्ति 2 होती है[3][4] (#कंपाइलर अनुकूलन पर चर्चा की गई)।

उदाहरण

शुद्ध कार्य

C++ फ़ंक्शंस के निम्नलिखित उदाहरण शुद्ध हैं:

  • floor, returning the floor of a number;
  • max, returning the maximum of two values.
  • the function f, defined as
    void f() {
      static std::atomic<unsigned int> x = 0;
      ++x;
    }
    
    The value of x can be only observed inside other invocations of f(), and as f() does not communicate the value of x to its environment, it is indistinguishable from function void f() {} that does nothing. Note that x is std::atomic so that modifications from multiple threads executing f() concurrently do not result in a data race, which has undefined behavior in C and C++.

अशुद्ध कार्य

निम्नलिखित C++ फ़ंक्शंस अशुद्ध हैं क्योंकि उनमें उपरोक्त गुण 1 का अभाव है:

  • because of return value variation with a static variable
    int f() {
      static int x = 0;
      ++x;
      return x;
    }
    
  • because of return value variation with a non-local variable
    int f() {
      return x;
    }
    
    For the same reason, e.g. the C++ library function sin() is not pure, since its result depends on the IEEE rounding mode which can be changed at runtime.
  • because of return value variation with a mutable reference argument
    int f(int* x) {
      return *x;
    }
    
  • because of return value variation with an input stream
    int f() {
      int x = 0;
      std::cin >> x;
      return x;
    }
    

निम्नलिखित C++ फ़ंक्शंस अशुद्ध हैं क्योंकि उनमें उपरोक्त गुण 2 का अभाव है:

  • because of mutation of a local static variable
    void f() {
      static int x = 0;
      ++x;
    }
    
  • because of mutation of a non-local variable
    void f() {
      ++x;
    }
    
  • because of mutation of a mutable reference argument
    void f(int* x) {
      ++*x;
    }
    
  • because of mutation of an output stream
    void f() {
      std::cout << "Hello, world!" << std::endl;
    }
    

निम्नलिखित C++ फ़ंक्शंस अशुद्ध हैं क्योंकि उनमें उपरोक्त दोनों गुणों 1 और 2 का अभाव है:

  • because of return value variation with a local static variable and mutation of a local static variable
    int f() {
      static int x = 0;
      ++x;
      return x;
    }
    
  • because of return value variation with an input stream and mutation of an input stream
    int f() {
      int x = 0;
      std::cin >> x;
      return x;
    }
    

शुद्ध कार्यों में I/O

I/O स्वाभाविक रूप से अशुद्ध है: इनपुट ऑपरेशंस संदर्भात्मक पारदर्शिता को कमजोर करते हैं, और आउटपुट ऑपरेशंस दुष्प्रभाव पैदा करते हैं। फिर भी, एक ऐसा अर्थ है जिसमें एक फ़ंक्शन इनपुट या आउटपुट निष्पादित कर सकता है और फिर भी शुद्ध हो सकता है, यदि प्रासंगिक I/O उपकरणों पर संचालन के अनुक्रम को तर्क और परिणाम दोनों के रूप में स्पष्ट रूप से मॉडलिंग किया जाता है, और I/O संचालन लिया जाता है विफल होना जब इनपुट अनुक्रम प्रोग्राम के निष्पादन शुरू होने के बाद से वास्तव में किए गए संचालन का वर्णन नहीं करता है।[clarification needed]

दूसरा बिंदु यह सुनिश्चित करता है कि तर्क के रूप में प्रयोग करने योग्य एकमात्र अनुक्रम प्रत्येक I/O कार्रवाई के साथ बदलना चाहिए; पहला, अनुक्रम तर्कों में बदलाव के कारण अलग-अलग परिणाम देने के लिए I/O-प्रदर्शन फ़ंक्शन में अलग-अलग कॉल की अनुमति देता है।[5][6] I/O मोनैड एक प्रोग्रामिंग मुहावरा है जिसका उपयोग आमतौर पर शुद्ध कार्यात्मक भाषाओं में I/O करने के लिए किया जाता है।

कंपाइलर अनुकूलन

जिन फ़ंक्शंस में केवल उपरोक्त संपत्ति 2 है, वे कंपाइलर अनुकूलन तकनीकों जैसे सामान्य उप-अभिव्यक्ति उन्मूलन और अंकगणित ऑपरेटरों के समान लूप अनुकूलन की अनुमति देते हैं।[7] C++ उदाहरण है length विधि, एक स्ट्रिंग का आकार लौटाती है, जो उस मेमोरी सामग्री पर निर्भर करती है जहां स्ट्रिंग इंगित करती है, इसलिए उपरोक्त संपत्ति 1 का अभाव है। फिर भी, एक थ्रेड (कंप्यूटिंग) | एकल-थ्रेडेड वातावरण में, निम्नलिखित C++ कोड <सिंटैक्सहाइलाइट लैंग= सी++ > std::string s = हेलो, दुनिया! ; int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; पूर्णांक एल = 0;

के लिए (int i = 0; i < 10; ++i) {

 एल += एस.लंबाई() + ए[आई];

} </सिंटैक्सहाइलाइट> इस प्रकार अनुकूलित किया जा सकता है कि का मान s.length() लूप से पहले केवल एक बार गणना की जाती है।

कुछ प्रोग्रामिंग भाषाएँ किसी फ़ंक्शन के लिए शुद्ध गुण घोषित करने की अनुमति देती हैं:

  • फोरट्रान और डी में, द pure कीवर्ड का उपयोग किसी फ़ंक्शन को केवल साइड-इफेक्ट मुक्त घोषित करने के लिए किया जा सकता है (यानी केवल उपरोक्त संपत्ति 2 है)।[8] संकलक घोषणा के शीर्ष पर संपत्ति 1 निकालने में सक्षम हो सकता है।[9]
  • जीएनयू कंपाइलर संग्रह में, pure विशेषता संपत्ति 2 निर्दिष्ट करती है, जबकि const विशेषता दोनों गुणों के साथ वास्तव में शुद्ध फ़ंक्शन निर्दिष्ट करती है।[10]
  • संकलन-समय फ़ंक्शन निष्पादन की पेशकश करने वाली भाषाओं को फ़ंक्शन को शुद्ध करने की आवश्यकता हो सकती है, कभी-कभी कुछ अन्य बाधाओं के साथ। उदाहरणों में शामिल constexpr C++ की (दोनों गुण)।[11]


इकाई परीक्षण

चूँकि शुद्ध फ़ंक्शंस में किसी फ़ंक्शन के समान तर्क के लिए समान रिटर्न स्टेटमेंट होता है, इसलिए वे इकाई परीक्षण के लिए उपयुक्त होते हैं।

यह भी देखें

संदर्भ

  1. Bartosz Milewski (2013). "हास्केल की मूल बातें". School of Haskell. FP Complete. Archived from the original on 2016-10-27. Retrieved 2018-07-13. Here are the fundamental properties of a pure function: 1. A function returns exactly the same result every time it's called with the same set of arguments. In other words a function has no state, nor can it access any external state. Every time you call it, it behaves like a newborn baby with blank memory and no knowledge of the external world. 2. A function has no side effects. Calling a function once is the same as calling it twice and discarding the result of the first call.
  2. Brian Lonsdorf (2015). "कार्यात्मक प्रोग्रामिंग के लिए प्रोफेसर फ्रिसबी की अधिकतर पर्याप्त मार्गदर्शिका". GitHub. Retrieved 2020-03-20. A pure function is a function that, given the same input, will always return the same output and does not have any observable side effect.
  3. "सामान्य फ़ंक्शन विशेषताएँ - जीएनयू कंपाइलर संग्रह (जीसीसी) का उपयोग करना". gcc.gnu.org, the GNU Compiler Collection. Free Software Foundation, Inc. Retrieved 2018-06-28.
  4. Fortran 95 language features#Pure Procedures
  5. Peyton Jones, Simon L. (2003). Haskell 98 Language and Libraries: The Revised Report (PDF). Cambridge, United Kingdom: Cambridge University Press. p. 95. ISBN 0-521 826144. Retrieved 17 July 2014.
  6. Hanus, Michael. "Curry: An Integrated Functional Logic Language" (PDF). www-ps.informatik.uni-kiel.de. Institut für Informatik, Christian-Albrechts-Universität zu Kiel. p. 33. Archived from the original (PDF) on 25 July 2014. Retrieved 17 July 2014.
  7. "सामान्य फ़ंक्शन विशेषताएँ - जीएनयू कंपाइलर संग्रह (जीसीसी) का उपयोग करना". gcc.gnu.org, the GNU Compiler Collection. Free Software Foundation, Inc. Retrieved 2018-06-28.
  8. Pure attribute in Fortran
  9. Pure attribute in D language
  10. "सामान्य कार्य विशेषताएँ". Using the GNU Compiler Collection (GCC. Retrieved 22 July 2021.
  11. constexpr attribute in C++