Lens<S,T,A,B> #1307
Replies: 1 comment
-
Eventually, yes, but lenses are not quite on my radar right now. If you're thinking of doing a PR then I would advise against it for the time being, as I am not accepting any PRs until my v5 work is done and merged into
Probably, but I really don't remember what they were. But now that we have proper support for higher-kinds the whole implementation could be revisited.
Something like this would be the equivalent to the Haskell defintions: // type Lens s t a b = forall f. Functor f => (a -> f b) -> s -> f t
public record Lens<F, S, T, A, B>(Func<Func<A, K<F, B>>, Func<S, K<F, T>>> runLens)
where F : Functor<F>;
// type Lens s a = Lens s s a a
public record Lens<F, S, A>(Func<Func<A, K<F, A>>, Func<S, K<F, S>>> runLens)
: Lens<F, S, S, A, A>(runLens)
where F : Functor<F>; You could construct them like so: public static Lens<F, S, T, A, B> lens<F, S, T, A, B>(
Func<S, A> get,
Func<S, Func<B, T>> set)
where F : Functor<F> =>
new (afb => s => F.Map(set(s), afb(get(s))));
public static Lens<F, S, A> lens<F, S, A>(
Func<S, A> get,
Func<S, Func<A, S>> set)
where F : Functor<F> =>
new (afb => s => F.Map(set(s), afb(get(s)))); Which is the equivalent of: lens :: (s -> a) -> (s -> b -> t) -> Lens s t a b
lens sa sbt afb s = sbt s <$> afb (sa s) This using the
You need the lenses to be non-value types for that to work, |
Beta Was this translation helpful? Give feedback.
-
Hey, I've noticed that the core library has Lens<A,B> but hasn't implemented Lens<S,T,A,B>.
I've got:
So essentially I want a Lens<X, X, Y, Z>.
I'm considering trying to implement this myself. I'm not super experienced so just checking if there's any obvious reasons why this might be a bad idea. e.g.:
Thanks.
Update:
I wrote a version that copies the existing Lens<A,B> (plus a couple of concat functions)
I assume there's an easy way to define Lens<A,B> in terms of Lens<S,T,A,B> but I can't seem to find it. The following doesn't work
public readonly struct Lens<A, B> : Lens<A, A, B, B> { }
Beta Was this translation helpful? Give feedback.
All reactions