Comments (7)
without losing type inference
What do you mean? Could you please show your implementation and an example reproducing the issue?
from monocle-ts.
My attempt at this:
interface Pair<A, B> { fst: A; snd: B }
const pair = <A, B>(fst: A, snd: B): Pair<A, B> => ({ fst, snd });
type Lens<S, T, A, B> = (val: S) => Pair<A, (val: B) => T>;
const _fst = <A, C>(s: Pair<A, C>) => pair(s.fst, <B>(b: B): Pair<B, C> => pair(b, s.snd));
const _snd = <A, C>(s: Pair<C, A>) => pair(s.snd, <B>(b: B): Pair<C, B> => pair(s.fst, b));
function view<S, T, A, B>(l: Lens<S, T, A, B>, val: S): A {
return l(val).fst;
}
function set<S, T, A, B>(l: Lens<S, T, A, B>, val: S, x: B): T {
return l(val).snd(x);
}
function over<S, T, A, B>(l: Lens<S, T, A, B>, val: S, f: (x: A) => B): T {
const r = l(val);
return r.snd(f(r.fst));
}
const p = pair(1, 'a');
const stringify = (x: number) => '' + x;
const first = view<Pair<number, string>, Pair<any, string>, number, any>(_fst, p);
const stringifyFstP = over<typeof p, Pair<string, string>, number, string>(_fst, p, stringify);
The last two lines won't work unless you explicitly give the generic type parameters.
from monocle-ts.
These exist in the original Scala monocle
library and I've partially ported them for my own projects. If there's interest, I could finish and file a PR. But it would obviate lots of code, since the current optics would be type aliases of the "full" ones.
from monocle-ts.
So that means you know a way to implement this that would not force the user to write down the type arguments explicitly every time?
from monocle-ts.
interface PLens<S,T,A,B> {
readonly get: (s:S) => A
readonly set: (b:B) => (s:S) => T
}
interface Foo{
x:number
}
interface Bar{
x:string
}
const l: PLens<Foo,Bar,number,string> = {
get: s => s.x,
set: b => s => ({ x: b }),
}
const _ = {
x: 5
}
const got = l.get(_) // TS infers this is type number
const sat = l.set('howdy')(_) // tS infers this is type Bar
console.log('should be 5:', got)
console.log('should be {x:"howdy"}:', sat)
from monocle-ts.
That's cool! But does it work with my Pair type above, a type with type parameters.
from monocle-ts.
Just a note - I think PLens
would be required to implement insertAt
and updateAt
, which are features under discussion
from monocle-ts.
Related Issues (20)
- pipe and composition of L.findFirst doesn't compile due to difference in readonly attribute
- generating lots of paths with optional steps and sets of steps
- Release 2.3.8 seems to be broken HOT 3
- Is dissoc possible? HOT 2
- Are the experimental features safe to use in production HOT 1
- Lens props should not requires at least two argument HOT 1
- Lens set HOT 1
- iso.iso constructor is not equivalent to iso class constructor
- optional: prop for partial keys HOT 3
- Customize Monocle policy regarding missing interposed nodes?
- `Lens#composeAt` method?
- Tree structure HOT 1
- No fromProp method in docs. HOT 1
- fromTraversable(array) - array is deprecated
- How-to: select optional props from record. HOT 1
- learnings resources section
- insertAt & renameAt
- missing dependencies? HOT 1
- Lens.fromPath with array HOT 1
- Optic implemented using Kliesli Arrows HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from monocle-ts.