I have two functions that are a composition of pure functions. The first function takes a parcel, builds a house on it, and take a picture to advertise it in a magazine:
let buildAndAdvertiseHouse parcel =
parcel
|> inspect
|> buildWalls
|> buildRoof
|> takePhoto
|> advertise
The second function takes also a parcel, builds a house on it, and adds a finishing touch to it:
let buildAndCompleteHouse parcel =
parcel
|> inspect
|> buildWalls
|> buildRoof
|> paintWalls
|> addFurniture
It's clear that the two functions are also pure, since they are a composition of pure functions. Now I have a parcel, let say niceParcel
and I want to apply both functions to it. However, I want to avoid that the first three sub-functions are calculated twice since they take a big time to compute and they are shared among the two functions.
How do I refactor my code, so these unnecessary calculations are avoided, while keeping these nice pure functions which have a clear meaning?