monocle-ts | Functional optics : a porting of Scala monocle | Functional Programming library
kandi X-RAY | monocle-ts Summary
kandi X-RAY | monocle-ts Summary
Functional optics: a (partial) porting of Scala monocle
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of monocle-ts
monocle-ts Key Features
monocle-ts Examples and Code Snippets
Community Discussions
Trending Discussions on monocle-ts
QUESTION
I'm using monocle-ts (https://github.com/gcanti/monocle-ts) library in my project. I have the following code
...ANSWER
Answered 2020-Oct-18 at 17:26The alternative notation you tried is a two-step process that first creates a lambda function using prop('a')
and then immediately invoking it with ...(stateLens)
. The prop()
function only receives a key name 'a', but cannot determine the object it belongs to as it only has the string parameter 'a' to work with. So, in the type definitions of prop
the type A
will be resolved to never
because it's not assigned and cannot be inferred.
I didn't test this, but you probably can do the same by manually passing along the type like
QUESTION
(Edit I'm using monocle-ts, but if it's not possible with monocle-ts (since the author even says it's just a partial port of the original Monocle for Scala) but if there is something in another optics package for any language, I'm open to porting those ideas to TypeScript.)
Suppose I have a helper type Partial
such that it represents a record that has some or all, but no non-members, of type A
. (So if A = { foo: number, bar: string }
then Partial
= { foo?: number, bar?: string }
) (Edit This is Typescript's built-in Partial utility type.)
I begin with
...ANSWER
Answered 2020-Sep-08 at 23:31I think, what you might want is a Polymorphic Traversal or PTraversal
.
A Traversal
says, "If I have a function A => A
, I can use modify
to obtain a function S => S
that uses the original function to modify all of the
A
s that appear in S
".
By comparison, a PTraversal
says, "if I have a function A => B
, I can use modify
to obtain a function S => T
", this converts all of the A
s in S
to B
, producing a T
.
Mnemonically, the type parameters of PTraversal
are:
S
the source of thePTraversal
T
the "modified" source of thePTraversal
A
the target of thePTraversal
B
the "modified" target of thePTraversal
PTraversal
s are useful, because they let you write things such as the following:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install monocle-ts
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page