<jupyter_start><jupyter_text>A simple Functor example<jupyter_code>def my_func(a, b):
    return a + b
my_functor = my_func
my_functor(10, 5)
my_func(10, 5)<jupyter_output><empty_output><jupyter_text>Using Functors for   1. Data hiding2. Method dispatching based on argument type<jupyter_code>class my_functor:
    def __call__(self, inp):
        
        if isinstance(inp[0], int):
            return self.add_2_int(inp)
        
        elif isinstance(inp[0], str):
            return self.add_2_str(inp)
    
    def add_2_int(self, inp):
        result = 0
        for x in inp:
            result += x
        return result
    
    def add_2_str(self, inp):
        result = " "
        for x in inp:
            result += x + " "
        return result
    
class exposed_Class(): 
    def __init__(self): 
        self.adder=my_functor() 
      
    def add_stuff(self, my_list): 
        return self.adder(my_list) 
  
my_obj = exposed_Class()
my_obj.add_stuff(["hello", "world"])
my_obj.add_stuff([1, 2, 3])<jupyter_output><empty_output><jupyter_text>The definition of Functors can be extended to Callable objects like classes with __call__ method implemented<jupyter_code>class my_operation_doer:
    # Class constructor
    def __init__(self, init_val, op):
        self.op = op
        self.val = init_val
    
    #Reduce by doing some operations
    def __call__(self, inp):
        result = self.op(self.val, inp)
        self.val = result
        return result
from operator import mul
my_obj = my_operation_doer(10, mul)
my_obj(100)
my_obj(2)
my_obj(4)
my_obj(10)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09733415995040298, 'fraction_numerical': 0.016738995660260384, 'mean_word_length': 2.7710280373831777, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '186980', 'license': 'Apache-2.0', 'n_tokens_mistral': 584, 'n_tokens_neox': 560, 'n_words': 134, 'path': '_src/Section 7/7.2 Functors.ipynb', 'repo_name': 'paullewallencom/functional-programming-978-1-7889-9029-5', 'size': '1613'}