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 / OtpService.kt
Last active June 7, 2022 12:19
OTP Service
class OtpService {
// .. other methods for cache store etc.
fun generateAndSendOtp(number: String) {
val otp = generateOtp(number)
// save OTP to cache
// send the OTP to number
sendOtp(number, otp)
final List<String> s = new ArrayList<>();
final boolean isOfTypeList = s instanceof List; // this works!
final String s = "Test String";
final boolean isOfTypeObject = s instanceof Object; // this is true
final List<String> s = new ArrayList<>();
final boolean isOfTypeListOfString = s instanceof List<String>;
val list: List<Any> = arrayListOf<String>()
if (list is List<String>)
// this will throw a compile time error saying
// Cannot check for instance of erased type: List<String>
val colorsList = listOf("blue", "red", "black", "yellow")
val list1: List<String> = colorsList
.filter { it != "red" } // this will generate a list of size 3
val list2: List<String> = colorsList
.asSequence()
.filter { it != "red" } // so far, no processing is done and the length of the sequence is infinite!
.toList() // this is a terminal operation and will generate a list of size 3
val colorsList = listOf("blue", "red", "black", "yellow")
val list1: List<String> = colorsList
.filter { it != "red" } // new array list of size 3
.map { it.toLowerCase() } // new array list of size 4
.take(2) // new array list of size 2
val list2: List<String> = colorsList
.asSequence()
.filter { it != "red" } // will run and take "blue" and "black"
.map { it.toLowerCase()} // new array list of default size
.take(2) // will run 3 iterations and then exit with just one array instance
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) {
int element$iv = var5[var7];
int var10 = false;
// a static INSTACE is no longer being used.
@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
@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()