Sequences क्या हैं
In this page:
val result = collection.asSequence()
.map { it transformation }
.filter { condition }
.toList() // terminal operation runs the pipeline
Creating a Sequence
किसी collection पर .asSequence() call करना इसे एक Sequence में बदल देता है, जो हर step पर eagerly intermediate lists बनाने के बजाय elements को lazily process करता है।
उदाहरण: Creating a Sequence
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `numbers`, set to `(1..5).asSequence()`
val numbers = (1..5).asSequence()
// Print `numbers.toList()` to the console, with a trailing newline
println(numbers.toList())
}
Login to try C/C++/Java/PHP code in the editor
Lazy Evaluation with map and filter
किसी sequence पर .map { } और .filter { } chain करना बस operations की एक pipeline बना देता है -- जब तक कोई terminal operation call न हो तब तक असल में कुछ नहीं चलता।
उदाहरण: Lazy Evaluation with map and filter
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `result`, set to `listOf(1, 2, 3, 4, 5)`
val result = listOf(1, 2, 3, 4, 5)
.asSequence()
.map { println("Mapping $it"); it * 2 }
.filter { it > 4 }
.toList()
// Print "Result: $result" to the console, with a trailing newline
println("Result: $result")
}
Login to try C/C++/Java/PHP code in the editor
Terminal Operations Trigger Execution
.toList(), .first(), .count(), या .sum() जैसे functions terminal operations हैं -- एक को call करना ही असल में पूरी lazy pipeline को चलाता है और एक concrete result produce करता है।
उदाहरण: Terminal Operations Trigger Execution
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `firstBigSquare`, set to `(1..100).asSequence()`
val firstBigSquare = (1..100).asSequence()
.map { it * it }
.first { it > 50 }
// Print "First square over 50: $firstBigSquare" to the console, with a trailing newline
println("First square over 50: $firstBigSquare")
}
Login to try C/C++/Java/PHP code in the editor
Sequences vs Regular Collections
.map() जैसे Regular List operations हर step पर eagerly एक पूरी नई list बनाते हैं, जबकि एक Sequence अगले element पर जाने से पहले हर element को पूरी pipeline से process करता है -- बड़े data पर लंबी chains के लिए उपयोगी।
उदाहरण: Sequences vs Regular Collections
// Entry point: execution of the program starts here
fun main() {
// Declare a read-only variable `eagerResult`, set to `(1..5).map { it * 2 }.filter { it > 4 }`
val eagerResult = (1..5).map { it * 2 }.filter { it > 4 }
// Declare a read-only variable `lazyResult`, set to `(1..5).asSequence().map { it * 2 }.filter { it > 4 }.toList()`
val lazyResult = (1..5).asSequence().map { it * 2 }.filter { it > 4 }.toList()
// Print "Eager: $eagerResult" to the console, with a trailing newline
println("Eager: $eagerResult")
// Print "Lazy: $lazyResult" to the console, with a trailing newline
println("Lazy: $lazyResult")
}
Login to try C/C++/Java/PHP code in the editor
- किसी बड़े dataset पर
.map().filter()की एक regularListchain का उपयोग करना और अनावश्यक intermediate lists से हैरान होना; एकSequencelazy होकर इससे बचता है। - किसी sequence chain के आखिर में
.toList()(या कोई दूसरा terminal operation) call करना भूल जाना, क्योंकि intermediate operations lazy हैं और किसी terminal के invoke होने तक नहीं चलते। - यह मान लेना कि sequences हमेशा तेज़ होते हैं; छोटी collections के लिए, किसी sequence बनाने का overhead साधारण list operations की तुलना में फायदे से ज़्यादा हो सकता है।
asSequence()किसी collection को एकSequenceमें बदल देता है, जो operations को lazily, element-by-element evaluate करता है।- किसी sequence पर intermediate operations (
map,filter) एक pipeline बना देते हैं लेकिन जब तक कोई terminal operation call न हो तब तक नहीं चलते। .toList(),.first(), या.sum()जैसे terminal operations किसी sequence की असली processing को trigger करते हैं।- Sequences हर step पर intermediate collections बनाने से बचते हैं, जो बड़े data या लंबी operation chains के लिए ज़्यादा efficient हो सकता है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: