Higher Order Functions before diving in FRP

TuyenBQ
4 min readJun 27, 2016

--

Recently, i’m spending time to learn Reactive paradigms, following the team in Github for many documents and complex examples. As you may know, to dive in deep learning these things, probably preparing a little bit knowledges about the way Reactive change to the way you still doing before. One of the thing you must first feel familiar with HOF-Higher order function, which provided in Swift.

Using map, filter, reduce to operate on Swift collection types such as Array or Dictionary is something that can take getting used to. Unless you have experience with functional languages your instinct may be to reach for the more familiar for-in loop. With that in mind here is the guide to using map, filter, reduce (and flatMap).

Map

Use map to loop over a collection and apply the same operation to each element in the collection.

The map function returns an array containing the results of applying a mapping or transform function to each item. Imagine that we need to x2 with each item in array. In traditional way, we do that such as:

And with map:

Both has the same result:

This is a big improvement. We don’t need the for loop as map takes care of that for us. Also the array result is now a let or non-mutating value and we did not even need to declare its type as Swift can infer it. The shorthand closure syntax can make this hard to follow at first. The map function has a single argument which is a closure (a function) that it calls as it loops over the collection. This closure takes the element from the collection as an argument and returns a result. The map function returns these results in an array.

The type of the results is not limited to the type of the elements in the original array. Here is an example of mapping an array of integers to strings:

Filter

Use filter to loop over a collection and return an Array containing only those elements that match an include condition.

The filter method has a single argument that specifies the include condition. This is a closure that takes as an argument the element from the collection and must return a Bool indicating if the item should be included in the result.

An example that filters as array of integers returning only the even values. In traditional way, we do that such as:

And with filter:

Simple?

Reduce

Use reduce to combine all items in a collection to create a single new value.

The reduce method takes two values, an initial value and a combine closure.

This will also work with strings using the + operator to concatenate:

FlatMap

The simplest use is as the name suggests to flatten a collection of collections.

Even more usefully it knows about optionals and will remove them from a collection.

Chaining

Sometimes, you could combine map, filter, flatMap… in one operator.

Conclusion

  • map returns an Array containing results of applying a transform to each item.
  • filter returns an Array containing only those items that match an include condition.
  • reduce returns a single value calculated by calling a combine closure for each item with an initial value.

P/S: this short blog was using some contents from other sources in the internet. Thanks for the useful source, helps me a lot.

If this article is useful for you, please click ❤ and comment below. Let me know if there is anything you wanna share with me. Have powerful day, friends.

--

--