- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 301字
- 2021-08-05 10:42:45
Lists
Lists are created by wrapping the elements we want inside it with square brackets ([ and ]), separating the values with commas. Internally, lists are implemented as singly linked lists, meaning that accessing the elements of a list is a O(n) operation. Lists aren't stored contiguously in memory as arrays in other languages. As with tuples, list elements can be of any type:
iex> [1, :an_atom, 0.5]
[1, :an_atom, 0.5]
We have the ++ and -- operators that are exclusive to lists, and serve to concatenate and subtract lists, respectively:
iex> [0, 1, 1] ++ [2, 3, 5]
[0, 1, 1, 2, 3, 5]
iex> [0, 1, 1] -- [1, 2, 3]
[0, 1]
To check whether a certain element is present in a list, you can use the in operator:
iex> 1 in [0, 1, 1, 2, 3, 5]
true
iex> 99 in [0, 1, 1, 2, 3, 5]
false
To get the head of a list, we use the hd function, whereas to get the tail of a list, we use the tl function:
iex> hd([0, 1, 1, 2, 3, 5])
0
iex> tl([0, 1, 1, 2, 3, 5])
[1, 1, 2, 3, 5]
Notice that the semantic of tail here is the list without its head (which is also a list), and not the last element of a list. We'll be exploring this concept in more depth, along with some more examples on how to work with lists, in the Working with collections section. For reference, you can find a detailed list of operations you can make on lists at https://hexdocs.pm/elixir/List.html.
- PHP動態網站程序設計
- Maven Build Customization
- 自己動手實現Lua:虛擬機、編譯器和標準庫
- Rust編程從入門到實戰
- WSO2 Developer’s Guide
- 算法精粹:經典計算機科學問題的Python實現
- 高級C/C++編譯技術(典藏版)
- Oracle Database 12c Security Cookbook
- Python Data Analysis(Second Edition)
- Go并發編程實戰
- Android程序設計基礎
- 深度學習:Java語言實現
- Python深度學習:模型、方法與實現
- Service Mesh實戰:基于Linkerd和Kubernetes的微服務實踐
- Python Interviews