Real World Example

Android: Intents, Bundles and Builders

Android code uses scope functions everywhere: apply for intents and builders, ?.let for nullable extras, also for logging.

// Open the order details screen
val intent = Intent(this, OrderDetailActivity::class.java).apply {
    putExtra("order_id", order.id)
    putExtra("from_notification", true)
    flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
startActivity(intent)

// Read an optional extra safely
intent.getStringExtra("coupon")?.let { code -> viewModel.applyCoupon(code) }

// Build a Retrofit client
val retrofit = Retrofit.Builder()
    .baseUrl(BuildConfig.API_URL)
    .client(OkHttpClient.Builder().apply {
        if (BuildConfig.DEBUG) addInterceptor(HttpLoggingInterceptor().apply { level = BODY })
    }.build())
    .addConverterFactory(Json.asConverterFactory("application/json".toMediaType()))
    .build()
    .also { Log.d("Network", "Retrofit ready") }

Registration Form Processing

Example

A runnable version of a sign-up flow: clean the input, validate, build the user, log it.

data class SignUpForm(val name: String?, val email: String?, val phone: String?)
data class User(var name: String = "", var email: String = "", var phone: String? = null, var verified: Boolean = false)

fun register(form: SignUpForm): User? {
    val email = form.email?.trim()?.lowercase()?.takeIf { "@" in it } ?: return null
    return User().apply {
        name = form.name?.trim()?.takeIf { it.isNotEmpty() } ?: email.substringBefore("@")
        this.email = email
        phone = form.phone?.filter { it.isDigit() }?.takeIf { it.length == 10 }
    }.also { println("Registered: $it") }
}

fun main() {
    register(SignUpForm(" Abhinav ", "[email protected]", "98765-43210"))
    register(SignUpForm(null, "[email protected]", "123"))
    println(register(SignUpForm("X", "invalid-email", null)))
}

Output

Registered: User(name=Abhinav, [email protected], phone=9876543210, verified=false)
Registered: User(name=ravi, [email protected], phone=null, verified=false)
null