- Notifications
You must be signed in to change notification settings - Fork253
Open
Description
Since there's no usable documentation for this library I'll ask a question here.
I have aCart which may have anItem in it. AnItem has aDiscount, which may beNoDiscount orPercentDiscount.
publicrecordCart(Stringid,Itemitem,intquantity)publicrecordItem(Stringsku,doubleprice,intleftInStock,Discountdiscount)sealedinterfaceDiscount permitsNoDiscount,PercentDiscountpublicrecordNoDiscount()implementsDiscountpublicrecordPercentDiscount(doublevalue)implementsDiscount
I have some Optics:
privatestaticfinalLens<Cart,Item>itemL =Lens.lens(Cart::item, (Itemi) -> (Cartc) ->newCart(c.id(),i,c.quantity()));privatestaticfinalLens<Item,Discount>discountL =Lens.lens(Item::discount,d ->i ->newItem(i.sku(),i.price(),i.leftInStock(),d));privatestaticfinalPrism<Discount,Double>onlyPctDiscount=Prism.prism(d -> {if(dinstanceofPercentageOff) {returnOption.some(((PercentageOff)d).value());}else {returnOption.none();}},PercentageOff::new);
This code recalulates theItem'sDiscount given aCart:
itemL.composeLens(discountL).composePrism(onlyPctDiscount).modify(calculation).f(cart);
If I change theCart to have aList ofItem's, to recalculate the discount for each item in the cart I'm pretty sure I need to change thatPrism to aTraversal.
How do I create a traversal for a list with any number ofItems. From what I can make out FJ only allowsTraversals for 2 to 6 items in aList.
Metadata
Metadata
Assignees
Labels
No labels