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 function to iterate over all shortcodes #15

Closed
wants to merge 1 commit into from

Conversation

boxdot
Copy link

@boxdot boxdot commented Mar 5, 2024

Also return clonable iterators.

Also return clonable iterators.
///
/// ```
/// let shortcodes: Vec<_> = emojis::shortcodes().take(3).collect();
/// assert_eq!(shortcodes, ["algeria", "pouting_face", "waning_crescent_moon"]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test will break every time we update gemoji because the map is not ordered.

/// ```
#[inline]
pub fn shortcodes() -> impl Iterator<Item = &'static str> + Clone {
crate::gen::shortcode::MAP.keys().into_iter().copied()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will include duplicates, is that the intention? E.g. :dolphin: and :flipper: both refer to the dolphin emoji.

Alternatives:

This includes only the first shortcode, and the order is more stable.

pub fn shortcodes() -> impl Iterator<Item = &'static str> + Clone {
    crate::gen::EMOJIS
        .iter()
        .filter_map(|emoji| emoji.shortcode())
}

This includes all shortcodes and the order is more stable.

pub fn shortcodes() -> impl Iterator<Item = &'static str> + Clone {
    crate::gen::EMOJIS
        .iter()
        .flat_map(|emoji| emoji.shortcodes())
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention is to be able to see all possible shortcodes to pick one based on a substring. The stability of the order is technically not important. The flatmap approach actually works quite well considering that there are 1898 emojis and 1913 shortcodes in total. I was concerned about performance but since the numbers are almost the same it does not matter.

@@ -515,12 +515,25 @@ impl Group {
/// assert_eq!(faces, ["😀", "😃", "😄", "😁", "😆"]);
/// ```
#[inline]
pub fn iter() -> impl Iterator<Item = &'static Emoji> {
pub fn iter() -> impl Iterator<Item = &'static Emoji> + Clone {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possibly can add Clone to all the other returned iterators?

@boxdot
Copy link
Author

boxdot commented Mar 6, 2024

Thanks for the review. Considering the flatmap approach, it looks like there is no need to add this function. WDYT about just closing this PR?

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 this pull request may close these issues.

2 participants