Imperative (Non-functional): Tells you how to got the answer from A to B.
If you need to use the sum functionality again, you probably have reused the code somewhere else.
Declarative (Functional): We don’t care about whole process but the result
Benefits of Functional Programming
- Ensures no mutability or no change in state when used, no explicit state management
- Fever lines of code
We are going to understand what goes under the hood of filter function.
Imperative way to find even numbers in array:
Declarative way to find even number in array: (with built-in method)
We will try to create filter function:
But, it doesn’t make sense to create a distinct function just for Int only. Let us utilize the Swift generics.
Purest form of filter: