Created
September 24, 2019 13:40
-
-
Save civilizeddev/1a698f3a99c1e2956cb3e92fd148e77c to your computer and use it in GitHub Desktop.
독립적으로 실행가능한 계산을 모으기: Apply 타입 클래스 사용
This file contains 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
case class Pack(orders: Seq[Order], orderItems: Seq[OrderItem], products: Seq[Product]) | |
// 1. for-comprehension에서 flatMap 체이닝: 순서대로 실행된다. | |
for { | |
orders <- orderRepository.findAll() | |
orderItems <- orderItemRepository.findAll() | |
products <- productRepository.findAll() | |
} yield Pack(orders, orderItems, products) | |
// 2. future를 먼저 받아놓고 flatMap 체이닝: 동시에 여러 개의 Future를 진행할 수 있지만, 코드가 많아진다. | |
val ordersFuture = orderRepository.findAll() | |
val orderItemsFuture = orderItemRepository.findAll() | |
val productsFuture = productRepository.findAll() | |
for { | |
orders <- ordersFuture | |
orderItems <- orderItemsFuture | |
products <- productsFuture | |
} yield Pack(orders, orderItems, products) | |
// 3. Apply.mapN 사용: 간단하다. | |
import cats.instances.future._ | |
import cats.syntax.apply._ | |
( | |
orderRepository.findAll(), | |
orderItemRepository.findAll(), | |
productRepository.findAll() | |
).mapN(Pack) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment