0

Given a functor and given a specific way you want to unwrap its value , is there a predefined typeclass that you should implement to unwrap it?

For example given a type: data X = X Int Int where can i specialize the unwrapping of a Maybe X?

Lets say i have a a = Just (X 2 3) and i want to unwrap it the in a custom way:

(Just 2 3)-> 2^3
Nothing -> 0

But i also want to use the same method/interface to unwrap another type like a Maybe (Int,Int) like:

Just (x, y) -> x + y
Nothing -> 0

The question is more about organisation:

Is there a typeclass method where i can implement for unwrapping a Maybe X,Maybe Y, Either Z, Either K, etc.?

Or do i have to provide ungrouped (not a typeclass method instance) methods for each type that i want to unwrap from the functor?

hnefatl
  • 5,860
  • 2
  • 27
  • 49
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • I think you could use the `Monad` class to handle the "wrapped" values with `>>=` – jneira Nov 05 '18 at 08:21
  • 1
    I really don't understand what you mean by “unwrap” here. Seems that your two examples do something completely different. Typeclasses are for when you're doing something that can be understood as a special case of some more general, abstract concept. A typeclass for which you can't come up with _laws_ is usually not worth it. So, I'd say you should probably go with separate, differently named functions. But perhaps I'm wrong and your examples just aren't representative of what you're really trying to do. – leftaroundabout Nov 05 '18 at 08:31
  • By `unwrap` i mean to `extract` the `value` from the `context`. **E.g:** `Just 1 -> 1 `
    What i mean is where do you place the code responsible for `extracting` the values from your functors?
    – Bercovici Adrian Nov 05 '18 at 08:38
  • 1
    But you examples don't seem to just “extract” values, but perform nontrivial computations on them. Please give a realistic example. – leftaroundabout Nov 05 '18 at 08:40
  • 2
    You generally can't do that: https://stackoverflow.com/a/51620647/126014 – Mark Seemann Nov 05 '18 at 08:40
  • So for example i am asking how would you place code that deals with a `Maybe Int`, a `Maybe Double` and a `Maybe Bool`. I want different behaviours for each type and `Maybe` value.Is there a place where i can treat all 3X2 cases ( 3 types * 2 (Just and Nothing for each) ).Is there a typeclass method where you can write all the code ? – Bercovici Adrian Nov 05 '18 at 08:44
  • 1
    Maybe the other answer could be useful: https://stackoverflow.com/a/51619859/49554 `extract :: Comonad w => w a -> a` – jneira Nov 05 '18 at 09:20
  • I see so from the thread..i have to write my methods in order to `extract` the values from the context given a `functor`,and the grouping of the methods is done by me in which way it seems to have most reason. – Bercovici Adrian Nov 05 '18 at 09:59
  • 1
    If it's only for `Maybe` and `Either`, you could write a `eitherToMaybe :: Either a b -> Maybe b` function and then use `fromMaybe` in both cases. Usually, though, one can use `fmap` or monadic combinators to "lift" the operation to the functor instead of "extracting" the (possibly missing) value. – chi Nov 05 '18 at 10:26
  • You might also be looking for functions like `maybe :: b -> (a -> b) -> Maybe a -> b` and `either :: (a -> c) -> (b -> c) -> Either a b -> c`. – chepner Nov 05 '18 at 16:09
  • 1
    Why not just use pattern matching directly? Your first example would be `case a of Just (X 2 3) -> 2^3 ; Nothing -> 0` (Note that this pattern match, as-is, doesn't cover all cases. It can cover them though with a small modification). – David Young Nov 05 '18 at 18:11

0 Answers0