[fp-enhancer] Apply functional programming and immutability improvements #12921
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.
Functional/Immutability Enhancements
This PR applies moderate, tasteful functional programming and immutability techniques to improve code clarity, safety, testability, and maintainability across the
pkg/directory.Summary of Changes
1. New Functional Helpers in sliceutil (pkg/sliceutil/sliceutil.go)
Added three generic helper functions to support functional programming patterns:
Filter[T any](slice []T, predicate func(T) bool) []T- Returns elements matching a predicate (pure function)Map[T, U any](slice []T, transform func(T) U) []U- Transforms each element (pure function)MapToSlice[K comparable, V any](m map[K]V) []K- Converts map keys to slice (pure function)These helpers enable immutable transformations throughout the codebase and are type-safe using Go generics.
Files affected:
pkg/sliceutil/sliceutil.go- Added 3 new generic functions2. Immutable Sort Operations (pkg/workflow/action_pins.go)
Changed
sortPinsByVersion()from in-place mutation to immutable operation:Benefits:
Files affected:
pkg/workflow/action_pins.go- Made sort immutablepkg/workflow/action_pins_test.go- Updated test to use returned value3. Functional Filter Operations (pkg/workflow/action_pins.go)
Replaced 4 imperative filter loops with functional
sliceutil.Filter:Locations converted:
GetActionPin()- Filter by repo (line ~139)GetActionPinWithData()- Filter by repo (line ~193)GetActionPinWithData()- Filter semver-compatible pins (line ~233)GetActionPinByRepo()- Filter by repo (line ~420)Files affected:
pkg/workflow/action_pins.go- 4 filter conversions4. Immutable Map-to-Slice Conversions (Multiple Files)
Replaced 11 map-to-slice extraction patterns with
sliceutil.MapToSlice:Locations converted:
pkg/workflow/sandbox.go- Domain map conversion (line ~170)pkg/workflow/mcp_setup_generator.go- Safe inputs tool names (line ~332)pkg/workflow/mcp_setup_generator.go- Safe inputs secrets (line ~410)pkg/workflow/mcp_setup_generator.go- MCP env vars (line ~450)pkg/workflow/mcp_setup_generator.go- Gateway env vars (line ~518)pkg/workflow/mcp_setup_generator.go- Gateway env vars Add workflow: githubnext/agentics/weekly-research #2 (line ~606)pkg/workflow/mcp_config_custom.go- Env keys (line ~319)pkg/workflow/mcp_config_custom.go- Header keys rejig docs #1 (line ~392)pkg/workflow/mcp_config_custom.go- Header keys Add workflow: githubnext/agentics/weekly-research #2 (line ~409)pkg/workflow/env.go- Header keys (line ~24)Files affected:
pkg/workflow/sandbox.go- 1 conversionpkg/workflow/mcp_setup_generator.go- 5 conversionspkg/workflow/mcp_config_custom.go- 3 conversionspkg/workflow/env.go- 1 conversionBenefits
Safety
Clarity
sliceutil.Filter(pins, predicate)is immediately understandableTestability
Maintainability
Principles Applied
Testing
go test ./pkg/workflow/... ./pkg/sliceutil/...(27.7s)TestSortPinsByVersionupdated for immutable sortgofmtcleanPerformance Considerations
The changes have minimal performance impact:
All performance-critical paths maintain their algorithmic complexity while gaining immutability benefits.
Examples
Example 1: Immutable Sort
Example 2: Functional Filter
Example 3: Map Keys Extraction
Review Focus
Please verify:
🤖 Generated by Functional/Immutability Enhancer - applying moderate functional programming techniques