I have the following pipe
pipe(
getProduct(), //step 1
chain((it) => E.right(Object.assign({}, it, { tax: 0.1 }))), //step 2
chain((it) => E.right(Object.assign({}, it, { delivery: 0.15 }))), //step 3
//chain((it) => E.left('ERROR')), //step 4
E.fold( //final step
(e) => {
console.log(`error: ${e}`)
},
(it) => {
console.log(
`ok ${it.count} ${it.pricePerItem} ${it.tax} ${it.delivery}`
)
}
)
)
where
getProduct = () => E.right({ count: 10, pricePerItem: 5 })
Step 1 produces output { count: 10, pricePerItem: 5 }
Step 2 takes the output of step 1 as input and produces output { count: 10, pricePerItem: 5, tax: 0.1 }
Step 3 takes the output of step 2 which is { count: 10, pricePerItem: 5, tax: 0.1 }
and produces output { count: 10, pricePerItem: 5, tax: 0.1, delivery: 0.15 }
Step 4 is just a placeholder where it could potentially produces a left
to indicate an error condition. I just left it out.
It works as expected in a pipe
. But I do NOT want that.
I want step 2 to takes the input { count: 10, pricePerItem: 5 }
and add tax
to it. Parallelly, I want step 3 to take the same input { count: 10, pricePerItem: 5 }
and add delivery
to it.
Then I want step 4 to take the output of step 2 and step 3 and merge them back.
I saw something involved bind
and/or do notation
such as in this answer but is not quite sure.
So how the flow be branched and merged as opposed to always run in a pipe?
Update The equivalent in imperative programming is as follow:
const products = getProducts()
const productsWithTax = getProductsWithTax(products)
const productsWithDelivery = getProductsWithDelivery(products)
const productsWithTaxAndDelivery = getWithTaxAndDelivery(productsWithTax, productsWithDelivery)
The point is to I do not want the true
pipe.