From 5d3a4d2ee2ee9b59fdcfa02359fac0d5e95e32fc Mon Sep 17 00:00:00 2001 From: Shahriar Heidrich Date: Fri, 28 Mar 2025 00:30:30 +0100 Subject: [PATCH] Add generator expr.s to Scala for Python Devs --- .../scala3-book/scala-for-python-devs.md | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/_overviews/scala3-book/scala-for-python-devs.md b/_overviews/scala3-book/scala-for-python-devs.md index 147f5977f7..551bd1426d 100644 --- a/_overviews/scala3-book/scala-for-python-devs.md +++ b/_overviews/scala3-book/scala-for-python-devs.md @@ -40,7 +40,7 @@ At a high level, Scala shares these *similarities* with Python: - Both have a relatively simple, concise syntax - Both support a [functional style of programming][fp-intro] - Both are object-oriented programming (OOP) languages -- Both have comprehensions: Python has list comprehensions and Scala has `for` comprehensions +- Both have comprehensions: Python has list comprehensions, dict comprehensions and generator expressions and Scala has `for` comprehensions - Both languages have support for lambdas and [higher-order functions][hofs] - Both can be used with [Apache Spark](https://spark.apache.org) for big data processing - Both have a wealth of terrific libraries @@ -693,6 +693,26 @@ Scala also has `match` expressions. +### Lazily evaluated comprehensions: + + + + + + + + + + +
+ from itertools import count +
all_squares = (n**2 for n in count())  # generator expression +
# all_squares: <generator object <genexpr> at ...>
+
+ val allSquares = for n <- LazyList.from(0) yield n * n +
// allSquares: LazyList(<not computed>)
+
+ ### `match` expressions: