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
{{ message }}
Repository navigation
How to create a Traversal for a List of arbitrary length #430
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.
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.
Activity
fourofclubs commentedon Apr 30, 2021
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.