Skip to content

Highlight code #3

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

Merged
merged 2 commits into from
Mar 6, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _includes/top.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>{{ page.title }}</title>
<link rel="stylesheet" type="text/css" href="/css/style.css">
<link rel="stylesheet" type="text/css" href="/css/syntax.css">
<link rel="shortcut icon" href="/images/logo/drop.png">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link rel="stylesheet" id="font-bitter-css" href="http://fonts.googleapis.com/css?family=Bitter&amp;ver=1" type="text/css" media="screen">
Expand Down Expand Up @@ -49,4 +50,4 @@ <h2 id="site-description">
</h2>
</div>

<div id="main">
<div id="main">
60 changes: 60 additions & 0 deletions css/syntax.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.highlight { background: #ffffff; }
.highlight .c { color: #999988; font-style: italic } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { font-weight: bold } /* Keyword */
.highlight .o { font-weight: bold } /* Operator */
.highlight .cm { color: #999988; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
.highlight .c1 { color: #999988; font-style: italic } /* Comment.Single */
.highlight .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #999999 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #aaaaaa } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { font-weight: bold } /* Keyword.Constant */
.highlight .kd { font-weight: bold } /* Keyword.Declaration */
.highlight .kp { font-weight: bold } /* Keyword.Pseudo */
.highlight .kr { font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #445588; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #009999 } /* Literal.Number */
.highlight .s { color: #d14 } /* Literal.String */
.highlight .na { color: #008080 } /* Name.Attribute */
.highlight .nb { color: #0086B3 } /* Name.Builtin */
.highlight .nc { color: #445588; font-weight: bold } /* Name.Class */
.highlight .no { color: #008080 } /* Name.Constant */
.highlight .ni { color: #800080 } /* Name.Entity */
.highlight .ne { color: #990000; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #990000; font-weight: bold } /* Name.Function */
.highlight .nn { color: #555555 } /* Name.Namespace */
.highlight .nt { color: #000080 } /* Name.Tag */
.highlight .nv { color: #008080 } /* Name.Variable */
.highlight .ow { font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mf { color: #009999 } /* Literal.Number.Float */
.highlight .mh { color: #009999 } /* Literal.Number.Hex */
.highlight .mi { color: #009999 } /* Literal.Number.Integer */
.highlight .mo { color: #009999 } /* Literal.Number.Oct */
.highlight .sb { color: #d14 } /* Literal.String.Backtick */
.highlight .sc { color: #d14 } /* Literal.String.Char */
.highlight .sd { color: #d14 } /* Literal.String.Doc */
.highlight .s2 { color: #d14 } /* Literal.String.Double */
.highlight .se { color: #d14 } /* Literal.String.Escape */
.highlight .sh { color: #d14 } /* Literal.String.Heredoc */
.highlight .si { color: #d14 } /* Literal.String.Interpol */
.highlight .sx { color: #d14 } /* Literal.String.Other */
.highlight .sr { color: #009926 } /* Literal.String.Regex */
.highlight .s1 { color: #d14 } /* Literal.String.Single */
.highlight .ss { color: #990073 } /* Literal.String.Symbol */
.highlight .bp { color: #999999 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #008080 } /* Name.Variable.Class */
.highlight .vg { color: #008080 } /* Name.Variable.Global */
.highlight .vi { color: #008080 } /* Name.Variable.Instance */
.highlight .il { color: #009999 } /* Literal.Number.Integer.Long */
208 changes: 125 additions & 83 deletions getting_started/1.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ If you don't use any of the distributions above, don't worry! Continue reading f

You can download and compile Elixir in few steps:

$ git clone https://github.com/elixir-lang/elixir.git
$ cd elixir
$ make test
{% highlight console %}
$ git clone https://github.com/elixir-lang/elixir.git
$ cd elixir
$ make test

$ bin/elixir -v
Elixir 0.4.0.dev
$ bin/elixir -v
Elixir 0.4.0.dev
{% endhighlight %}

If tests pass, you are ready to go. Otherwise, feel free to open an issue [in the issues tracker on Github](https://github.com/elixir-lang/elixir).

Expand All @@ -49,88 +51,110 @@ If tests pass, you are ready to go. Otherwise, feel free to open an issue [in th

We can start Elixir interactive mode by running `bin/iex` in the same directory you compiled Elixir or by simply running `iex` if you installed it from a distribution. In interactive mode, we can type any Elixir expression. Let's warm up with some basic arithmetic expressions:

iex> 1 + 1
2
iex> 10 - 5
5
iex> 10 / 2
5.0
{% highlight iex %}
iex> 1 + 1
2
iex> 10 - 5
5
iex> 10 / 2
5.0
{% endhighlight %}

Notice `10 / 2` returned a float `5.0` instead of an integer. This is expected, in Elixir the operator `/` always returns a float. In case you want to do integer division or get the division remainder, you can invoke the `div` and `rem` functions:

iex> div(10, 2)
5
iex> div 10, 2
5
iex> rem 10, 3
1
{% highlight iex %}
iex> div(10, 2)
5
iex> div 10, 2
5
iex> rem 10, 3
1
{% endhighlight %}

In the example above, we called two functions called `div` and `rem`. Notice that parenthesis are not required in order to invoke a function. We are going to discuss more about it later. Let's move forward and see which other data types we have in Elixir:

## 1.3 Basic types

Some basic types are:

iex> 1 # integer
iex> 1.0 # float
iex> :atom # atom / symbol
iex> {1,2,3} # tuple
iex> [1,2,3] # list
{% highlight iex %}
iex> 1 # integer
iex> 1.0 # float
iex> :atom # atom / symbol
iex> {1,2,3} # tuple
iex> [1,2,3] # list
{% endhighlight %}

Elixir also provides functions (note the dot between the variable and arguments when calling a function):

# function
iex> x = fn(a, b) -> a + b end
#Fun<erl_eval.12.111823515>
iex> x.(1, 2)
3
{% highlight iex %}
# function
iex> x = fn(a, b) -> a + b end
#Fun<erl_eval.12.111823515>
iex> x.(1, 2)
3
{% endhighlight %}

And double-quoted strings:

iex> "string"
"string"
{% highlight iex %}
iex> "string"
"string"
{% endhighlight %}

Strings in Elixir are UTF-8 binaries delimited by double quotes. A single-quoted string in Elixir is called a char list and is simply a list of characters:

iex> 'string'
'string'
{% highlight iex %}
iex> 'string'
'string'
{% endhighlight %}

There is also string interpolation:

iex> name = "world"
iex> "hello #{name}"
"hello world"
{% highlight iex %}
iex> name = "world"
iex> "hello #{name}"
"hello world"
{% endhighlight %}

We can use the `is_binary` and `is_list` helpers to detect if a given string is a binary (double-quoted) or a list (single-quoted):

iex> is_binary "string"
true
iex> is_list "string"
false
{% highlight iex %}
iex> is_binary "string"
true
iex> is_list "string"
false

iex> is_binary 'string'
false
iex> is_list 'string'
true
iex> is_binary 'string'
false
iex> is_list 'string'
true
{% endhighlight %}

Although they represent the same thing, double-quoted and single-quoted strings are different and best suited for different scenarios:

iex> "string" == 'string'
false
{% highlight iex %}
iex> "string" == 'string'
false
{% endhighlight %}

Most of the cases, developers should use double-quoted strings as their representation is more compact. We are going to discuss this subject with more detail in the next chapter.

Elixir also provides `true` and `false` as booleans:

iex> true
true
iex> is_boolean false
true
{% highlight iex %}
iex> true
true
iex> is_boolean false
true
{% endhighlight %}

Booleans are represented internally as atoms:

iex> is_atom(true)
true
{% highlight iex %}
iex> is_atom(true)
true
{% endhighlight %}

Elixir also provides Port, References and PIDs as data types (usually used in process communication) but they are out of the scope of a getting started tutorial. For now, let's take a look at the basic operators in Elixir before we move on to the next chapter.

Expand All @@ -140,70 +164,88 @@ As we saw earlier, Elixir provides `+`, `-`, `*`, `/` as arithmetic operators.

Elixir also provides `++` and `--` to manipulate lists:

iex> [1,2,3] ++ [4,5,6]
[1,2,3,4,5,6]
iex> [1,2,3] -- [2]
[1,3]
{% highlight iex %}
iex> [1,2,3] ++ [4,5,6]
[1,2,3,4,5,6]
iex> [1,2,3] -- [2]
[1,3]
{% endhighlight %}

Since single-quoted strings are lists, we can also use `++` and `--` as operators to manipulate them:

iex> 'some' ++ ' example'
'some example'
iex> 'some' -- 'oe'
'sm'
{% highlight iex %}
iex> 'some' ++ ' example'
'some example'
iex> 'some' -- 'oe'
'sm'
{% endhighlight %}

Notice that `++` and `--` cannot be used to manipulate double quoted strings, as they are binaries. That said, string concatenation is done via `<>`:

iex> "foo" <> "bar"
"foobar"
{% highlight iex %}
iex> "foo" <> "bar"
"foobar"
{% endhighlight %}

Elixir also provides three boolean operators: `or`, `and` and `not`. Those operators are strict in the sense those operators expects only booleans (true or false) as arguments:

iex> true and true
true
iex> false or is_atom(:example)
true
{% highlight iex %}
iex> true and true
true
iex> false or is_atom(:example)
true
{% endhighlight %}

Giving a non-boolean as argument will raise an exception:

iex> 1 and true
** (::ArgumentError) argument error
{% highlight iex %}
iex> 1 and true
** (::ArgumentError) argument error
{% endhighlight %}

`or` and `and` are short-circuit operators. They just execute the right side in case the left side is not enough to determine the result:

iex> false and error("This error will never be raised")
false
{% highlight iex %}
iex> false and error("This error will never be raised")
false

iex> true or error("This error will never be raised")
true
iex> true or error("This error will never be raised")
true
{% endhighlight %}

> Note: If you are an Erlang developer, `and` and `or` in Elixir actually map to the `andalso` and `orelse` operators in Erlang.

Elixir also provides `==`, `!=`, `===`, `!===`, `<=`, `>=`, `<` and `>` as comparison operators:

iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true
{% highlight iex %}
iex> 1 == 1
true
iex> 1 != 2
true
iex> 1 < 2
true
{% endhighlight %}

The difference between `==` and `===` is that the latter is more strict when comparing integers and floats:

iex> 1 == 1.0
true
iex> 1 === 1.0
false
{% highlight iex %}
iex> 1 == 1.0
true
iex> 1 === 1.0
false
{% endhighlight %}

In Elixir, we can compare two different data types:

iex> 1 < :atom
true
{% highlight iex %}
iex> 1 < :atom
true
{% endhighlight %}

The reason we can compare different data types is for pragmatism. Sorting algorithms don't need to worry about different data types in order to sort. The overall sorting order is defined below:

number < atom < reference < functions < port < pid < tuple < list < bit string

You actually don't need to memorize this ordering, it is important just to know an order exists.

Well, that is it for the introduction. In the next chapter, we are going to discuss some basic functions, data types conversions and a bit of control-flow.
Well, that is it for the introduction. In the next chapter, we are going to discuss some basic functions, data types conversions and a bit of control-flow.
Loading