Skip to content

Commit 5d3a4d2

Browse files
committed
Add generator expr.s to Scala for Python Devs
1 parent 67bb2f6 commit 5d3a4d2

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

_overviews/scala3-book/scala-for-python-devs.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ At a high level, Scala shares these *similarities* with Python:
4040
- Both have a relatively simple, concise syntax
4141
- Both support a [functional style of programming][fp-intro]
4242
- Both are object-oriented programming (OOP) languages
43-
- Both have comprehensions: Python has list comprehensions and Scala has `for` comprehensions
43+
- Both have comprehensions: Python has list comprehensions, dict comprehensions and generator expressions and Scala has `for` comprehensions
4444
- Both languages have support for lambdas and [higher-order functions][hofs]
4545
- Both can be used with [Apache Spark](https://spark.apache.org) for big data processing
4646
- Both have a wealth of terrific libraries
@@ -693,6 +693,26 @@ Scala also has `match` expressions.
693693
</tbody>
694694
</table>
695695

696+
### Lazily evaluated comprehensions:
697+
698+
<table>
699+
<tbody>
700+
<tr>
701+
<td class="python-block">
702+
<code>from itertools import count
703+
<br>all_squares = (n**2 for n in count())&nbsp; # generator expression
704+
<br># all_squares: &lt;generator object &lt;genexpr&gt; at ...&gt;</code>
705+
</td>
706+
</tr>
707+
<tr>
708+
<td class="scala-block">
709+
<code>val allSquares = for n &lt;- LazyList.from(0) yield n * n
710+
<br>// allSquares: LazyList(&lt;not computed&gt;)</code>
711+
</td>
712+
</tr>
713+
</tbody>
714+
</table>
715+
696716
### `match` expressions:
697717

698718
<table>

0 commit comments

Comments
 (0)