Added a simple function for bytecode inspection.
This commit is contained in:
		
							parent
							
								
									8b8fda7d0b
								
							
						
					
					
						commit
						7f5789c274
					
				
							
								
								
									
										28
									
								
								core-kotlin/src/main/kotlin/com/baeldung/functions/Inline.kt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								core-kotlin/src/main/kotlin/com/baeldung/functions/Inline.kt
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,28 @@ | ||||
| package com.baeldung.functions | ||||
| 
 | ||||
| import kotlin.random.Random | ||||
| 
 | ||||
| /** | ||||
|  * An extension function on all collections to apply a function to all collection | ||||
|  * elements. | ||||
|  */ | ||||
| fun <T> Collection<T>.each(block: (T) -> Unit) { | ||||
|     for (e in this) block(e) | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * In order to see the the JVM bytecode: | ||||
|  *  1. Compile the Kotlin file using `kotlinc Inline.kt` | ||||
|  *  2. Take a peek at the bytecode using the `javap -c InlineKt` | ||||
|  */ | ||||
| fun main() { | ||||
|     val numbers = listOf(1, 2, 3, 4, 5) | ||||
|     val random = random() | ||||
| 
 | ||||
|     numbers.each { println(random * it) } // capturing the random variable | ||||
| } | ||||
| 
 | ||||
| /** | ||||
|  * Generates a random number. | ||||
|  */ | ||||
| private fun random(): Int = Random.nextInt() | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user