Practical Applications
Data Processing Pipelines
def clean_data(data):
return [x.strip() for x in data if x]
def normalize_data(data):
return [x.lower() for x in data]
def validate_data(data):
return [x for x in data if len(x) > 3]
def process_pipeline(raw_data):
return validate_data(normalize_data(clean_data(raw_data)))
data = [' Hello ', ' WORLD ', '', 'Python']
result = process_pipeline(data)
print(result) ## ['hello', 'world', 'python']
Mathematical Function Composition
def calculate_statistics(numbers):
def mean(data):
return sum(data) / len(data)
def variance(data):
avg = mean(data)
return sum((x - avg) ** 2 for x in data) / len(data)
def standard_deviation(data):
return variance(data) ** 0.5
return {
'mean': mean(numbers),
'variance': variance(numbers),
'std_dev': standard_deviation(numbers)
}
data = [1, 2, 3, 4, 5]
stats = calculate_statistics(data)
print(stats)
Functional Error Handling
def safe_divide(numerator):
def divide_by(denominator):
try:
return numerator / denominator
except ZeroDivisionError:
return None
return divide_by
divide_by_two = safe_divide(10)
print(divide_by_two(2)) ## 5.0
print(divide_by_two(0)) ## None
Composition Techniques Comparison
Technique |
Use Case |
Complexity |
Performance |
Direct Composition |
Simple transformations |
Low |
High |
Functional Pipelines |
Complex data processing |
Medium |
Medium |
Decorator Composition |
Cross-cutting concerns |
High |
Low |
Event Processing System
def log_event(event):
print(f"Logging: {event}")
return event
def validate_event(event):
if not event:
return None
return event
def process_events(events):
return list(filter(None, map(validate_event, map(log_event, events))))
events = ['click', '', 'submit', None, 'login']
processed = process_events(events)
print(processed)
flowchart TD
A[Raw Events] --> B[Log Events]
B --> C[Validate Events]
C --> D[Processed Events]
Configuration Management
def load_config(base_config):
def merge_config(additional_config):
return {**base_config, **additional_config}
return merge_config
default_config = {
'debug': False,
'log_level': 'INFO'
}
dev_config = load_config(default_config)({
'debug': True,
'log_level': 'DEBUG'
})
print(dev_config)
Machine Learning Feature Engineering
def normalize_feature(feature):
def transform(data):
min_val, max_val = min(data), max(data)
return [(x - min_val) / (max_val - min_val) for x in data]
return transform
def scale_features(features):
return [normalize_feature(feature)(feature) for feature in features]
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
scaled_data = scale_features(data)
print(scaled_data)
Key Takeaways
- Function composition is powerful for complex data transformations
- LabEx recommends modular, composable function design
- Practical applications span multiple domains
- Composition enhances code readability and maintainability