ndmitchell / extra

Extra Haskell functions
Other
94 stars 37 forks source link

Make replace not be partial #99

Closed josephcsible closed 1 year ago

josephcsible commented 1 year ago

https://github.com/ndmitchell/extra/blob/45249bdff87dad47a8e52bfa3fa90c75bc447917/src/Data/List/Extra.hs#L560

I don't really like that this function is partial. Two ideas on what we could do about it:

  1. Make a new function with type Eq a => NonEmpty a -> [a] -> [a] -> [a] and deprecate this one in favor of it
  2. Change this function to do something sensible instead of erroring when the search string is empty, maybe something like this (e.g., replace "" "x" "123" would be "x1x2x3x"):
replace [] to [] = to
replace [] to (x:xs) = to ++ x : replace [] to xs

Do you like either of these ideas?

ndmitchell commented 1 year ago

The problem with non-empty is that usually this is used on strings, and making a string explicitly a non-empty is a bit of a pain. The problem with giving it a sensible behavior is that it might surprise people. Looking at other languages:

So I guess the behavior you describe makes sense, and I'd be fine with changing it to never error and do the intersperse.

ndmitchell commented 1 year ago

Actually, I'll do it.

ndmitchell commented 1 year ago

Released as 1.7.13

josephcsible commented 1 year ago

Thanks!