कार्यक्षमता प्रोग्रामिंग: व्यवहार में

Programming Paradigm Concepts

Eleanor Thomas

Senior Data Analytics Engineer

कार्यक्षमता प्रोग्रामिंग: व्यवहार में

  • शुद्ध फंक्शन के तीन उदाहरण
  • शुद्ध और सामान्य Python फंक्शन में मुख्य फर्क: कोई side effects नहीं
  • शुद्ध फंक्शन, दूसरे शुद्ध फंक्शन को कॉल कर सकते हैं और शुद्ध ही रहते हैं

ड्रॉइंग बोर्ड

Programming Paradigm Concepts

उदाहरण 1 - एक शुद्ध फंक्शन लिखना

def square_list(input_list):
    new_list = []

for item in input_list:
new_item = item ** 2
new_list.append(new_item)
return new_list
  • पहले एक नई, खाली list बनाएँ
  • input list के हर item से गुजरें
    • उसे square करें
    • नई list में जोड़ें
  • नई list लौटाएँ
Programming Paradigm Concepts

उदाहरण 2 - "अशुद्ध" फंक्शन को ठीक करना

sample_mean = 10
scale_factor = 2

def scale_list(input_list):
    new_list = []
    for item in input_list:
        new_item = (item - sample_mean) / scale_factor
        new_list.append(new_item)
    return new_list
  • फंक्शन बॉडी के बाहर के वैरिएबल पर निर्भर
  • यह शुद्ध फंक्शन नहीं है
Programming Paradigm Concepts

उदाहरण 2 - "अशुद्ध" फंक्शन सुधारा गया

def scale_list(input_list, sample_mean, scale_factor):
    new_list = []
    for item in input_list:
        new_item = (item - sample_mean) / scale_factor
        new_list.append(new_item)
    return new_list
  • sample_mean और scale_factor अब फंक्शन के input parameters हैं
  • फंक्शन अब "शुद्ध" है
Programming Paradigm Concepts

उदाहरण 3 - शुद्ध फंक्शनों का संयोजन

def scale_value(value, sample_mean, scale_factor):
    scaled_value = (value - sample_mean) / scale_factor
    return scaled_value

def scale_list(input_list, sample_mean, scale_factor):
    new_list = []
    for item in input_list:
        new_item = scale_value(item, sample_mean, scale_factor)
        new_list.append(new_item)
    return new_list
Programming Paradigm Concepts

अभ्यास करते हैं!

Programming Paradigm Concepts

Preparing Video For Download...