Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generator expressions to Scala for Python Devs #3181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion _overviews/scala3-book/scala-for-python-devs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -693,6 +693,26 @@ Scala also has `match` expressions.
</tbody>
</table>

### Lazily evaluated comprehensions:

<table>
<tbody>
<tr>
<td class="python-block">
<code>from itertools import count
<br>all_squares = (n**2 for n in count())&nbsp; # generator expression
<br># all_squares: &lt;generator object &lt;genexpr&gt; at ...&gt;</code>
</td>
</tr>
<tr>
<td class="scala-block">
<code>val allSquares = for n &lt;- LazyList.from(0) yield n * n
<br>// allSquares: LazyList(&lt;not computed&gt;)</code>
</td>
</tr>
</tbody>
</table>

### `match` expressions:

<table>
Expand Down