- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between "fold" and "reduce" in Kotlin
Kotlin is a cross-platform and statically typed general-purpose programming language. Kotlin provides many optional methods to traverse through the collections. fold() and reduce() are two different methods, both are helpful for traversing a collection. In this article, we will see how and when to use these two methods.
Example – fold()
If we want to traverse through a collection serially, then we can use fold().
fold() takes one initial value and one operation to perform the operation on the initial value.
There are different kinds of fold(), for example, foldRight() folds from right to left. By default, fold() will traverse from left to right.
The following example demonstrates how to use fold() to traverse through a collection.
fun main(args: Array<String>) { val MyTotal = listOf(1, 2, 3, 4, 5).fold(0) { initialValue, result -> result + initialValue } print("The sum of list members: " +MyTotal) }
Output
fold() will start the operation from the 0th index and it will end its operation and store the final value in MyTotal.
The sum of list members: 15
Example - reduce()
reduce() is one of the default methods in Kotlin that helps to convert a given set of collection into a single set output result. reduce() is one of the built-in functions that can be applied on a given collection. It takes the first and second elements as the operation argument and this is the main difference between reduce() and fold().
fun main(args: Array<String>) { val x = listOf(1,2,3,4,5,6,7,8,9) val y = x.reduce { y, vars -> y + vars } println("The value of reduce() ---> "+y) }
Output
It will yield the following output
The value of reduce() ---> 45
Here, reduce() starts its operation from the first two elements, applies the function on them, and stores the result in "y". It continues the operation till the last element is reached and returns the final value.
- Related Articles
- Difference between "*" and "Any" in Kotlin generics
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference between List and Array types in Kotlin
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- Difference between Java and Kotlin in Android with Examples
- Difference between a "class" and an "object" in Kotlin
- What's the difference between "!!" and "?" in Kotlin?
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between "var" and "val" in Kotlin?
- How to Fold a Shirt?
- Reduce download conflicts between web browsers
- How to Fold a Fortune Teller?
- How to Fold a Paper Box?
- How to communicate between Activity and Service in Android using Kotlin?
- Difference between \'and\' and \'&\' in Python
