a good Scala version of P2