Xóa bài viết
Bạn có chắc chắn muốn xóa bài viết này không ?
Xóa bình luận
Bạn có chắc chắn muốn xóa bình luận này không ?
Kotlin List reduce(), reduceIndexed(), reduceRight(), reduceRightIndexed() methods example
Kotlin List reduce(), reduceIndexed(), reduceRight(), reduceRightIndexed() methods example
In the tutorial, Grokonez will show you how to work with Kotlin List methods reduce()
, reduceIndexed()
, reduceRight()
, reduceRightIndexed()
.
I. Kotlin List reduce methods
1. reduce()
reduce()
accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
Method signature:
public inline fun Iterable.reduce(operation: (acc: S, T) -> S): S
Practice:
val intList = listOf(1, 2, 3, 4, 5, 6)
var intResult = intList.reduce { acc, it ->
println("acc = $acc, it = $it")
acc + it }
println(intResult)
/*
acc = 1, it = 2
acc = 3, it = 3
acc = 6, it = 4
acc = 10, it = 5
acc = 15, it = 6
21
*/
More at:
Kotlin List reduce(), reduceIndexed(), reduceRight(), reduceRightIndexed() methods example
Bình luận

{{ comment.user.name }}
Bỏ hay
Hay

Cùng một tác giả

1
0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...

1
0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...

1
0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
Bài viết liên quan

0
0
https://grokonez.com/kotlin/kotlinreadwritecsvfileexample How to read/write CSV file in Kotlin In this tutorial, we're gonna look at examples tha...

0
0
Kotlin Spring Boot + Angular 6 CRUD HttpClient + MySQL example | Spring Data JPA + REST APIs example
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...