Lazy sequence generation in Rust

Rust does have generators, but they are highly experimental and not currently available in stable Rust. Works in stable Rust 1.0 and above Range handles your concrete example. You can use it with the syntactical sugar of ..: fn main() { let sum: u64 = (0..1_000_000).sum(); println!(“{}”, sum) } What if Range didn’t exist? We … Read more

double stream feed to prevent unneeded memoization?

Normally, definition of primes stream in Richard Bird’s formulation of the sieve of Eratosthenes is self-referential: import Data.List.Ordered (minus, union, unionAll) ps = 2 : minus [3..] — `:` is “cons” (foldr (\p r -> p*p : union [p*p+p, p*p+2*p..] r) [] ps) The primes ps produced by this definition are used as the input … Read more

Kotlin’s Iterable and Sequence look exactly same. Why are two types required?

The key difference lies in the semantics and the implementation of the stdlib extension functions for Iterable<T> and Sequence<T>. For Sequence<T>, the extension functions perform lazily where possible, similarly to Java Streams intermediate operations. For example, Sequence<T>.map { … } returns another Sequence<R> and does not actually process the items until a terminal operation like … Read more