Skip to content

Commit f04a522

Browse files
Add toFrequencyMap, and multiplyListBy
1 parent f6be33f commit f04a522

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/main/kotlin/KotlinFunctionLibrary.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import kotlin.system.measureNanoTime
1717

1818

1919
/**
20-
* Library of Kotlin utility functions. Version: 2.0.4
20+
* Library of Kotlin utility functions. Version: 2.1.0
2121
* */
2222
@Suppress("UNUSED")
2323
object KotlinFunctionLibrary{
@@ -901,8 +901,24 @@ println("workingList2=$workingList2")*/
901901
return null
902902
}
903903

904+
fun <T> Iterable<T>.toFrequencyMap(): Map<T,Int>{
905+
val frequencies: MutableMap<T, Int> = mutableMapOf()
906+
this.forEach{ frequencies[it] = frequencies.getOrDefault(it, 0) + 1 }
907+
return frequencies
908+
}
909+
910+
/**
911+
* Returns a list which contains a copy of [this] [n] times: e.g. listOf(1,2,3).multiplyBy(3){it+1} == listOf(1,2,3, 2,3,4, 2,3,4)
912+
* */
913+
fun <E> MutableList<E>.multiplyListBy(n: Int, transform: (E) -> E): MutableList<E> {
914+
return also {
915+
val original = it.toList()
916+
(1 until n).forEach { i -> it.addAll(original.map { it1 -> transform(it1) }) }
917+
}
918+
}
919+
904920
@JvmStatic
905921
fun main(args: Array<String>) {
906-
println("KotlinFunctionLibrary v2.0.4")
922+
println("KotlinFunctionLibrary v2.1.0")
907923
}
908924
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Main-Class: KotlinFunctionLibrary
3+

0 commit comments

Comments
 (0)