Emojicode Documentation 0.5
Guides Language Reference Package Index

The s package

This chapter provides a very brief overview of the s package and its most important classes.

The s package can be compared to whatโ€™s called standard library in other programing languages. It provides some of the most important classes and value types to write meaningful programs.

Hint

This chapter only introduces the most important classes although there are many more.

You can browse the whole API of the s package here.

๐Ÿ‘Œ ๐Ÿš‚ ๐Ÿš€ ๐Ÿ”ฃ

Youโ€™ve already got to know the types ๐Ÿ‘Œ ๐Ÿš‚ ๐Ÿš€ ๐Ÿ”ฃ in a previous chapter. These types are, of course, also defined in the s package.

๐Ÿ”ก Strings

In Emojicode a string is an ordered immutable collection of symbols (or easily speaking a text). Strings are represented by the ๐Ÿ”ก class which represent a collection of ๐Ÿ”ฃ, a primitive type representing a single Unicode code point.

String Literals

You can include strings in your code by surrounding the characters by a pair of ๐Ÿ”ค:

๐Ÿฆ aString ๐Ÿ”คThis is a string.๐Ÿ”ค

You can of course also create an empty string:

๐Ÿฆ emptyString ๐Ÿ”ค๐Ÿ”ค

Such a string is 0 symbols long and doesnโ€™t have any value.

Obviously, you cannot just include the ๐Ÿ”ค symbol in a string literal as it would be understood as the ending of the string. You can however escape the ๐Ÿ”ค using the โŒ.

๐Ÿฆ aboutStringLiterals ๐Ÿ”คA string literal begins with a โŒ๐Ÿ”ค symbol.๐Ÿ”ค

If you want to include the โŒ symbol in a string literal you will need to escape it as well:

๐Ÿฆ theEscapeSymbol ๐Ÿ”คThe escape character is โŒโŒ (red cross).๐Ÿ”ค

The escape character can also be used to produce the following characters:

All other combinations of a โŒ and another character lead to a compiler error.

Formally, the syntax is:

string-literal โŸถ ๐Ÿ”ค [string-literal-characters] ๐Ÿ”ค
string-literal-characters โŸถ string-literal-character |  string-literal-character string-literal-characters
string-literal-character โŸถ unicode except ๐Ÿ”ค except โŒ | string-escape-sequence
string-escape-sequence โŸถ โŒ string-escape-tail
string-escape-tail โŸถ n | t | r | e

Comparing Strings

If you want to determine whether to strings represent the same value, you can use the equality method ๐Ÿ˜›.

๐Ÿ˜› ๐Ÿช๐Ÿ”คStraw๐Ÿ”ค ๐Ÿ”คberries๐Ÿ”ค๐Ÿช ๐Ÿ”คStrawberries๐Ÿ”ค
Hint

Note that the example above used a method call. ๐Ÿ˜› is a method defined in the ๐Ÿ”ก class. Youโ€™ll learn more about classes and methods in the next chapter. For now, just remember that methods are called by placing an emoji before the object on which the method should be called.

Concatenating Strings

To add two or more strings together (concatenate) you should use the ๐Ÿช language struct. Wrap all strings you want to concatenate between two ๐Ÿช. They will get concatenated in order of appearance into one string.

๐Ÿฆ string1 ๐Ÿ”คHello ๐Ÿ”ค
๐Ÿฆ string2 ๐Ÿ”คmy dear๐Ÿ”ค

๐Ÿ˜€ ๐Ÿช string1 string2 ๐Ÿ”ค World!๐Ÿ”ค ๐Ÿช

๐Ÿชs are the most efficient way of concatenating strings as the compiler can ๐Ÿชoptimize the concatenation. Please note that ๐Ÿชs are not methods but part of the language.

The syntax formally is:

concatenate-literal โŸถ ๐Ÿช concatenate-expressions ๐Ÿช
concatenate-expressions โŸถ concatenate-expression | concatenate-expression concatenate-expressions
concatenate-expression โŸถ expression except concatenate-literal

String Pooling

At compile time all strings will be pooled. This means that each string literal with the same value occurs only once in the bytecode file and at runtime equal strings created from string literals will be represented by the same object.

Consider the following example. While the first comparison of object references will evaluate to true, the second one wonโ€™t.

๐Ÿ˜œ ๐Ÿ”คStrawberries๐Ÿ”ค ๐Ÿ”คStrawberries๐Ÿ”ค ๐Ÿ‘ดtrue
๐Ÿ˜œ ๐Ÿช๐Ÿ”คStraw๐Ÿ”ค ๐Ÿ”คberries๐Ÿ”ค๐Ÿช ๐Ÿ”คStrawberries๐Ÿ”ค ๐Ÿ‘ดfalse
Caution

Never use ๐Ÿ˜œ to compare strings. Use ๐Ÿ˜› instead.

String Mutablility

Strings are immutable. This means that you canโ€™t modify any string. You can however call methods on strings that return modified copies of the orginal string, but those themselves are then immutable again.

๐Ÿจ Lists

Lists are ordered mutable collections of values. They are represeneted by the ๐Ÿจ class. Their sizes are theoretically unlimited, but practically hardware and the fact that Emojicodeโ€™s integers have an upper bound makes them limited.

Lists are optimized for fast by index access. You can access by index, pop and append in O(1).

List indexing starts at 0, as in C or Java. A negative index is assumed to be relative to the end of the list โ€” that is, an index of -1 indicates the last element of the array, -2 is the next to last element in the array, and so on.

The ๐Ÿจ Type

The ๐Ÿจ class is generic and needs to know of what type the values are. If you use the class name you will also have to specify this generic argument. To specify a list that can hold strings you would write:

๐Ÿจ๐Ÿš๐Ÿ”ก

If you created a list like this

๐Ÿฎ list ๐Ÿ”ท ๐Ÿจ๐Ÿš๐Ÿ”ก ๐Ÿธ

you would only be able to add strings to the list and you will always get back strings from the list. (The syntax used above will be discussed in Classes.)

List Literals

You can create a list just by listing the values for the list between ๐Ÿจ and ๐Ÿ†. The example below creates a list with the values 14, 67, 2434.

๐Ÿจ 14 67 2434 ๐Ÿ†

The compiler will try to infer the generic type argument for the list.

list-literal โŸถ ๐Ÿจ [expressions] ๐Ÿ†
expressions โŸถ expression | expression expressions

๐Ÿฏ Dictionaries

Dictionaries can be used to assign values to string keys. The size of a dictionary is theoretically unlimited.

The ๐Ÿฏ Type

The ๐Ÿฏ class is also generic and requires you to specify the type of the values. For instance if the values were booleans the type would be written like this:

๐Ÿฏ๐Ÿš๐Ÿ‘Œ

Dictionary Literals

The shortcut syntax to create a dictionary is:

dictionary-literal โŸถ ๐Ÿฏ [kv-pairs] ๐Ÿ†
kv-pairs โŸถ kv-pair kv-pairs | kv-pair
kv-pair โŸถ key expression
key โŸถ expression

key must be a string. The compiler will try to infer the generic type argument for the dictionary.

Hereโ€™s an example for a dictionary that associates artists with their birthplaces:

๐Ÿฏ
  ๐Ÿ”คAaron Copland๐Ÿ”ค ๐Ÿ”คBrooklyn๐Ÿ”ค
  ๐Ÿ”คMichael Jackson๐Ÿ”ค ๐Ÿ”คGary๐Ÿ”ค
  ๐Ÿ”คFalco๐Ÿ”ค ๐Ÿ”คVienna๐Ÿ”ค
๐Ÿ†

โฉ Ranges

Emojicode supports a range type called โฉ. A range is an immutable sequence of numbers, or more strictly speaking of integers.

A range is always defined by three values: start, stop and step.

Every number f(x) = start + x * step that matches the constraint start โ‰ค f(x) < stop is an element of the range. If step is negative the constraint stop < f(x) โ‰ค start applies instead.

Ranges can be created by using the shortcut syntax, which is depending on your needs either

range-literal โŸถ range-literal-with-step range-literal-without-step
range-literal-with-step โŸถ โญ start stop step
start โŸถ expression
stop โŸถ expression
step โŸถ expression

or

range-literal-without-step โŸถ โฉ start stop

The latter uses 1 as step value if start is less than stop, otherwise -1 is used as step value. If you provide 0 as step value step will be set to an appropriate value as described before.

Ranges can be very helpful in combination with ๐Ÿ”‚ if you need to repeat something for a specific number of times:

๐Ÿ”‚ i โญ 0 10 2 ๐Ÿ‡
  ๐Ÿ˜€ ๐Ÿ”ก i 10
๐Ÿ‰
๐Ÿ”‚ i โฉ 0 10 ๐Ÿ‡
  ๐Ÿ˜€ ๐Ÿ”ก i 10
๐Ÿ‰
๐Ÿ”‚ i โฉ 10 0 ๐Ÿ‡
  ๐Ÿ˜€ ๐Ÿ”ก i 10
๐Ÿ‰
๐Ÿ”‚ i โญ 100 -10 -10 ๐Ÿ‡
  ๐Ÿ˜€ ๐Ÿ”ก i 10
๐Ÿ‰

The result of running these examples would be:

0
2
4
6
8
0
1
2
3
4
5
6
7
8
9
10
9
8
7
6
5
4
3
2
1
100
90
80
70
60
50
40
30
20
10
0
โ† Previous Next โ†’
Want to improve this page? You can edit the source on GitHub and send us a pull request!