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/2013-08-08-elixir-design-goals.markdown
+1-1
Original file line number
Diff line number
Diff line change
@@ -136,7 +136,7 @@ defmodule Hello do
136
136
end
137
137
```
138
138
139
-
Given my previous background in Ruby, it is natural that some of the constructs added were borrowed from Ruby. However, those additions were a by-product, never a language goal.
139
+
Given my previous background in Ruby, it is natural that some of the constructs added were borrowed from Ruby. However, those additions were a by-product, and not a language goal.
140
140
141
141
Many language constructs are also inspired by their Erlang counter-parts, like some of the control-flow macros, operators and containers. Notice how some Elixir code:
Copy file name to clipboardExpand all lines: getting-started/basic-types.markdown
+9-12
Original file line number
Diff line number
Diff line change
@@ -44,7 +44,7 @@ iex> rem 10, 3
44
44
1
45
45
```
46
46
47
-
Notice that parentheses are not required in order to invoke a function.
47
+
Notice Elixir allows you to drop the parentheses when invoking named functions. Such feature gives a cleaner syntax when writing declarations and control-flow constructs.
48
48
49
49
Elixir also supports shortcut notations for entering binary, octal, and hexadecimal numbers:
50
50
@@ -94,7 +94,6 @@ false
94
94
95
95
Elixir provides a bunch of predicate functions to check for a value type. For example, the `is_boolean/1` function can be used to check if a value is a boolean or not:
96
96
97
-
98
97
```iex
99
98
iex> is_boolean(true)
100
99
true
@@ -104,7 +103,7 @@ false
104
103
105
104
You can also use `is_integer/1`, `is_float/1` or `is_number/1` to check, respectively, if an argument is an integer, a float, or either.
106
105
107
-
> Note: At any moment you can type `h` in the shell to print information on how to use the shell. The `h` helper can also be used to access documentation for any function. For example, typing `h is_integer/1` is going to print the documentation for the `is_integer/1` function. It also works with operators and other constructs (try `h ==/2`).
106
+
> Note: At any moment you can type `h()` in the shell to print information on how to use the shell. The `h` helper can also be used to access documentation for any function. For example, typing `h is_integer/1` is going to print the documentation for the `is_integer/1` function. It also works with operators and other constructs (try `h ==/2`).
108
107
109
108
## Atoms
110
109
@@ -197,24 +196,24 @@ iex> String.upcase("hellö")
197
196
198
197
## Anonymous functions
199
198
200
-
Functions are delimited by the keywords `fn` and `end`:
199
+
Anonymous functions can be created inline and are delimited by the keywords `fn` and `end`:
201
200
202
201
```iex
203
202
iex> add = fn a, b -> a + b end
204
203
#Function<12.71889879/2 in :erl_eval.expr/5>
204
+
iex> add.(1, 2)
205
+
3
205
206
iex> is_function(add)
206
207
true
207
-
iex> is_function(add, 2)
208
+
iex> is_function(add, 2) # check if it expects exactly 2 arguments
208
209
true
209
-
iex> is_function(add, 1)
210
+
iex> is_function(add, 1) # check if it expects exactly 1 argument
210
211
false
211
-
iex> add.(1, 2)
212
-
3
213
212
```
214
213
215
-
Functions are "first class citizens" in Elixir meaning they can be passed as arguments to other functions as integers and strings can. In the example, we have passed the function in the variable `add` to the `is_function/1` function which correctly returned `true`. We can also check the arity of the function by calling `is_function/2`.
214
+
Functions are "first class citizens" in Elixir meaning they can be passed as arguments to other functions in the same way as integers and strings. In the example, we have passed the function in the variable `add` to the `is_function/1` function which correctly returned `true`. We can also check the arity of the function by calling `is_function/2`.
216
215
217
-
Note a dot (`.`) between the variable and parenthesis is required to invoke an anonymous function.
216
+
Note a dot (`.`) between the variable and parentheses is required to invoke an anonymous function. The dot ensures there is no ambiguity between calling an anonymous function named `add` and a named function `add/2`. In this sense, Elixir makes a clear distinction between anonymous functions and named functions. We will explore those differences in [Chapter 8](/getting-started/modules.html).
218
217
219
218
Anonymous functions are closures and as such they can access variables that are in scope when the function is defined. Let's define a new anonymous function that uses the `add` anonymous function we have previously defined:
220
219
@@ -236,8 +235,6 @@ iex> x
236
235
42
237
236
```
238
237
239
-
The capture syntax [`&()`](https://hexdocs.pm/elixir/Kernel.SpecialForms.html) can also be used for creating anonymous functions. This type of syntax will be discussed in [Chapter 8](/getting-started/modules.html).
240
-
241
238
## (Linked) Lists
242
239
243
240
Elixir uses square brackets to specify a list of values. Values can be of any type:
Copy file name to clipboardExpand all lines: getting-started/modules.markdown
+2
Original file line number
Diff line number
Diff line change
@@ -156,6 +156,8 @@ iex> fun.(0)
156
156
true
157
157
```
158
158
159
+
Remember Elixir makes a distinction between anonymous functions and named functions, where the former must be invoked with a dot (`.`) between the variable name and parentheses. The capture operator bridges this gap by allowing named functions to be assigned to variables and passed as arguments in the same way we assign, invoke and pass anonymous functions.
160
+
159
161
Local or imported functions, like `is_function/1`, can be captured without the module:
0 commit comments