KT-20357: Add samples for associate* functions (#2798)
* KT-20357: Add samples for functions related to associate * KT-20357: Use same fib function for both samples * KT-20357: Use examples with duplicate keys and simplify examples with primitives * KT-20357: Use primitive samples for all arrays * KT-20357: Use String splitting example to better illustrate the use for associate over associateBy with a valueSelector
This commit is contained in:
@@ -583,6 +583,8 @@ public inline fun String.reversed(): String {
|
||||
* If any of two pairs would have the same key the last one gets added to the map.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original char sequence.
|
||||
*
|
||||
* @sample samples.text.Strings.associate
|
||||
*/
|
||||
public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>): Map<K, V> {
|
||||
val capacity = mapCapacity(length).coerceAtLeast(16)
|
||||
@@ -596,6 +598,8 @@ public inline fun <K, V> CharSequence.associate(transform: (Char) -> Pair<K, V>)
|
||||
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original char sequence.
|
||||
*
|
||||
* @sample samples.text.Strings.associateBy
|
||||
*/
|
||||
public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K, Char> {
|
||||
val capacity = mapCapacity(length).coerceAtLeast(16)
|
||||
@@ -608,6 +612,8 @@ public inline fun <K> CharSequence.associateBy(keySelector: (Char) -> K): Map<K,
|
||||
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
|
||||
*
|
||||
* The returned map preserves the entry iteration order of the original char sequence.
|
||||
*
|
||||
* @sample samples.text.Strings.associateByWithValueTransform
|
||||
*/
|
||||
public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valueTransform: (Char) -> V): Map<K, V> {
|
||||
val capacity = mapCapacity(length).coerceAtLeast(16)
|
||||
@@ -620,6 +626,8 @@ public inline fun <K, V> CharSequence.associateBy(keySelector: (Char) -> K, valu
|
||||
* and value is the character itself.
|
||||
*
|
||||
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
|
||||
*
|
||||
* @sample samples.text.Strings.associateByTo
|
||||
*/
|
||||
public inline fun <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K): M {
|
||||
for (element in this) {
|
||||
@@ -634,6 +642,8 @@ public inline fun <K, M : MutableMap<in K, in Char>> CharSequence.associateByTo(
|
||||
* and value is provided by the [valueTransform] function applied to characters of the given char sequence.
|
||||
*
|
||||
* If any two characters would have the same key returned by [keySelector] the last one gets added to the map.
|
||||
*
|
||||
* @sample samples.text.Strings.associateByToWithValueTransform
|
||||
*/
|
||||
public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(destination: M, keySelector: (Char) -> K, valueTransform: (Char) -> V): M {
|
||||
for (element in this) {
|
||||
@@ -647,6 +657,8 @@ public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateByTo(
|
||||
* provided by [transform] function applied to each character of the given char sequence.
|
||||
*
|
||||
* If any of two pairs would have the same key the last one gets added to the map.
|
||||
*
|
||||
* @sample samples.text.Strings.associateTo
|
||||
*/
|
||||
public inline fun <K, V, M : MutableMap<in K, in V>> CharSequence.associateTo(destination: M, transform: (Char) -> Pair<K, V>): M {
|
||||
for (element in this) {
|
||||
@@ -676,6 +688,8 @@ public inline fun <V> CharSequence.associateWith(valueSelector: (Char) -> V): Ma
|
||||
* where key is the character itself and value is provided by the [valueSelector] function applied to that key.
|
||||
*
|
||||
* If any two characters are equal, the last one overwrites the former value in the map.
|
||||
*
|
||||
* @sample samples.text.Strings.associateWithTo
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
public inline fun <V, M : MutableMap<in Char, in V>> CharSequence.associateWithTo(destination: M, valueSelector: (Char) -> V): M {
|
||||
|
||||
Reference in New Issue
Block a user