Skip to content

Instantly share code, notes, and snippets.

View shahsurajk's full-sized avatar
🎯
Focusing

Suraj Shah shahsurajk

🎯
Focusing
View GitHub Profile
@shahsurajk
shahsurajk / Power-of-inline.md
Last active November 30, 2019 13:52
Presentation on power of inline, given as on 23/03/19 at a BlrKotlin Meetup
        ##### ##                                                     
     ######  /###                                                    
    /#   /  /  ###                                                   
   /    /  /    ###        ##                                        
       /  /      ##        ##                                        
      ## ##      ##  /###   ##    ###    ####      /##  ###  /###    
      ## ##      ## / ###  / ##    ###     ###  / / ###  ###/ #### / 
    /### ##      / /   ###/  ##     ###     ###/ /   ###  ##   ###/  
@shahsurajk
shahsurajk / pushing_aars_to_maven.md
Created July 15, 2019 12:14
Pushing AARs to maven-central

Publishing AAR to maven-central:

We will be using the Nexus Software Repository for pushing our aars to maven-central, there are different methods to do this, another simple way is to upload to bintray and then push to maven-central from there, which one to use can completely depend upon the developer.

The Nexus Software Repository:

Nexus is a tool used by Sonatype to manage repositories. To use nexus, create an account and remember the user and password, this will be required in the automation script to deploy the artifacts.

The automation script:

Back in 2013 (2013, seriously!) Chris Banes wrote a blog post about an automation script he had written for pushing aars to maven, he had written this script for ActionBar-PullToRefresh (Again, this is 2013 we're talking about), this script can now be found on [github](https://github.

@shahsurajk
shahsurajk / NonInlinedLambda.kt
Last active November 27, 2019 19:02
Non inlined lambda
fun testLambdas(index: Int, myLambda: (index: Int) -> Unit){
myLambda.invoke(index+1)
}
fun myBigLoop(){
(0 .. 50).forEach {
// our lambda being consumed here.
testLambdas(it) { lambdaValue->
println(lambdaValue)
}
@shahsurajk
shahsurajk / NonInlinedLambdaWithClosure.kt
Last active November 27, 2019 19:05
Non-inlined lambda with closure
fun testLambdas(index: Int, myLambda: (index: Int) -> Unit){
myLambda.invoke(index+1)
}
fun IntArray.myBigLoopWithClosure(salt: Double){
forEach {
testLambdas(it) { myLambda ->
// the salt value over here is captured in a closure.
// thus, this will create a new instance of the lambda for every iteration
println(myLambda * salt)
@shahsurajk
shahsurajk / DecompiledInlinedClosureLambda.java
Last active July 16, 2019 13:31
Decompiled Inlined Closure Lambda
public static final void myBigLoopWithClosure(@NotNull int[] $this$myBigLoopWithClosure, double salt) {
Intrinsics.checkParameterIsNotNull($this$myBigLoopWithClosure, "$this$myBigLoopWithClosure");
int $i$f$forEach = false;
int[] var5 = $this$myBigLoopWithClosure;
int var6 = $this$myBigLoopWithClosure.length;
for(int var7 = 0; var7 < var6; ++var7) {
// magical, isn't it! no new instance, nothing.
// the logical block is inlined here!
int element$iv = var5[var7];
@shahsurajk
shahsurajk / ReifiedInlinedWithParameter.kt
Created July 17, 2019 14:15
Reified inline function with parameter
inline fun <reified T> myGenericFunction(value: T): T {
// we can't do this without marking the parameter 'T' as reified and the function as inline.
val whoAmI = when (T::class.java.simpleName) {
Int::class.java.simpleName -> {
"I am an Int"
}
String::class.java.simpleName -> {
"I am Unknown"
}
@shahsurajk
shahsurajk / DecompiledReifiedInlinedWithGenericParameter.java
Last active July 17, 2019 14:27
Decomplied reified inlined with generic parameter
public static final void main() {
Object value$iv = "String";
int value$iv = false;
String var3 = String.class.getSimpleName(); // the compiler adds it local here, thus making it persist post-compile. This value is then checked in the line below.
String whoAmI$iv = Intrinsics.areEqual(var3, Integer.TYPE.getSimpleName()) ? "I am an Int" : (Intrinsics.areEqual(var3, String.class.getSimpleName()) ? "I am Unknown" : "I am Groot!");
boolean $i$f$myGenericFunction = false;
System.out.println(whoAmI$iv);
value$iv = true;
$i$f$myGenericFunction = false;
whoAmI$iv = Integer.class.getSimpleName(); // here as well.
@shahsurajk
shahsurajk / CrossInlinedAndInlineLambdas.kt
Created July 17, 2019 14:41
Cross Inlined And Inlined Lambdas
inline fun mixedLambdaHolder(crossinline nonLocalReturnBlockedLambda: () -> Unit, normalLambda: () -> Unit){
nonLocalReturnBlockedLambda.invoke()
normalLambda.invoke()
}
fun main(){
mixedLambdaHolder(nonLocalReturnBlockedLambda = {
return@mixedLambdaHolder // only this can be use, this only returns the lambda and not the main function
return // this throws a compiling error, saying 'return' is not allowed here.
}, normalLambda = {
@shahsurajk
shahsurajk / NoInlineDemo.kt
Created July 17, 2019 15:01
No inline demo
inline fun parameterPassedToOtherInlineFunction(lambda1: () -> Unit, noinline lambda2: () -> Boolean){
// normal invoke, this is a normal lambda.
lambda1.invoke()
// passing the lambda to another function which doesn't inline this lambda
// will throw an error if lambda2 is not marked as noinline
someNonInlinedLambdaConsumingFunction(lambda2)
}
fun someNonInlinedLambdaConsumingFunction(lambda: () -> Boolean) : Boolean{
return lambda.invoke()
@shahsurajk
shahsurajk / DecompiledLambaWithoutInline.java
Created November 27, 2019 18:35
Decompiled lambda without inline
public static final void myBigLoop() {
byte var0 = 0;
Iterable $receiver$iv = (Iterable)(new IntRange(var0, 50));
Iterator var1 = $receiver$iv.iterator();
while(var1.hasNext()) {
int element$iv = ((IntIterator)var1).nextInt();
int var4 = false;
// our function being called with a singleton instance of our Function1 implementated class
testLambdas(element$iv, (Function1)PresentationKt$myBigLoop$1$1.INSTANCE); // note the use of a singleton here