The fold and scan Functions

Learn about fold and scan functions and see their visualizations.

We'll cover the following

fold

If we use collection processing functions, we might recognize fold. It’s used to combine all the values in this collection into one by applying an operation that combines two values into one for each element (starting from the initial value).

For example, if the initial value is 0 and the operation is addition, then the result is the sum of all the numbers. We first take the initial value 0. Then, we add the first element 1 to it. To the result, 1, we add the second number, 2. To the result, 3, we add the third number, 3. To the result, 6, we add the last number, 4. The result of this operation, 10, is what will be returned from the fold.

Get hands-on with 1200+ tech skills courses.