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

Dict.first, Dict.last, Set.first, Set.last #844

Closed
wants to merge 1 commit into
base: master
from

Conversation

Projects
None yet
5 participants
@billstclair

billstclair commented Mar 15, 2017

I have an application that needs to process the elements of a Set one at a time. Currently, the only way to get an element from a set is to convert it to a list and take an element of the list. It's quicker and conses less to just get the first or last element out of the internal storage, and use that.

Since Set is based on Dict, I added first and last to both.

My current code:

popSet : Set comparable -> Maybe (comparable, Set comparable)
popSet set =
    case Set.toList set of
        [] ->
            Nothing
        res :: _ ->
            Just (res, Set.remove res set)

With these changes:

popSet : Set comparable -> Maybe (comparable, Set comparable)
popSet set =
    case Set.first set of
        Nothing ->
            Nothing
        Just res ->
            Just (res, Set.remove res set)
@process-bot

This comment has been minimized.

Show comment
Hide comment
@process-bot

process-bot Mar 15, 2017

Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

process-bot commented Mar 15, 2017

Thanks for the pull request! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@Skinney

This comment has been minimized.

Show comment
Hide comment
@Skinney

Skinney May 9, 2017

Contributor

You'll have more luck trying to add this to elm-community/dict-extra.

Contributor

Skinney commented May 9, 2017

You'll have more luck trying to add this to elm-community/dict-extra.

@billstclair

This comment has been minimized.

Show comment
Hide comment
@billstclair

billstclair May 9, 2017

@Skinney I currently use in my code an implementation based on Set.toList and Dict.toList. The purpose of this patch is to make that faster with no memory allocation by using the internals of the opaque Set and Dict types. That can't be done in elm-community/dict-extra.

billstclair commented May 9, 2017

@Skinney I currently use in my code an implementation based on Set.toList and Dict.toList. The purpose of this patch is to make that faster with no memory allocation by using the internals of the opaque Set and Dict types. That can't be done in elm-community/dict-extra.

@myrho

This comment has been minimized.

Show comment
Hide comment
@myrho

myrho Oct 10, 2017

+1

The elm-community/intdict also has this already. It's called findMin and findMax there.

myrho commented Oct 10, 2017

+1

The elm-community/intdict also has this already. It's called findMin and findMax there.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz Mar 7, 2018

Member

At some point we will do a review of the *-extras and bring things in. These seem like good candidates because of their asymptotics, but there are other Dict changes that mean it will be easier to just do the changes by hand, rather than through PR.

Member

evancz commented Mar 7, 2018

At some point we will do a review of the *-extras and bring things in. These seem like good candidates because of their asymptotics, but there are other Dict changes that mean it will be easier to just do the changes by hand, rather than through PR.

@evancz evancz closed this Mar 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment