@@ -82,13 +82,13 @@ object KotlinFunctionLibrary {
8282 * A version of print which passes [this] into [message] to create the message to pass to print, and returns [this], facilitating fluent interfaces, functional programming, and assisting in debugging.
8383 * Also useful for debugging values without breaking a call change or interrupting the flow of code for logging values.
8484 * */
85- fun <T , R > T.print (message : (T ) -> R ) = this .apply { kotlin.io.print (message(this )) }
85+ inline fun <T , R > T.print (message : (T ) -> R ) = this .apply { kotlin.io.print (message(this )) }
8686
8787 /* *
8888 * A version of println which passes [this] into [message] to create the message to pass to println, and returns [this], facilitating fluent interfaces, functional programming, and assisting in debugging.
8989 * Also useful for debugging values without breaking a call change or interrupting the flow of code for logging values.
9090 * */
91- fun <T , R > T.println (message : (T ) -> R ) = this .apply { kotlin.io.println (message(this )) }
91+ inline fun <T , R > T.println (message : (T ) -> R ) = this .apply { kotlin.io.println (message(this )) }
9292
9393 /* *
9494 * Returns a list equivalent to appending the contents of [this] to [this] (e.g. listOf(1,2,3).doubled() == listOf(1,2,3,1,2,3) )
@@ -98,7 +98,7 @@ object KotlinFunctionLibrary {
9898 /* *
9999 * Return result of [block] or, if block threw an error (i.e. did not complete), run [onError] and return null. NOTE: Will also return null if [block] completed and returned null
100100 * */
101- fun <T , R > tryAndReturn (
101+ inline fun <T , R > tryAndReturn (
102102 onError : (e: Throwable , result: R ? ) -> T = { e, result -> kotlin.io.println("Error thrown; Throwable : $e, Result : $result") as T },
103103 block : () -> R
104104 ): R ? {
@@ -117,7 +117,7 @@ object KotlinFunctionLibrary {
117117 * NOTE: Will also return null if [block] completed and returned null
118118 * @param returnErrorBlock true if caller wants to return the result of the error block in case it is run. Assumes that [onError] will return a nullable version of whatever type [block] returns
119119 * */
120- fun <T , R > tryAndReturn (
120+ inline fun <T , R > tryAndReturn (
121121 returnErrorBlock : Boolean ,
122122 onError : (e: Throwable , result: R ? ) -> T = { e, result -> kotlin.io.println("Throwable : $e, Result : $result") as T },
123123 block : () -> R
@@ -137,7 +137,7 @@ object KotlinFunctionLibrary {
137137 * @param message the message to be displayed when printing the time to complete. Will be passed to System.out.printf, so can use "%d"
138138 * as the template/placeholder for the time to complete
139139 * */
140- fun measureNanoTimeAndPrintWithoutReturningResult (
140+ inline fun measureNanoTimeAndPrintWithoutReturningResult (
141141 inSeconds : Boolean = false,
142142 message : String = if (inSeconds) "Time to complete : %f seconds" else "Time to complete : %d nanoseconds",
143143 block : () -> Unit
@@ -153,7 +153,7 @@ object KotlinFunctionLibrary {
153153 * as the template/placeholder for the time to complete
154154 * @see #measureNanoTimeAndPrintAndReturnResult(Boolean, String, T, (T) -> R)}
155155 * */
156- fun <T > measureNanoTimeAndPrintAndReturnResult (
156+ inline fun <T > measureNanoTimeAndPrintAndReturnResult (
157157 inSeconds : Boolean = false,
158158 message : String = if (inSeconds) "Time to complete : %f seconds" else "Time to complete : %d nanoseconds",
159159 block : () -> T
@@ -171,7 +171,7 @@ object KotlinFunctionLibrary {
171171 * as the template/placeholder for the time to complete
172172 * @param param parameter to pass to [block]
173173 * */
174- fun <T , R > measureNanoTimeAndPrintAndReturnResult (
174+ inline fun <T , R > measureNanoTimeAndPrintAndReturnResult (
175175 inSeconds : Boolean = false,
176176 message : String = if (inSeconds) "Time to complete : %d seconds" else "Time to complete : %d nanoseconds",
177177 param : T ,
@@ -218,7 +218,7 @@ object KotlinFunctionLibrary {
218218 * @param predicate function that takes the input and returns the result of predicate evaluation on the input.
219219 * @return an input from the user which matches [predicate]
220220 * */
221- fun getValidatedInput (
221+ inline fun getValidatedInput (
222222 firstMessageToDisplay : String ,
223223 messageToDisplayOnError : String ,
224224 predicate : (String? ) -> Boolean
@@ -508,7 +508,7 @@ object KotlinFunctionLibrary {
508508 * Returns a list containing the results of applying the given [transform] function
509509 * to each element in the original collection.
510510 * */
511- fun <T , R > Iterable<T>.toListBy (transform : (T ) -> R ): MutableList <R > {
511+ inline fun <T , R > Iterable<T>.toListBy (transform : (T ) -> R ): MutableList <R > {
512512 val mutableList: MutableList <R > = mutableListOf ()
513513 forEach { mutableList.add(transform(it)) }
514514 return mutableList
@@ -517,7 +517,7 @@ object KotlinFunctionLibrary {
517517 /* *
518518 * @param nullable to distinguish between nullable and not
519519 * */
520- fun <T , R > Iterable<T?>.toListBy (nullable : Boolean , transform : (T ) -> R ): MutableList <R > {
520+ inline fun <T , R > Iterable<T?>.toListBy (nullable : Boolean , transform : (T ) -> R ): MutableList <R > {
521521 val mutableList: MutableList <R > = mutableListOf ()
522522 forEach { it?.let { it1 -> mutableList.add(transform(it1)) } }
523523 return mutableList
@@ -591,7 +591,7 @@ println("workingList2=$workingList2")*/
591591 /* *
592592 * Version of {@link MutableList#replaceAll(UnaryOperator)} which compiles to earlier versions of Kotlin and Android by avoiding the use of UnaryOperator
593593 * */
594- fun <E > MutableList<E>.replaceAll (operator : (E ? ) -> E ) {
594+ inline fun <E > MutableList<E>.replaceAll (operator : (E ? ) -> E ) {
595595 val li: MutableListIterator <E > = this .listIterator()
596596 while (li.hasNext()) {
597597 li.set(operator (li.next()))
@@ -652,7 +652,7 @@ println("workingList2=$workingList2")*/
652652 * Creates a file (or overwrites if already existing and [overwrite] is true), and takes care of releasing the[BufferedWriter]
653653 * which is passed in to [action].
654654 * */
655- fun createFileAndPerformWrite (filename : String , overwrite : Boolean , action : (BufferedWriter ) -> Unit ) {
655+ inline fun createFileAndPerformWrite (filename : String , overwrite : Boolean , action : (BufferedWriter ) -> Unit ) {
656656 File (filename).apply {
657657 createFile(overwrite)
658658 performWrite(action)
@@ -663,7 +663,7 @@ println("workingList2=$workingList2")*/
663663 * Creates a file (or overwrites if already existing and [overwrite] is true), and takes care of releasing the[BufferedReader]
664664 * which is passed in to [action].
665665 * */
666- fun createFileAndPerformRead (filename : String , overwrite : Boolean , action : (BufferedReader ) -> Unit ) {
666+ inline fun createFileAndPerformRead (filename : String , overwrite : Boolean , action : (BufferedReader ) -> Unit ) {
667667 File (filename).apply {
668668 createFile(overwrite)
669669 performRead(action)
@@ -674,7 +674,7 @@ println("workingList2=$workingList2")*/
674674 * Creates a file (or overwrites if already existing and [overwrite] is true), and takes care of releasing the[BufferedWriter]
675675 * which is passed in to [action].
676676 * */
677- fun File.createFileAndPerformWrite (overwrite : Boolean , action : (BufferedWriter ) -> Unit ) {
677+ inline fun File.createFileAndPerformWrite (overwrite : Boolean , action : (BufferedWriter ) -> Unit ) {
678678 createFile(overwrite)
679679 performWrite(action)
680680 }
@@ -683,7 +683,7 @@ println("workingList2=$workingList2")*/
683683 * Creates a file (or overwrites if already existing and [overwrite] is true), and takes care of releasing the[BufferedReader]
684684 * which is passed in to [action].
685685 * */
686- fun File.createFileAndPerformRead (overwrite : Boolean , action : (BufferedReader ) -> Unit ) {
686+ inline fun File.createFileAndPerformRead (overwrite : Boolean , action : (BufferedReader ) -> Unit ) {
687687 createFile(overwrite)
688688 performRead(action)
689689 }
@@ -712,7 +712,7 @@ println("workingList2=$workingList2")*/
712712 /* *
713713 * Performs an on a file by passing the file's [bufferedWriter] to [action] and closing it after the action is done
714714 * */
715- fun File.performWrite (action : (BufferedWriter ) -> Unit ) {
715+ inline fun File.performWrite (action : (BufferedWriter ) -> Unit ) {
716716 val writer = bufferedWriter()
717717 action(writer)
718718 writer.close()
@@ -745,7 +745,7 @@ println("workingList2=$workingList2")*/
745745 * Performs an [action] on a file by passing the file's [bufferedReader] to [action] and closing it after the action is done
746746 * @return the result of the lambda [action]
747747 * */
748- fun <R > File.performRead (action : (BufferedReader ) -> R ): R {
748+ inline fun <R > File.performRead (action : (BufferedReader ) -> R ): R {
749749 val r: R
750750 val reader = bufferedReader()
751751 r = action(reader)
@@ -758,7 +758,7 @@ println("workingList2=$workingList2")*/
758758 * Use case: it.appendLine("High-risk demographic: ".appendIf("Male(${first.ageRange.first}-${first.ageRange.last})") { demographics.size == 1 }.appendIf("Female(${first.ageRange.first}-${first.ageRange.last}") { demographics.size == 2 } )
759759
760760 * */
761- fun String.appendIf (append : String , predicate : (String ) -> Boolean ): String {
761+ inline fun String.appendIf (append : String , predicate : (String ) -> Boolean ): String {
762762 return if (predicate(this )) this + append else this
763763 }
764764
@@ -767,17 +767,17 @@ println("workingList2=$workingList2")*/
767767 * Use case: it.appendLine("High-risk demographic: ".appendIf("Male(${first.ageRange.first}-${first.ageRange.last})","N\\A") { demographics.size == 1 }.appendIf("Female(${first.ageRange.first}-${first.ageRange.last}") { demographics.size == 2 } )
768768
769769 * */
770- fun String.appendIf (append : String , `else `: String , predicate : (String ) -> Boolean ): String {
770+ inline fun String.appendIf (append : String , `else `: String , predicate : (String ) -> Boolean ): String {
771771 return if (predicate(this )) this + append else this + `else `
772772 }
773773
774- fun <T > myBuildList (capacity : Int , action : () -> T ): List <T > {
774+ inline fun <T > myBuildList (capacity : Int , action : () -> T ): List <T > {
775775 val list = mutableListOf<T >()
776776 for (i in 0 until capacity) list.add(action())
777777 return list.toList()
778778 }
779779
780- fun <T > myBuildMutableList (capacity : Int , action : () -> T ): MutableList <T > {
780+ inline fun <T > myBuildMutableList (capacity : Int , action : () -> T ): MutableList <T > {
781781 val list = mutableListOf<T >()
782782 for (i in 0 until capacity) list.add(action())
783783 return list
@@ -957,7 +957,7 @@ println("workingList2=$workingList2")*/
957957 /* *
958958 * 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)
959959 * */
960- fun <E > MutableList<E>.multiplyListBy (n : Int , transform : (E ) -> E ): MutableList <E > {
960+ inline fun <E > MutableList<E>.multiplyListBy (n : Int , transform : (E ) -> E ): MutableList <E > {
961961 return also {
962962 val original = it.toList()
963963 (1 until n).forEach { i -> it.addAll(original.map { it1 -> transform(it1) }) }
@@ -1018,7 +1018,7 @@ println("workingList2=$workingList2")*/
10181018 list.recursiveFind({ it.a == 'a' }) { it.b } != null == true
10191019 list.recursiveFind({ it.a == 'f' }) { it.b } != null == true
10201020 * */
1021- fun <E > List<E>.recursiveFind (predicate : (E ) -> Boolean , recursiveSelector : (E ) -> List <E >): E ? {
1021+ inline fun <E > List<E>.recursiveFind (predicate : (E ) -> Boolean , recursiveSelector : (E ) -> List <E >): E ? {
10221022 if (isEmpty()) return null
10231023 for (element in this ) {
10241024 return if (predicate(element)) element else /* check the next layer*/ recursiveSelector(element).recursiveFind(
@@ -1028,7 +1028,7 @@ println("workingList2=$workingList2")*/
10281028 }
10291029 return null
10301030 }
1031- fun <E > List<E>.recursiveForEach (action : (E ) -> Unit , recursiveSelector : (E ) -> List <E >): Unit {
1031+ inline fun <E > List<E>.recursiveForEach (action : (E ) -> Unit , recursiveSelector : (E ) -> List <E >): Unit {
10321032 if (isEmpty()) return
10331033 for (element in this ) {
10341034 action(element)
@@ -1050,9 +1050,9 @@ println(x in z) //prints true
10501050*/
10511051 operator fun <T > List<Iterable<T>>.contains (other : List <Iterable <T >>): Boolean = contains(other) { thisList, otherList -> thisList.any { it in otherList } }
10521052
1053- fun <T > List<T>.contains (other : List <T >, predicate : (thisElement: T , otherElement: T ) -> Boolean ): Boolean = indexOf(other, 0 , this .size, predicate) >= 0
1053+ inline fun <T > List<T>.contains (other : List <T >, predicate : (thisElement: T , otherElement: T ) -> Boolean ): Boolean = indexOf(other, 0 , this .size, predicate) >= 0
10541054
1055- private fun <T > List<T>.indexOf (other : List <T >, startIndex : Int , endIndex : Int , predicate : (thisElement: T , otherElement: T ) -> Boolean ): Int {
1055+ private inline fun <T > List<T>.indexOf (other : List <T >, startIndex : Int , endIndex : Int , predicate : (thisElement: T , otherElement: T ) -> Boolean ): Int {
10561056 fun <T > List<T>.regionMatches (thisOffset : Int , other : List <T >, otherOffset : Int , size : Int , predicate : (thisElement: T , otherElement: T ) -> Boolean ): Boolean {
10571057 if ((otherOffset < 0 ) || (thisOffset < 0 ) || (thisOffset > this .size - size) || (otherOffset > other.size - size)) {
10581058 return false
@@ -1076,12 +1076,12 @@ println(x in z) //prints true
10761076
10771077 private fun <T > Iterable<T>.collectionSizeOrDefault (default : Int ): Int = if (this is Collection <* >) this .size else default
10781078
1079- fun <E , R > Iterable<E>.recursiveMap (
1079+ inline fun <E , R > Iterable<E>.recursiveMap (
10801080 transform : (E ) -> R ,
10811081 recursiveSelector : (E ) -> Iterable <E >
10821082 ): List <R > = recursiveMapTo(ArrayList (collectionSizeOrDefault(10 )), transform, recursiveSelector)
10831083
1084- fun <T , R , C : MutableCollection <in R >> Iterable<T>.recursiveMapTo (
1084+ inline fun <T , R , C : MutableCollection <in R >> Iterable<T>.recursiveMapTo (
10851085 destination : C ,
10861086 transform : (T ) -> R ,
10871087 recursiveSelector : (T ) -> Iterable <T >
0 commit comments