spebbe / dartz

Functional programming in Dart
MIT License
755 stars 59 forks source link

MonadLenses #29

Closed modulovalue closed 4 years ago

modulovalue commented 4 years ago

Hey @spebbe,

1) Do you think there's a way, with the current hierarchy or even at all in Dart (especially now with static extension methods), for a MonadLens to exist that could become the superclass of OptionLens/EitherLens and other monads?

2) Have you ever thought about having 'Left/Right Monad lenses'. e.g. an OptionLens where the getter always results in a value and the setter might not and vice versa.

  final Function1<AIn, BOut> _getter;
  final Function2<AIn, BIn, Option<AOut>> _setter;

3) I'm not sure if this concept even makes sense in pure FP, but do you know of a name or a concept for a lens that has the following structure:

  final Function1<void, BOut> _getter;
  final Function2<AIn, BIn, void> _setter;

For the lack of a better name I'm calling them ClassLenses because they just represent a simple property getter and setter. Do they look familiar to you? I found that they are quite useful if combined with other concepts and I'd love to read more about them, but it's hard to google for something abstract.

These are not necessarily suggestions for dartz, I'd just really appreciate your Input or any keywords related to that.

Thanks

spebbe commented 4 years ago

Hi @modulovalue!

1. I did try to abstract a bit when I originally added OptionLens and EitherLens, but I think my attempt just grew into a hairy ball of casts and hidden assumptions that weren't reflected in the types. It is possible to get to work, but just like with MonadTrans (higher-kinded type parameterized on other higher-kinded types...), I felt that it got intolerably messy and inexact.

It is very possible that it can be expressed in a more elegant way that I haven't thought of though! I'll dig up the previous attempt and post it here -- maybe inspiration strikes?

2. That sounds useful and looks like it would fit onto the existing Lens contract! Would be fun to try out and see how they behave -- do you know if these things have a standardized name in Haskell/Monocle/similar?

3. Not sure I follow -- would these be mutating? If so, I don't think they would compose in any meaningful way and therefore not be very useful, but I'm probably just missing the point :-)

Also... Please excuse the shameless recruiting, but you have some really cool music related projects going on. Shouldn't you be working for Soundtrap @ Spotify (https://www.soundtrap.com)? We build a DAW on the web using Dart and have some cool Flutter projects in the works as well.

modulovalue commented 4 years ago

do you know if these things have a standardized name in Haskell/Monocle/similar?

I barely have any experience with real FP languages. dartz is the reason why I got into FP in the first place and I still have to build up my FP toolset to draw from. I'm learning everything as I go when I feel that I'm missing an abstraction. Monocle sound promising, I'll have to take a look.

Not sure I follow -- would these be mutating? If so, I don't think they would compose in any meaningful way and therefore not be very useful

It's a WIP, but I think I found a good use for them to connect the concept of Signals from FRP with lenses from FP. A MonadLens is all that I'm missing, but I didn't want to fork your Option and Either just to change the hierarchy, I was hoping there was something that I'm missing. Let's see how it goes.

you have some really cool music related projects going on. Shouldn't you be working for Soundtrap @ Spotify (https://www.soundtrap.com)? We build a DAW on the web using Dart

Thank you. That does sound intriguing, but I don't think that a company would let me explore concepts the way I do now, especially if it's related to architecture and abstract ideas.

You answered my questions so I'll close this. Tag me here if you find your monad-lens attempt, I'd really like to see If I can and understand why It didn't work out.