snoyberg / mono-traversable

Type classes for mapping, folding, and traversing monomorphic containers
152 stars 63 forks source link

add function fmapNonNull #150

Closed cdepillabout closed 6 years ago

cdepillabout commented 6 years ago

When I am working with NonNull in a new codebase, I often reimplement the following function:

fmapNonNull
  :: (Functor f, MonoFoldable (f b))
  => (a -> b) -> NonNull (f a) -> NonNull (f b)
fmapNonNull f = impureNonNull . fmap f . toNullable

This lets you call fmap on the underlying container in a NonNull. Here's an example:

> let nonNullable = impureNonNull [1,2,3] :: NonNull [Int]
> fmapNonNull show nonNullable :: NonNull [String]
["1","2","3"]

This is not possible with omap, because it does not let you change the type of the element in the underlying container.

I think this fmapNonNull would be widely useful, because NonNull is often used with Functors like [] or Seq.

This function could go into the Data.NonNull module.

Here are some possible names for this function:

I think any of those four names would be suitable.

If the maintainers agree that this could go into Data.NonNull, I will send a PR to include it.

snoyberg commented 6 years ago

I have no objection, looks sensible. mapNonNull seems best to be.

On Wed, Dec 13, 2017, 5:15 PM Dennis Gosnell notifications@github.com wrote:

When I am working with NonNull in a new codebase, I often reimplement the following function:

fmapNonNull :: (Functor f, MonoFoldable (f b)) => (a -> b) -> NonNull (f a) -> NonNull (f b) fmapNonNull f = impureNonNull . fmap f . toNullable

This lets you call fmap on the underlying container in a NonNull. Here's an example:

let nonNullable = impureNonNull [1,2,3] :: NonNull [Int]> fmapNonNull show nonNullable :: NonNull [String] ["1","2","3"]

This is not possible with omap https://hackage.haskell.org/package/mono-traversable-1.0.5.0/docs/Data-MonoTraversable.html#t:MonoFunctor, because it does not let you change the type of the element in the underlying container.

I think this fmapNonNull would be widely useful, because NonNull is often used with Functors like [] or Seq.

This function could go into the Data.NonNull module.

Here are some possible names for this function:

  • fmapNonNull
  • fmapNonNullable
  • mapNonNull
  • mapNonNullable

I think any of those four names would be suitable.

If the maintainers agree that this could go into Data.NonNull, I will send a PR to include it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/snoyberg/mono-traversable/issues/150, or mute the thread https://github.com/notifications/unsubscribe-auth/AADBB-dEqSLi7MXIfkjz1ZFh0OQ-T7JVks5s_-n4gaJpZM4RAswE .

cdepillabout commented 6 years ago

Added in #151.