A pure function in Swift
A pure function is a function that consistently produces the same output for the same input and has no side effects. It solely relies on its input parameters to compute and return a result without modifying any external state or interacting with external resources.
Key characteristics of pure functions include:
1. Deterministic: Pure functions produce predictable results, as their output is solely determined by their input parameters. Given the same input, a pure function will always return the same output.
2. No Side Effects: Pure functions do not cause any observable changes outside their scope. They don’t modify global variables, mutate input parameters, perform I/O operations, or update external state. Instead, they create and return new values based on the input.
3. Independence: Pure functions are independent of the context in which they are called. They do not rely on external state or mutable data. The output of a pure function depends solely on its input.
Benefits of pure functions include:
1. Testability: Pure functions are easier to test because they produce deterministic results. Given the same input, you can confidently assert the expected output without worrying about any hidden side effects.
2. Readability and Maintainability: Pure functions are self-contained and have clear input-output relationships. This makes the code more readable, understandable, and easier to reason about. Additionally, pure functions promote code modularity and reusability.
3. Concurrency: Pure functions are inherently thread-safe and can be safely executed in parallel. Since they do not rely on shared mutable state, multiple threads or processes can call the same pure function simultaneously without causing conflicts.
By using pure functions whenever possible, you can write cleaner, more predictable code that is less prone to bugs and easier to maintain and reason about.
Here are some examples of pure functions in Swift:
Addition:
func add(_ a: Int, _ b: Int) -> Int {
return a + b
}
The add
function takes two integers as input and returns their sum. It doesn't modify any external state and always produces the same output for the same input.
String Concatenation:
func concatenate(_ str1: String, _ str2: String) -> String {
return str1 + str2
}
The concatenate
function takes two strings and returns their concatenation. It doesn't have any side effects and purely relies on its input parameters.
Array Sorting:
func sortArray(_ array: [Int]) -> [Int] {
return array.sorted()
}
The sortArray
function takes an array of integers and returns a new array with the elements sorted in ascending order. It doesn't modify the original array and consistently produces the same sorted output for the same input.
Filter Even Numbers:
func filterEvenNumbers(_ numbers: [Int]) -> [Int] {
return numbers.filter { $0 % 2 == 0 }
}
The filterEvenNumbers
function takes an array of integers and returns a new array containing only the even numbers. It doesn't modify the original array and produces the same result for the same input every time.
These examples demonstrate pure functions in Swift, which have no side effects and depend solely on their input parameters. Pure functions are predictable, easier to test, and promote code reusability.