Filter,Map and Reduce Higher Order Functions in Swift

Let's code :

Take an Input array :

let inputArr = [1,2,3,4,5,6,7,8,9,10,11]
print("Input Array : \(inputArr)")


Perform Filter on array :

let filterArr = inputArr.filter({ return $0 % 2 == 0 })
print("Filtered Array (even elements) \(filterArr)")

Perform Map on array :

let mapArr = inputArr.map({ return $0 * 2 })
print("Map Array (*2) \(mapArr)")

Perform Reduce on array :

let reduce = inputArr.reduce(0, +)
print("Reduced Value (Sum of elements) \(reduce)")

Here is the output :


Comments

Popular posts from this blog

Best coding practice and structure for redux saga with react hooks | Separate watcher and worker saga | React clean code

How to use redux with React Hooks - Creating TodoList

Setting up Redux Devtools for React applications