[fp-enhancer] Functional Programming & Immutability Enhancements #12973
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary of Changes
This PR applies tasteful functional programming and immutability improvements to enhance code clarity, safety, and maintainability. The changes focus on utilizing existing patterns and adding reusable utilities without over-engineering.
Changes by Category
1. New Functional Utilities in
pkg/sliceutil/(3 additions)Added three new generic functional helpers that address common patterns in the codebase:
FilterMap[T, U]: Combines filter and transform in a single pass (more efficient than separate operations)FilterMapKeys[K, V]: Filters map keys based on a predicate (common pattern for selecting map entries)Deduplicate[T]: Removes duplicates while preserving first-occurrence orderAll functions are:
2. Applied Functional Patterns (3 locations)
pkg/cli/workflows.go(2 improvements):filterWorkflowFiles()- Replaced imperative loop withsliceutil.Filter(files, isWorkflowFile)getAvailableWorkflowNames()- Replaced imperative loop withsliceutil.Map()pkg/workflow/compiler_jobs.go(2 improvements):getCustomJobsDependingOnPreActivation()- Used newFilterMapKeys()helpergetReferencedCustomJobs()- Used newFilterMapKeys()helper3. Immutability Improvements (2 locations)
pkg/cli/run_workflow_execution.go:lockFileNameandlockFilePathlockFileNamewas assigned the same value in both branches of an if-else:=, simplified control flowpkg/workflow/compiler_safe_outputs_config.go:make(map[string]any)tomap[string]any{}literal innewHandlerConfigBuilder()make()for empty map initializationBenefits Summary
Safety Improvements:
Clarity Improvements:
Maintainability Improvements:
Performance:
FilterMapis more efficient than separate filter + map operationsDesign Principles Applied
sliceutil.Filterandsliceutil.Mapfunctions:=unless mutation is genuinely neededTesting & Validation
go test ./pkg/sliceutil/...,./pkg/cli/...,./pkg/workflow/...)make golint)Files Changed (5 files, +65/-44 lines)
pkg/sliceutil/sliceutil.gopkg/cli/workflows.gopkg/workflow/compiler_jobs.gopkg/cli/run_workflow_execution.gopkg/workflow/compiler_safe_outputs_config.goReview Focus Areas
Please verify:
sliceutilfunctions maintain pure function contractsExamples
Before: Imperative filtering
After: Declarative filtering
Before: Mutable map key filtering
After: Functional pattern
References: