-
Notifications
You must be signed in to change notification settings - Fork 12
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
How to traverse Record
types?
#78
Comments
Ok, I think this is at least one answer to my own question. You can make the entries traversal by composing a object -> entries iso with the elems traversal. import { optic } from 'optic-ts';
let entries = optic()
.iso(
(o) => Object.entries(o),
(entries) => entries.reduce(
(o, [k, v]) => Object.assign(o, { [k]: v })
, {}))
.elems()
let keys = entries.nth(0)
let values = entries.nth(1) Wow, that felt great! I think this is the first optics library where I was actually able to figure it out on my own. Congratulations on making a really nice API. Have you considered adding an |
Nice! I can add the record traversals. I don’t think there’s a way to amend the interface with new optics. However, I’m working on a new API in which optics are just functions or values instead of object methods. There you could just create new optics and use them without having to worry about any builders. Here’s one example usage of the new API: https://github.com/akheron/optics-ts/blob/floating/src/floating/valueOr.spec.ts#L8 |
I'll have a look. I was playing around with them and I did tweak the Here's the playground I was working with if it helps https://runkit.com/cowboyd/optics-ts-record-traversal When do you think the new API will come out? |
Also, fwiw, I don't mind the builder syntax so much since you've included top-level functions to use once you've constructed the lens. |
How do you type this correctly in TypeScript? I've tried but failed :( |
I was wondering if there was any movement on implementing the records() traversal? |
There's PR #81, but at least back then the TS compiler couldn't typecheck that. |
Is there the equivalent of the array value
elems()
traversal for Record values? I'm thinking things likekeys()
,values()
, andentries()
.Also, if you want to compose optics that are not included in the builder syntax is there an "escape hatch"? In other words, how would I define a
values()
traversal in user-land that didn't require a change to optics-ts itself?The text was updated successfully, but these errors were encountered: