Skip to content
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

Add methods to fetch the first or last item in a Map #35

Closed
larry0x opened this issue Apr 19, 2023 · 5 comments · Fixed by #42
Closed

Add methods to fetch the first or last item in a Map #35

larry0x opened this issue Apr 19, 2023 · 5 comments · Fixed by #42
Milestone

Comments

@larry0x
Copy link
Contributor

larry0x commented Apr 19, 2023

I think something like this would be useful:

// pseudocode
impl<K, V> Map<K, V>
where
    K: PrimaryKey + KeyDeserialize,
    V: Serialize + Deserialize,
{
    /// Return the first item in the map when items are ordered by key in the specified order.
    /// None if the map is empty.
    pub fn first(storage: &dyn Storage, order: Order) -> StdResult<(<K as KeyDeserialize>::Output, V)> {
        // ...
    }

    pub fn last(storage: &dyn Storage, order: Order) -> StdResult<(<K as KeyDeserialize>::Output, V)> {
        // ...
    }
}
@chipshort
Copy link
Collaborator

Would this be different from the following?

let first = MAP
    .range(&deps.storage, None, None, Order::Ascending)
    .next();

let last = MAP
    .range(&deps.storage, None, None, Order::Descending)
    .next();

@larry0x
Copy link
Contributor Author

larry0x commented Apr 19, 2023

Would this be different from the following?

let first = MAP

    .range(&deps.storage, None, None, Order::Ascending)

    .next();



let last = MAP

    .range(&deps.storage, None, None, Order::Descending)

    .next();

Yeah this is in fact how I would implement it

@webmaster128
Copy link
Member

Why do you need order: Order in this API? Can't first just be the first and last be the last in ascending order and that's it?

@larry0x
Copy link
Contributor Author

larry0x commented Jun 7, 2023

Why do you need order: Order in this API? Can't first just be the first and last be the last in ascending order and that's it?

True, we don't need order here

@webmaster128 webmaster128 added this to the 1.1 milestone Jun 7, 2023
@webmaster128
Copy link
Member

Done in #42. Will be released as partof 1.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants