You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since there's no usable documentation for this library I'll ask a question here.
I have a Cart which may have an Item in it. An Item has a Discount, which may be NoDiscount or PercentDiscount.
public record Cart(Stringid, Itemitem, intquantity)
public record Item(Stringsku, doubleprice, intleftInStock, Discountdiscount)
sealedinterfaceDiscount permits NoDiscount, PercentDiscountpublic record NoDiscount() implementsDiscountpublic record PercentDiscount(doublevalue) implementsDiscount
If I change the Cart to have a List of Item's, to recalculate the discount for each item in the cart I'm pretty sure I need to change that Prism to a Traversal.
How do I create a traversal for a list with any number of Items. From what I can make out FJ only allows Traversals for 2 to 6 items in a List.
The text was updated successfully, but these errors were encountered:
Creating a custom Traversal can be tricky, but there is a Traversal for Lists. It's at List.Optic.traversal().
If your Cart changes to include a List of Items, that Prism is still fine, but itemL would need to change to a Lens<Cart, List<Item>> (and probably change the name to itemsL). Then, I believe you should only need to compose an extra traversal: itemsL.composeTraversal(List.Optic.traversal()).composeLens(discountL).composePrism(onlyPctDiscount).modify(calculation).f(cart)
That should modify each item's discount.
Since there's no usable documentation for this library I'll ask a question here.
I have a
Cart
which may have anItem
in it. AnItem
has aDiscount
, which may beNoDiscount
orPercentDiscount
.I have some Optics:
This code recalulates the
Item
'sDiscount
given aCart
:If I change the
Cart
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 of
Item
s. From what I can make out FJ only allowsTraversal
s for 2 to 6 items in aList
.The text was updated successfully, but these errors were encountered: