purescript-newtype icon indicating copy to clipboard operation
purescript-newtype copied to clipboard

Some traversey type thing

Open garyb opened this issue 7 years ago • 3 comments

So I came up with this thing:

what
  :: forall f g t a b
   . Functor f
  => Functor g
  => Newtype t b
  => (b -> t)
  -> ((g b -> g t) -> a -> f t)
  -> a
  -> f b
what _ f = map unwrap <<< f (map wrap)

I was thinking about calling it traverseF, but that's not really right at all aside from superficially having the same shape for the value being operated on and result.

The use case is motivated by @matthewleon's semigroups, for example, the First semigroup can replace the current First monoid provided by Maybe, but it works in a different way, you'd use it something like this:

map (un First) $ foldMap1 (map First) =<< NEA.fromArray [Nothing, Just "foo", Just "bar", Nothing]

Or with the combinator:

what First foldMap1 =<< NEA.fromArray [Nothing, Just "foo", Just "bar", Nothing]

So it's an ala kind of thing, but kinda traverse-y too :smile: It's part way between ala (map unwrap (f wrap) and alaF (map unwrap <<< f <<< map wrap). alaT? :wink:

garyb avatar Apr 13 '18 02:04 garyb

Or maybe it shouldn't exist at all?

garyb avatar Apr 13 '18 02:04 garyb

I was thinking, when I wrote the example code for using the First semigroup, that it would benefit from something like this. Will have a closer look later.

matthewleon avatar Apr 13 '18 11:04 matthewleon

I think the code shown here can be written differently now that we have coerce.

JordanMartinez avatar Dec 04 '21 05:12 JordanMartinez