generalize type of Data.Set.unions from List to Foldable

David Feuer david.feuer at gmail.com
Sun Feb 4 20:27:26 UTC 2018


Anyone but the newest beginner who needs the functionality of unions
or (for Data.Map) unionsWith should be able to write it themselves
with barely a second thought, using foldl'. The newest beginner might
find this a good opportunity to learn about foldl'. In my personal
opinion, the bar is set by Data.Sequence.intersperse; users *could*
write their own efficient version, but I don't expect a user to
necessarily come up with

  intersperse :: a -> Seq a -> Seq a
  intersperse y xs = case viewl xs of
    EmptyL -> empty
    p :< ps -> p <| (ps <**> (const y <| singleton id))

at the drop of a hat.

On Sun, Feb 4, 2018 at 3:17 PM, Henning Thielemann
<lemming at henning-thielemann.de> wrote:
>
> On Sun, 4 Feb 2018, Joachim Breitner wrote:
>
>> Hi,
>>
>> Am Samstag, den 03.02.2018, 20:44 -0500 schrieb David Feuer:
>>>
>>> It is fold, although fold is not so great for lists in this context. It's
>>> also foldl' union Set.empty, which is better for lists, and probably also
>>> for balanced trees. I initially thought that we should surely generalize,
>>> but now another alternative comes to mind: remove. As a containers
>>> maintainer, I believe we should either:
>>>
>>> 1. Generalize as proposed, or
>>> 2. Deprecate and remove.
>>>
>>> I'm currently somewhat in favor of the second option.
>>
>>
>> please don’t remove!
>>
>> …is first reaction. Now I just have to rationalize my gut feeling…
>>
>> I like the readability of it in code, it is more descriptive.
>
>
> Right.
>
> As always, I see no sense in only preserving a most general version of a
> function. Code that consists of nested fold, fmap, (<>) is practically
> unreadable. Only use that if you really want generic code.
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
>


More information about the Libraries mailing list