Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lawful lenses #177

Open
davidchambers opened this issue Apr 13, 2016 · 9 comments
Open

lawful lenses #177

davidchambers opened this issue Apr 13, 2016 · 9 comments

Comments

@davidchambers
Copy link
Member

R.lensProp and R.lensIndex aren't quite lawful, as @scott-christopher explained in ramda/ramda#1281 (comment).

Scott, have you any interest in working on Sanctuary equivalents of Ramda's lens functions, which would use the Maybe type where appropriate to satisfy the lens laws?

@masaeedu
Copy link
Member

masaeedu commented Jul 15, 2018

Wouldn't the following lens be lawful?

const fail = e => { throw new TypeError(e) }
const lensProp = p => ({
  view: o => o.hasOwnProperty(p) ? o[p] : fail(`Missing property ${p}`),
  update: x => o => o.hasOwnProperty(p) ? { ...o, [p]: x } : fail(`Missing property ${p}`)
})

@davidchambers
Copy link
Member Author

I think so, @masaeedu, given the strictness of S.prop. In fact, using S.prop would simplify the definitions of view and update.

@masaeedu
Copy link
Member

masaeedu commented Jul 15, 2018

@davidchambers Sweet. So basically const lensProp = p => ({ view: S.prop(p), update: S.insert(p) })

@scott-christopher
Copy link
Member

scott-christopher commented Jul 17, 2018

Apologies, I completely missed this issue ... 2 years ago. I'd be happy to help migrate https://github.com/flunc/optics over to become a Sanctuary project if there's interest.

The existing atObject :: String -> LensP (Object a) (Maybe a) seems to already fit the bill for this specific request.

As it stands, the library is void of documentation and there has been no focus on performance, but these are things we can change :)

@davidchambers
Copy link
Member Author

We needn't move flunc/optics to the @sanctuary-js namespace, though if you would like to do this, @scott-christopher, I'd be very happy for you to do so. I could not at this point commit much of my time to the effort.

@protoEvangelion
Copy link

@davidchambers is there anything on the roadmap still for implementing lenses?

@coodoo
Copy link

coodoo commented Dec 3, 2019

As a side note, what's the story for lens/optics from sanctuary at this moment? Seemed there's only getter (like gets and props), but no setters?

@davidchambers
Copy link
Member Author

@protoEvangelion and @coodoo, lenses will be the next major addition to Sanctuary.

As it may be of interest, I will share the backstory. In order to define van Laarhoven lenses, which have the desirable property of being compatible with S.compose, we need two types: Identity a and Constant a b. The former exists as sanctuary-identity; the latter will exist once sanctuary-js/sanctuary-constant#1 has been merged. As I was working on sanctuary-constant I was also working on fantasyland/fantasy-land#315, a proposal that will, if accepted, necessitate changes to every Fantasy Land -compliant ADT. It seemed sensible at the time to release a new major version of the Fantasy Land specification before publishing any new ADT packages.

In mid 2019 I started a full-time job, as I was running out of money. My ambitious plans may have made sense when I had a lot of time to devote to Sanctuary, but when the demands on my time increased I should have reconsidered them. The Fantasy Land changes are themselves several weeks of full-time work, as they involve updating fantasy-laws and the many Sanctuary ADTs.

Let's finalize and merge sanctuary-js/sanctuary-constant#1 without waiting for fantasyland/fantasy-land#315. We will then have what we need to define S.lens, S.view, S.over, and S.set. Later, once FL5 has been published, we will release [email protected].

@Avaq
Copy link
Member

Avaq commented Jun 14, 2020

In order to define van Laarhoven lenses, which have the desirable property of being compatible with S.compose, we need two types: Identity a and Constant a b.

As remarked in Gitter, we don't necessarily have to use van Laarhoven encoding to get lenses to work with S.compose. Instead we can use any object with Semigroupoid implemented. This removes the need for the Identity a and Constant a b types.

The downside of this approach is that these lenses will not work with function-specialized compose functions from other sources, or if JavaScript would ever introduce a composition operator I don't imagine it will follow the Fantasy Land Semigroupoid spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants