fp-ts / optic

A porting of zio-optics to TypeScript
https://fp-ts.github.io/optic/
MIT License
115 stars 4 forks source link

feat: added data/Bounded #25

Closed jessekelly881 closed 1 year ago

jessekelly881 commented 1 year ago

Added data/Bounded optics.

jessekelly881 commented 1 year ago

@gcanti

gcanti commented 1 year ago

@jessekelly881 I don't understand what's use case for this, putting a Bounded instance nested in an object looks strange

jessekelly881 commented 1 year ago

@gcanti I added it primarily for completeness but I could definitely imagine circumstances where a bounded in an object makes sense. Given an object like a Point for example you could create a function that creates a bound for the point given bounds for each coordinate(x,y,z...) either as a struct or a tuple. Similar to constructing an Eq from a struct or a tuple given a struct or tuple of Eqs. In that circumstance you may want to update the min or max value of the bound along the y coordinate for example. This isn't too unrealistic actually. You might want to bound a point to the limits of a canvas and then update the bounds for the x or y coordinates on resize.

gcanti commented 1 year ago

I don't think it's worth adding code for a use case that is not clearly frequent.

But in any case, isn't it already covered by at?

type S = {
  readonly a: {
    readonly b: Bounded<number>
  }
}

const s: S = { a: { b: N.Bounded } }

describe("Bounded", () => {
  it("min", () => {
    const _b = Optic.id<S>().at("a").at("b").at("minBound")
    expect(Optic.modify(_b)(() => 10)(s)).toEqual({ a: { b: { ...N.Bounded, minBound: 10 } } })
  })
  it("max", () => {
    const _b = Optic.id<S>().at("a").at("b").at("maxBound")
    expect(Optic.modify(_b)(() => 10)(s)).toEqual({ a: { b: { ...N.Bounded, maxBound: 10 } } })
  })
})
jessekelly881 commented 1 year ago

Good point. At clearly works better here. Closing the pr.