Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learning RxJava

You're reading from   Learning RxJava Reactive, Concurrent, and responsive applications

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781787120426
Length 400 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
 Nield Nield
Author Profile Icon Nield
Nield
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Title Page
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface
1. Thinking Reactively FREE CHAPTER 2. Observables and Subscribers 3. Basic Operators 4. Combining Observables 5. Multicasting, Replaying, and Caching 6. Concurrency and Parallelization 7. Switching, Throttling, Windowing, and Buffering 8. Flowables and Backpressure 9. Transformers and Custom Operators 10. Testing and Debugging 11. RxJava on Android 12. Using RxJava for Kotlin New 13. Appendix

Using let() and apply()


In Kotlin, every type has a let() and apply() extension function. These are two simple but helpful tools to make your code more fluent and expressive.

Using let()

let() simply accepts a lambda that maps the invoked object T to another object R. It is similar to how RxJava offers the to() operator, but it applies to any type T and not just Observables/Flowables. For example, we can call let() on a string that has been lowercased and then immediately do any arbitrary transformation on it, such as concatenating its reversed() string to it. Take a look at this operation:

fun main(args: Array<String>) {

     val str = "GAMMA"

     val lowerCaseWithReversed = str.toLowerCase().let { it + " " +
     it.reversed() }

     println(lowerCaseWithReversed)
 }

The output is as follows:

gamma ammag

The let() comes in handy when you do not want to save a value to a variable just so you can refer to it multiple times. In the preceding code, we did not have to save the result of...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at £13.99/month. Cancel anytime
Visually different images