1

I would like to combine queries programatically to create a query resulting in all requirements satisfied. I can see there is a union and ++ operator, but I see no "intersection" or **.

Assuming Slick FirstExample, let us have a code:

  val notCheap = coffees.filter(_.price>8.0)
  val notExpensive = coffees.filter(_.price<9.0)

  val midprice = coffees.filter(_.price>8.0).filter(_.price<9.0)

  println("Midprice coffees:")
  midprice foreach { case (name, supID, price, sales, total) =>
    println("  " + name + "\t" + supID + "\t" + price + "\t" + sales + "\t" + total)
  }

How can create notCheap, notExpensive and midprice so that midprice is created from notCheap and notExpensive to avoid code repetition?

Suma
  • 33,181
  • 16
  • 123
  • 191

1 Answers1

4
implicit class CoffeesExtensions(q: Query[Coffees, Coffee]){
  def notCheap = q.filter(_.price > 8.0)
  def notExpensive = q.filter(_.price < 8.0)
  def midprice = q.notCheap.notExpensive
}

coffees.midPrice

There is also info about this in our Scala Days 2013 talk and the Slick patterns talk at Scala eXchange 2013. http://slick.typesafe.com/docs/

Suma
  • 33,181
  • 16
  • 123
  • 191
cvogt
  • 11,260
  • 30
  • 46
  • Thanks, look nice. The link http://slick.typesafe.com/docs/ does not seem to be very specific, though. I suggest either removing it, or replacing it with a link to some page describing this technique. – Suma May 29 '14 at 10:47