You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _posts/2012-04-17-what-s-new-in-elixir-1.markdown
+39-45
Original file line number
Diff line number
Diff line change
@@ -10,36 +10,34 @@ Last week Elixir has seen a lot of new features, improvements, and bug fixes. In
10
10
11
11
* Access protocol has been added for tuples, lists, strings, and whatnot. It allows us to easily access elements of a collection. We can also use a regex to find the first match in a string or a list. Examples follow:
12
12
13
-
{% highlight elixir %}
14
-
dict = [a: 1, b: 2, c: 3]
15
-
dict[:a] #=> 1
16
-
dict[:d] #=> nil
17
-
18
-
tuple = {5, 4, 3, 2, 1}
19
-
tuple[1] #=> 5
20
-
tuple[0] #=> nil
21
-
tuple[-1] #=> 1
22
-
23
-
defrecord TestRec, red: 0, green: 0, blue: 0
24
-
r = TestRec[red: 255, blue: 80] #=> new record
25
-
s = "The quick brown fox jumps over the lazy dog."
26
-
s[%r/[a-z]+o[a-z]+/] #=> "brown"
27
-
{% endhighlight %}
13
+
14
+
dict = [a: 1, b: 2, c: 3]
15
+
dict[:a] #=> 1
16
+
dict[:d] #=> nil
17
+
18
+
tuple = {5, 4, 3, 2, 1}
19
+
tuple[1] #=> 5
20
+
tuple[0] #=> nil
21
+
tuple[-1] #=> 1
22
+
23
+
defrecord TestRec, red: 0, green: 0, blue: 0
24
+
r = TestRec[red: 255, blue: 80] #=> new record
25
+
s = "The quick brown fox jumps over the lazy dog."
26
+
s[%r/[a-z]+o[a-z]+/] #=> "brown"
28
27
29
28
* Access protocol also makes it possible to pattern-match records:
30
29
31
-
{% highlight elixir %}
32
-
defrecord TestRec, red: 0, green: 0, blue: 0
33
-
r = TestRec[red: 255, blue: 80] #=> new record
34
-
case r do
35
-
match: TestRec[red: 0]
36
-
:no_match
37
-
match: TestRec[red: red, blue: 80]
38
-
:ok
39
-
end
40
-
#=> :ok
41
-
red === 255 #=> true
42
-
{% endhighlight %}
30
+
31
+
defrecord TestRec, red: 0, green: 0, blue: 0
32
+
r = TestRec[red: 255, blue: 80] #=> new record
33
+
case r do
34
+
match: TestRec[red: 0]
35
+
:no_match
36
+
match: TestRec[red: red, blue: 80]
37
+
:ok
38
+
end
39
+
#=> :ok
40
+
red === 255 #=> true
43
41
44
42
* The `Orddict` module is no longer with us, it has been renamed to `Keyword`. The new module only allows atoms to be used as keys. A general purpose module for dicts will be added sooner or later.
45
43
@@ -49,28 +47,24 @@ red === 255 #=> true
49
47
50
48
* Support for nested modules has been added.
51
49
52
-
{% highlight elixir %}
53
-
defmodule Father do
54
-
defmodule Child do
55
-
def child_fun(str) do
56
-
IO.puts str
50
+
defmodule Father do
51
+
defmodule Child do
52
+
def child_fun(str) do
53
+
IO.puts str
54
+
end
55
+
end
56
+
def fun do
57
+
Child.child_fun "some argument"
58
+
end
57
59
end
58
-
end
59
-
def fun do
60
-
Child.child_fun "some argument"
61
-
end
62
-
end
63
-
Father.Child.child_fun "hello!"
64
-
{% endhighlight %}
60
+
Father.Child.child_fun "hello!"
65
61
66
62
* The `Regex` module has received new functions, namely, `source` and `opts`. It can also be `inspect`ed now.
67
63
68
-
{% highlight elixir %}
69
-
reg = %r/[a-z]+o[a-z]+/im
70
-
Regex.source reg #=> "[a-z]+o[a-z]+"
71
-
Regex.opts reg #=> "im"
72
-
inspect reg #=> "%r\"[a-z]+o[a-z]+\"im"
73
-
{% endhighlight %}
64
+
reg = %r/[a-z]+o[a-z]+/im
65
+
Regex.source reg #=> "[a-z]+o[a-z]+"
66
+
Regex.opts reg #=> "im"
67
+
inspect reg #=> "%r\"[a-z]+o[a-z]+\"im"
74
68
75
69
* A new `read_info` function has been added to the [`File` module](https://github.com/elixir-lang/elixir/blob/35b22c598defd8be07d46d2e7e8fc0ddf9ec4e80/lib/file.ex) allowing
Copy file name to clipboardExpand all lines: _posts/2012-04-17-what-s-new-in-elixir-2.markdown
+23-29
Original file line number
Diff line number
Diff line change
@@ -11,46 +11,40 @@ Let's get started with our usual overview. I'm using the latest master (`2851da4
11
11
12
12
* Literal support for hexadecimal, octal and binary numbers has been added.
13
13
14
-
{% highlight elixir %}
15
-
0xFF #=> 255
16
-
0o10 #=> 8
17
-
0b1010 #=> 10
18
-
{% endhighlight %}
14
+
0xFF #=> 255
15
+
0o10 #=> 8
16
+
0b1010 #=> 10
19
17
20
18
* New functions in the [List module](https://github.com/elixir-lang/elixir/blob/master/lib/list.ex): `sort`, `zip`, `unzip`.
21
19
22
-
{% highlight elixir %}
23
-
# Charlists are sorted in lexicographic order
24
-
List.sort ['10', '2', '4', '1', '21']
25
-
#=> ['1', '10', '2', '21', '4']
20
+
# Charlists are sorted in lexicographic order
21
+
List.sort ['10', '2', '4', '1', '21']
22
+
#=> ['1', '10', '2', '21', '4']
26
23
27
-
# Numerical sort for charlists using a custom function
28
-
List.sort ['10', '2', '4', '1', '21'], fn(a, b) ->
29
-
{na, _} = :string.to_integer a
30
-
{nb, _} = :string.to_integer b
31
-
na <= nb
32
-
end
33
-
#=> ['1', '2', '4', '10', '21']
24
+
# Numerical sort for charlists using a custom function
25
+
List.sort ['10', '2', '4', '1', '21'], fn(a, b) ->
26
+
{na, _} = :string.to_integer a
27
+
{nb, _} = :string.to_integer b
28
+
na <= nb
29
+
end
30
+
#=> ['1', '2', '4', '10', '21']
34
31
35
-
List.zip [[1, 2], [:a, :b], ["one", "two"]]
36
-
#=> [{1,:a,"one"},{2,:b,"two"}]
37
-
{% endhighlight %}
32
+
List.zip [[1, 2], [:a, :b], ["one", "two"]]
33
+
#=> [{1,:a,"one"},{2,:b,"two"}]
38
34
39
35
* The [System module](https://github.com/elixir-lang/elixir/blob/master/lib/system.ex) has been merged into master. It provides functions for communicating with OS environment, running external commands, getting the stacktrace, etc.
40
36
41
-
{% highlight elixir %}
42
-
System.pwd
43
-
#=> "/Users/alco/Documents/git/elixir"
37
+
System.pwd
38
+
#=> "/Users/alco/Documents/git/elixir"
44
39
45
-
System.get_env "PAGER"
46
-
#=> "less"
40
+
System.get_env "PAGER"
41
+
#=> "less"
47
42
48
-
System.cmd 'date'
49
-
#=> "Fri Apr 13 19:35:13 EEST 2012\n"
43
+
System.cmd 'date'
44
+
#=> "Fri Apr 13 19:35:13 EEST 2012\n"
50
45
51
-
System.stacktrace
52
-
#=> (usually long output)
53
-
{% endhighlight %}
46
+
System.stacktrace
47
+
#=> (usually long output)
54
48
55
49
* In other news, we're getting closer to having a dedicated site for documentation, JSON parsing/serialization is currently in the works, and there's also work being done on bringing dicts back into Elixir.
0 commit comments