Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offer safe variants for the RIO.List.Partial functions in the RIO.List module #82

Closed
roman opened this issue Apr 9, 2018 · 5 comments
Closed

Comments

@roman
Copy link
Contributor

roman commented Apr 9, 2018

Iterating over the work from #43, what do you think about adding all the functions from RIO.List.Partial with a safe prefix in the RIO.List module?

There is no clear replacement for the functions in RIO.List.Partial, and I would avoid them as much as possible if I can.

@roman roman changed the title Offer safe functions for RIO.List.Partial functions on RIO.List Offer safe variants for the RIO.List.Partial functions in the RIO.List module Apr 9, 2018
@snoyberg
Copy link
Collaborator

In principle: I'm OK with this approach. I'm not convinced that safe will be the right prefix in all cases. For example: maximum would probably be better replaced by maximumMay or maximumMaybe to match existing naming conventions IMO.

@roman
Copy link
Contributor Author

roman commented Apr 12, 2018

That is fair, honestly, my intent was not to strongly advocate names, may I create a pull request and we revisit the names as we go?

@mitchellwrosen
Copy link

FYI: scanl1, scanr1 aren't partial

@snoyberg
Copy link
Collaborator

snoyberg commented Jun 6, 2018

Implemented in #96

@snoyberg snoyberg closed this as completed Jun 6, 2018
@akhra
Copy link
Collaborator

akhra commented Jun 6, 2018

Late entry, but perhaps relevant... yesterday I had to switch some lists over to vectors so I could use statistics operations on them. I had been using some of the (list-specific) *Def functions from safe, and needed to replicate those; and some of the statistics operations I wanted were partial on empty vectors, too, which needed fixing. I wound up writing this:

defaulting :: Foldable f => b -> (f a -> b) -> f a -> b
defaulting d f xs
  | null xs   = d
  | otherwise = f xs

So now I have e.g. defaulting 0 Vector.maximum. The *Maybe pattern is a trivial special case:

mayhap :: Foldable f => (f a -> b) -> f a -> Maybe b
mayhap f = defaulting Nothing (Just . f)

Are there any particular downsides to this technique? It seems far more elegant than a slew of specialized, type-specific functions.

Edit: https://hackage.haskell.org/package/safe-foldable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants