My understanding about pure functions are that the output of the function only depends on the input. And they are referentially transparent.
But then there are side effects that happens in our code. Below is an example
import scala.concurrent.Future
case class Customer(fName: String, lName: String)
case class Order(orderId: Int, item: String, quantity: Int)
case class Shipment(shipmentId: Int, orderId: Int, address: String)
trait CustomerRepo {
def findCustomer(userId: String): Future[Customer] = ???
def findLastOrder(customer: Customer): Future[Order] = ???
def findShipment(orderId: Int): Future[Shipment] = ???
}
class ShoppingApp(customerRepo: CustomerRepo) {
def getLastShipment(userId: String): Future[Shipment] = {
for {
customer <- customerRepo.findCustomer(userId)
lastOrder <- customerRepo.findLastOrder(customer)
shipment <- customerRepo.findShipment(lastOrder.orderId)
} yield shipment
}
}
Above is a basic example where we do multiple side effects (call to database).
getLastShipment returns a Future. Does that makes is a pure function.
If getLastShipment is not pure then how to make it pure???
getLastShipment
. The only "problem" is that you're usingFuture
, which is eagerly evaluated and breaks RT. Other than that, you're good. – Yuval ItzchakovgetLastShipment
function should not depend on the context which means that it should explicitly requireCustomerRepo
as a function parameter. – sarveshseri