emojicode / emojicode

๐Ÿ˜€๐Ÿ˜œ๐Ÿ”‚ Worldโ€™s only programming language thatโ€™s bursting with emojis

Home Page:https://emojicode.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Unable to convert strings to integers inside closure

markjreed opened this issue ยท comments

Tried to create a simple comparator to use with ๐Ÿฆ to sort a list of numeric strings in reverse order:

๐Ÿ‡a๐Ÿ”กb๐Ÿ”กโžก๏ธ ๐Ÿ”ข
โ†ฉ๏ธ ๐Ÿ”ขb 10โ—๏ธโž– ๐Ÿ”ขa 10โ—๏ธ
๐Ÿ‰ โžก๏ธ rev

But the compiler complains that " ๐Ÿฌs๐Ÿ”ข does not provide methods.". Where did that ๐Ÿฌ come from?

Assuming that meant they'd been wrapped into Optionals somehow, I tried to unwrap them with ๐Ÿบ, but got "๐Ÿบ can only be used with optionals or error-prone calls."

Not sure what's going on here.

Hey @markjreed , I took a look at your code and the documentation and I think I know what is going on here.

According to the documentation, calling ๐Ÿ”ขโ—๏ธreturns a ๐Ÿฌ๐Ÿ”ข, which means an optional of type number. You can read more about it in the guide on optionals, but essentially this protects the program against the possibility of an invalid value being passed into the function, for example the string ๐Ÿ”คsldkfjsldkj๐Ÿ”ค cannot be parsed as a ๐Ÿ”ข. So after parsing you can compare the result to No Value (๐Ÿคท) then if it's not that you can use the ๐Ÿบ operator to unwrap the optional (๐Ÿฌ) to a ๐Ÿ”ข. Does that make sense?

The only other alternative would be to make the function error-prone and just use the ๐Ÿบ without checking, but I'm not sure you would want to go that route for this example.

Thanks for using Emojicode!

Here is a full working example:

๐Ÿ ๐Ÿ‡
    ๐Ÿ‡a๐Ÿ”กb๐Ÿ”กโžก๏ธ ๐Ÿ”ข
        ๐Ÿ”ขa 10โ— โžก๏ธ aInt?
        ๐Ÿ”ขb 10โ— โžก๏ธ bInt?
        โ†ช๏ธ aInt? ๐Ÿ™Œ ๐Ÿคท ๐Ÿค bInt? ๐Ÿ™Œ ๐Ÿคท ๐Ÿ‡
            ๐Ÿ’ญ They are both invalid, treat them as the same
            โ†ฉ๏ธ 0
        ๐Ÿ‰ ๐Ÿ™…โ†ช๏ธ aInt? ๐Ÿ™Œ ๐Ÿคท ๐Ÿ‡
            ๐Ÿ’ญ a is invalid, put b first
            โ†ฉ๏ธ 1
        ๐Ÿ‰ ๐Ÿ™…โ†ช๏ธ bInt? ๐Ÿ™Œ ๐Ÿคท ๐Ÿ‡
            ๐Ÿ’ญ b is invalid, put a first
            โ†ฉ๏ธ -1
        ๐Ÿ‰ ๐Ÿ™… ๐Ÿ‡
            โ†ฉ๏ธ ๐ŸบbInt? โž– ๐ŸบaInt?
        ๐Ÿ‰
    ๐Ÿ‰ โžก๏ธ rev

    ๐Ÿฟ
        ๐Ÿ”ค123๐Ÿ”ค
        ๐Ÿ”ค321๐Ÿ”ค
        ๐Ÿ”คasdf๐Ÿ”ค
    ๐Ÿ† โžก๏ธ ๐Ÿ–๏ธ๐Ÿ†•list

    ๐Ÿฆlist revโ—

    ๐Ÿ”‚ item list ๐Ÿ‡
        ๐Ÿ˜€itemโ—
    ๐Ÿ‰
๐Ÿ‰

The output is:

321
123
asdf

Personal style note: I like to use the ? character in my variable names when I know they are going to be optionals. That way I remember to unwrap them or handle them properly. Since Emojicode will allow pretty much any non-emoji character (excluding whitespace) in variable names, it works pretty well for me.

Another approach would be to use ๐Ÿญโ— and ๐Ÿฐโ—to no longer have to deal with conversion from string to int in the sorting function:

๐Ÿ ๐Ÿ‡
    ๐Ÿฟ
        ๐Ÿ”ค123๐Ÿ”ค
        ๐Ÿ”ค321๐Ÿ”ค
        ๐Ÿ”คasdf๐Ÿ”ค
    ๐Ÿ† โžก๏ธ list

    ๐Ÿ‡ a๐Ÿ”ก โžก๏ธ ๐Ÿ‘Œ
        ๐Ÿ”ขa 10โ— โžก๏ธ aInt?
        โ†ฉ๏ธ โŽaInt? ๐Ÿ™Œ ๐Ÿคทโ—
    ๐Ÿ‰ โžก๏ธ filterOutInvalidIntStrings
    ๐Ÿญlist filterOutInvalidIntStringsโ— โžก๏ธ onlyIntStrings

    ๐Ÿ‡ a๐Ÿ”ก โžก๏ธ ๐Ÿ”ข
        ๐Ÿ”ขa 10โ— โžก๏ธ aInt?
        โ†ฉ๏ธ ๐ŸบaInt?
    ๐Ÿ‰ โžก๏ธ convertToInt
    ๐ŸฐonlyIntStrings convertToIntโ— โžก๏ธ ๐Ÿ–๏ธ๐Ÿ†•ints

    
    ๐Ÿ‡a๐Ÿ”ขb๐Ÿ”ขโžก๏ธ ๐Ÿ”ข
        โ†ฉ๏ธ b โž– a
    ๐Ÿ‰ โžก๏ธ rev
    ๐Ÿฆints revโ—

    ๐Ÿ”‚ item ints ๐Ÿ‡
        ๐Ÿ˜€๐Ÿ”ค๐Ÿงฒitem๐Ÿงฒ๐Ÿ”คโ—
    ๐Ÿ‰
๐Ÿ‰

The output is:

321
123

Oh, of course. It was complaining about ๐Ÿฌ๐Ÿ”ข, not ๐Ÿฌ๐Ÿ”ก, so it was the result of the ๐Ÿ”ข calls that needed unwrapping, not the parameters. D'oh! Thanks for the help!