String Transformation Pipeline
This challenge focuses on building a flexible string manipulation pipeline in Go. You'll create a function that takes a string and a series of transformation functions, applying each function sequentially to the string and returning the final transformed string. This is a common pattern in data processing and text manipulation.
Problem Description
You are tasked with creating a function called TransformString that accepts a string and a slice of functions. Each function in the slice takes a string as input and returns a modified string. TransformString should apply each function in the slice to the input string in the order they appear, accumulating the transformations. The final result should be the string after all transformations have been applied.
Key Requirements:
- The
TransformStringfunction must accept a string and a slice of functions as input. Each function in the slice should have the signaturefunc(string) string. - The function must iterate through the slice of functions and apply each function to the current string.
- The function must return the final transformed string after all functions have been applied.
- The function should handle an empty slice of functions gracefully (returning the original string).
Expected Behavior:
The function should correctly apply each transformation function in the provided slice, ensuring that the output of one function becomes the input of the next. The order of application is crucial.
Edge Cases to Consider:
- Empty input string.
- Empty slice of transformation functions.
- Transformation functions that return empty strings.
- Transformation functions that modify the string in unexpected ways (e.g., adding extra characters).
Examples
Example 1:
Input: "hello", []func(string) string{func(s string) string { return s + " world";}, func(s string) string { return strings.ToUpper(s);}}
Output: "HELLO WORLD"
Explanation: The first function adds " world" to "hello", resulting in "hello world". The second function converts "hello world" to uppercase, resulting in "HELLO WORLD".
Example 2:
Input: "go", []func(string) string{func(s string) string { return s + "lang";}, func(s string) string { return strings.ToLower(s);}}
Output: "golang"
Explanation: The first function adds "lang" to "go", resulting in "golang". The second function converts "golang" to lowercase, resulting in "golang".
Example 3:
Input: "test", []func(string) string{}
Output: "test"
Explanation: An empty slice of functions should return the original string unchanged.
Constraints
- The input string can contain any valid UTF-8 characters.
- The slice of transformation functions can contain any number of functions (including zero).
- The transformation functions themselves can perform any valid string manipulation.
- The length of the input string will not exceed 1000 characters.
- The number of transformation functions in the slice will not exceed 100.
Notes
- Consider using a loop to iterate through the slice of functions.
- Remember that each transformation function modifies the string and returns the modified string.
- The
stringspackage in Go provides many useful string manipulation functions. You may find them helpful. - Think about how to handle the case where the input string is empty.
- Focus on clarity and readability in your code.
import "strings"
// TransformString applies a series of transformations to the input string.
// It takes a string and a slice of functions, where each function takes a string
// and returns a modified string. The functions are applied sequentially.
func TransformString(input string, transformations []func(string) string) string {
// Your code here
}