- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 409字
- 2021-08-05 10:42:46
MapSets
If you're looking for an implementation of a set in Elixir, you're looking for MapSet. You create and manipulate them with the functions from the MapSet module. Here are some examples:
iex> set = MapSet.new
#MapSet<[]>
iex> set = MapSet.put(set, 1)
#MapSet<[1]>
iex> set = MapSet.put(set, 2)
#MapSet<[1, 2]>
iex> set = MapSet.put(set, 1)
#MapSet<[1, 2]>
Sets, by definition, can't contain duplicates. So, inserting a value that's already there has no effect. You can find the documentation for the MapSet module at https://hexdocs.pm/elixir/MapSet.html.
There are three types, related to the underlying Erlang VM, that we have to mention before closing this section. They are as following:
- Reference: A reference is a type created by the Kernel.make_ref function. This functions creates an almost-unique reference, which gets repeated around every 282 calls. We will not use references in this book.
- Port: A port is a reference to a resource. The Erlang VM uses it to interact with external resources, such as an operating system process. We will talk a bit more about ports later in this chapter, when we discuss the interoperability between Elixir and Erlang.
- PID: A PID is the type used to identify processes in the Erlang VM. You'll see PIDs in action later in this book, when we start working with Erlang VM processes.
To complete this section, there's a function that we want to highlight: the i function, which is auto-imported from the Kernel module. You can use it to find out more information about a data type. It will print information about the data type of the term you pass as an argument. Here is an example with a string:
iex> i("a string")
Term
"a string"
Data type
BitString
Byte size
8
Description
This is a string: a UTF-8 encoded binary. It's printed surrounded by
"double quotes" because all UTF-8 encoded codepoints in it are printable.
Raw representation
<<97, 32, 115, 116, 114, 105, 110, 103>>
Reference modules
String, :binary
Implemented protocols
IEx.Info, Collectable, List.Chars, String.Chars, Inspect
And, with this, we've finished our tour of the data types in Elixir! We didn't go into much detail, but with the links we left throughout this section, you'll see how incredible the documentation in Elixir is, and how easy it is to figure out what a certain function does or the purpose of a certain argument.
We will now jump into one of the most prominent features of Elixir (and also one that will definitely change how you write programs): pattern matching.
- ReSharper Essentials
- Monitoring Elasticsearch
- Java SE實踐教程
- Swift 4從零到精通iOS開發
- Modern C++ Programming Cookbook
- JavaScript Concurrency
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- 網頁設計與制作
- 數據結構與算法詳解
- Implementing Splunk(Second Edition)
- Visual Basic 開發從入門到精通
- Real-time Web Application Development using Vert.x 2.0
- Visual C++ 2017網絡編程實戰
- Python程序設計教程
- QlikView for Finance