-
Notifications
You must be signed in to change notification settings - Fork 7
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
Conversation
Also return clonable iterators.
/// | ||
/// ``` | ||
/// let shortcodes: Vec<_> = emojis::shortcodes().take(3).collect(); | ||
/// assert_eq!(shortcodes, ["algeria", "pouting_face", "waning_crescent_moon"]); |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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())
}
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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?
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? |
Also return clonable iterators.