Proposal for containers: Add 'pop' function to Data.Map

Oleg Grenrus oleg.grenrus at iki.fi
Sun Dec 6 22:33:56 UTC 2020


so it's uninsert (re uncons).

- Oleg

On 6.12.2020 21.41, Simon Jakobi via Libraries wrote:
> Regarding the type signature:
>
> pop :: Ord k => k -> Map k a -> (Maybe a, Map k a)
>
> I think it might be better to be consistent with similar functions like
>
> minView :: Map k a -> Maybe (a, Map k a)
>
> and
>
> uncons :: [a] -> Maybe (a, [a])
>
> Therefore the type should be
>
> pop :: Ord k => k -> Map k a -> Maybe (a, Map k a)
>
> ---
>
> I like the "pop" name though – I think the analogy to stacks is pretty obvious.
>
> ---
>
> Like Andreas Abel I believe, that if there is a good, simple
> implementation, it might be a good first step to simply document the
> implementation as an example.
>
> If there's much demand for exporting the function or if a fast
> implementation is more complex, we can still enhance the API later on.
>
>
> Am So., 6. Dez. 2020 um 17:20 Uhr schrieb Martijn Bastiaan via
> Libraries <libraries at haskell.org>:
>> Hi all,
>>
>> Proposal:
>>
>>    * Add `pop` and `popWithDefault` to `Data.Map` and `Data.IntMap`.
>>    * See https://github.com/haskell/containers/pull/757 for exact definition
>>
>> Why:
>>
>>    * They're useful functions I expected to be in `Data.Map` and
>>      `Data.IntMap`. (This might be influenced by the fact that
>>      they're defined on Python's `dict`.)
>>
>>    * Their implementations (~ `updateLookupWithKey (\_ _ -> Nothing)`)
>>      are harder to parse than a simple `pop`, which should help Haskell
>>      codebases become a bit cleaner :).
>>
>>    * Their implementations are a bit non-obvious. My first instinct was
>>      to write `(Map.lookup ..., Map.delete ...)`, which would have done
>>      two traversals. Having "properly" implemented functions in the lib
>>      would prevent people from writing their own suboptimal ones.
>>
>> Details and implementation:
>>
>>    * https://github.com/haskell/containers/pull/757
>>
>> Kind regards,
>> Martijn Bastiaan
>> _______________________________________________
>> Libraries mailing list
>> Libraries at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries


More information about the Libraries mailing list