Giter VIP home page Giter VIP logo

Comments (7)

gcanti avatar gcanti commented on June 8, 2024

without losing type inference

What do you mean? Could you please show your implementation and an example reproducing the issue?

from monocle-ts.

atennapel avatar atennapel commented on June 8, 2024

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.

kylegoetz avatar kylegoetz commented on June 8, 2024

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.

atennapel avatar atennapel commented on June 8, 2024

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.

kylegoetz avatar kylegoetz commented on June 8, 2024
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.

atennapel avatar atennapel commented on June 8, 2024

That's cool! But does it work with my Pair type above, a type with type parameters.

from monocle-ts.

anthonyjoeseph avatar anthonyjoeseph commented on June 8, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.