Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to create a Traversal for a List of arbitrary length #430

Open
@JohnMcGuinness

Description

@JohnMcGuinness

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp