This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.Future | |
| trait AsyncJob { | |
| def execute(): Future[Boolean] | |
| } | |
| class MyLongRunningJob(asyncJob: AsyncJob) extends Runnable { | |
| override def run(): Unit = { | |
| asyncJob.execute() | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.ExecutionContext.Implicits.global | |
| import scala.concurrent.Future | |
| // YESS :( | |
| someFutureJob().map { value => | |
| operate(value) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import scala.concurrent.duration.Duration | |
| import scala.concurrent.{Await, Future} | |
| val future = someFutureJob() | |
| // NOO :( | |
| val value = Await.result(future, Duration.Inf) | |
| operate(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def divide3(a: Int, b: Int): MaybeDivideable = { | |
| for { | |
| x <- MaybeDivideable(a) | |
| y <- MaybeDivideable(b) | |
| } yield (x / y) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait MaybeDivideable { | |
| def get: Int | |
| def flatMap(f: Int => MaybeDivideable): MaybeDivideable | |
| /* New map method */ | |
| def map(f: Int => Int): MaybeDivideable | |
| } | |
| case class Divideable(value: Int) extends MaybeDivideable { | |
| require(value != 0, "value must not be 0") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def divide2(a: Int, b: Int): MaybeDivideable = { | |
| val aMayBe = MaybeDivideable(a) | |
| val bMayBe = MaybeDivideable(b) | |
| aMayBe.flatMap { x => | |
| bMayBe.flatMap { y => | |
| MaybeDivideable(x / y) | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def divide1(a: Int, b: Int): MaybeDivideable = { | |
| MaybeDivideable(a) match { | |
| case UnDivideable => | |
| UnDivideable | |
| case Divideable(x) => | |
| MaybeDivideable(b) match { | |
| case UnDivideable => | |
| UnDivideable | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| sealed trait MaybeDivideable { | |
| def get: Int //Extraction | |
| def flatMap(f: Int => MaybeDivideable): MaybeDivideable | |
| } | |
| object MaybeDivideable { | |
| def apply(value: Int): MaybeDivideable = | |
| if (value == 0) UnDivideable else Divideable(value) // Lifting | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public int sumOfSquaresOfEvenElements(List<Integer> list) { | |
| if (list == null) { | |
| return 0; | |
| } | |
| int acc = 0; | |
| for (int elem: list) { | |
| if (elem % 2 == 0) { | |
| acc += (elem * elem); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.Random; | |
| public class Main { | |
| private static final MyBlockingQueue<Integer> queue = new MyBlockingQueue<>(); | |
| static class Producer extends Thread { | |
| Producer(int i) { | |
| super("Producer-" + i); | |
| } | |
| @Override |