Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitcec81b3

Browse files
authored
Add generator expr.s to Scala for Python Devs (#3181)
1 parent1a0a11e commitcec81b3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
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)

[8]ページ先頭

©2009-2025 Movatter.jp