How to convert types with EmojiCode
HenrySaba opened this issue · comments
Emojicode newbie here.
How does one convert:
🔤123🔤 ➡️ test
Into an integer?
Probably best to teach a new Emojicoder "how to fish." I do see this in the docs:
🔢❗️
❗️ 🔢 base 🔢 ➡️ 🍬🔢
This methods tries to construct an integer from this string in the given base. It returns the integer or no value if the string does not match the regular expression [+-]?[0-9a-zA-Z]+ or it does not represent a valid value in the given base.
...but many failed attempts have showed me that I do not yet know how interpret that "declaration." Any help here would be appreciated. Thanks.
Don't worry, jumping into Emojicode can be a bit overwhelming. Let me try to teach you how to fish.
When you have a string 🔤123🔤
it is an instance of the class 🔡. That class is documented here: https://www.emojicode.org/docs/packages/s/1f521.html
The 🔡 class has an instance member 🔢❗️ defined here: https://www.emojicode.org/docs/packages/s/1f521.html#s.class_1f521.1f522-s.vt_1f522. This instance method of the string class converts the value into an integer.
- 🔢 is the name of the method, which in this case just happens to also be the same emoji used as the name of the integer value type.
- ❗️is the mood of the method, in this case imperative.
In general, you will call a method using the instance of that class, in your case, to call 🔢❗️
on the test
variable of type 🔡
, you would do this: 🔢 test❗️
. This is the equivalent of doing something like test.toInteger()
in another language. One snag, 🔢 test❗️
doesn't actually work, because the 🔢❗️
method takes a parameter base
of type 🔢:
DECLARATION
❗️ 🔢 base 🔢 ➡️ 🍬🔢
So in addition to providing the class instance, you also have to provide all the required parameters. The parameters are space-separated after the instance, so to convert it to an integer treating the string as a base-10 value, you would call it as so: 🔢 test 10❗️
.
🔤123🔤 ➡️ test
🔢test 10❗ ➡️ testAsInt
Now that you know how to call the method, you need to understand what it's returning. If you look again at the declaration, it says it returns (➡️) a value of type 🍬🔢. This can be read as "an optional of type integer". There is a good guide on Optional types here: https://www.emojicode.org/docs/reference/optionals.html. Basically what we need to know here is this:
- an optional may be equal to 🤷 (null)
- if you know it's not 🤷, you can unwrap the optional using 🍺
You will likely need to decide what to do if the parsing doesn't work, whether it is throwing an error, or writing a message to the user. Let's write an if statement:
🔢test 10❗ ➡️ intValueMaybe
↪️ intValueMaybe 🙌 🤷 🍇
😀 🔤the value "🧲test🧲" didn't successfully convert to an integer🔤 ❗
🍉 🙅 🍇
🍺intValueMaybe ➡️ intValue
😀 🔤successfully parsed to integer: 🧲 intValue 🧲🔤 ❗
🍉
If you want to make this code reusable you need to wrap it in a class. Here is my final working code:
🏁 🍇
🆕🔰 ❗ ➡️ instance
🌯instance 🔤123🔤❗
🌯instance 🔤one two three🔤❗
🍉
🐇 🔰 🍇
🆕 🍇🍉
📗
Tries to convert the value into a 🔢. If it succeeds, it prints it out;
otherwise prints a message that it wasn't a valid integer.
📗
❗ 🌯 stringValue🔡 🍇
🔢stringValue 10❗ ➡️ intValue
↪️ intValue 🙌 🤷 🍇
😀 🔤the value "🧲stringValue🧲" didn't successfully convert to an integer🔤 ❗
🍉 🙅 🍇
😀 🔤🧲 🍺intValue 🧲🔤 ❗
🍉
🍉
🍉
Running this should yield the result:
123
the value "one two three" didn't successfully convert to an integer
Hope this makes sense and gets you more comfortable with some of the fundamentals of Emojicode. It's really a fun language once you get the hang of it.
WOW! Thanks to both of you! Is there a better location for me to interact more with the Emojicode community?