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

.indexed(), .reread(), .rewrite() #19

Merged
merged 3 commits into from
Dec 3, 2020
Merged

Conversation

akheron
Copy link
Owner

@akheron akheron commented Dec 3, 2020

No description provided.

@akheron
Copy link
Owner Author

akheron commented Dec 3, 2020

@merisbahti FYI

@akheron akheron merged commit 5f0d8fb into master Dec 3, 2020
@akheron akheron deleted the indexed-reread-rewrite branch December 3, 2020 16:44
@merisbahti
Copy link

Hello Petri, thank you for this, very interesting with the reread and rewrite optics.

I've reimplemented klyva's todo example using "pure" optics now:
merisbahti/klyva@ec20eea

The changes were:

  1. Add indexed() optic before filter optic, this results in an Atom<Array<[number, Todo]>> (previously Atom<Array<Todo>>.
  2. Use useAtomSlice to slice atom, into an Array<Atom<...>> instead.
  3. Map this sliced atom and pass todoAtom prop, but use the nth-optic to focus on the 2nd element in the [index, Todo]-tuple:
              todoAtom={focusAtom(todoAtom, optic => optic.nth(1))}

2 questions:
This seems to work without the rewrite sort lens, thought it would be needed?
Have I done it correctly?

@merisbahti
Copy link

merisbahti commented Dec 3, 2020

This seems to work without the rewrite sort lens, thought it would be needed?

Oh, yes, after reading the docs this is mentioned that it's being done automatically. Cool. Thanks.

@akheron
Copy link
Owner Author

akheron commented Dec 3, 2020

Yeah, the thing is that if you want to use O.remove() you have to target an element of the [index, value] array instead of the 2nd element of the tuple, which is not removable. But yeah, the sorting that is applied when writing through indexed handles the rest nicely.

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

Successfully merging this pull request may close these issues.

None yet

2 participants