Dealing with emoji in Clojure
Generally, I hate emoji and try to avoid them everywhere I could. Those colored faces look dull to me comparing to a good old text smile. But still, emoji might be helpful replacing icons with them. When you need a globe, a mail envelope or a flight sign, putting a proper emoji could be a fast and good enough solution.
After long Python experience, I though Java supports long unicode literals started with capital U and two bytes as follows (Python version):
>>> print len(u"\U0001F535") # prints 2
Surprisingly, it doesn’t. But I needed to put a blue circle sign that’s
got U+1F535
number. So how should I turn that number into a string?
After googling for a while, I’ve done with a short Clojure function:
(defn unicode-to-string
"Turns a hex unicode symbol into a string.
Deals with such long numbers as 0x1F535 for example."
[code]
(-> code Character/toChars String.))
Usage example:
Adding it into business logic:
(let [caption "Some important feature"
is-on? (get-feature-state)
sign (if is-on?
(unicode-to-string 0x1F535) ;; blue circle
(unicode-to-string 0x26AA))] ;; white circle
(str sign \space caption))
Depending on whether the feature was enabled or not, the result message will have either a blue (active) or white (inactive) circle in front of it.
Нашли ошибку? Выделите мышкой и нажмите Ctrl/⌘+Enter
mr_progo, 18th Oct 2017, link
Icons have no business being in an effing font or a charset standard, but as they nonetheless are, I also use them in my projects out of sheer laziness.
One point about using color balls in web apps though: not every browser and OS supply the actual icons for emoji glyphs. When the original glyphs are used, they have no color. Just a heads up with using the color and not the shape to signal a value.
Ivan Grishaev, 18th Oct 2017, link , parent
I agree with your points. However, the result looks nice when rendering iCalendar file. Both Google and iOS calendars display those emoji as well.
mr_progo, 18th Oct 2017, link , parent
I hear you. If it works for you then it works obviously. I just meant that if one builds a web app for larger audience then the icons may not be trusted
Olim Saidov, 29th Jul 2018, link
Why not to put emojis right into the source code?
Tim Robinson, 22nd Jul 2022, link
Some emoji use multiple code points (e.g. female police officer is 1F46E - 200D - 2640 - FE0F) for these you can just put the integer values in an array, then map through your unicode-to-string function and finally `apply str` to concatenate them all together