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
+47-37
Original file line number
Diff line number
Diff line change
@@ -2,38 +2,44 @@
2
2
layout: post
3
3
title: What's New in Elixir #1
4
4
author: Alexei Sholik
5
-
category: whatsnew
5
+
category: "What's New in Elixir"
6
6
excerpt: Last week Elixir has seen a lot of new features, improvements, and bug fixes. In this little post I'm going to highlight some of the most prominent ones.
7
7
---
8
8
9
9
Last week Elixir has seen a lot of new features, improvements, and bug fixes. In this little post I'm going to highlight some of the most prominent ones.
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
-
dict = [a: 1, b: 2, c: 3]
14
-
dict[:a] #=> 1
15
-
dict[:d] #=> nil
16
-
tuple = {5, 4, 3, 2, 1}
17
-
tuple[1] #=> 5
18
-
tuple[0] #=> nil
19
-
tuple[-1] #=> 1
20
-
defrecord TestRec, red: 0, green: 0, blue: 0
21
-
r = TestRec[red: 255, blue: 80] #=> new record
22
-
s = "The quick brown fox jumps over the lazy dog."
23
-
s[%r/[a-z]+o[a-z]+/] #=> "brown"
13
+
```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
+
defrecordTestRec, 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
+
```
24
28
25
29
* Access protocol also makes it possible to pattern-match records:
26
30
27
-
defrecord TestRec, red: 0, green: 0, blue: 0
28
-
r = TestRec[red: 255, blue: 80] #=> new record
29
-
case r do
30
-
match: TestRec[red: 0]
31
-
:no_match
32
-
match: TestRec[red: red, blue: 80]
33
-
:ok
34
-
end
35
-
#=> :ok
36
-
red === 255 #=> true
31
+
```elixir
32
+
defrecordTestRec, 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
+
```
37
43
38
44
* 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.
39
45
@@ -43,24 +49,28 @@ Last week Elixir has seen a lot of new features, improvements, and bug fixes. In
43
49
44
50
* Support for nested modules has been added.
45
51
46
-
defmodule Father do
47
-
defmodule Child do
48
-
def child_fun(str) do
49
-
IO.puts str
50
-
end
51
-
end
52
-
def fun do
53
-
Child.child_fun "some argument"
54
-
end
55
-
end
56
-
Father.Child.child_fun "hello!"
52
+
```elixir
53
+
defmoduleFatherdo
54
+
defmoduleChilddo
55
+
defchild_fun(str) do
56
+
IO.puts str
57
+
end
58
+
end
59
+
deffundo
60
+
Child.child_fun"some argument"
61
+
end
62
+
end
63
+
Father.Child.child_fun"hello!"
64
+
```
57
65
58
66
* The `Regex` module has received new functions, namely, `source` and `opts`. It can also be `inspect`ed now.
59
67
60
-
reg = %r/[a-z]+o[a-z]+/im
61
-
Regex.source reg #=> "[a-z]+o[a-z]+"
62
-
Regex.opts reg #=> "im"
63
-
inspect reg #=> "%r\"[a-z]+o[a-z]+\"im"
68
+
```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
+
```
64
74
65
75
* 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
+35-24
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
layout: post
3
3
title: What's New in Elixir #2
4
4
author: Alexei Sholik
5
-
category: whatsnew
5
+
category: "What's New in Elixir"
6
6
excerpt: This week has not seen as many prominent new features as the previous one. Still, more bugs have been fixed and a number of small improvements has been made here and there, so the overall progress is quite noticeable.
7
7
---
8
8
This week has not seen as many prominent new features as the previous one. Still, more bugs have been fixed and a number of small improvements has been made here and there, so the overall progress is quite noticeable.
@@ -11,35 +11,46 @@ 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
-
0xFF #=> 255
15
-
0o10 #=> 8
16
-
0b1010 #=> 10
14
+
```elixir
15
+
0xFF#=> 255
16
+
0o10#=> 8
17
+
0b1010#=> 10
18
+
```
17
19
18
20
* New functions in the [List module](https://github.com/elixir-lang/elixir/blob/master/lib/list.ex): `sort`, `zip`, `unzip`.
19
21
20
-
# Charlists are sorted in lexicographic order
21
-
List.sort ['10', '2', '4', '1', '21']
22
-
#=> ['1', '10', '2', '21', '4']
23
-
# Numerical sort for charlists using a custom function
24
-
List.sort ['10', '2', '4', '1', '21'], fn(a, b) ->
25
-
{na, _} = :string.to_integer a
26
-
{nb, _} = :string.to_integer b
27
-
na <= nb
28
-
end
29
-
#=> ['1', '2', '4', '10', '21']
30
-
List.zip [[1, 2], [:a, :b], ["one", "two"]]
31
-
#=> [{1,:a,"one"},{2,:b,"two"}]
22
+
```elixir
23
+
# Charlists are sorted in lexicographic order
24
+
List.sort ['10', '2', '4', '1', '21']
25
+
#=> ['1', '10', '2', '21', '4']
26
+
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']
34
+
35
+
List.zip [[1, 2], [:a, :b], ["one", "two"]]
36
+
#=> [{1,:a,"one"},{2,:b,"two"}]
37
+
```
32
38
33
39
* 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.
34
40
35
-
System.pwd
36
-
#=> "/Users/alco/Documents/git/elixir"
37
-
System.get_env "PAGER"
38
-
#=> "less"
39
-
System.cmd 'date'
40
-
#=> "Fri Apr 13 19:35:13 EEST 2012\n"
41
-
System.stacktrace
42
-
#=> (usually long output)
41
+
```elixir
42
+
System.pwd
43
+
#=> "/Users/alco/Documents/git/elixir"
44
+
45
+
System.get_env"PAGER"
46
+
#=> "less"
47
+
48
+
System.cmd'date'
49
+
#=> "Fri Apr 13 19:35:13 EEST 2012\n"
50
+
51
+
System.stacktrace
52
+
#=> (usually long output)
53
+
```
43
54
44
55
* 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