Skip to content

Instantly share code, notes, and snippets.

fun main (args: Array<String>) {
val x = arrayListOf("A.B", "B.C", "C.D", "D.E", "E.F")
println(x.map { stringStirrer(it) })
// above line prints [Axx, Bxx, Cxx, Dxx, Exx]
}
fun stringStirrer(param: String): String {
var changes = param.substringBefore(".")
changes += "xx"
return changes
// Add a function toINR() to kotlin's Double type
fun Double.toINR() = this * oneDollarInINR
// Yes it is a single line function
val netAmountDeposited = 230.0
println("Net amount deposited(in INR): ${netAmountDeposited.toINR()}")
// Did you notice the string interpolation above
import android.database.sqlite.SQLiteDatabase
// Add an extension function to perform SQLite operations
// This is a higher order function as it takes a function as parameter (operation)
// @param operation: a function that takes no parameter and returns nothing
fun SQLiteDatabase.performDBTransaction(operation: () -> Unit) {
beginTransaction()
operation() // this is the passed function
endTransaction()
}
class NEFT: Transaction ... // class NEFT extends Transaction
class IMPS: Transaction ... // class IMPS extends Transaction
class RTGS: Transaction ... // class RTGS extends Transaction
val transaction: Transaction = getCurrentTransaction()
// Now we don't know the type of transaction. It could be either of NEFT, IMPS or RTGS
// So we need to identify the type of trsaction and process it accordingly
when (transaction) {
is NEFT -> transaction.processNEFT()
val blank = " ".isBlank() // true
val first = "Kaushal.Dhruw".substringBefore('.') // "Kaushal"
val last = "Kaushal.Dhruw".substringAfter('.') // "Dhruw"
val addSpaces = "1".padStart(2) // " 1"
val addSpacesEnd = "1".padEnd(3, '0') // "100"
val dropStart = "Dhruw".drop(2) // "ruw"
val dropEnd = "Dhruw".dropLast(2) // "Dhr"
// Java POJO
public class Person {
private String name;
private int age;
private char gender;
public Person(String name, int age, char gender) {
this.name = name;
this.age = age;
this.gender = gender;
@drulabs
drulabs / FCM_message_single_device_curl_command.txt
Last active June 14, 2023 15:27
Curl command for sending FCM message
curl -i -H 'Content-type: application/json' -H 'Authorization: key=<your_server_key>' -XPOST https://fcm.googleapis.com/fcm/send -d '{
"registration_ids":["registration_ids", "of the", "target", "devices as array"],
"notification": {
"title":"Title of your notification",
"body":"content of your notification"
},
"data": {
"key1" : "value1",
"key2" : "value2",
"key3" : 23.56565,