From e95a91bd94cd8c54c44053c08f9d239079006e8e Mon Sep 17 00:00:00 2001 From: Vojtech Letal Date: Tue, 9 Jun 2020 17:44:37 +0200 Subject: [PATCH 1/2] add additional information about for-comprehention desugaring --- _overviews/FAQ/yield.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/_overviews/FAQ/yield.md b/_overviews/FAQ/yield.md index 638312bd61..91f7950a7b 100644 --- a/_overviews/FAQ/yield.md +++ b/_overviews/FAQ/yield.md @@ -63,8 +63,31 @@ is translated into c.map(x => (x, ...)).map((x,y) => {...}) +#### Example 5 -When you look at very simple for comprehensions, the map/foreach alternatives +When pattern matching is used in for comprehensions on objects which do not +implement `filter` or `withFilter` compilation fails with the following error + + value withFilter is not a member of ... + +That it because, for example, the following statement + + for((a, b) <- c) yield {...} + +is translated into + + c.withFiler{ + case (a, b) => true + case _ => false + }.map{case (a, b) => {...} + +where the `withFiler` ensures that the pattern in the subsequent function is +always satisfied + +Clarity +---------------------------------- + +When you look at very simple for comprehensions, the `map`/`foreach` alternatives look, indeed, better. Once you start composing them, though, you can easily get lost in parenthesis and nesting levels. When that happens, for comprehensions are usually much clearer. From 7d71cb31a0b55e55d33597e7a88988f8d31ca7fe Mon Sep 17 00:00:00 2001 From: Seth Tisue Date: Wed, 17 Jun 2020 15:48:02 -0700 Subject: [PATCH 2/2] Update yield.md fix typos --- _overviews/FAQ/yield.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/_overviews/FAQ/yield.md b/_overviews/FAQ/yield.md index 91f7950a7b..4866a1f1a4 100644 --- a/_overviews/FAQ/yield.md +++ b/_overviews/FAQ/yield.md @@ -76,12 +76,12 @@ That it because, for example, the following statement is translated into - c.withFiler{ + c.withFilter{ case (a, b) => true case _ => false }.map{case (a, b) => {...} -where the `withFiler` ensures that the pattern in the subsequent function is +where the `withFilter` ensures that the pattern in the subsequent function is always satisfied Clarity