ndmitchell / extra

Extra Haskell functions
Other
93 stars 37 forks source link

Add replaceRight and replaceLeft functions #89

Closed mrcjkb closed 2 years ago

mrcjkb commented 3 years ago

I find the replaceLeft function useful for "translating" error handling from a library's domain to my own domain (to encapsulate the library's domain).

Example (pseudo code):

ndmitchell commented 3 years ago

Thanks for the patch. I wonder how useful these functions are? Wouldn't you usually want to replace the left by wrapping the underlying error, so that the original error message is still available? I note that replaceRight is already available as $> - which I'm generally a fan of more specialised functions, things like $> are pretty universal, so a little less concerning.

mrcjkb commented 3 years ago

I wonder how useful these functions are? Wouldn't you usually want to replace the left by wrapping the underlying error, so that the original error message is still available?

In most cases, I would agree. In some cases, I am replacing an error that has a no-arg constructor with my own error that has a no-arg constructor. There is a direct mapping between SpecialError and a specific error constructor, (so no information is lost by not wrapping the error) and the sole purpose is encapsulation of the libraries I am using. I could use SpecialError a with a generic argument to encapsulate the library's error from my API, but in this case it would just lead to more code and a less simple API without any benefits.

Another use case would be if I want to log the original Left and then replace it with a simpler error message that is later printed to stderr in a command line app.

I personally have not had any use for replaceRight. I just added it because the module has a Right function for each Left function and vice versa.

ndmitchell commented 2 years ago

Thanks for the contribution. Alas, I think these functions probably don't being in extra just yet, for 4 interlinked reasons: