Skip to content

SCALA-46 A Comprehensive Guide to For-comprehension in Scala #46

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

Merged
merged 2 commits into from
Apr 26, 2020
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.baedung.scala.forcomprehension;

import java.util.Arrays;
import java.util.List;

public class ForComprehension {
public static void main(String[] args) {
final List<TestResult> results =
Arrays.asList(
new TestResult("test 1",10, 10),
new TestResult("test 2",2, 6)
);
int totalPassedAssertsInSucceededTests = 0;
for (int i = 0; i < results.size(); i++) {
final TestResult result = results.get(i);
if (result.isSucceeded()) {
totalPassedAssertsInSucceededTests += result.getSuccessfulAsserts();
}
}

final long totalPassedAssertsInSucceededTests1 =
results.stream()
.filter(TestResult::isSucceeded)
.mapToInt(TestResult::getSuccessfulAsserts)
.sum();
}

private static class TestResult {
private String id;
private boolean succeeded;
private int successfulAsserts;
private int totalAsserts;

public TestResult(String id, int successfulAsserts, int totalAsserts) {
this.id = id;
this.succeeded = successfulAsserts == totalAsserts;
this.successfulAsserts = successfulAsserts;
this.totalAsserts = totalAsserts;
}

public String getId() {
return id;
}

public boolean isSucceeded() {
return succeeded;
}

public int getSuccessfulAsserts() {
return successfulAsserts;
}

public int getTotalAsserts() {
return totalAsserts;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.baeldung.scala.forcomprehension

object ForComprehension {

val results = List(TestResult("test 1", 10, 10), TestResult("test 2", 2, 6))
val executionTimeList = List(("test 1", 100), ("test 2", 230))
val listOfPassedAssertsInSucceededTests: List[Int] =
for {
result <- results
if result.succeeded
} yield result.successfulAsserts
val passedAssertsInSucceededTests: Int = listOfPassedAssertsInSucceededTests.sum

val numberOfAssertsWithExecutionTime: List[(String, Int, Int)] =
for {
result <- results
(id, time) <- executionTimeList
if result.id == id
} yield (id, result.totalAsserts, time)

val hugeNumberOfAssertsForATest: Int = 10
val resultsWithAHugeAmountOfAsserts: List[TestResult] =
for {
result <- results
if result.totalAsserts >= hugeNumberOfAssertsForATest
} yield result

case class TestResult(id: String, successfulAsserts: Int, totalAsserts: Int) {
def succeeded: Boolean = successfulAsserts == totalAsserts
}

val result: Result[Int] = Result(42)
for {
res <- result
} println(s"The result is $res")

val magic: Int = 42
for {
res <- result
} yield res * magic

val anotherResult: Result[Int] = Result(10)
result
.flatMap(res =>
anotherResult
.map(another => res + another)
)

for {
res <- result
if res == 10
} yield res

case class Result[A](result: A) {
def foreach(f: A => Unit): Unit = f(result)
def map[B](f: A => B): Result[B] = Result(f(result))
def flatMap[B](f: A => Result[B]): Result[B] = f(result)
def withFilter(f: A => Boolean): Result[_] = if (f(result)) this else EmptyResult
}

object EmptyResult extends Result[Null](null)
}