Functional Style in Kotlin
In general a function has 4 parts
- name
- return type
- parameter list
- body
A lambda function only contains the last two.
general syntax for a lambda function in Kotlin
{ parameter -> body }
General Syntax
The range.none
// signature - one argument - a lambda function
none(predicate: (Int) -> Boolean): Boolean
// call to check if any equal numbers is in a range
2..10.none({ i: Int -> i % 2 == 0})
// the parameter type can be dropped
2..10.none({ i -> i % 2 == 0})
// the parenthesis can be dropped as well when the function only takes the lambda function as argument
2..10.none { i -> i % 2 == 0}
// for lambdas that only has one parameter it is explicitly named 'it'
2..10.none { it % 2 == 0}
Function References
When passing through parameters function references can be used
({ x -> someMethod(x) })
// can be written with a function reference
(::someMethod)
// if the pass-through is to another lambda, :: is not needed
1..5.forEach { action(it) }
// can be simplified
1..5.forEach { action }
Internal Iterators
Standard functional operations defined on Iterable
filter
- picks specific items from the collectionmap
- map all items to another type of itemsreduce
- takes two argumentsaccumulated
anditem
- the general cumulative function - special functions also existssum
average
joinToString
first
- picks the first element that matcheslast
- picks the last element that matchesflatten
- flatten a list of lists, into one combined listflatMap
-map
andflatten
sortedBy
andsortedByDescending
- return the sorted collectiongroupBy
- group into multiple collections
Sequences
The Sequence
is a lazy evaluated iterator. This should be preferred when doing iteration on large collections.
A normal array and Iterable
can easily be wrapped in a Sequence
by using asSequence