Skip to content

Instantly share code, notes, and snippets.

@civilizeddev
Created September 24, 2019 13:40
Show Gist options
  • Save civilizeddev/1a698f3a99c1e2956cb3e92fd148e77c to your computer and use it in GitHub Desktop.
Save civilizeddev/1a698f3a99c1e2956cb3e92fd148e77c to your computer and use it in GitHub Desktop.
독립적으로 실행가능한 계산을 모으기: Apply 타입 클래스 사용
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