Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5591ab2dff | ||
|
|
59910fff71 | ||
|
|
f179a23675 | ||
|
|
bfa16ebf35 | ||
|
|
4906beeb2d | ||
|
|
7ab6d2e982 | ||
|
|
ac502ebee2 | ||
|
|
4fd0c4e013 | ||
|
|
05fecbcbcf | ||
|
|
448f473d08 | ||
|
|
eb8d293ac5 | ||
|
|
59588ca424 | ||
|
|
64ffd185cc | ||
|
|
40beaf4f07 | ||
|
|
139d2eac59 | ||
|
|
84b33461f3 | ||
|
|
e88a1b6738 | ||
|
|
2e4c7abe4d | ||
|
|
a6723dcbc6 | ||
|
|
e878ddee4c |
@@ -1,6 +1,10 @@
|
||||
language: erlang
|
||||
|
||||
otp_release:
|
||||
- 17.0
|
||||
- 17.3
|
||||
- 17.4
|
||||
- 17.5
|
||||
- 18.0
|
||||
|
||||
sudo: false
|
||||
|
||||
+161
-471
@@ -1,523 +1,213 @@
|
||||
# Changelog for Elixir v1.3
|
||||
# Changelog for Elixir v1.1
|
||||
|
||||
Elixir v1.3 brings many improvements to the language, the compiler and its tooling, specially Mix (Elixir's build tool) and ExUnit (Elixir's test framework).
|
||||
v1.1 brings enhancements, bug fixes, performance improvements and more
|
||||
into Elixir.
|
||||
|
||||
## Language improvements
|
||||
Elixir v1.1 supports both Erlang 17 and Erlang 18. This is, however, the
|
||||
last release supporting Erlang 17, so upgrading to Erlang 18 is advised.
|
||||
Elixir v1.2 will introduce features that are Erlang 18 only.
|
||||
|
||||
The language has been improved semantically and includes new types and APIs. Let's see the three major features.
|
||||
On the enhancements side, the most notable changes are the new functions
|
||||
added to `Enum`, `Dict` and `Task` modules, and a new datatype called `MapSet`.
|
||||
`MapSet` implements the `Set` API on top of a map and, for Elixir v1.1,
|
||||
it is useful for holding only dozens of entries. Future Elixir versions,
|
||||
however, will be able to rely on `MapSet` from dozens of keys up to
|
||||
millions, with better performance than `HashSet`.
|
||||
|
||||
### Deprecation of imperative assignment
|
||||
On the deprecation side, this release includes one major deprecation and
|
||||
some soft deprecations.
|
||||
|
||||
Elixir will now warn if constructs like `if`, `case` and friends assign to a variable that is accessed in an outer scope. As an example, imagine a function called `format` that receives a message and some options and it must return a path alongside the message:
|
||||
The major deprecation relates to the Access protocol. Due to performance
|
||||
issues, the access syntax `opts[key]` will no longer be powered by the
|
||||
`Access` protocol, instead, it will use a subset of the `Dict` module.
|
||||
Therefore this release will emit warnings if you attempt to implement
|
||||
the `Access` protocol. Note the `Access` module and the `opts[key]`
|
||||
syntax are not affected and they are not deprecated, only the underlying
|
||||
protocol dispatch.
|
||||
|
||||
```elixir
|
||||
def format(message, opts) do
|
||||
path =
|
||||
if (file = opts[:file]) && (line = opts[:line]) do
|
||||
relative = Path.relative_to_cwd(file)
|
||||
message = Exception.format_file_line(relative, line) <> " " <> message
|
||||
relative
|
||||
end
|
||||
The soft deprecations are minor and they won't emit warnings. It simply
|
||||
means the documentation has been updated to mention the new best
|
||||
practices. Warnings will be emitted in the future though (when they are
|
||||
promoted to deprecations).
|
||||
|
||||
{path, message}
|
||||
end
|
||||
```
|
||||
On the tooling side, ExUnit got the ability to skip tests and a couple
|
||||
new configuration options. Mix got improved warnings and error messages,
|
||||
faster compilation times and the brand new `mix profile.fprof` task.
|
||||
|
||||
The `if` block above is implicitly rebinding the outer scope variable `message` to a new value. Now imagine we want to move the `if` block to its own function to clean up the implementation:
|
||||
Mix now also ships with `local.public_keys` to safely manage the
|
||||
installation of Hex and Rebar alongside the ability to checksum
|
||||
archive installs. By default, Elixir will always ship with a valid
|
||||
public key and this feature should work transparently for users.
|
||||
|
||||
```elixir
|
||||
def format(message, opts) do
|
||||
path = with_file_and_line(message, opts)
|
||||
{path, message}
|
||||
end
|
||||
Finally, we have added a `CODE_OF_CONDUCT.md` file to our repository.
|
||||
If you haven't read it yet, please do it. We are here to help!
|
||||
|
||||
defp with_file_and_line(message, opts) do
|
||||
if (file = opts[:file]) && (line = opts[:line]) do
|
||||
relative = Path.relative_to_cwd(file)
|
||||
message = Exception.format_file_line(relative, line) <> " " <> message
|
||||
relative
|
||||
end
|
||||
end
|
||||
```
|
||||
Note: Erlang 17.1 contains a regression in its wildcard implementation that
|
||||
causes tools like rebar to fail. If you have a project with rebar dependencies
|
||||
and is using Erlang 17.1, remember to update to at least Erlang 17.3.
|
||||
|
||||
The refactored version is broken because the `if` block was actually returning two values, the relative path *and* the new message. Elixir v1.3 will warn on such cases, forcing both variables to be explicitly returned from `if`, `case` and other constructs. Furthermore, this change gives us the opportunity to unify the language scoping rules in future releases.
|
||||
## v1.1.1 (2015-09-28)
|
||||
|
||||
### Calendar types and sigils
|
||||
### Bug fixes
|
||||
|
||||
Elixir v1.3 introduces the `Calendar` module as well as 4 new calendar types:
|
||||
* [Enum] Fix non-integer member checks with ranges
|
||||
* [ExUnit] Fix assertion errors with more than 1 pinned var
|
||||
* [Mix] Ensure umbrella apps can boot with build_embedded is true
|
||||
|
||||
* `Date` - used to store dates (year, month, day) in a given calendar
|
||||
* `Time` - used to store time (hour, minute, second, microseconds)
|
||||
* `NaiveDateTime` - used to store datetimes without a timezone (year, month, day, hour, minute, second, microseconds) in a given calendar. It is called naïve because without a timezone, the datetime may not actually exist. For example, when there are daylight savings changes, a whole hour may not exist (when the clock moves forward) or a particular instant may happen twice (when the clock moves backwards)
|
||||
* `DateTime` - used to store datetimes with timezone (year, month, day, hour, minute, second, microsecond and time zone, with abbreviation, UTC and standard offset)
|
||||
|
||||
The current Calendar modules and its types is to provide a base for interoperatibility in the ecosystem instead of full-featured datetime API. This release includes basic functionality for building new types and converting them from and back strings.
|
||||
|
||||
Elixir v1.3 also introduces 3 new sigils related to the types above:
|
||||
|
||||
* `~D[2016-05-29]` - builds a new date
|
||||
* `~T[08:00:00]` and `~T[08:00:00.285]` - builds a new time (with different precisions)
|
||||
* `~N[2016-05-29 08:00:00]` - builds a naive date time
|
||||
|
||||
### Access selectors
|
||||
|
||||
This release introduces new accessors to make it simpler for developers to traverse nested data structures, traversing and updating data in different ways. For instance, given a user with a list of languages, here is how to deeply traverse the map and convert all language names to uppercase:
|
||||
|
||||
```iex
|
||||
iex> user = %{name: "john",
|
||||
...> languages: [%{name: "elixir", type: :functional},
|
||||
...> %{name: "c", type: :procedural}]}
|
||||
iex> update_in user, [:languages, Access.all(), :name], &String.upcase/1
|
||||
%{name: "john",
|
||||
languages: [%{name: "ELIXIR", type: :functional},
|
||||
%{name: "C", type: :procedural}]}
|
||||
```
|
||||
|
||||
You can see the new accessors in the `Access` module.
|
||||
|
||||
## Mix
|
||||
|
||||
Mix includes new tasks to improve your everyday workflow. Some of those tasks relies on many compiler improvements to know more about your code, providing static analysis to find possible bugs in your code and faster compilation cycles.
|
||||
|
||||
### Compiling n files
|
||||
|
||||
Mix no longer announces every file it compiles. Instead it outputs how many files there is to compile per compilers. Here is the output for a project like [`gettext`](https://github.com/elixir-lang/gettext):
|
||||
|
||||
```
|
||||
Compiling 1 file (.yrl)
|
||||
Compiling 1 file (.erl)
|
||||
Compiling 19 files (.ex)
|
||||
Generated gettext app
|
||||
```
|
||||
|
||||
In case a file is taking too long to compile, Mix will announce such, for example:
|
||||
|
||||
```
|
||||
Compiling lib/gettext.ex (it's taking more than 5s)
|
||||
```
|
||||
|
||||
The goal of these changes is to put an increased focus on the "warnings" emitted by the compiler.
|
||||
|
||||
In any case, the previous behaviour can be brought back with the `--verbose` flag and the compilation threshold for files that are taking long can be set via the `--long-compilation-threshold` option.
|
||||
|
||||
### mix xref
|
||||
|
||||
Speaking about warnings, Mix v1.3 includes a new task called `xref` that performs cross reference checks in your code. One of such checks is the ability to find calls to modules and functions that do not exist. For example, if in your library code you call `ThisModuleDoesNotExist.foo(1, 2, 3)`, `mix xref unreachable` will be able to find such code and let you know about it.
|
||||
|
||||
Since such checks can discover possible bugs in your codebase, a new compiler called `xref` has been added to `Mix.compilers/0`, so they run by default every time you compile your code.
|
||||
|
||||
We have included other modes in `xref`, such as `mix xref callers Foo`, to find all places in your code that a function from the module `Foo` is called. We hope other tools and text editors can leverage such features to provide useful functionality for their users.
|
||||
|
||||
### Better dependency tracking
|
||||
|
||||
Besides `xref`, Elixir v1.3 provides better module tracking generally. For example, in previous versions, if you changed a `:path` dependency, Elixir would always fully recompile the current project. In this release, we have improved the tracking algorithms such that, if you change a `:path` dependency, only the files that depend on such dependency are recompiled.
|
||||
|
||||
Such improvements do not only make compilation faster but they also make working with umbrella applications much more productive. Previously, changing a sibling application triggered a full project recompilation, now Elixir can track between sibling applications and recompile only what is needed.
|
||||
|
||||
### mix app.tree and deps.tree
|
||||
|
||||
Mix also includes both `mix app.tree` and `mix deps.tree`. The first will list all applications your current project needs to start in order to boot (i.e. the ones listed in `application/0` in your `mix.exs`) while the second will lists all of your dependencies and so on recursively.
|
||||
|
||||
Here is a quick example from [Plug](https://github.com/elixir-lang/plug):
|
||||
|
||||
```elixir
|
||||
$ mix app.tree
|
||||
plug
|
||||
├── elixir
|
||||
├── crypto
|
||||
├── logger
|
||||
│ └── elixir
|
||||
└── mime
|
||||
└── elixir
|
||||
```
|
||||
|
||||
### mix escript.install
|
||||
|
||||
Mix also includes `mix escript.install` and `mix escript.uninstall` tasks for managing escripts. The tasks was designed in a way to mimic the existing `mix archive` functionality except that:
|
||||
|
||||
* Archives must be used sparingly because every new archive installed affects Mix performance, as every new archive is loaded when Mix boots. Escripts solve this by being managed apart from your Elixir/Mix installed
|
||||
* Archives depends on the current Elixir version. Therefore, updating your Elixir version may break an archive. Fortunately, escripts include Elixir inside themselves, and therefore do not depend on your Elixir system version
|
||||
|
||||
Escripts will be installed at `~/.mix/escripts` which must be added to your [`PATH` environment variable](https://en.wikipedia.org/wiki/PATH_(variable)).
|
||||
|
||||
### Option parser integration
|
||||
|
||||
Elixir v1.3 includes improvements to the option parser, including `OptionParser.parse!/2` and `OptionParser.parse_head!/2` functions that will raise in case of invalid or unknown switches. Mix builds on top of this functionality to provide automatic error reporting solving a common complaint where invalid options were not reported by Mix tasks.
|
||||
|
||||
For example, invoking `mix test --unknown` in earlier Elixir versions would silently discard the `--unknown` option. Now `mix test` correctly reports such errors:
|
||||
|
||||
```
|
||||
$ mix test --unknown
|
||||
** (Mix) Could not invoke task "test": 1 error found!
|
||||
--unknown : Unknown option
|
||||
```
|
||||
|
||||
Note not all tasks have been updated to use strict option parsing. Some tasks, like `mix compile`, are actually a front-end to many other tasks, and as such, it cannot effectively assert which options are valid.
|
||||
|
||||
## ExUnit
|
||||
|
||||
ExUnit packs many improvements on the tooling side, better integration with external tools, as well as mechanisms to improve the readability of your tests.
|
||||
|
||||
### mix test --stale
|
||||
|
||||
ExUnit builds on top of `mix xref` to provide the `mix test --stale` functionality. When the `--stale` flag is given, `mix` will only run the tests that may have changed since the last time you ran `mix test --stale`. For example:
|
||||
|
||||
* If you saved a test file on disk, Mix will run that file and ignore the ones that have not changed
|
||||
* If you changed a library file, for example, `lib/foo.ex` that defines `Foo`, any test that invokes a function in `Foo` directly or indirectly will also run
|
||||
* If you modify your `mix.exs` or your `test/test_helper.exs`, Mix will run the whole test suite
|
||||
|
||||
This feature provides a great workflow for developers, allowing them to effortlessly focus on parts of the codebase when developing new features.
|
||||
|
||||
### Diffing
|
||||
|
||||
ExUnit will now include diff-ing output every time a developer asserts `assert left == right` in their tests. For example, the assertion:
|
||||
|
||||
```elixir
|
||||
assert "fox jumps over the lazy dog" ==
|
||||
"brown fox jumps over the dog"
|
||||
```
|
||||
|
||||
will fail with
|
||||
|
||||
```elixir
|
||||
1) test compare (Difference)
|
||||
lib/ex_unit/examples/difference.exs:10
|
||||
Assertion with == failed
|
||||
lhs: "fox jumps over the lazy dog"
|
||||
rhs: "brown fox jumps over the dog"
|
||||
stacktrace:
|
||||
lib/ex_unit/examples/difference.exs:11: (test)
|
||||
```
|
||||
|
||||
in a way that "lazy" in "lhs" will be shown in red to denote it has been removed from "rhs" while "brown" in "rhs" will be shown in green to denote it has been added to the "rhs".
|
||||
|
||||
When working with large or nested data structures, the diffing algorithm makes it fast and convenient to spot the actual differences in the asserted values.
|
||||
|
||||
### Test types
|
||||
|
||||
ExUnit v1.3 includes the ability to register different test types. This means libraries like QuickCheck can now provide functionality such as:
|
||||
|
||||
```elixir
|
||||
defmodule StringTest do
|
||||
use ExUnit.Case, async: true
|
||||
use PropertyTestingLibrary
|
||||
|
||||
property "starts_with?" do
|
||||
forall({s1, s2} <- {utf8, utf8}) do
|
||||
String.starts_with?(s1 <> s2, s1)
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
At the end of the run, ExUnit will also report it as a property, including both the amount of tests and properties:
|
||||
|
||||
```
|
||||
1 property, 10 tests, 0 failures
|
||||
```
|
||||
|
||||
### Named setups and describes
|
||||
|
||||
Finally, ExUnit v1.3 includes the ability to organize tests together in describe blocks:
|
||||
|
||||
```elixir
|
||||
defmodule StringTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
describe "String.capitalize/2" do
|
||||
test "uppercases the first grapheme" do
|
||||
assert "T" <> _ = String.capitalize("test")
|
||||
end
|
||||
|
||||
test "lowercases the remaining graphemes" do
|
||||
assert "Test" = String.capitalize("TEST")
|
||||
end
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
Every test inside a describe block will be tagged with the describe block name. This allows developers to run tests that belong to particular blocks, be them in the same file or across many files:
|
||||
|
||||
```
|
||||
$ mix test --only describe:"String.capitalize/2"
|
||||
```
|
||||
|
||||
Note describe blocks cannot be nested. Instead of relying on hierarchy for composition, we want developers to build on top of named setups. For example:
|
||||
|
||||
```elixir
|
||||
defmodule UserManagementTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
describe "when user is logged in and is an admin" do
|
||||
setup [:log_user_in, :set_type_to_admin]
|
||||
|
||||
test ...
|
||||
end
|
||||
|
||||
describe "when user is logged in and is a manager" do
|
||||
setup [:log_user_in, :set_type_to_manager]
|
||||
|
||||
test ...
|
||||
end
|
||||
|
||||
defp log_user_in(context) do
|
||||
# ...
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
By restricting hierarchies in favor of named setups, it is straight-forward for the developer to glance at each describe block and know exactly the setup steps involved.
|
||||
|
||||
## v1.3.4 (2016-10-09)
|
||||
|
||||
### 1. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Ensure the compiler does not generate unecessary variable bindings inside case statements. This improves the code emitted and make sure "unused variable warnings" are not mistakenly silenced
|
||||
* [Kernel] Move `raise` checks to runtime to avoid crashing cover on Erlang 19.1
|
||||
* [Protocol] Do not emit warnings when using protocols on opaque types
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.CaptureLog] Flush Erlang's `:error_logger` before capturing to avoid mixed messages
|
||||
|
||||
## v1.3.3 (2016-09-17)
|
||||
## v1.1.0 (2015-09-25)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [DateTime] Support negative integer in `DateTime.from_unix/2`
|
||||
* [Kernel.LexicalTracker] Do not consider remote typespecs as a compile-time dependency
|
||||
* [Kernel.ParallelCompiler] Do not emit deadlock messages when the process is waiting on itself
|
||||
* [Kernel.Typespec] Mark struct update syntax as generated to avoid false positives from dialyzer
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Make ExUnit server timeout configurable
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Use `:ansi_color` if one is available in metadata
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add support for the `:sparse` option in `Mix.SCM.Git`
|
||||
* [Mix] Skip dependendency loading if `MIX_NO_DEPS` is set to `1`
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [System] Use `NUL` instead of `/dev/null` on Windows when building `System.build_info`
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Autocomplete] Resolves issue with autocompletion on structs not working
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Also store external resources that are not part of the current working directory in compilation manifest
|
||||
* [Mix] Always include the compiled file source in manifests
|
||||
|
||||
## v1.3.2 (2016-07-15)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Support guards in `else` clauses in `with`
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add `MIX_NO_DEPS` env var for disabling dep loading. Used for third-party scripts and tools like Nix package manager
|
||||
* [Mix] Add `mix test --listen-on-stdin` that automatically reruns tests on stdin
|
||||
* [Mix] Disable `--warnings-as-errors` when compiling dependencies
|
||||
* [Mix] Add `--filter` option to `mix deps.unlock` to unlock only matching dependencies
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Enum] Return `nil` if enumerable halts in `Enum.find_index/3`
|
||||
* [Kernel] Do not attempt to load modules that have not been required when invoking regular functions, otherwise this invalidates the `@compile {:autoload, false}` directive.
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Ensure missing protocol dependencies are discarded in umbrella projects with shared build
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit.Diff] Ensure no leading or trailing when diffing some maps
|
||||
|
||||
## v1.3.1 (2016-06-28)
|
||||
|
||||
### 1. Enhancements
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] `Add import_file_if_available` for importing files only if they are available
|
||||
* [IEx.Helpers] `Add import_if_available` for importing modules only if they are available
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Kernel] Ensure structs can be expanded in dynamic module names
|
||||
* [Kernel] Ensure aliases warnings are not accidentally discarded when the same module is imported
|
||||
* [Kernel.ParallelCompiler] Ensure two modules with cyclic struct dependencies cannot run into a deadlock when compiling
|
||||
* [Kernel.Typespec] Support module attributes in remote types
|
||||
* [Module] Do not expect stacktraces to be always present when dispatching to locals during the module compilation
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx.Helpers] Fix `h` helper for operators
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Do not load modules for xref purposes, instead use BEAM info
|
||||
* [Mix] Ensure `deps.check` does not check archives (that's done in loadpaths)
|
||||
* [Mix] Validate application properties before traversing them
|
||||
* [Mix] Check for proper Makefile when compiling on Windows
|
||||
* [Mix] Enforce space after comma in `mix do`
|
||||
|
||||
## v1.3.0 (2016-06-21)
|
||||
|
||||
### 1. Enhancements
|
||||
* [Application] Add `Application.fetch_env!/2`, `Application.loaded_applications/0` and `Application.started_applications/0`
|
||||
* [CLI] Add support for `--werl` in Windows bash-like shells
|
||||
* [Dict] Add `Dict.get_and_update/3` which behaves similar to the now deprecated Access protocol
|
||||
* [Dict] Add `Dict.get_lazy/3`, `Dict.pop_lazy/3` and `Dict.put_new_lazy/3`
|
||||
* [Enum] Add `Enum.random/1`, `Enum.take_random/2`, `Enum.min_max/1`, `Enum.min_max_by/2`, `Enum.reverse_slice/3`, `Enum.reduce_while/3`, `Enum.dedup/1` and `Enum.dedup_by/2`
|
||||
* [Enum] Inline common map usage in `Enum` functions for performance
|
||||
* [File] Add `File.lstat/1` and `File.lstat/1` that works like `File.stat/1` but is able to return symlink information (i.e. it does not traverse symlinks)
|
||||
* [File] Add `File.rename/2`
|
||||
* [Integer] Add `Integer.digits/2` and `Integer.undigits/2`
|
||||
* [Inspect] Add the `:safe` option to `inspect/2` and make it safe by default, meaning failures while inspecting won't trigger other failures. Instead, it will be wrapped in an exception which is properly formatted
|
||||
* [IO] Support fenced code blocks on `IO.ANSI.Docs`
|
||||
* [GenServer] Add `GenServer.whereis/1` that expands `GenServer` dispatches into a proper pid
|
||||
* [Kernel] No longer include `:crypto` and `:syntax_tools` as dependencies. The former is only needed if you have encrypted debug info (therefore you can add `:crypto` as a dependency manually) and the latter is no longer used
|
||||
* [Kernel] Raise when `var.Alias` syntax is used and it does not expand to an atom at compile time (previously it emitted warnings)
|
||||
* [Kernel] Improve generation of argument names for function signatures
|
||||
* [Kernel] `::/2` is now a special form
|
||||
* [Kernel] Warn when a variable with underscore is used
|
||||
* [Kernel] Allow underscores in binary, octal and hex literals
|
||||
* [Kernel] Warn when module attributes, variables, strings and numbers are used in code but the expression has no effect
|
||||
* [Kernel] Support `\uXXXX` and `\u{X*}` in strings and char lists to map to Unicode codepoints
|
||||
* [List] Add `List.keytake/3`
|
||||
* [Module] Improve name inference for function signatures in documentation metadata
|
||||
* [Process] Add `Process.hibernate/3`
|
||||
* [Process] Allow a list of specs in `Process.info/2`
|
||||
* [Set] Introduce `MapSet` data type. This new data type uses maps behind the scenes and is useful for storing a dozens of items in Erlang 17. In future versions when maps efficiently support large collections, it is meant to be the main Set abstraction in Elixir
|
||||
* [Stream] Add `Stream.dedup/1`, `Stream.dedup_by/2` and `Stream.transform/4`
|
||||
* [String] Support calculation of the jaro distance between strings (usually names) via `String.jaro_distance/2`. This is used by Mix to support "Did you mean?" feature when a task does not exist
|
||||
* [String] Add `String.splitter/3` that splits strings as a stream
|
||||
* [StringIO] `StringIO.flush/1` was added to flush the output of a StringIO device
|
||||
* [Task] Introduce `Task.yield/2` and `Task.shutdown/2` to check if a task is still executing and shutdown otherwise
|
||||
* [Tuple] Add `Tuple.append/2`
|
||||
* [URI] Default ports were added for "ws" and "wss" schemas
|
||||
* [URI] Add `URI.to_string/1`
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx.Engine] Support an `init/1` function in engines that will return the initial buffer (defaults to an empty string)
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Access] Add support for `Access.all/0`, `Access.elem/1`, `Access.key/2` and `Access.key!/1` for traversing nested data structures
|
||||
* [Calendar] Add `Calendar` and `Date`, `Time`, `NaiveDateTime` and `DateTime` types
|
||||
* [CLI] Add `--logger-otp-reports BOOL` and `--logger-sasl-reports BOOL` switches
|
||||
* [Compiler] Emit a summary of compilation errors when modules are missing
|
||||
* [Enum] Add `Enum.group_by/3` that allows developers to map on the value being grouped
|
||||
* [Enum] Make list values in maps returned by `Enum.group_by/2` and `Enum.group_by/3` preserve the order of the input enumerable instead of reversing it.
|
||||
* [Enum] Add `Enum.drop_every/2` that drops every `nth`, including the first one
|
||||
* [Exception] Suggest possible functions on `UndefinedFunctionError` for existing modules
|
||||
* [Exception] Warn if unknown fields are given to `raise/2`
|
||||
* [File] Support IO devices in `File.copy/3`
|
||||
* [GenServer] Raise a more meaningful exit if you try to `GenServer.call/3` yourself
|
||||
* [Inspect] Support `:base` option when inspecting binaries
|
||||
* [IO] Add `IO.warn/2` that will print a warning message with stacktrace and notify the compiler a warning was printed (in case --warnings-as-errors was enabled)
|
||||
* [Kernel] Support `generated: true` in quote
|
||||
* [Kernel] Support `Kernel.pop_in/1` and `Kernel.pop_in/2` for yanking a value from a nested data structure
|
||||
* [Kernel] Allow variable struct names when matching, for example, `%module{key: "value"} = struct`
|
||||
* [Kernel] Allow guards on the left side of `<-` in `for` and `with` special forms
|
||||
* [Kernel] Support `else` chunks in `with`
|
||||
* [Kernel] Track `{module, function, arity}` imports and warn on unused ones when such are specified in `:only`
|
||||
* [Kernel] Add `keyword/0` and `keyword/1` built-in types to typespecs
|
||||
* [Kernel] Add sigils for date (`~D[2015-04-17]`), time (`~T[08:00:00]`) and naive date times `~N[2015-04-17 08:00:00]`
|
||||
* [Kernel] Support `@enforce_keys` on `defstruct/1` to guarantee some keys are explicitly given when building structs
|
||||
* [OptionParser] Add support for `:count` switch type
|
||||
* [OptionParser] Add `parse!/2` and `parse_head!/2` that raise `OptionParser.ParseError` in case of errors
|
||||
* [Process] Add `Process.sleep/1`
|
||||
* [Range] `Range.range?/1` now checks the validity of a range.
|
||||
* [Regex] Support `:include_captures` in `Regex.split/3`
|
||||
* [String] Add `String.myers_difference/2` for calculating the difference between two strings
|
||||
* [System] Add `System.os_time/0` and `System.os_time/1`
|
||||
* [Typespec] Add support for `%{required(foo) => bar}` and `%{optional(foo) => bar}` forms (Erlang 19 only)
|
||||
* [Typespec] Add support for `@optional_callbacks` to mark certain that certain callbacks may be optionally implemented
|
||||
* [Typespec] Introduce `%{...}` to mean any map (Erlang 19 only)
|
||||
* [URI] Add `URI.merge/2`
|
||||
* [Version] Add `Version.parse!/1`
|
||||
* [EEx] Add `:trim` option to EEx that automatically trims the left side of `<%` and right side `%>` if only spaces and new lines preceed/follow them
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Show pinned variables on failed `assert ^left = right` and `assert match?(^left, right)` assertions
|
||||
* [ExUnit] Add `ExUnit.Case.register_attribute` which allow attributes to be cleaned up whenever a test is defined
|
||||
* [ExUnit] Add `ExUnit.Case.register_test` and support the ability to tag "tests" by type. This will allow projects like QuickCheck to change the wording in formatters to say "10 properties" instead of "10 tests"
|
||||
* [ExUnit] Support diffing of values when using `==` in `assert`
|
||||
* [ExUnit] Start running tests as soon as cases are loaded. This feature is enabled by default when running tests through Mix
|
||||
* [ExUnit] Raise a straight-forward error message in case a duplicate test name is defined
|
||||
* [ExUnit] Bump the default number of max cases to double of schedulers to support both IO and CPU bound tests
|
||||
* [ExUnit] Support for named setups in `setup` and `setup_all`
|
||||
* [ExUnit] Support for bundling tests together with `describe/2`
|
||||
* [ExUnit] Add number of skipped tests to `ExUnit` output
|
||||
* [ExUnit] Make timeout configurable for the whole test suite via the `:timeout` configuration
|
||||
* [ExUnit] Allow moduledoc to be filtered/skipped in doctests
|
||||
* [ExUnit] Provide built-in log capturing functionality
|
||||
* [ExUnit] Allow `assert_receive_timeout` and `refute_receive_timeout` to be configured in the ExUnit application
|
||||
* [ExUnit] Allow tests to be skipped with `@tag :skip` or `@tag skip: "reason"`
|
||||
* [ExUnit] Add tests without implementation (missing the do block) which automatically fail. Such tests are also automatically tagged as `:not_implemented`, allowing them to be skipped
|
||||
* [ExUnit] Increase by default stacktrace depth to 20 (this value is also configurable)
|
||||
* [ExUnit] Improve formatting on `assert_raise` errors for message mismatch
|
||||
* [ExUnit] Improve formatting on `assert_receive` when using pinned variables
|
||||
|
||||
#### IEx
|
||||
|
||||
* [IEx] Add `nl/2` that loads a given module on a list of nodes
|
||||
* [IEx.Helpers] No longer restart applications on `recompile/1`
|
||||
* [IEx.Autocomplete] Improve IEx expand to handle functions after `&`
|
||||
* [IEx] Support `IEx.pry` with `--remsh` for remote debugging
|
||||
* [IEx] Add `b/1` helper that shows documentation for behaviour modules and its callback functions
|
||||
* [IEx] Provide tab completion for aliases and allow aliases like `Foo.Bar.Baz` to autocomplete even if `Foo.Bar` is not defined
|
||||
* [IEx] Provide a `pid/3` helper for buildings pids from numbers
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Introduce `Logger.reset_metadata/0,1`
|
||||
* [Logger] Support printing pids and refs in Logger metadata
|
||||
* [Logger] Allow Logger metadata to be removed from pdict by setting it to `nil`
|
||||
* [Logger] Add application configuration `translator_inspect_opts` for logger to customize how state and message are formatted when translating OTP errors and reports
|
||||
* [Logger] Automatically include the current application in metadata when compiled via Mix
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Add `mix xref` and `mix compile.xref` that runs cross-reference checks, with the latter running after compilation by default
|
||||
* [Mix] Add `mix app.tree` and `mix deps.tree`
|
||||
* [Mix] Add `Mix.Task.rerun/2` that reenables and re-runs a task
|
||||
* [Mix] Integrate `OptionParser.ParseError` into Mix, automatically converting such exceptions into `Mix.Error` and embedding the task information
|
||||
* [Mix] Support `@preferred_cli_env` attribute when defining tasks
|
||||
* [Mix] Support `mix test --raise` that will raise when a test suite fails (instead of setting the exit code to 1)
|
||||
* [Mix] Enable rebar3 manager by default for Hex dependencies
|
||||
* [Mix] Add `mix escript.install` to install escripts
|
||||
* [Mix] Print stacktraces for `Mix.Error` when `MIX_DEBUG=1` is set
|
||||
* [Mix] Add a user friendly error for merge conflicts on `mix.lock`
|
||||
* [Mix] Track files between path dependencies. This means umbrella applications will no longer trigger full recompilation when a sibling changes. Instead it will only recompile the files affected by the sibling changes
|
||||
* [Mix] No longer print every file being compiled. Instead a generic "Compiling N files (.ext)" will be printed and files will only be logged in case they take more than 5 seconds to compile. This threshold can be customized by passing the `--long-compilation-threshold` flag and the previous behaviour can be reenabled by giving `--verbose` to `mix compile`
|
||||
* [Mix] Add `mix test --stale` that uses static analysis on source files to know which tests should run when source files changes. If any test file changes, it will also re-run. Changing a configuration file or the test helper will trigger a full recompilation
|
||||
* [Mix] Check Elixir version right after archive installation and provide feedback if there is a mismatch
|
||||
* [Mix] Allow rebar dependencies with `mix.exs` to be compiled with Mix
|
||||
* [Mix] Allow rebar dependencies to be specified via `:path`
|
||||
* [Mix] Also consider subdirectories in `config` directory for `Mix.Project.config_files/0`
|
||||
* [Mix] Allow dynamic configuration in Mix projects by storing config in an agent
|
||||
* [Mix] Support rebar3 style Git refs in `rebar.config` files
|
||||
* [Mix] Only recompile compile time dependencies in mix projects. This should considerably speed up recompilation times in Elixir projects
|
||||
* [Mix] Warn when configuring an application that is not available
|
||||
* [Mix] Add `mix profile.fprof` for easy code profiling
|
||||
* [Mix] Abort when dependencies have conflicting `:only` definitions
|
||||
* [Mix] Fully recompile projects if Elixir or SCM changes
|
||||
* [Mix] Allow checksum to be checked on archive install via `--sha512` option
|
||||
* [Mix] Add `mix local.public_keys` to safely manage installation of Hex and Rebar dependencies
|
||||
|
||||
### 2. Bug fixes
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Application] Ensure `Application.spec/2` returns nil for unknown applications
|
||||
* [GenServer] Ensures `cast/2` returns `:ok` if locally registered process is not found
|
||||
* [Inspect] Ensure binaries break into new lines when inspected
|
||||
* [Kernel] Do not choke on capture operator with argument above `&191`
|
||||
* [Kernel] Raise if `defstruct` is called multiple times
|
||||
* [Kernel] Ensure `Module.create/3` respects var/alias hygiene
|
||||
* [Kernel] Support non-literal ranges on the right side of `in/2`
|
||||
* [Macro] Fix `Macro.to_string/1` on a call of a capture argument, for example `&(&1).(:x)`
|
||||
* [OptionParser] Allow `OptionParser` to parse negative numbers
|
||||
* [Record] Fix `Record.is_record/2` when dealing with non-record tuples
|
||||
* [String] Ensure `strip` also removes non-breaking whitespaces (and ensure `split` still does not split on them)
|
||||
* [URI] Use square brackets for IPv6 in `URI.to_string/1`
|
||||
* [CLI] Ensure Logger messages are flushed when executing commands
|
||||
* [Code] `:delegate_locals_to` failed to delegate to the chosen module in many situations and messed up stacktraces. This option has therefore been replaced by imports
|
||||
* [Code] Store the documentation line in the metadata returned by `Code.get_docs/2`
|
||||
* [Exception] Do not fail when calculating an exception message, even if the message is invalid
|
||||
* [File] Ensure `File.touch/2` and `File.stat/2` receive and return universal times. Previously they would work with local times which are not monotonically increasing, which could present issues on scripts. If the times are being shown to the user, `time: :local` can be given as argument
|
||||
* [Float] Support complete scientific notation in `Float.parse/1`
|
||||
* [Kernel] Do not expand `in/2` argument in module body
|
||||
* [Kernel] Throw syntax error for undefind atom/alias syntax `:foo.Bar`
|
||||
* [Kernel] Improve error message when we can't compile because the target directory is not writeable
|
||||
* [Kernel] Allow capture of non-symbolic operators like `&and/2`, `¬/1` and others
|
||||
* [Kernel] Raise if heredoc terminal is accidentally found in the middle of a line without escaping
|
||||
* [Kernel] Don't warn on missing imports if nothing was imported
|
||||
* [Kernel] Raise if arity bigger than 255 is given to capture operator
|
||||
* [Macro] Properly convert captures in `Macro.to_string/1`
|
||||
* [Module] Do not accept non-Elixir module names in `Module.split/1`
|
||||
* [Protocol] Guarantee that derived protocols go through `Any` instead of `Map`
|
||||
* [Range] Restrict ranges to integers to fix diverse bugs of values being included in the range when they should not (false positives)
|
||||
* [Regex] Fix splitting of empty strings with regexes when trim is set to `true`. Now both `String.split/3` and `Regex.split/3` return an empty list when called with an empty string and trim is enabled
|
||||
* [Regex] Fix `Regex.replace/4` so it doesn't discard escape characters
|
||||
* [Stream] Ensure suspending `Stream.flat_map/2` and `Stream.transform/3` does not consume unecessary items from the given enumerable
|
||||
|
||||
#### EEx
|
||||
|
||||
* [EEx] Allow EEx interpolation to also apply inside quotations `<%%= ... %>`
|
||||
|
||||
#### ExUnit
|
||||
|
||||
* [ExUnit] Skipped tests now correctly count towards the total of tests in the result returned by `ExUnit.run/0`
|
||||
* [ExUnit] Fix a bug where failures when inspecting data structure or retrieving error messages could bring the whole ExUnit runner down
|
||||
* [ExUnit] Do not change the semantics of evaluated code with `assert`/`refute`. For example, from now on, `assert nil = some_expr()` will now raise as expected, as the expression still evaluates to a falsy value
|
||||
* [ExUnit] Report proper line number for doctest failures
|
||||
|
||||
#### Logger
|
||||
|
||||
* [Logger] Include metadata in `Logger.log/3`, use `Logger.bare_log/3` for runtime-only, with no metadata behaviour
|
||||
|
||||
#### Mix
|
||||
|
||||
* [Mix] Improve task not found message when Mix would include the not found task as a suggestion due to different casing
|
||||
* [Mix] Ignore lock revision when the lock is out of date when updating Mix dependencies. Before this fix, Git tags and branches in the lock file would erroneously take higher precedence than the one in `mix.exs`
|
||||
* [Mix] Only recompile empty Elixir files if they change instead of recompiling them on every run
|
||||
* [Mix] Ensure .app file is written in UTF-8 (this allows app descriptions to contain UTF-8 characters)
|
||||
* [Mix.Dep] Always specify the `:env` option internally for dependencies to avoid false positives in the dependency resolution
|
||||
* [Mix.Dep] Correctly detect conflict from cousin optional dependencies in the dependency resolution algorithm
|
||||
* [Mix] Use the safer `https` protocol instead of `git` for `:github` dependencies
|
||||
* [Mix] Ensure automatic protocol consolidation via `:consolidate_protocols` is triggered in umbrella apps
|
||||
* [Mix] Do not raise if wildcard given to `import_config` does not match any file
|
||||
* [Mix] Applications with `:build_embedded` set to true require explicit compilation step
|
||||
* [Mix] Also remove consolidated protocols on `mix clean`
|
||||
* [Mix] Ensure `--exclude` in `mix test` concatenates with test helper excludes
|
||||
|
||||
### 3. Soft deprecations (no warnings emitted)
|
||||
|
||||
* [Float] `Float.to_string/2` and `Float.to_char_list/2` has been soft-deprecated as Elixir will now attempt to print the shortest and most accurate representation by default. Developers can always fallback to `:erlang.float_to_binary/2` and `:erlang.float_to_list/2` if they need the previous functionality
|
||||
* [Kernel] `to_char_list` functions have been soft-deprecated in favor of `to_charlist`. This aligns with the naming conventions in both Erlang and Elixir
|
||||
* [String] The confusing `String.strip/2`, `String.lstrip/2` and `String.rstrip/2` API has been soft deprecated in favor of `String.trim/2`, `String.trim_leading/2` and `String.trim_trailing/2`
|
||||
* [String] The confusing `String.ljust/3` and `String.rjust/3` API has been soft deprecated in favor of `String.pad_leading/3` and `String.pad_trailing/3`
|
||||
* [Typespec] `char_list` is soft-deprecated in favor of `charlist`
|
||||
#### Elixir
|
||||
|
||||
* [Behaviour] The module `Behaviour` is deprecated. Instead of `defcallback`, one can simply use `@callback`. Instead of `defmacrocallback`, one can simply use `@macrocallback`
|
||||
* [Enum] `Enum.uniq/2` is deprecated in favor of `Enum.uniq_by/2`
|
||||
* [Kernel] `\x` inside strings and charlists is deprecated in favor of `\uXXXX` and `\u{X*}`. The values emitted by `\x` are unfortunately wrong (they should be bytes but currently it emits codepoints). `\u` is meant to correctly map to codepoints and `\x` will be fixed in the future to map to bytes
|
||||
* [Regex] Ungreedy option `r` is deprecated in favor of `U` (which is standard in regular expressions in other languages)
|
||||
|
||||
### 4. Deprecations
|
||||
|
||||
This release deprecates many APIs that have been soft-deprecated in previous Elixir versions.
|
||||
|
||||
#### Elixir
|
||||
|
||||
* [Dict] `Dict` is no longer a behaviour and its functions will be deprecated in upcoming releases
|
||||
* [Enum] Passing a dictionary to `Enum.group_by/3` is deprecated
|
||||
* [Kernel] `\x{H*}` in strings/sigils/charlists is deprecated
|
||||
* [Kernel] Add deprecation for `defdelegate` list arguments and `:append_first` option. The previously undocumented and deprecated support for matching has been removed
|
||||
* [Kernel] Warn if a variable is assigned inside `case`/`if`/etc and used outside the block
|
||||
* [Keyword] `Keyword.size/1` is deprecated in favor of `Kernel.length/1`
|
||||
* [Map] `Map.size/1` is deprecated in favor of `Kernel.map_size/1`
|
||||
* [Regex] The option `/r` (for ungreedy) has been deprecated in favor of `/U`
|
||||
* [Set] `Set` is no longer a behaviour and its functions will be deprecated in upcoming releases
|
||||
* [String] `String.valid_character?/1` is deprecated in favor of `String.valid?/1` with pattern matching
|
||||
* [Task] `Task.find/2` is deprecated in favor of explicit message matching
|
||||
* [URI] Passing a non-map to `URI.decode_query/2` is deprecated
|
||||
* [Access] Implementing the Access protocol is deprecated. The Access protocol relies on the code server in development and test mode (when protocol consolidation is not applied) and it generated a bottleneck when working with multiple processes and the Access protocol was invoked hundreds of times (which is not uncommon). Note the `Access` module and the `opts[key]` syntax are not affected and they are not deprecated, only the underlying protocol dispatch
|
||||
* [Kernel] `?\xHEX` is deprecated in favor of `0xHEX`. There is no situation where the former should be used in favor of the latter and the latter is always cleaner
|
||||
* [Kernel] Giving `as: true | false` to `alias/2` and `require/2` have been deprecated (it was undocumented behaviour)
|
||||
* [String] Passing an empty string to `starts_with?`, `contains?` and `ends_with?` had dubious behaviour and have been deprecated to help developers identify possible bugs in their source code
|
||||
|
||||
## v1.2
|
||||
## v1.0
|
||||
|
||||
The CHANGELOG for v1.2 releases can be found [in the v1.2 branch](https://github.com/elixir-lang/elixir/blob/v1.2/CHANGELOG.md).
|
||||
The CHANGELOG for v1.0 releases can be found [in the v1.0 branch](https://github.com/elixir-lang/elixir/blob/v1.0/CHANGELOG.md).
|
||||
|
||||
+14
-48
@@ -1,56 +1,22 @@
|
||||
# Code of Conduct
|
||||
# Contributor Code of Conduct
|
||||
|
||||
Contact: elixir-lang-conduct@googlegroups.com
|
||||
As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
||||
|
||||
## Why have a Code of Conduct?
|
||||
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.
|
||||
|
||||
As contributors and maintainers of this project, we are committed to providing a friendly, safe and welcoming environment for all, regardless of age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
The goal of the Code of Conduct is to specify a baseline standard of behavior so that people with different social values and communication styles can talk about Elixir effectively, productively, and respectfully, even in face of disagreements. The Code of Conduct also provides a mechanism for resolving conflicts in the community when they arise.
|
||||
* The use of sexualized language or imagery
|
||||
* Personal attacks
|
||||
* Trolling or insulting/derogatory comments
|
||||
* Public or private harassment
|
||||
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
|
||||
* Other unethical or unprofessional conduct.
|
||||
|
||||
## Our Values
|
||||
The Elixir Core Team has the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, we commit ourselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.
|
||||
|
||||
These are the values Elixir developers should aspire to:
|
||||
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
|
||||
|
||||
* Be friendly and welcoming
|
||||
* Be patient
|
||||
* Remember that people have varying communication styles and that not everyone is using their native language. (Meaning and tone can be lost in translation.)
|
||||
* Be thoughtful
|
||||
* Productive communication requires effort. Think about how your words will be interpreted.
|
||||
* Remember that sometimes it is best to refrain entirely from commenting.
|
||||
* Be respectful
|
||||
* In particular, respect differences of opinion. It is important that we resolve disagreements and differing views constructively.
|
||||
* Avoid destructive behavior
|
||||
* Derailing: stay on topic; if you want to talk about something else, start a new conversation.
|
||||
* Unconstructive criticism: don't merely decry the current state of affairs; offer (or at least solicit) suggestions as to how things may be improved.
|
||||
* Snarking (pithy, unproductive, sniping comments).
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by sending an e-mail to elixir-lang-conduct@googlegroups.com.
|
||||
|
||||
The following actions are explicitly forbidden:
|
||||
|
||||
* Insulting, demeaning, hateful, or threatening remarks.
|
||||
* Discrimination based on age, disability, gender, nationality, race, religion, sexuality, or similar personal characteristic.
|
||||
* Bullying or systematic harassment.
|
||||
* Unwelcome sexual advances.
|
||||
* Incitement to any of these.
|
||||
|
||||
## Where does the Code of Conduct apply?
|
||||
|
||||
If you participate in or contribute to the Elixir ecosystem in any way, you are encouraged to follow the Code of Conduct while doing so.
|
||||
|
||||
Explicit enforcement of the Code of Conduct applies to the official mediums operated by the Elixir project:
|
||||
|
||||
* The official GitHub projects and code reviews.
|
||||
* The official elixir-lang mailing lists.
|
||||
* The #elixir-lang IRC channel on Freenode.
|
||||
|
||||
Other Elixir activities (such as conferences, meetups, and other unofficial forums) are encouraged to adopt this Code of Conduct. Such groups must provide their own contact information.
|
||||
|
||||
Project maintainers may remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by emailing: elixir-lang-conduct@googlegroups.com. All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. **All reports will be kept confidential**.
|
||||
|
||||
**The goal of the Code of Conduct is to resolve conflicts in the most harmonious way possible**. We hope that in most cases issues may be resolved through polite discussion and mutual agreement. Bannings and other forceful measures are to be employed only as a last resort. **Do not** post about the issue publicly or try to rally sentiment against a particular individual or group.
|
||||
|
||||
## Acknowledgements
|
||||
|
||||
This document was based on the Code of Conduct from the Go project with parts derived from Django's Code of Conduct, Rust's Code of Conduct and the Contributor Covenant.
|
||||
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
|
||||
+275
@@ -0,0 +1,275 @@
|
||||
# Contributing to Elixir
|
||||
|
||||
Please take a moment to review this document in order to make the contribution
|
||||
process easy and effective for everyone involved!
|
||||
|
||||
Also make sure you read our [Code of Conduct](CODE_OF_CONDUCT.md) that
|
||||
outlines our commitment towards an open and welcoming environment.
|
||||
|
||||
## Using the issue tracker
|
||||
|
||||
Use the issues tracker for:
|
||||
|
||||
* [bug reports](#bug-reports)
|
||||
* [submitting pull requests](#pull-requests)
|
||||
|
||||
Please **do not** use the issue tracker for personal support requests nor feature requests. Support requests should be sent to:
|
||||
|
||||
* [the elixir-talk mailing list](https://groups.google.com/group/elixir-lang-talk)
|
||||
* [Stack Overflow](http://stackoverflow.com/questions/ask?tags=elixir)
|
||||
* **[#elixir-lang](irc://chat.freenode.net/elixir-lang)** IRC channel on [chat.freenode.net](http://www.freenode.net/)
|
||||
|
||||
Feature requests can be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core).
|
||||
|
||||
We do our best to keep the issue tracker tidy and organized, making it useful
|
||||
for everyone. For example, we classify open issues per application and perceived
|
||||
difficulty of the issue, making it easier for developers to
|
||||
[contribute to Elixir](#contributing).
|
||||
|
||||
## Bug reports
|
||||
|
||||
A bug is a _demonstrable problem_ that is caused by the code in the repository.
|
||||
Good bug reports are extremely helpful - thank you!
|
||||
|
||||
Guidelines for bug reports:
|
||||
|
||||
1. **Use the GitHub issue search** — [check if the issue has already been
|
||||
reported](https://github.com/elixir-lang/elixir/search?type=Issues).
|
||||
|
||||
2. **Check if the issue has been fixed** — try to reproduce it using the
|
||||
`master` branch in the repository.
|
||||
|
||||
3. **Isolate and report the problem** — ideally create a reduced test
|
||||
case.
|
||||
|
||||
Please try to be as detailed as possible in your report. Include information about
|
||||
your Operating System, your Erlang and Elixir versions. Please provide steps to
|
||||
reproduce the issue as well as the outcome you were expecting! All these details
|
||||
will help developers to fix any potential bugs.
|
||||
|
||||
Example:
|
||||
|
||||
> Short and descriptive example bug report title
|
||||
>
|
||||
> A summary of the issue and the environment in which it occurs. If suitable,
|
||||
> include the steps required to reproduce the bug.
|
||||
>
|
||||
> 1. This is the first step
|
||||
> 2. This is the second step
|
||||
> 3. Further steps, etc.
|
||||
>
|
||||
> `<url>` - a link to the reduced test case (e.g. a GitHub Gist)
|
||||
>
|
||||
> Any other information you want to share that is relevant to the issue being
|
||||
> reported. This might include the lines of code that you have identified as
|
||||
> causing the bug, and potential solutions (and your opinions on their
|
||||
> merits).
|
||||
|
||||
## Feature requests
|
||||
|
||||
Feature requests are welcome and should be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core). But take a moment to find
|
||||
out whether your idea fits with the scope and aims of the project. It's up to *you*
|
||||
to make a strong case to convince the community of the merits of this feature.
|
||||
Please provide as much detail and context as possible.
|
||||
|
||||
## Contributing
|
||||
|
||||
We incentivize everyone to contribute to Elixir and help us tackle
|
||||
existing issues! To do so, there are a few things you need to know
|
||||
about the code. First, Elixir code is divided in applications inside
|
||||
the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
send your pull requests.
|
||||
|
||||
## Contributing Documentation
|
||||
|
||||
Code documentation (`@doc`, `@moduledoc`, `@typedoc`) has a special convention:
|
||||
the first paragraph is considered to be a short summary.
|
||||
|
||||
For functions, macros and callbacks say what it will do. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
...
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
For modules, protocols and types say what it is. For example write
|
||||
something like:
|
||||
|
||||
```elixir
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
Information about a file.
|
||||
|
||||
...
|
||||
"""
|
||||
|
||||
defstruct [...]
|
||||
end
|
||||
```
|
||||
|
||||
Keep in mind that the first paragraph might show up in a summary somewhere, long
|
||||
texts in the first paragraph create very ugly summaries. As a rule of thumb
|
||||
anything longer than 80 characters is too long.
|
||||
|
||||
Try to keep unnecessary details out of the first paragraph, it's only there to
|
||||
give a user a quick idea of what the documented "thing" does/is. The rest of the
|
||||
documentation string can contain the details, for example when a value and when
|
||||
`nil` is returned.
|
||||
|
||||
If possible include examples, preferably in a form that works with doctests. For
|
||||
example:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Enum.filter([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
|
||||
[2]
|
||||
|
||||
"""
|
||||
def filter(collection, fun) ...
|
||||
```
|
||||
|
||||
This makes it easy to test the examples so that they don't go stale and examples
|
||||
are often a great help in explaining what a function does.
|
||||
|
||||
## Pull requests
|
||||
|
||||
Good pull requests - patches, improvements, new features - are a fantastic
|
||||
help. They should remain focused in scope and avoid containing unrelated
|
||||
commits.
|
||||
|
||||
**NOTE**: Do not send code style changes as pull requests like changing
|
||||
the indentation of some particular code snippet or how a function is called.
|
||||
Those will not be accepted as they pollute the repository history with non
|
||||
functional changes and are often based on personal preferences.
|
||||
|
||||
**IMPORTANT**: By submitting a patch, you agree that your work will be
|
||||
licensed under the license used by the project.
|
||||
|
||||
If you have any large pull request in mind (e.g. implementing features,
|
||||
refactoring code, etc), **please ask first** otherwise you risk spending
|
||||
a lot of time working on something that the project's developers might
|
||||
not want to merge into the project.
|
||||
|
||||
Please adhere to the coding conventions in the project (indentation,
|
||||
accurate comments, etc.) and don't forget to add your own tests and
|
||||
documentation. When working with Git, we recommend the following process
|
||||
in order to craft an excellent pull request:
|
||||
|
||||
1. [Fork](https://help.github.com/fork-a-repo/) the project, clone your fork,
|
||||
and configure the remotes:
|
||||
|
||||
```sh
|
||||
# Clone your fork of the repo into the current directory
|
||||
git clone https://github.com/<your-username>/elixir
|
||||
# Navigate to the newly cloned directory
|
||||
cd elixir
|
||||
# Assign the original repo to a remote called "upstream"
|
||||
git remote add upstream https://github.com/elixir-lang/elixir
|
||||
```
|
||||
|
||||
2. If you cloned a while ago, get the latest changes from upstream:
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
```
|
||||
|
||||
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
||||
or fix.
|
||||
|
||||
**IMPORTANT**: Making changes in `master` is discouraged. You should always
|
||||
keep your local `master` in sync with upstream `master` and make your
|
||||
changes in topic branches.
|
||||
|
||||
```sh
|
||||
git checkout -b <topic-branch-name>
|
||||
```
|
||||
|
||||
4. Commit your changes in logical chunks. Keep your commit messages organized,
|
||||
with a short description in the first line and more detailed information on
|
||||
the following lines. Feel free to use Git's
|
||||
[interactive rebase](https://help.github.com/articles/interactive-rebase)
|
||||
feature to tidy up your commits before making them public.
|
||||
|
||||
5. Make sure all the tests are still passing.
|
||||
|
||||
```sh
|
||||
make test
|
||||
```
|
||||
|
||||
This command will compile the code in your branch and use that
|
||||
version of Elixir to run the tests. This is needed to ensure your changes can
|
||||
pass all the tests.
|
||||
|
||||
6. Push your topic branch up to your fork:
|
||||
|
||||
```sh
|
||||
git push origin <topic-branch-name>
|
||||
```
|
||||
|
||||
7. [Open a Pull Request](https://help.github.com/articles/using-pull-requests/)
|
||||
with a clear title and description.
|
||||
|
||||
8. If you haven't updated your pull request for a while, you should consider
|
||||
rebasing on master and resolving any conflicts.
|
||||
|
||||
**IMPORTANT**: _Never ever_ merge upstream `master` into your branches. You
|
||||
should always `git rebase` on `master` to bring your changes up to date when
|
||||
necessary.
|
||||
|
||||
```sh
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
git checkout <your-topic-branch>
|
||||
git rebase master
|
||||
```
|
||||
|
||||
We have saved some excellent pull requests we have received in the past in case
|
||||
you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? – Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? – Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit – Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
Thank you for your contributions!
|
||||
@@ -1,18 +0,0 @@
|
||||
### Precheck
|
||||
|
||||
* Do not use the issues tracker for help or support (try Stack Overflow, IRC, mailing list, etc)
|
||||
* For proposing a new feature, please start a discussion on the Elixir Core mailing list
|
||||
* For bugs, do a quick search and make sure the bug has not yet been reported
|
||||
* Finally, be nice and have fun!
|
||||
|
||||
### Environment
|
||||
|
||||
* Elixir version (elixir -v):
|
||||
* Operating system:
|
||||
|
||||
### Current behavior
|
||||
|
||||
Include code samples, errors and stacktraces if appropriate.
|
||||
|
||||
### Expected behavior
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are copyright (c) 2012 Plataformatec
|
||||
covered under Elixir's license (see the file LICENSE) except the file
|
||||
mentioned below that contains sections that are copyright
|
||||
(c) 1996 Ericsson AB under Erlang's License (EPL):
|
||||
|
||||
lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
@@ -1,6 +1,6 @@
|
||||
REBAR ?= "$(CURDIR)/rebar"
|
||||
PREFIX ?= /usr/local
|
||||
DOCS := v1.3
|
||||
DOCS := v1.1
|
||||
CANONICAL := stable
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
@@ -12,19 +12,19 @@ INSTALL = install
|
||||
INSTALL_DIR = $(INSTALL) -m755 -d
|
||||
INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
GIT_REVISION = $(strip $(shell git rev-parse HEAD 2> /dev/null ))
|
||||
GIT_TAG = $(strip $(shell head="$(call GIT_REVISION)"; git tag --points-at $$head 2> /dev/null | tail -1) )
|
||||
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean install_man clean_man docs Docs.zip Precompiled.zip publish_zips publish_docs publish_mix
|
||||
.NOTPARALLEL: compile
|
||||
|
||||
#==> Functions
|
||||
|
||||
# This check should work for older versions like R16B
|
||||
# as well as new verions like 17.1 and 18
|
||||
define CHECK_ERLANG_RELEASE
|
||||
$(Q) erl -noshell -eval 'io:fwrite("~s", [erlang:system_info(otp_release) >= "18"])' -s erlang halt | grep -q '^true'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 18.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
$(Q) erl -noshell -eval 'io:fwrite("~s", [erlang:system_info(otp_release)])' -s erlang halt | grep -q '^1[789]'; \
|
||||
if [ $$? != 0 ]; then \
|
||||
echo "At least Erlang 17.0 is required to build Elixir"; \
|
||||
exit 1; \
|
||||
fi;
|
||||
endef
|
||||
|
||||
@@ -43,7 +43,7 @@ lib/$(1)/ebin/Elixir.$(2).beam: $(wildcard lib/$(1)/lib/*.ex) $(wildcard lib/$(1
|
||||
@ rm -rf lib/$(1)/ebin
|
||||
$(Q) cd lib/$(1) && ../../$$(ELIXIRC) "lib/**/*.ex" -o ebin
|
||||
|
||||
test_$(1): compile $(1)
|
||||
test_$(1): $(1)
|
||||
@ echo "==> $(1) (exunit)"
|
||||
$(Q) cd lib/$(1) && ../../bin/elixir -r "test/test_helper.exs" -pr "test/**/*_test.exs";
|
||||
endef
|
||||
@@ -74,7 +74,7 @@ elixir: stdlib lib/eex/ebin/Elixir.EEx.beam mix ex_unit logger eex iex
|
||||
|
||||
stdlib: $(KERNEL) VERSION
|
||||
$(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
$(Q) if [ ! -f $(KERNEL) ]; then \
|
||||
echo "==> bootstrap (compile)"; \
|
||||
$(ERL) -s elixir_compiler core -s erlang halt; \
|
||||
fi
|
||||
@@ -88,6 +88,7 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@ echo "==> unicode (compile)";
|
||||
@ echo "Embedding the Unicode database... (this may take a while)"
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) unicode/unicode.ex -o ebin;
|
||||
|
||||
$(eval $(call APP_TEMPLATE,ex_unit,ExUnit))
|
||||
@@ -126,18 +127,18 @@ clean:
|
||||
clean_exbeam:
|
||||
$(Q) rm -f lib/*/ebin/Elixir.*.beam
|
||||
|
||||
#==> Create Documentation
|
||||
#==> Create Documentation
|
||||
|
||||
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||
SOURCE_REF = $(shell tag="$(call GIT_TAG)" revision="$(call GIT_REVISION)"; echo "$${tag:-$$revision}\c")
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "$(call SOURCE_REF)" $(call LOGO_PATH) -o doc/$(2) -a http://elixir-lang.org/docs/$(CANONICAL)/$(2)/ -p http://elixir-lang.org/docs.html $(4)
|
||||
SOURCE_REF = $(shell head="$$(git rev-parse HEAD)" tag="$$(git tag --points-at $$head | tail -1)" ; echo "$${tag:-$$head}\c")
|
||||
COMPILE_DOCS = bin/elixir ../ex_doc/bin/ex_doc "$(1)" "$(VERSION)" "lib/$(2)/ebin" -m "$(3)" -u "https://github.com/elixir-lang/elixir" --source-ref "v$(VERSION)" $(call LOGO_PATH) -o doc/$(2) -a http://elixir-lang.org/docs/$(CANONICAL)/$(2)/ -p http://elixir-lang.org/docs.html
|
||||
|
||||
docs: compile ../ex_doc/bin/ex_doc docs_elixir docs_eex docs_mix docs_iex docs_ex_unit docs_logger
|
||||
|
||||
docs_elixir: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (elixir)"
|
||||
$(Q) rm -rf doc/elixir
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel,-e "lib/elixir/pages/Behaviours.md" -e "lib/elixir/pages/Naming Conventions.md" -e "lib/elixir/pages/Typespecs.md" -e "lib/elixir/pages/Writing Documentation.md")
|
||||
$(call COMPILE_DOCS,Elixir,elixir,Kernel)
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
@@ -172,12 +173,12 @@ docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
|
||||
Docs.zip: docs
|
||||
rm -rf Docs-v$(VERSION).zip
|
||||
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
zip -9 -r Docs-v$(VERSION).zip doc
|
||||
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
|
||||
|
||||
Precompiled.zip: build_man compile
|
||||
rm -rf Precompiled-v$(VERSION).zip
|
||||
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md lib/*/ebin LICENSE man NOTICE README.md VERSION
|
||||
zip -9 -r Precompiled-v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE man README.md VERSION
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
|
||||
|
||||
#==> Publish
|
||||
@@ -188,6 +189,16 @@ publish_docs: docs
|
||||
rm -rf ../docs/$(DOCS)/*/
|
||||
cp -R doc/* ../docs/$(DOCS)
|
||||
|
||||
# This task requires aws-cli to be installed and set up for access to s3.hex.pm
|
||||
# See: http://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-set-up.html
|
||||
|
||||
publish_mix: compile
|
||||
cd lib/mix && MIX_ENV=prod mix escript.build
|
||||
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/v$(VERSION)/mix --acl public-read
|
||||
aws s3 cp lib/mix/mix s3://s3.hex.pm/builds/mix/mix --acl public-read
|
||||
rm lib/mix/mix
|
||||
rm -rf lib/mix/_build
|
||||
|
||||
#==> Tests tasks
|
||||
|
||||
test: test_erlang test_elixir
|
||||
@@ -205,7 +216,11 @@ $(TEST_EBIN)/%.beam: $(TEST_ERL)/%.erl
|
||||
$(Q) mkdir -p $(TEST_EBIN)
|
||||
$(Q) $(ERLC) -o $(TEST_EBIN) $<
|
||||
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_mix test_eex test_iex
|
||||
test_elixir: test_stdlib test_ex_unit test_logger test_doc_test test_mix test_eex test_iex
|
||||
|
||||
test_doc_test: compile
|
||||
@ echo "==> doctest (exunit)"
|
||||
$(Q) cd lib/elixir && ../../bin/elixir -r "test/doc_test.exs";
|
||||
|
||||
test_stdlib: compile
|
||||
@ echo "==> elixir (exunit)"
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are copyright (c) 2012 Plataformatec
|
||||
covered under Elixir's license (see the file LICENSE) except the cases
|
||||
below.
|
||||
|
||||
== lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
|
||||
Copyright Ericsson AB 1996-2015
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -1,14 +1,12 @@
|
||||

|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
|
||||
For more about Elixir, installation and documentation,
|
||||
[check Elixir's website](http://elixir-lang.org/).
|
||||
For more about Elixir, installation and documentation, [check Elixir's website](http://elixir-lang.org/).
|
||||
|
||||
## Usage
|
||||
|
||||
To run Elixir from source, clone this repository to your machine, compile and test it:
|
||||
If you want to contribute to Elixir or run it from source, clone this repository to your machine, compile and test it:
|
||||
|
||||
```sh
|
||||
git clone https://github.com/elixir-lang/elixir.git
|
||||
@@ -16,120 +14,54 @@ cd elixir
|
||||
make clean test
|
||||
```
|
||||
|
||||
> Note: if you are running on Windows,
|
||||
[this article includes important notes for compiling Elixir from source
|
||||
on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
> Note: if you are running on Windows, [this article includes important notes for compiling Elixir from source on Windows](https://github.com/elixir-lang/elixir/wiki/Windows).
|
||||
|
||||
If Elixir fails to build (specifically when pulling in a new version via
|
||||
`git`), be sure to remove any previous build artifacts by running
|
||||
`make clean`, then `make test`.
|
||||
If Elixir fails to build (specifically when pulling in a new version via `git`), be sure to remove any previous build artifacts by running `make clean`, then `make test`.
|
||||
|
||||
If tests pass, you are ready to move on to the
|
||||
[Getting Started guide][1] or to try Interactive Elixir by running:
|
||||
`bin/iex` in your terminal.
|
||||
If tests pass, you are ready to move on to the [Getting Started guide][1] or to try Interactive Elixir by running: `bin/iex` in your terminal.
|
||||
|
||||
However, if tests fail, it is likely you have an outdated Erlang version
|
||||
(Elixir requires Erlang 18.0 or later). You can check your Erlang version
|
||||
by calling `erl` in the command line. You will see some information as follows:
|
||||
However, if tests fail, it is likely you have an outdated Erlang version (Elixir requires Erlang 17.0 or later). You can check your Erlang version by calling `erl` in the command line. You will see some information as follows:
|
||||
|
||||
Erlang/OTP 18 [erts-7.0] [source] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false]
|
||||
`Erlang/OTP 17 [erts-6.0] [source-07b8f44] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]`
|
||||
|
||||
If you have the correct version and tests still fail, please
|
||||
[open an issue][2].
|
||||
|
||||
## Contributing
|
||||
|
||||
We welcome everyone to contribute to Elixir and help us tackle
|
||||
existing issues! To do so, there are a few things you need to know
|
||||
about the code. First, Elixir code is divided in applications inside
|
||||
the `lib` folder:
|
||||
|
||||
* `elixir` - Contains Elixir's kernel and stdlib
|
||||
|
||||
* `eex` - Template engine that allows you to embed Elixir
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
|
||||
* `logger` - The built-in logger
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
|
||||
You can run all tests in the root directory with `make test` and you can
|
||||
also run tests for a specific framework `make test_#{NAME}`, for example,
|
||||
`make test_ex_unit`.
|
||||
|
||||
In case you are changing a single file, you can compile and run tests only
|
||||
for that particular file for fast development cycles. For example, if you
|
||||
are changing the String module, you can compile it and run its tests as:
|
||||
|
||||
```sh
|
||||
bin/elixirc lib/elixir/lib/string.ex -o lib/elixir/ebin
|
||||
bin/elixir lib/elixir/test/elixir/string_test.exs
|
||||
```
|
||||
|
||||
After your changes are done, please remember to run the full suite with
|
||||
`make test`.
|
||||
|
||||
From time to time, your tests may fail in an existing Elixir checkout and
|
||||
may require a clean start by running `make clean compile`. You can always
|
||||
check [the official build status on Travis-CI](https://travis-ci.org/elixir-lang/elixir).
|
||||
|
||||
With tests running and passing, you are ready to contribute to Elixir and
|
||||
[send a pull request](https://help.github.com/articles/using-pull-requests/).
|
||||
We have saved some excellent pull requests we have received in the past in
|
||||
case you are looking for some examples:
|
||||
|
||||
* [Implement Enum.member? – Pull Request](https://github.com/elixir-lang/elixir/pull/992)
|
||||
* [Add String.valid? – Pull Request](https://github.com/elixir-lang/elixir/pull/1058)
|
||||
* [Implement capture_io for ExUnit – Pull Request](https://github.com/elixir-lang/elixir/pull/1059)
|
||||
|
||||
We usually keep a list of enhancements and bugs [in the issue tracker][2].
|
||||
For proposing a new feature, please start a discussion in the
|
||||
[Elixir Core mailing list][3]. Keep in mind that it is your responsibility
|
||||
to argue and explain why a feature is useful and how it will impact the
|
||||
codebase and the community. Finally, remember all interactions in our official
|
||||
spaces follow our [Code of Conduct][7].
|
||||
If you have the correct version and tests still fail, feel free to [open an issue][2].
|
||||
|
||||
## Building documentation
|
||||
|
||||
Building the documentation requires [ExDoc](https://github.com/elixir-lang/ex_doc)
|
||||
to be installed and built alongside Elixir:
|
||||
Building the documentation requires [ExDoc](https://github.com/elixir-lang/ex_doc) to be installed and built in the same containing folder as Elixir.
|
||||
|
||||
```sh
|
||||
# After cloning and compiling Elixir, in its parent directory:
|
||||
# After cloning and compiling Elixir
|
||||
git clone git://github.com/elixir-lang/ex_doc.git
|
||||
cd ex_doc && ../elixir/bin/mix do deps.get, compile
|
||||
cd ../elixir && make docs
|
||||
```
|
||||
|
||||
This will produce documentation sets for `elixir`, `mix`, etc., under
|
||||
the `doc` directory. If you are planning to contribute documentation,
|
||||
[please check our best practices for writing documentation](http://elixir-lang.org/docs/stable/elixir/writing-documentation.html).
|
||||
## Contributing
|
||||
|
||||
## Development links
|
||||
We appreciate any contribution to Elixir. Check our [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) and [CONTRIBUTING.md](CONTRIBUTING.md) guides for more information. We usually keep a list of features and bugs [in the issue tracker][2].
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][6]
|
||||
* [Elixir Core Mailing list (development)][3]
|
||||
* [Issues tracker][2]
|
||||
* [Code of Conduct][7]
|
||||
* **[#elixir-lang][4]** on [Freenode][5] IRC
|
||||
## Important links
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][7]
|
||||
* **[#elixir-lang][5]** on [Freenode][6] IRC
|
||||
* [Issue tracker][2]
|
||||
* [elixir-talk Mailing list (questions)][3]
|
||||
* [elixir-core Mailing list (development)][4]
|
||||
|
||||
[1]: http://elixir-lang.org
|
||||
[2]: https://github.com/elixir-lang/elixir/issues
|
||||
[3]: https://groups.google.com/group/elixir-lang-core
|
||||
[4]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[5]: http://www.freenode.net
|
||||
[6]: http://elixir-lang.org/docs.html
|
||||
[7]: CODE_OF_CONDUCT.md
|
||||
[3]: https://groups.google.com/group/elixir-lang-talk
|
||||
[4]: https://groups.google.com/group/elixir-lang-core
|
||||
[5]: irc://chat.freenode.net/elixir-lang
|
||||
[6]: http://www.freenode.net/
|
||||
[7]: http://elixir-lang.org/docs.html
|
||||
|
||||
## License
|
||||
|
||||
"Elixir" and the Elixir logo are copyright (c) 2012 Plataformatec.
|
||||
|
||||
Elixir source code is released under Apache 2 License.
|
||||
Elixir source code is released under Apache 2 License with some parts under Erlang's license (EPL).
|
||||
|
||||
Check [NOTICE](NOTICE) and [LICENSE](LICENSE) files for more
|
||||
information.
|
||||
Check [LEGAL](LEGAL) and [LICENSE](LICENSE) files for more information.
|
||||
|
||||
+8
-14
@@ -24,26 +24,20 @@ This document simply outlines the release process:
|
||||
|
||||
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
11. Build and push standalone Mix with `make publish_mix` (requires AWS credentials)
|
||||
|
||||
## New vMAJOR.MINOR releases
|
||||
|
||||
11. Create a new branch "vMAJOR.MINOR"
|
||||
12. Create a new branch "vMAJOR.MINOR"
|
||||
|
||||
12. Move docs generation to `docs/vMAJOR.MINOR` in Makefile, set CANONICAL to stable and copy them to `docs/stable` (change index.html accordingly)
|
||||
13. Move docs generation to `docs/vMAJOR.MINOR` and copy them from `docs/stable`
|
||||
|
||||
13. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
14. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
|
||||
15. `make release_docs` and push it to `elixir-lang/docs`
|
||||
|
||||
## Places where version is mentioned
|
||||
|
||||
* VERSION
|
||||
* VERSION (make sure there is no newline in this file)
|
||||
* CHANGELOG.md
|
||||
* src/elixir.app.src (not lib/elixir/src/elixir.app.src)
|
||||
|
||||
## Deprecation policy
|
||||
|
||||
Elixir deprecations happens in 3 steps:
|
||||
|
||||
1. The feature is soft-deprecated. It means both CHANGELOG and documentation must list the feature as deprecated but no warning is effectively emitted by running the code. There is no requirement to soft-deprecate a feature.
|
||||
|
||||
2. The feature is effectively deprecated by emitting warnings on usage. In order to deprecate a feature, the proposed alternative MUST exist for AT LEAST two versions. For example, `Enum.uniq/2` was soft-deprecated in favor of `Enum.uniq_by/2` in Elixir v1.1. This means a deprecation warning may only be emitted by Elixir v1.3 or later.
|
||||
|
||||
3. The feature is removed. This can only happen on major releases. This means deprecated features in Elixir v1.x shall only be removed by Elixir v2.x.
|
||||
|
||||
+16
-33
@@ -2,25 +2,22 @@
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
-v Prints version and exit
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
@@ -71,20 +68,6 @@ while [ $I -le $# ]; do
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL `echo $PEEK | cut -c 2-` "$VAL""
|
||||
;;
|
||||
--logger-otp-reports)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" == 'true' ] || [ "$VAL" == 'false' ]; then
|
||||
ERL="$ERL -logger handle_otp_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--logger-sasl-reports)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
if [ "$VAL" == 'true' ] || [ "$VAL" == 'false' ]; then
|
||||
ERL="$ERL -logger handle_sasl_reports "$VAL""
|
||||
fi
|
||||
;;
|
||||
--erl)
|
||||
I=$(expr $I + 1)
|
||||
eval "VAL=\${$I}"
|
||||
|
||||
+29
-35
@@ -1,38 +1,34 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@echo off
|
||||
setlocal
|
||||
if ""%1""=="""" goto :documentation
|
||||
if /I ""%1""==""--help"" goto :documentation
|
||||
if /I ""%1""==""-h"" goto :documentation
|
||||
if /I ""%1""==""/h"" goto :documentation
|
||||
if ""%1""==""/?"" goto :documentation
|
||||
if ""%1""=="""" goto :documentation
|
||||
if ""%1""==""--help"" goto :documentation
|
||||
if ""%1""==""-h"" goto :documentation
|
||||
if ""%1""==""/h"" goto :documentation
|
||||
goto parseopts
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo -v Prints version and exits
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo -v Prints version and exit
|
||||
echo -e command Evaluates the given command (*)
|
||||
echo -r file Requires the given files/patterns (*)
|
||||
echo -S script Finds and executes the given script
|
||||
echo -pr file Requires the given files/patterns in parallel (*)
|
||||
echo -pa path Prepends the given path to Erlang code path (*)
|
||||
echo -pz path Appends the given path to Erlang code path (*)
|
||||
echo --app app Start the given app and its dependencies (*)
|
||||
echo --erl switches Switches to be passed down to erlang (*)
|
||||
echo --name name Makes and assigns a name to the distributed node
|
||||
echo --sname name Makes and assigns a short name to the distributed node
|
||||
echo --cookie cookie Sets a cookie for this distributed node
|
||||
echo --hidden Makes a hidden node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --werl Uses Erlang's Windows shell GUI
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
echo ** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:parseopts
|
||||
@@ -70,7 +66,7 @@ if "%par%"=="""" (
|
||||
rem ******* EXECUTION OPTIONS **********************
|
||||
IF "%par%"==""--werl"" (Set useWerl=1)
|
||||
IF "%par%"==""+iex"" (Set runMode="iex")
|
||||
rem ******* ELIXIR PARAMETERS **********************
|
||||
rem ******* elixir parameters **********************
|
||||
rem Note: we don't have to do anything with options that don't take an argument
|
||||
IF """"=="%par:-e=%" (shift)
|
||||
IF """"=="%par:-r=%" (shift)
|
||||
@@ -80,14 +76,12 @@ IF """"=="%par:-pz=%" (shift)
|
||||
IF """"=="%par:--app=%" (shift)
|
||||
IF """"=="%par:--remsh=%" (shift)
|
||||
rem ******* ERLANG PARAMETERS **********************
|
||||
IF """"=="%par:--detached=%" (Set parsErlang=%parsErlang% -detached)
|
||||
IF """"=="%par:--hidden=%" (Set parsErlang=%parsErlang% -hidden)
|
||||
IF """"=="%par:--cookie=%" (Set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
IF """"=="%par:--sname=%" (Set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
IF """"=="%par:--name=%" (Set parsErlang=%parsErlang% -name %1 && shift)
|
||||
IF """"=="%par:--logger-otp-reports=%" (Set parsErlang=%parsErlang% -logger handle_otp_reports %1 && shift)
|
||||
IF """"=="%par:--logger-sasl-reports=%" (Set parsErlang=%parsErlang% -logger handle_sasl_reports %1 && shift)
|
||||
IF """"=="%par:--erl=%" (Set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
IF """"=="%par:--detached=%" (Set parsErlang=%parsErlang% -detached)
|
||||
IF """"=="%par:--hidden=%" (Set parsErlang=%parsErlang% -hidden)
|
||||
IF """"=="%par:--cookie=%" (Set parsErlang=%parsErlang% -setcookie %1 && shift)
|
||||
IF """"=="%par:--sname=%" (Set parsErlang=%parsErlang% -sname %1 && shift)
|
||||
IF """"=="%par:--name=%" (Set parsErlang=%parsErlang% -name %1 && shift)
|
||||
IF """"=="%par:--erl=%" (Set beforeExtra=%beforeExtra% %~1 && shift)
|
||||
goto:startloop
|
||||
|
||||
rem ******* assume all pre-params are parsed ********************
|
||||
|
||||
+7
-7
@@ -2,16 +2,16 @@
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [elixir switches] [compiler switches] [.ex files]
|
||||
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
--verbose Print compilation status
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
-o The directory to output compiled files
|
||||
--no-docs Do not attach documentation to compiled modules
|
||||
--no-debug-info Do not attach debug info to compiled modules
|
||||
--ignore-module-conflict
|
||||
--warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
--verbose Print informational messages.
|
||||
|
||||
** Options given after -- are passed down to the executed code
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
** Options can be passed to the erlang compiler using ERL_COMPILER_OPTIONS" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
+10
-17
@@ -1,11 +1,9 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
setlocal
|
||||
@echo off
|
||||
set argc=0
|
||||
for %%A in (%*) do (
|
||||
if /I "%%A"=="--help" goto documentation
|
||||
if /I "%%A"=="-h" goto documentation
|
||||
if /I "%%A"=="/h" goto documentation
|
||||
if "%%A"=="/?" goto documentation
|
||||
if "%%A"=="--help" goto documentation
|
||||
if "%%A"=="-h" goto documentation
|
||||
if "%%A"=="/h" goto documentation
|
||||
set /A argc+=1
|
||||
)
|
||||
if %argc%==0 goto documentation
|
||||
@@ -14,20 +12,15 @@ goto run
|
||||
:documentation
|
||||
echo Usage: %~nx0 [elixir switches] [compiler switches] [.ex files]
|
||||
echo.
|
||||
echo -o The directory to output compiled files
|
||||
echo --no-docs Do not attach documentation to compiled modules
|
||||
echo --no-debug-info Do not attach debug info to compiled modules
|
||||
echo --verbose Print compilation status
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo -o The directory to output compiled files
|
||||
echo --no-docs Do not attach documentation to compiled modules
|
||||
echo --no-debug-info Do not attach debug info to compiled modules
|
||||
echo --ignore-module-conflict
|
||||
echo --warnings-as-errors Treat warnings as errors and return non-zero exit code
|
||||
echo --verbose Print informational messages.
|
||||
echo.
|
||||
echo ** Options given after -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS
|
||||
goto end
|
||||
|
||||
echo ** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS >&2
|
||||
:run
|
||||
call "%~dp0\elixir.bat" +elixirc %*
|
||||
|
||||
:end
|
||||
endlocal
|
||||
|
||||
@@ -2,29 +2,24 @@
|
||||
if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-e COMMAND Evaluates the given command (*)
|
||||
-r FILE Requires the given files/patterns (*)
|
||||
-S SCRIPT Finds and executes the given script in PATH
|
||||
-pr FILE Requires the given files/patterns in parallel (*)
|
||||
-pa PATH Prepends the given path to Erlang code path (*)
|
||||
-pz PATH Appends the given path to Erlang code path (*)
|
||||
|
||||
--app APP Starts the given app and its dependencies (*)
|
||||
--cookie COOKIE Sets a cookie for this distributed node
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
--hidden Makes a hidden node
|
||||
--logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
--logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
--name NAME Makes and assigns a name to the distributed node
|
||||
--no-halt Does not halt the Erlang VM after execution
|
||||
--sname NAME Makes and assigns a short name to the distributed node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
|
||||
--dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
--remsh NAME Connects to a node using a remote shell
|
||||
-v Prints version
|
||||
-e \"command\" Evaluates the given command (*)
|
||||
-r \"file\" Requires the given files/patterns (*)
|
||||
-S \"script\" Finds and executes the given script
|
||||
-pr \"file\" Requires the given files/patterns in parallel (*)
|
||||
-pa \"path\" Prepends the given path to Erlang code path (*)
|
||||
-pz \"path\" Appends the given path to Erlang code path (*)
|
||||
--app \"app\" Start the given app and its dependencies (*)
|
||||
--erl \"switches\" Switches to be passed down to Erlang (*)
|
||||
--name \"name\" Makes and assigns a name to the distributed node
|
||||
--sname \"name\" Makes and assigns a short name to the distributed node
|
||||
--cookie \"cookie\" Sets a cookie for this distributed node
|
||||
--hidden Makes a hidden node
|
||||
--werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
--detached Starts the Erlang VM detached from console
|
||||
--remsh \"name\" Connects to a node using a remote shell
|
||||
--dot-iex \"path\" Overrides default .iex.exs file and uses path instead;
|
||||
path can be empty, then no file will be loaded
|
||||
|
||||
** Options marked with (*) can be given more than once
|
||||
** Options given after the .exs file or -- are passed down to the executed code
|
||||
|
||||
+2
-45
@@ -1,45 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
SETLOCAL
|
||||
if /I ""%1""==""--help"" goto documentation
|
||||
if /I ""%1""==""-h"" goto documentation
|
||||
if /I ""%1""==""/h"" goto documentation
|
||||
if ""%1""==""/?"" goto documentation
|
||||
goto run
|
||||
|
||||
:documentation
|
||||
echo Usage: %~nx0 [options] [.exs file] [data]
|
||||
echo.
|
||||
echo -v Prints version and exits
|
||||
echo -e COMMAND Evaluates the given command (*)
|
||||
echo -r FILE Requires the given files/patterns (*)
|
||||
echo -S SCRIPT Finds and executes the given script in PATH
|
||||
echo -pr FILE Requires the given files/patterns in parallel (*)
|
||||
echo -pa PATH Prepends the given path to Erlang code path (*)
|
||||
echo -pz PATH Appends the given path to Erlang code path (*)
|
||||
echo.
|
||||
echo --app APP Starts the given app and its dependencies (*)
|
||||
echo --cookie COOKIE Sets a cookie for this distributed node
|
||||
echo --detached Starts the Erlang VM detached from console
|
||||
echo --erl SWITCHES Switches to be passed down to Erlang (*)
|
||||
echo --hidden Makes a hidden node
|
||||
echo --logger-otp-reports BOOL Enables or disables OTP reporting
|
||||
echo --logger-sasl-reports BOOL Enables or disables SASL reporting
|
||||
echo --name NAME Makes and assigns a name to the distributed node
|
||||
echo --no-halt Does not halt the Erlang VM after execution
|
||||
echo --sname NAME Makes and assigns a short name to the distributed node
|
||||
echo --werl Uses Erlang's Windows shell GUI (Windows only)
|
||||
echo.
|
||||
echo --dot-iex PATH Overrides default .iex.exs file and uses path instead;
|
||||
echo path can be empty, then no file will be loaded
|
||||
echo --remsh NAME Connects to a node using a remote shell
|
||||
echo.
|
||||
echo ** Options marked with (*) can be given more than once
|
||||
echo ** Options given after the .exs file or -- are passed down to the executed code
|
||||
echo ** Options can be passed to the Erlang VM using ELIXIR_ERL_OPTIONS or --erl
|
||||
goto end
|
||||
|
||||
:run
|
||||
@if defined IEX_WITH_WERL (@set __ELIXIR_IEX_FLAGS=--werl) else (set __ELIXIR_IEX_FLAGS=)
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %__ELIXIR_IEX_FLAGS% %*
|
||||
:end
|
||||
ENDLOCAL
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %*
|
||||
|
||||
+2
-2
@@ -1,2 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
call "%~dp0\elixir.bat" "%~dp0\mix" %*
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" -e Mix.start -e Mix.CLI.main %*
|
||||
|
||||
+6
-7
@@ -9,7 +9,7 @@ end
|
||||
defmodule EEx do
|
||||
@moduledoc ~S"""
|
||||
EEx stands for Embedded Elixir. It allows you to embed
|
||||
Elixir code inside a string in a robust way.
|
||||
Elixir code inside a string in a robust way:
|
||||
|
||||
iex> EEx.eval_string "foo <%= bar %>", [bar: "baz"]
|
||||
"foo baz"
|
||||
@@ -35,14 +35,13 @@ defmodule EEx do
|
||||
|
||||
## Options
|
||||
|
||||
All functions in this module accept EEx-related options.
|
||||
All functions in this module accepts EEx-related options.
|
||||
They are:
|
||||
|
||||
* `:line` - the line to be used as the template start. Defaults to 1.
|
||||
* `:file` - the file to be used in the template. Defaults to the given
|
||||
file the template is read from or to "nofile" when compiling from a string.
|
||||
* `:engine` - the EEx engine to be used for compilation.
|
||||
* `:trim` - trims whitespace left/right of quotation tags
|
||||
|
||||
## Engine
|
||||
|
||||
@@ -65,7 +64,7 @@ defmodule EEx do
|
||||
**must** use the equals sign (`=`). Since everything in
|
||||
Elixir is an expression, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-case
|
||||
`if/2` clauses, they are treated the same in EEx and
|
||||
`if` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
@@ -86,11 +85,11 @@ defmodule EEx do
|
||||
iex> EEx.eval_string "<%= @foo %>", assigns: [foo: 1]
|
||||
"1"
|
||||
|
||||
In other words, `<%= @foo %>` translates to:
|
||||
In other words, `<%= @foo %>` is simply translated to:
|
||||
|
||||
<%= {:ok, v} = Access.fetch(assigns, :foo); v %>
|
||||
<%= Dict.get assigns, :foo %>
|
||||
|
||||
The `assigns` extension is useful when the number of variables
|
||||
The assigns extension is useful when the number of variables
|
||||
required by the template is not specified at compilation time.
|
||||
"""
|
||||
|
||||
|
||||
+12
-12
@@ -17,7 +17,7 @@ defmodule EEx.Compiler do
|
||||
{:ok, tokens} ->
|
||||
state = %{engine: opts[:engine] || @default_engine,
|
||||
file: file, line: line, quoted: [], start_line: nil}
|
||||
generate_buffer(tokens, state.engine.init(opts), [], state)
|
||||
generate_buffer(tokens, "", [], state)
|
||||
{:error, line, message} ->
|
||||
raise EEx.SyntaxError, line: line, file: file, message: message
|
||||
end
|
||||
@@ -25,38 +25,38 @@ defmodule EEx.Compiler do
|
||||
|
||||
# Generates the buffers by handling each expression from the tokenizer
|
||||
|
||||
defp generate_buffer([{:text, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:text, chars}|t], buffer, scope, state) do
|
||||
buffer = state.engine.handle_text(buffer, IO.chardata_to_string(chars))
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:expr, line, mark, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:expr, line, mark, chars}|t], buffer, scope, state) do
|
||||
expr = Code.string_to_quoted!(chars, [line: line, file: state.file])
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), expr)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars} | t], buffer, scope, state) do
|
||||
defp generate_buffer([{:start_expr, start_line, mark, chars}|t], buffer, scope, state) do
|
||||
{contents, line, t} = look_ahead_text(t, start_line, chars)
|
||||
{contents, t} = generate_buffer(t, "", [contents | scope],
|
||||
{contents, t} = generate_buffer(t, "", [contents|scope],
|
||||
%{state | quoted: [], line: line, start_line: start_line})
|
||||
buffer = state.engine.handle_expr(buffer, IO.chardata_to_string(mark), contents)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
defp generate_buffer([{:middle_expr, line, _, chars} | t], buffer, [current | scope], state) do
|
||||
defp generate_buffer([{:middle_expr, line, _, chars}|t], buffer, [current|scope], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
generate_buffer(t, "", [wrapped | scope], %{state | line: line})
|
||||
generate_buffer(t, "", [wrapped|scope], %{state | line: line})
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | t], buffer, [current | _], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|t], buffer, [current|_], state) do
|
||||
{wrapped, state} = wrap_expr(current, line, buffer, chars, state)
|
||||
tuples = Code.string_to_quoted!(wrapped, [line: state.start_line, file: state.file])
|
||||
buffer = insert_quoted(tuples, state.quoted)
|
||||
{buffer, t}
|
||||
end
|
||||
|
||||
defp generate_buffer([{:end_expr, line, _, chars} | _], _buffer, [], state) do
|
||||
defp generate_buffer([{:end_expr, line, _, chars}|_], _buffer, [], state) do
|
||||
raise EEx.SyntaxError, message: "unexpected token #{inspect chars}", file: state.file, line: line
|
||||
end
|
||||
|
||||
@@ -74,14 +74,14 @@ defmodule EEx.Compiler do
|
||||
defp wrap_expr(current, line, buffer, chars, state) do
|
||||
new_lines = List.duplicate(?\n, line - state.line)
|
||||
key = length(state.quoted)
|
||||
placeholder = '__EEX__(' ++ Integer.to_charlist(key) ++ ');'
|
||||
placeholder = '__EEX__(' ++ Integer.to_char_list(key) ++ ');'
|
||||
{current ++ placeholder ++ new_lines ++ chars,
|
||||
%{state | quoted: [{key, buffer} | state.quoted]}}
|
||||
%{state | quoted: [{key, buffer}|state.quoted]}}
|
||||
end
|
||||
|
||||
# Look text ahead on expressions
|
||||
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars} | t]=list, start, contents) do
|
||||
defp look_ahead_text([{:text, text}, {:middle_expr, line, _, chars}|t]=list, start, contents) do
|
||||
if only_spaces?(text) do
|
||||
{contents ++ text ++ chars, line, t}
|
||||
else
|
||||
|
||||
@@ -4,8 +4,6 @@ defmodule EEx.Engine do
|
||||
|
||||
An engine needs to implement three functions:
|
||||
|
||||
* `init(opts)` - returns the initial buffer
|
||||
|
||||
* `handle_body(quoted)` - receives the final built quoted
|
||||
expression, should do final post-processing and return a
|
||||
quoted expression.
|
||||
@@ -27,7 +25,6 @@ defmodule EEx.Engine do
|
||||
default implementations for the functions above.
|
||||
"""
|
||||
|
||||
@callback init(Keyword.t) :: Macro.t
|
||||
@callback handle_body(Macro.t) :: Macro.t
|
||||
@callback handle_text(Macro.t, String.t) :: Macro.t
|
||||
@callback handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
@@ -37,10 +34,6 @@ defmodule EEx.Engine do
|
||||
quote do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def init(opts) do
|
||||
EEx.Engine.init(opts)
|
||||
end
|
||||
|
||||
def handle_body(body) do
|
||||
EEx.Engine.handle_body(body)
|
||||
end
|
||||
@@ -53,16 +46,13 @@ defmodule EEx.Engine do
|
||||
EEx.Engine.handle_expr(buffer, marker, expr)
|
||||
end
|
||||
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2, init: 1]
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Handles assigns in quoted expressions.
|
||||
|
||||
A warning will be printed on missing assigns.
|
||||
Future versions will raise.
|
||||
|
||||
This can be added to any custom engine by invoking
|
||||
`handle_assign/1` with `Macro.prewalk/2`:
|
||||
|
||||
@@ -74,34 +64,13 @@ defmodule EEx.Engine do
|
||||
"""
|
||||
def handle_assign({:@, meta, [{name, _, atom}]}) when is_atom(name) and is_atom(atom) do
|
||||
line = meta[:line] || 0
|
||||
quote line: line, do: EEx.Engine.fetch_assign!(var!(assigns), unquote(name))
|
||||
quote line: line, do: Dict.get(var!(assigns), unquote(name))
|
||||
end
|
||||
|
||||
def handle_assign(arg) do
|
||||
arg
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Raise on 2.0
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
{:ok, val} ->
|
||||
val
|
||||
:error ->
|
||||
keys = Enum.map(assigns, &elem(&1, 0))
|
||||
IO.warn "assign @#{key} not available in EEx template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect keys}"
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an empty string as initial buffer.
|
||||
"""
|
||||
def init(_opts) do
|
||||
""
|
||||
end
|
||||
|
||||
@doc """
|
||||
The default implementation simply returns the given expression.
|
||||
"""
|
||||
|
||||
@@ -2,7 +2,7 @@ defmodule EEx.Tokenizer do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Tokenizes the given charlist or binary.
|
||||
Tokenizes the given char list or binary.
|
||||
|
||||
It returns {:ok, list} with the following tokens:
|
||||
|
||||
@@ -17,7 +17,7 @@ defmodule EEx.Tokenizer do
|
||||
def tokenize(bin, line, opts \\ [])
|
||||
|
||||
def tokenize(bin, line, opts) when is_binary(bin) do
|
||||
tokenize(String.to_charlist(bin), line, opts)
|
||||
tokenize(String.to_char_list(bin), line, opts)
|
||||
end
|
||||
|
||||
def tokenize(list, line, opts) do
|
||||
@@ -25,7 +25,7 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [?%, ?< | buffer], acc
|
||||
tokenize t, line, opts, [?%, ?<|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, opts, buffer, acc) do
|
||||
@@ -52,11 +52,11 @@ defmodule EEx.Tokenizer do
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line + 1, opts, [?\n | buffer], acc
|
||||
tokenize t, line + 1, opts, [?\n|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([h | t], line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [h | buffer], acc
|
||||
defp tokenize([h|t], line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([], _line, _opts, buffer, acc) do
|
||||
@@ -75,16 +75,16 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
|
||||
defp expr([?%, ?> | t], line, buffer) do
|
||||
defp expr([?%, ?>|t], line, buffer) do
|
||||
{:ok, buffer, line, t}
|
||||
end
|
||||
|
||||
defp expr('\n' ++ t, line, buffer) do
|
||||
expr t, line + 1, [?\n | buffer]
|
||||
expr t, line + 1, [?\n|buffer]
|
||||
end
|
||||
|
||||
defp expr([h | t], line, buffer) do
|
||||
expr t, line, [h | buffer]
|
||||
defp expr([h|t], line, buffer) do
|
||||
expr t, line, [h|buffer]
|
||||
end
|
||||
|
||||
defp expr([], line, _buffer) do
|
||||
@@ -98,11 +98,11 @@ defmodule EEx.Tokenizer do
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
|
||||
defp token_name([h | t]) when h in [?\s, ?\t, ?)] do
|
||||
defp token_name([h|t]) when h in [?\s, ?\t, ?)] do
|
||||
token_name(t)
|
||||
end
|
||||
|
||||
defp token_name('od' ++ [h | _]) when h in [?\s, ?\t, ?)] do
|
||||
defp token_name('od' ++ [h|_]) when h in [?\s, ?\t, ?)] do
|
||||
:start_expr
|
||||
end
|
||||
|
||||
@@ -143,7 +143,7 @@ defmodule EEx.Tokenizer do
|
||||
Enum.find_index tokens, fn
|
||||
{:fn_paren, _} -> true
|
||||
{:fn, _} -> true
|
||||
_ -> false
|
||||
_ -> false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -190,7 +190,7 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n | _] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[?\n|_] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
@@ -198,14 +198,14 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
defp trim_right(rest, line) do
|
||||
case trim_whitespace(rest) do
|
||||
[?\r, ?\n | trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\n | trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\r, ?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[?\n|trimmed_rest] -> {true, trimmed_rest, line + 1}
|
||||
[] -> {true, [], line}
|
||||
_ -> {false, rest, line}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h | t]) when h == ?\s or h == ?\t do
|
||||
defp trim_whitespace([h|t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
|
||||
@@ -3,8 +3,6 @@ Code.require_file "../test_helper.exs", __DIR__
|
||||
defmodule EEx.SmartEngineTest do
|
||||
use ExUnit.Case, async: true
|
||||
|
||||
import ExUnit.CaptureIO
|
||||
|
||||
test "evaluates simple string" do
|
||||
assert_eval "foo bar", "foo bar"
|
||||
end
|
||||
@@ -17,13 +15,6 @@ defmodule EEx.SmartEngineTest do
|
||||
assert_eval "1", "<%= @foo %>", assigns: %{foo: 1}
|
||||
end
|
||||
|
||||
test "error with missing assigns" do
|
||||
stderr = capture_io(:stderr, fn ->
|
||||
assert_eval "", "<%= @foo %>", assigns: %{}
|
||||
end)
|
||||
assert stderr =~ "assign @foo not available in EEx template"
|
||||
end
|
||||
|
||||
test "evaluates with loops" do
|
||||
assert_eval "1\n2\n3\n", "<%= for x <- [1, 2, 3] do %><%= x %>\n<% end %>"
|
||||
end
|
||||
|
||||
@@ -158,6 +158,5 @@ baz %>
|
||||
|
||||
test "raise syntax error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||
assert T.tokenize('<%# true ', 1) == {:error, 1, "missing token '%>'"}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -99,7 +99,7 @@ defmodule EExTest do
|
||||
end
|
||||
|
||||
test "evaluates with parentheses after end in end token" do
|
||||
assert_eval " 101 102 103 ", "<%= Enum.map([1, 2, 3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
assert_eval " 101 102 103 ", "<%= Enum.map([1,2,3], (fn x -> %> <%= 100 + x %> <% end) ) %>"
|
||||
end
|
||||
|
||||
test "evaluates with defined variable" do
|
||||
@@ -236,10 +236,6 @@ foo
|
||||
assert_eval expected, string
|
||||
end
|
||||
|
||||
test "respects files" do
|
||||
assert_eval "sample.ex", "<%= __ENV__.file %>", [], file: "sample.ex"
|
||||
end
|
||||
|
||||
test "properly handle functions" do
|
||||
expected = """
|
||||
|
||||
@@ -316,7 +312,7 @@ foo
|
||||
assert_eval "\ndone\n", string, packages: nil, all: nil
|
||||
end
|
||||
|
||||
test "Unicode" do
|
||||
test "unicode" do
|
||||
template = """
|
||||
• <%= "•" %> •
|
||||
<%= "Jößé Vâlìm" %> Jößé Vâlìm
|
||||
@@ -344,7 +340,7 @@ foo
|
||||
end
|
||||
|
||||
test "raises an Exception when there's an error with the given file" do
|
||||
assert_raise File.Error, "could not read file \"non-existent.eex\": no such file or directory", fn ->
|
||||
assert_raise File.Error, "could not read file non-existent.eex: no such file or directory", fn ->
|
||||
filename = "non-existent.eex"
|
||||
EEx.compile_file(filename)
|
||||
end
|
||||
@@ -370,7 +366,7 @@ foo
|
||||
{EExTest.Compiled,
|
||||
:before_compile,
|
||||
0,
|
||||
[file: to_charlist(Path.relative_to_cwd(__ENV__.file)), line: 7]
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 7]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -379,7 +375,7 @@ foo
|
||||
{EExTest.Compiled,
|
||||
:after_compile,
|
||||
0,
|
||||
[file: to_charlist(Path.relative_to_cwd(__ENV__.file)), line: 22]
|
||||
[file: to_char_list(Path.relative_to_cwd(__ENV__.file)), line: 22]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,10 +392,6 @@ foo
|
||||
defmodule TestEngine do
|
||||
@behaviour EEx.Engine
|
||||
|
||||
def init(_opts) do
|
||||
""
|
||||
end
|
||||
|
||||
def handle_body(body) do
|
||||
{:wrapped, body}
|
||||
end
|
||||
|
||||
@@ -1 +1 @@
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
ExUnit.start [trace: "--trace" in System.argv]
|
||||
+49
-398
@@ -1,23 +1,15 @@
|
||||
defmodule Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures using the `data[key]` syntax.
|
||||
Dictionary-like access to data structures via the `foo[bar]` syntax.
|
||||
|
||||
Elixir provides two syntaxes for accessing values. `user[:name]`
|
||||
is used by dynamic structures, like maps and keywords, while
|
||||
`user.name` is used by structs. The main difference is that
|
||||
`user[:name]` won't raise if the key `:name` is missing but
|
||||
`user.name` will raise if there is no `:name` key.
|
||||
This module also empowers `Kernel`s nested update functions
|
||||
`Kernel.get_in/2`, `Kernel.put_in/3`, `Kernel.update_in/3` and
|
||||
`Kernel.get_and_update_in/3`.
|
||||
|
||||
Besides the cases above, this module provides convenience
|
||||
functions for accessing other structures, like `at/1` for
|
||||
lists and `elem/1` for tuples. Those functions can be used
|
||||
by the nested update functions in `Kernel`, such as
|
||||
`Kernel.get_in/2`, `Kernel.put_in/3`, `Kernel.update_in/3`,
|
||||
`Kernel.get_and_update_in/3` and friends.
|
||||
## Examples
|
||||
|
||||
## Dynamic lookups
|
||||
|
||||
Out of the box, Access works with `Keyword` and `Map`:
|
||||
Out of the box, Access works with built-in dictionaries: `Keyword`
|
||||
and `Map`:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -31,88 +23,13 @@ defmodule Access do
|
||||
iex> star_ratings[1.5]
|
||||
"★☆"
|
||||
|
||||
Access can be combined with `Kernel.put_in/3` to put a value
|
||||
in a given key:
|
||||
|
||||
iex> map = %{a: 1, b: 2}
|
||||
iex> put_in map[:a], 3
|
||||
%{a: 3, b: 2}
|
||||
|
||||
This syntax is very convenient as it can be nested arbitrarily:
|
||||
|
||||
iex> users = %{"john" => %{age: 27}, "meg" => %{age: 23}}
|
||||
iex> put_in users["john"][:age], 28
|
||||
%{"john" => %{age: 28}, "meg" => %{age: 23}}
|
||||
|
||||
Furthermore, Access transparently ignores `nil` values:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:c][:unknown]
|
||||
nil
|
||||
|
||||
Since Access is a behaviour, it can be implemented to key-value
|
||||
data structures. The implementation should be added to the
|
||||
module that defines the struct being access. Access requires the
|
||||
key comparison to be implemented using the `===` operator.
|
||||
|
||||
## Static lookups
|
||||
|
||||
The Access syntax (`foo[bar]`) cannot be used to access fields in
|
||||
structs, since structs do not implement the Access behaviour by
|
||||
default. It is also design decision: the dynamic access lookup
|
||||
is meant to be used for dynamic key-value structures, like maps
|
||||
and keywords, and not by static ones like structs.
|
||||
|
||||
Therefore Elixir provides a static lookup for map and structs
|
||||
fields. Imagine a struct named `User` with name and age fields.
|
||||
The following would raise:
|
||||
|
||||
user = %User{name: "john"}
|
||||
user[:name]
|
||||
** (UndefinedFunctionError) undefined function User.fetch/2
|
||||
(User does not implement the Access behaviour)
|
||||
|
||||
Structs instead use the `user.name` syntax:
|
||||
|
||||
user.name
|
||||
#=> "john"
|
||||
|
||||
The same `user.name` syntax can also be used by `Kernel.put_in/2`
|
||||
to for updating structs fields:
|
||||
|
||||
put_in user.name, "mary"
|
||||
%User{name: "mary"}
|
||||
|
||||
Differently from `user[:name]`, `user.name` is not extensible via
|
||||
a behaviour and is restricted to only maps and structs.
|
||||
|
||||
Summing up:
|
||||
|
||||
* `user[:name]` is used by dynamic structures, is extensible and
|
||||
does not raise on missing keys
|
||||
* `user.name` is used by static structures, it is not extensible
|
||||
and it will raise on missing keys
|
||||
|
||||
## Accessors
|
||||
|
||||
While Elixir provides built-in syntax only for traversing dynamic
|
||||
and static key-value structures, this module provides convenience
|
||||
functions for traversing other structures, like tuples and lists,
|
||||
to be used alongside `Kernel.put_in/2` in others.
|
||||
|
||||
For instance, given a user with a list of languages, here is how to
|
||||
deeply traverse the map and convert all language names to uppercase:
|
||||
|
||||
iex> user = %{name: "john",
|
||||
...> languages: [%{name: "elixir", type: :functional},
|
||||
...> %{name: "c", type: :procedural}]}
|
||||
iex> update_in user, [:languages, Access.all(), :name], &String.upcase/1
|
||||
%{name: "john",
|
||||
languages: [%{name: "ELIXIR", type: :functional},
|
||||
%{name: "C", type: :procedural}]}
|
||||
|
||||
See the functions `key/1`, `key!/1`, `elem/1` and `all/0` for the current
|
||||
accessors.
|
||||
The key comparison must be implemented using the `===` operator.
|
||||
"""
|
||||
|
||||
@type t :: list | map | nil
|
||||
@@ -120,23 +37,7 @@ defmodule Access do
|
||||
@type value :: any
|
||||
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback get(t, key, value) :: value
|
||||
@callback get_and_update(t, key, (value -> {value, value} | :pop)) :: {value, t}
|
||||
@callback pop(t, key) :: {value, t}
|
||||
|
||||
defmacrop raise_undefined_behaviour(e, struct, top) do
|
||||
quote do
|
||||
stacktrace = System.stacktrace
|
||||
e =
|
||||
case stacktrace do
|
||||
[unquote(top) | _] ->
|
||||
%{unquote(e) | reason: "#{inspect unquote(struct)} does not implement the Access behaviour"}
|
||||
_ ->
|
||||
unquote(e)
|
||||
end
|
||||
reraise e, stacktrace
|
||||
end
|
||||
end
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
|
||||
@doc """
|
||||
Fetches the container's value for the given key.
|
||||
@@ -146,27 +47,19 @@ defmodule Access do
|
||||
|
||||
def fetch(%{__struct__: struct} = container, key) do
|
||||
struct.fetch(container, key)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :fetch, [^container, ^key], _}
|
||||
end
|
||||
|
||||
def fetch(%{} = map, key) do
|
||||
:maps.find(key, map)
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) and is_atom(key) do
|
||||
def fetch(list, key) when is_list(list) do
|
||||
case :lists.keyfind(key, 1, list) do
|
||||
{^key, value} -> {:ok, value}
|
||||
false -> :error
|
||||
end
|
||||
end
|
||||
|
||||
def fetch(list, key) when is_list(list) do
|
||||
raise ArgumentError,
|
||||
"the Access calls for keywords expect the key to be an atom, got: " <> inspect(key)
|
||||
end
|
||||
|
||||
def fetch(nil, _key) do
|
||||
:error
|
||||
end
|
||||
@@ -185,28 +78,29 @@ defmodule Access do
|
||||
@doc """
|
||||
Gets and updates the container's value for the given key, in a single pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed
|
||||
from the map and returned.
|
||||
The argument function `fun` must receive the value for the given `key` (or
|
||||
`nil` if the key doesn't exist in `container`). It must return a tuple
|
||||
containing the `get` value and the new value to be stored in the `container`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new map with the updated value under `key`.
|
||||
This function returns a two-element tuple.
|
||||
The first element is the `get` value, as returned by `fun`.
|
||||
The second element is the container, updated with the value returned by `fun`.
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {get, value})) :: {get, t} when get: var
|
||||
@spec get_and_update(t, term, (term -> {get, term})) :: {get, t} when get: var
|
||||
def get_and_update(container, key, fun)
|
||||
|
||||
def get_and_update(%{__struct__: struct} = container, key, fun) do
|
||||
struct.get_and_update(container, key, fun)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :get_and_update, [^container, ^key, ^fun], _}
|
||||
end
|
||||
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
Map.get_and_update(map, key, fun)
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(list, key, fun) when is_list(list) do
|
||||
@@ -217,282 +111,39 @@ defmodule Access do
|
||||
raise ArgumentError,
|
||||
"could not put/update key #{inspect key} on a nil value"
|
||||
end
|
||||
end
|
||||
|
||||
def pop(%{__struct__: struct} = container, key) do
|
||||
struct.pop(container, key)
|
||||
rescue
|
||||
e in UndefinedFunctionError ->
|
||||
raise_undefined_behaviour e, struct, {^struct, :pop, [^container, ^key], _}
|
||||
end
|
||||
def pop(list, key) when is_list(list), do: Keyword.pop(list, key)
|
||||
def pop(map, key) when is_map(map) do
|
||||
case map do
|
||||
%{^key => value} -> {value, :maps.remove(key, map)}
|
||||
%{} -> {nil, map}
|
||||
# Callbacks invoked when inlining code for *_in in Kernel.
|
||||
# TODO: Remove me on 1.2
|
||||
defmodule Access.Map do
|
||||
@moduledoc false
|
||||
|
||||
def update!(%{} = map, key, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} ->
|
||||
:maps.put(key, fun.(value), map)
|
||||
:error ->
|
||||
raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
def pop(nil, key) do
|
||||
|
||||
def update!(other, key, _fun) do
|
||||
raise ArgumentError,
|
||||
"could not pop key #{inspect key} on a nil value"
|
||||
"could not put/update key #{inspect key}. Expected map/struct, got: #{inspect other}"
|
||||
end
|
||||
|
||||
## Accessors
|
||||
|
||||
@doc """
|
||||
Accesses the given key in a map/struct.
|
||||
|
||||
Uses the default value if the key does not exist
|
||||
or if the value being accessed is `nil`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_in(%{}, [Access.key(:unknown), Access.key(:name)])
|
||||
nil
|
||||
iex> get_in(%{}, [Access.key(:unknown, %{name: "john"}), Access.key(:name)])
|
||||
"john"
|
||||
iex> get_in(%{}, [Access.key(:unknown), Access.key(:name, "john")])
|
||||
"john"
|
||||
|
||||
iex> map = %{user: %{name: "john"}}
|
||||
iex> get_in(map, [Access.key(:unknown), Access.key(:name, "john")])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [Access.key(:user), Access.key(:name)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: %{name: "JOHN"}}}
|
||||
iex> pop_in(map, [Access.key(:user), Access.key(:name)])
|
||||
{"john", %{user: %{}}}
|
||||
|
||||
An error is raised if the accessed structure is not a map/struct/nil:
|
||||
|
||||
iex> get_in([], [Access.key(:foo)])
|
||||
** (RuntimeError) Access.key/1 expected a map/struct or nil, got: []
|
||||
|
||||
"""
|
||||
def key(key, default \\ nil) do
|
||||
fn
|
||||
:get, data, next ->
|
||||
next.(Map.get(to_map(data), key, default))
|
||||
:get_and_update, data, next ->
|
||||
value = Map.get(to_map(data), key, default)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, Map.put(data, key, update)}
|
||||
:pop -> {value, Map.delete(data, key)}
|
||||
end
|
||||
def get_and_update!(%{} = map, key, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} ->
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
:error ->
|
||||
raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
|
||||
defp to_map(nil), do: %{}
|
||||
defp to_map(%{} = map), do: map
|
||||
defp to_map(data), do: raise "Access.key/1 expected a map/struct or nil, got: #{inspect data}"
|
||||
|
||||
@doc """
|
||||
Accesses the given key in a map/struct.
|
||||
|
||||
Raises if the key does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{user: %{name: "john"}}
|
||||
iex> get_in(map, [Access.key!(:user), Access.key!(:name)])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [Access.key!(:user), Access.key!(:name)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: %{name: "JOHN"}}}
|
||||
iex> pop_in(map, [Access.key!(:user), Access.key!(:name)])
|
||||
{"john", %{user: %{}}}
|
||||
iex> get_in(map, [Access.key!(:user), Access.key!(:unknown)])
|
||||
** (KeyError) key :unknown not found in: %{name: \"john\"}
|
||||
|
||||
An error is raised if the accessed structure is not a map/struct:
|
||||
|
||||
iex> get_in([], [Access.key!(:foo)])
|
||||
** (RuntimeError) Access.key!/1 expected a map/struct, got: []
|
||||
|
||||
"""
|
||||
def key!(key) do
|
||||
fn
|
||||
:get, %{} = data, next ->
|
||||
next.(Map.fetch!(data, key))
|
||||
:get_and_update, %{} = data, next ->
|
||||
value = Map.fetch!(data, key)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, Map.put(data, key, update)}
|
||||
:pop -> {value, Map.delete(data, key)}
|
||||
end
|
||||
_op, data, _next ->
|
||||
raise "Access.key!/1 expected a map/struct, got: #{inspect data}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Accesses the element at the given index in a tuple.
|
||||
|
||||
Raises if the index is out of bounds.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{user: {"john", 27}}
|
||||
iex> get_in(map, [:user, Access.elem(0)])
|
||||
"john"
|
||||
iex> get_and_update_in(map, [:user, Access.elem(0)], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", %{user: {"JOHN", 27}}}
|
||||
iex> pop_in(map, [:user, Access.elem(0)])
|
||||
** (RuntimeError) cannot pop data from a tuple
|
||||
|
||||
An error is raised if the accessed structure is not a tuple:
|
||||
|
||||
iex> get_in(%{}, [Access.elem(0)])
|
||||
** (RuntimeError) Access.elem/1 expected a tuple, got: %{}
|
||||
|
||||
"""
|
||||
def elem(index) when is_integer(index) do
|
||||
pos = index + 1
|
||||
|
||||
fn
|
||||
:get, data, next when is_tuple(data) ->
|
||||
next.(:erlang.element(pos, data))
|
||||
:get_and_update, data, next when is_tuple(data) ->
|
||||
value = :erlang.element(pos, data)
|
||||
case next.(value) do
|
||||
{get, update} -> {get, :erlang.setelement(pos, data, update)}
|
||||
:pop -> raise "cannot pop data from a tuple"
|
||||
end
|
||||
_op, data, _next ->
|
||||
raise "Access.elem/1 expected a tuple, got: #{inspect data}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Accesses all the elements in a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.all(), :name])
|
||||
["john", "mary"]
|
||||
iex> get_and_update_in(list, [Access.all(), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{["john", "mary"], [%{name: "JOHN"}, %{name: "MARY"}]}
|
||||
iex> pop_in(list, [Access.all(), :name])
|
||||
{["john", "mary"], [%{}, %{}]}
|
||||
|
||||
Here is an example that traverses the list dropping even
|
||||
numbers and multipling odd numbers by 2:
|
||||
|
||||
iex> require Integer
|
||||
iex> get_and_update_in([1, 2, 3, 4, 5], [Access.all], fn
|
||||
...> num -> if Integer.is_even(num), do: :pop, else: {num, num * 2}
|
||||
...> end)
|
||||
{[1, 2, 3, 4, 5], [2, 6, 10]}
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.all()])
|
||||
** (RuntimeError) Access.all/0 expected a list, got: %{}
|
||||
|
||||
"""
|
||||
def all() do
|
||||
&all/3
|
||||
end
|
||||
|
||||
defp all(:get, data, next) when is_list(data) do
|
||||
Enum.map(data, next)
|
||||
end
|
||||
|
||||
defp all(:get_and_update, data, next) when is_list(data) do
|
||||
all(data, next, [], [])
|
||||
end
|
||||
|
||||
defp all(_op, data, _next) do
|
||||
raise "Access.all/0 expected a list, got: #{inspect data}"
|
||||
end
|
||||
|
||||
defp all([head | rest], next, gets, updates) do
|
||||
case next.(head) do
|
||||
{get, update} -> all(rest, next, [get | gets], [update | updates])
|
||||
:pop -> all(rest, next, [head | gets], updates)
|
||||
end
|
||||
end
|
||||
|
||||
defp all([], _next, gets, updates) do
|
||||
{:lists.reverse(gets), :lists.reverse(updates)}
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Accesses the element at `index` (zero based) of a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(1), :name])
|
||||
"mary"
|
||||
iex> get_and_update_in(list, [Access.at(0), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{"john", [%{name: "JOHN"}, %{name: "mary"}]}
|
||||
|
||||
`at/1` can also be used to pop elements out of a list or
|
||||
a key inside of a list:
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> pop_in(list, [Access.at(0)])
|
||||
{%{name: "john"}, [%{name: "mary"}]}
|
||||
iex> pop_in(list, [Access.at(0), :name])
|
||||
{"john", [%{}, %{name: "mary"}]}
|
||||
|
||||
When the index is out of bounds, `nil` is returned and the update function is never called:
|
||||
|
||||
iex> list = [%{name: "john"}, %{name: "mary"}]
|
||||
iex> get_in(list, [Access.at(10), :name])
|
||||
nil
|
||||
iex> get_and_update_in(list, [Access.at(10), :name], fn
|
||||
...> prev -> {prev, String.upcase(prev)}
|
||||
...> end)
|
||||
{nil, [%{name: "john"}, %{name: "mary"}]}
|
||||
|
||||
An error is raised for negative indexes:
|
||||
|
||||
iex> get_in([], [Access.at(-1)])
|
||||
** (FunctionClauseError) no function clause matching in Access.at/1
|
||||
|
||||
An error is raised if the accessed structure is not a list:
|
||||
|
||||
iex> get_in(%{}, [Access.at(1)])
|
||||
** (RuntimeError) Access.at/1 expected a list, got: %{}
|
||||
"""
|
||||
def at(index) when index >= 0 do
|
||||
fn(op, data, next) -> at(op, data, index, next) end
|
||||
end
|
||||
|
||||
defp at(:get, data, index, next) when is_list(data) do
|
||||
data |> Enum.at(index) |> next.()
|
||||
end
|
||||
|
||||
defp at(:get_and_update, data, index, next) when is_list(data) do
|
||||
get_and_update_at(data, index, next, [])
|
||||
end
|
||||
|
||||
defp at(_op, data, _index, _next) do
|
||||
raise "Access.at/1 expected a list, got: #{inspect data}"
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], 0, next, updates) do
|
||||
case next.(head) do
|
||||
{get, update} -> {get, :lists.reverse([update | updates], rest)}
|
||||
:pop -> {head, :lists.reverse(updates, rest)}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update_at([head | rest], index, next, updates) do
|
||||
get_and_update_at(rest, index - 1, next, [head | updates])
|
||||
end
|
||||
|
||||
defp get_and_update_at([], _index, _next, updates) do
|
||||
{nil, :lists.reverse(updates)}
|
||||
def get_and_update!(other, key, _fun) do
|
||||
raise ArgumentError,
|
||||
"could not put/update key #{inspect key}. Expected map/struct, got: #{inspect other}"
|
||||
end
|
||||
end
|
||||
|
||||
+9
-15
@@ -17,7 +17,7 @@ defmodule Agent do
|
||||
|
||||
defmodule Mix.TasksServer do
|
||||
def start_link do
|
||||
Agent.start_link(fn -> MapSet.new end, name: __MODULE__)
|
||||
Agent.start_link(fn -> HashSet.new end, name: __MODULE__)
|
||||
end
|
||||
|
||||
@doc "Checks if the task has already executed"
|
||||
@@ -31,13 +31,13 @@ defmodule Agent do
|
||||
@doc "Marks a task as executed"
|
||||
def put_task(task, project) do
|
||||
item = {task, project}
|
||||
Agent.update(__MODULE__, &MapSet.put(&1, item))
|
||||
Agent.update(__MODULE__, &Set.put(&1, item))
|
||||
end
|
||||
|
||||
@doc "Resets the executed tasks and returns the previous list of tasks"
|
||||
@doc "Resets the executed tasks and return the previous list of tasks"
|
||||
def take_all() do
|
||||
Agent.get_and_update(__MODULE__, fn set ->
|
||||
{Enum.into(set, []), MapSet.new}
|
||||
{Enum.into(set, []), HashSet.new}
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -295,18 +295,12 @@ defmodule Agent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the agent with the given `reason`.
|
||||
Stops the agent.
|
||||
|
||||
It returns `:ok` if the server terminates with the given
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
Returns `:ok` if the agent is stopped within the given `timeout`.
|
||||
"""
|
||||
@spec stop(agent, reason :: term, timeout) :: :ok
|
||||
def stop(agent, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(agent, reason, timeout)
|
||||
@spec stop(agent, timeout) :: :ok
|
||||
def stop(agent, timeout \\ 5000) do
|
||||
GenServer.call(agent, :stop, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,6 +23,10 @@ defmodule Agent.Server do
|
||||
{:reply, :ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
def handle_call(:stop, _from, state) do
|
||||
{:stop, :normal, :ok, state}
|
||||
end
|
||||
|
||||
def handle_call(msg, from, state) do
|
||||
super(msg, from, state)
|
||||
end
|
||||
@@ -39,6 +43,17 @@ defmodule Agent.Server do
|
||||
{:ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
def terminate(_reason, _state) do
|
||||
# There is a race condition if the agent is
|
||||
# restarted too fast and it is registered.
|
||||
try do
|
||||
self |> :erlang.process_info(:registered_name) |> elem(1) |> Process.unregister
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
defp initial_call(mfa) do
|
||||
_ = Process.put(:"$initial_call", get_initial_call(mfa))
|
||||
:ok
|
||||
|
||||
+21
-128
@@ -44,6 +44,9 @@ defmodule Application do
|
||||
including new values that are not defined in the environment file (although
|
||||
this should be avoided).
|
||||
|
||||
In the future, we plan to support configuration files which allow
|
||||
developers to configure the environment of their dependencies.
|
||||
|
||||
Keep in mind that each application is responsible for its environment.
|
||||
Do not use the functions in this module for directly accessing or modifying
|
||||
the environment of other applications (as it may lead to inconsistent
|
||||
@@ -90,65 +93,10 @@ defmodule Application do
|
||||
supervisor is automatically handled by the VM.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Called when an application is started.
|
||||
|
||||
This function is called when an the application is started using
|
||||
`Application.start/2` (and functions on top of that, such as
|
||||
`Application.ensure_started/2`). This function should start the top-level
|
||||
process of the application (which should be the top supervisor of the
|
||||
application's supervision tree if the application follows the OTP design
|
||||
principles around supervision).
|
||||
|
||||
`start_type` defines how the application is started:
|
||||
|
||||
* `:normal` - used if the startup is a normal startup or if the application
|
||||
is distributed and is started on the current node because of a failover
|
||||
from another mode and the application specification key `:start_phases`
|
||||
is `:undefined`.
|
||||
* `{:takeover, node}` - used if the application is distributed and is
|
||||
started on the current node because of a failover on the node `node`.
|
||||
* `{:failover, node}` - used if the application is distributed and is
|
||||
started on the current node because of a failover on node `node`, and the
|
||||
application specification key `:start_phases` is not `:undefined`.
|
||||
|
||||
`start_args` are the arguments passed to the application in the `:mod`
|
||||
specification key (e.g., `mod: {MyApp, [:my_args]}`).
|
||||
|
||||
This function should either return `{:ok, pid}` or `{:ok, pid, state}` if
|
||||
startup is successful. `pid` should be the pid of the top supervisor. `state`
|
||||
can be an arbitrary term, and if omitted will default to `[]`; if the
|
||||
application is later stopped, `state` is passed to the `stop/1` callback (see
|
||||
the documentation for the `stop/2` callback for more information).
|
||||
|
||||
`use Application` provides no default implementation for the `start/2`
|
||||
callback.
|
||||
"""
|
||||
@callback start(start_type, start_args :: term) ::
|
||||
{:ok, pid} |
|
||||
{:ok, pid, state} |
|
||||
{:error, reason :: term}
|
||||
|
||||
@doc """
|
||||
Called when an application is stopped.
|
||||
|
||||
This function is called when an application has stopped, i.e., when its
|
||||
supervision tree has been stopped. It should do the opposite of what the
|
||||
`start/2` callback did, and should perform any necessary cleanup. The return
|
||||
value of this callback is ignored.
|
||||
|
||||
`state` is the return value of the `start/2` callback or the return value of
|
||||
the `prep_stop/1` function if the application module defines such a function.
|
||||
|
||||
`use Application` defines a default implementation of this function which does
|
||||
nothing and just returns `:ok`.
|
||||
"""
|
||||
@callback stop(state) :: term
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour Application
|
||||
@behaviour :application
|
||||
|
||||
@doc false
|
||||
def stop(_state) do
|
||||
@@ -162,60 +110,8 @@ defmodule Application do
|
||||
@type app :: atom
|
||||
@type key :: atom
|
||||
@type value :: term
|
||||
@type state :: term
|
||||
@type start_type :: :permanent | :transient | :temporary
|
||||
|
||||
@application_keys [:description, :id, :vsn, :modules, :maxP, :maxT, :registered,
|
||||
:included_applications, :applications, :mod, :start_phases]
|
||||
|
||||
@doc """
|
||||
Returns the spec for `app`.
|
||||
|
||||
The following keys are returned:
|
||||
|
||||
* #{Enum.map_join @application_keys, "\n * ", &inspect/1}
|
||||
|
||||
Note the environment is not returned as it can be accessed via
|
||||
`fetch_env/2`. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app) :: [{key, value}] | nil
|
||||
def spec(app) do
|
||||
case :application.get_all_key(app) do
|
||||
{:ok, info} -> :lists.keydelete(:env, 1, info)
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s specification.
|
||||
|
||||
See `spec/1` for the supported keys. If the given
|
||||
specification parameter does not exist, this function
|
||||
will raise. Returns `nil` if the application is not loaded.
|
||||
"""
|
||||
@spec spec(app, key) :: value | nil
|
||||
def spec(app, key) when key in @application_keys do
|
||||
case :application.get_key(app, key) do
|
||||
{:ok, value} -> value
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the application for the given module.
|
||||
|
||||
The application is located by analyzing the spec
|
||||
of all loaded applications. Returns `nil` if
|
||||
the module is not listed in any application spec.
|
||||
"""
|
||||
@spec get_application(atom) :: atom | nil
|
||||
def get_application(module) when is_atom(module) do
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} -> app
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all key-value pairs for `app`.
|
||||
"""
|
||||
@@ -432,13 +328,10 @@ defmodule Application do
|
||||
@doc """
|
||||
Returns the given path inside `app_dir/1`.
|
||||
"""
|
||||
@spec app_dir(app, String.t | [String.t]) :: String.t
|
||||
@spec app_dir(app, String.t) :: String.t
|
||||
def app_dir(app, path) when is_binary(path) do
|
||||
Path.join(app_dir(app), path)
|
||||
end
|
||||
def app_dir(app, path) when is_list(path) do
|
||||
Path.join([app_dir(app) | path])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which are currently running.
|
||||
@@ -464,7 +357,7 @@ defmodule Application do
|
||||
@spec format_error(any) :: String.t
|
||||
def format_error(reason) do
|
||||
try do
|
||||
do_format_error(reason)
|
||||
impl_format_error(reason)
|
||||
catch
|
||||
# A user could create an error that looks like a builtin one
|
||||
# causing an error.
|
||||
@@ -474,68 +367,68 @@ defmodule Application do
|
||||
end
|
||||
|
||||
# exit(:normal) call is special cased, undo the special case.
|
||||
defp do_format_error({{:EXIT, :normal}, {mod, :start, args}}) do
|
||||
defp impl_format_error({{:EXIT, :normal}, {mod, :start, args}}) do
|
||||
Exception.format_exit({:normal, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# {:error, reason} return value
|
||||
defp do_format_error({reason, {mod, :start, args}}) do
|
||||
defp impl_format_error({reason, {mod, :start, args}}) do
|
||||
Exception.format_mfa(mod, :start, args) <> " returned an error: " <>
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
|
||||
# error or exit(reason) call, use exit reason as reason.
|
||||
defp do_format_error({:bad_return, {{mod, :start, args}, {:EXIT, reason}}}) do
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, {:EXIT, reason}}}) do
|
||||
Exception.format_exit({reason, {mod, :start, args}})
|
||||
end
|
||||
|
||||
# bad return value
|
||||
defp do_format_error({:bad_return, {{mod, :start, args}, return}}) do
|
||||
defp impl_format_error({:bad_return, {{mod, :start, args}, return}}) do
|
||||
Exception.format_mfa(mod, :start, args) <>
|
||||
" returned a bad value: " <> inspect(return)
|
||||
end
|
||||
|
||||
defp do_format_error({:already_started, app}) when is_atom(app) do
|
||||
defp impl_format_error({:already_started, app}) when is_atom(app) do
|
||||
"already started application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:not_started, app}) when is_atom(app) do
|
||||
defp impl_format_error({:not_started, app}) when is_atom(app) do
|
||||
"not started application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:bad_application, app}) do
|
||||
defp impl_format_error({:bad_application, app}) do
|
||||
"bad application: #{inspect(app)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:already_loaded, app}) when is_atom(app) do
|
||||
defp impl_format_error({:already_loaded, app}) when is_atom(app) do
|
||||
"already loaded application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:not_loaded, app}) when is_atom(app) do
|
||||
defp impl_format_error({:not_loaded, app}) when is_atom(app) do
|
||||
"not loaded application #{app}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_restart_type, restart}) do
|
||||
defp impl_format_error({:invalid_restart_type, restart}) do
|
||||
"invalid application restart type: #{inspect(restart)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_name, name}) do
|
||||
defp impl_format_error({:invalid_name, name}) do
|
||||
"invalid application name: #{inspect(name)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:invalid_options, opts}) do
|
||||
defp impl_format_error({:invalid_options, opts}) do
|
||||
"invalid application options: #{inspect(opts)}"
|
||||
end
|
||||
|
||||
defp do_format_error({:badstartspec, spec}) do
|
||||
defp impl_format_error({:badstartspec, spec}) do
|
||||
"bad application start specs: #{inspect(spec)}"
|
||||
end
|
||||
|
||||
defp do_format_error({'no such file or directory', file}) do
|
||||
defp impl_format_error({'no such file or directory', file}) do
|
||||
"could not find application file: #{file}"
|
||||
end
|
||||
|
||||
defp do_format_error(reason) do
|
||||
defp impl_format_error(reason) do
|
||||
Exception.format_exit(reason)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -22,23 +22,18 @@ defmodule Atom do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts an atom to a charlist.
|
||||
Converts an atom to a char list.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_charlist(:"An atom")
|
||||
iex> Atom.to_char_list(:"An atom")
|
||||
'An atom'
|
||||
|
||||
"""
|
||||
@spec to_charlist(atom) :: charlist
|
||||
def to_charlist(atom) do
|
||||
@spec to_char_list(atom) :: char_list
|
||||
def to_char_list(atom) do
|
||||
:erlang.atom_to_list(atom)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
@spec to_char_list(atom) :: charlist
|
||||
def to_char_list(atom), do: Atom.to_charlist(atom)
|
||||
end
|
||||
|
||||
+142
-494
@@ -98,68 +98,54 @@ defmodule Base do
|
||||
b32_alphabet = Enum.with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
||||
b32hex_alphabet = Enum.with_index '0123456789ABCDEFGHIJKLMNOPQRSTUV'
|
||||
|
||||
Enum.each [{:enc16, :dec16, b16_alphabet},
|
||||
{:enc32, :dec32, b32_alphabet},
|
||||
{:enc64, :dec64, b64_alphabet},
|
||||
{:enc64url, :dec64url, b64url_alphabet},
|
||||
{:enc32hex, :dec32hex, b32hex_alphabet}], fn({enc, dec, alphabet}) ->
|
||||
Enum.each [ {:enc16, :dec16, b16_alphabet},
|
||||
{:enc64, :dec64, b64_alphabet},
|
||||
{:enc32, :dec32, b32_alphabet},
|
||||
{:enc64url, :dec64url, b64url_alphabet},
|
||||
{:enc32hex, :dec32hex, b32hex_alphabet} ], fn({enc, dec, alphabet}) ->
|
||||
for {encoding, value} <- alphabet do
|
||||
defp unquote(enc)(unquote(value)), do: unquote(encoding)
|
||||
defp unquote(dec)(unquote(encoding)), do: unquote(value)
|
||||
end
|
||||
defp unquote(dec)(c) do
|
||||
raise ArgumentError, "non-alphabet digit found: #{inspect <<c>>, binaries: :as_strings} (byte #{c})"
|
||||
raise ArgumentError, "non-alphabet digit found: #{<<c>>}"
|
||||
end
|
||||
end
|
||||
|
||||
@compile {:inline, from_upper: 1, from_lower: 1, from_mixed: 1,
|
||||
to_lower: 1, to_upper: 1, enc16: 1, dec16: 1,
|
||||
enc32: 1, dec32: 1, enc32hex: 1, dec32hex: 1,
|
||||
enc64: 1, dec64: 1, enc64url: 1, dec64url: 1}
|
||||
defp encode_case(:upper, func),
|
||||
do: func
|
||||
defp encode_case(:lower, func),
|
||||
do: &to_lower(func.(&1))
|
||||
|
||||
defp decode_case(:upper, func),
|
||||
do: func
|
||||
defp decode_case(:lower, func),
|
||||
do: &func.(from_lower(&1))
|
||||
defp decode_case(:mixed, func),
|
||||
do: &func.(from_mixed(&1))
|
||||
|
||||
defp to_lower(char) when char in ?A..?Z,
|
||||
do: char + (?a - ?A)
|
||||
defp to_lower(char),
|
||||
do: char
|
||||
|
||||
defp to_upper(char), do: char
|
||||
|
||||
defp from_upper(char), do: char
|
||||
|
||||
defp from_lower(char) when char in ?a..?z,
|
||||
do: char - (?a - ?A)
|
||||
defp from_lower(char) when not char in ?A..?Z,
|
||||
do: char
|
||||
defp from_lower(char),
|
||||
do: raise(ArgumentError, "non-alphabet digit found: \"#{<<char>>}\" (byte #{char})")
|
||||
do: raise(ArgumentError, "non-alphabet digit found: #{<<char>>}")
|
||||
|
||||
defp from_mixed(char) when char in ?a..?z,
|
||||
do: char - (?a - ?A)
|
||||
defp from_mixed(char),
|
||||
do: char
|
||||
|
||||
defp maybe_pad(subject, false, _, _),
|
||||
do: subject
|
||||
defp maybe_pad(subject, _, group_size, pad) do
|
||||
case rem(byte_size(subject), group_size) do
|
||||
0 -> subject
|
||||
x -> subject <> String.duplicate(pad, group_size - x)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 16 encoded string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - use upper case characters (default)
|
||||
* `:lower` - use lower case characters
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -174,23 +160,16 @@ defmodule Base do
|
||||
@spec encode16(binary, Keyword.t) :: binary
|
||||
def encode16(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_encode16(case, data)
|
||||
do_encode16(data, encode_case(case, &enc16/1))
|
||||
end
|
||||
|
||||
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -206,8 +185,9 @@ defmodule Base do
|
||||
"""
|
||||
@spec decode16(binary) :: {:ok, binary} | :error
|
||||
@spec decode16(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode16(string, opts \\ []) do
|
||||
{:ok, decode16!(string, opts)}
|
||||
def decode16(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
{:ok, do_decode16(string, decode_case(case, &dec16/1))}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -215,17 +195,9 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 16 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
@@ -244,70 +216,37 @@ defmodule Base do
|
||||
"""
|
||||
@spec decode16!(binary) :: binary
|
||||
@spec decode16!(binary, Keyword.t) :: binary
|
||||
def decode16!(string, opts \\ [])
|
||||
|
||||
def decode16!(string, opts) when is_binary(string) and rem(byte_size(string), 2) == 0 do
|
||||
def decode16!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
do_decode16(case, string)
|
||||
end
|
||||
|
||||
def decode16!(string, _opts) when is_binary(string) do
|
||||
raise ArgumentError, "odd-length string"
|
||||
do_decode16(string, decode_case(case, &dec16/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode64("foobar")
|
||||
"Zm9vYmFy"
|
||||
|
||||
iex> Base.encode64("foob")
|
||||
"Zm9vYg=="
|
||||
|
||||
iex> Base.encode64("foob", padding: false)
|
||||
"Zm9vYg"
|
||||
|
||||
"""
|
||||
@spec encode64(binary) :: binary
|
||||
@spec encode64(binary, Keyword.t) :: binary
|
||||
def encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad?)
|
||||
def encode64(data) when is_binary(data) do
|
||||
do_encode64(data, &enc64/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy")
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg==")
|
||||
{:ok, "foob"}
|
||||
|
||||
iex> Base.decode64("Zm9vYg", padding: false)
|
||||
{:ok, "foob"}
|
||||
|
||||
"""
|
||||
@spec decode64(binary) :: {:ok, binary} | :error
|
||||
@spec decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, decode64!(string, opts)}
|
||||
def decode64(string) when is_binary(string) do
|
||||
{:ok, do_decode64(string, &dec64/1)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -315,11 +254,7 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 64 encoded string into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
The following alphabet is used both for encoding and decoding:
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
@@ -329,72 +264,40 @@ defmodule Base do
|
||||
iex> Base.decode64!("Zm9vYmFy")
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYmFy\\n", ignore: :whitespace)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg==")
|
||||
"foob"
|
||||
|
||||
iex> Base.decode64!("Zm9vYg", padding: false)
|
||||
"foob"
|
||||
|
||||
"""
|
||||
@spec decode64!(binary) :: binary
|
||||
@spec decode64!(binary, Keyword.t) :: binary
|
||||
def decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64(pad?)
|
||||
def decode64!(string) when is_binary(string) do
|
||||
do_decode64(string, &dec64/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 64 encoded string with URL and filename
|
||||
safe alphabet.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>)
|
||||
"_3_-_A=="
|
||||
|
||||
iex> Base.url_encode64(<<255, 127, 254, 252>>, padding: false)
|
||||
"_3_-_A"
|
||||
|
||||
"""
|
||||
@spec url_encode64(binary) :: binary
|
||||
@spec url_encode64(binary, Keyword.t) :: binary
|
||||
def url_encode64(data, opts \\ []) when is_binary(data) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad?)
|
||||
def url_encode64(data) when is_binary(data) do
|
||||
do_encode64(data, &enc64url/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==")
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A==\\n", ignore: :whitespace)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
iex> Base.url_decode64("_3_-_A", padding: false)
|
||||
{:ok, <<255, 127, 254, 252>>}
|
||||
|
||||
"""
|
||||
@spec url_decode64(binary) :: {:ok, binary} | :error
|
||||
@spec url_decode64(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def url_decode64(string, opts \\ []) when is_binary(string) do
|
||||
{:ok, url_decode64!(string, opts)}
|
||||
def url_decode64(string) when is_binary(string) do
|
||||
{:ok, do_decode64(string, &dec64url/1)}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -403,12 +306,6 @@ defmodule Base do
|
||||
Decodes a base 64 encoded string with URL and filename safe alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts `ignore: :whitespace` option which will ignore all the
|
||||
whitespace characters in the input string.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
@@ -417,39 +314,17 @@ defmodule Base do
|
||||
iex> Base.url_decode64!("_3_-_A==")
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A==\\n", ignore: :whitespace)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
iex> Base.url_decode64!("_3_-_A", padding: false)
|
||||
<<255, 127, 254, 252>>
|
||||
|
||||
"""
|
||||
@spec url_decode64!(binary) :: binary
|
||||
@spec url_decode64!(binary, Keyword.t) :: binary
|
||||
def url_decode64!(string, opts \\ []) when is_binary(string) do
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
string |> remove_ignored(opts[:ignore]) |> do_decode64url(pad?)
|
||||
def url_decode64!(string) when is_binary(string) do
|
||||
do_decode64(string, &dec64url/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
* `:padding` - specifies whether to apply padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - use upper case characters (default)
|
||||
* `:lower` - use lower case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - pad the output string to the nearest multiple of 8 (default)
|
||||
* `false` - omit padding from the output string
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -459,38 +334,20 @@ defmodule Base do
|
||||
iex> Base.encode32("foobar", case: :lower)
|
||||
"mzxw6ytboi======"
|
||||
|
||||
iex> Base.encode32("foobar", padding: false)
|
||||
"MZXW6YTBOI"
|
||||
|
||||
"""
|
||||
@spec encode32(binary) :: binary
|
||||
@spec encode32(binary, Keyword.t) :: binary
|
||||
def encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_encode32(case, data, pad?)
|
||||
do_encode32(data, encode_case(case, &enc32/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - require the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignore padding from the input string
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -503,14 +360,12 @@ defmodule Base do
|
||||
iex> Base.decode32("mzXW6ytBOi======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.decode32("MZXW6YTBOI", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec decode32(binary) :: {:ok, binary} | :error
|
||||
@spec decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def decode32(string, opts \\ []) do
|
||||
{:ok, decode32!(string, opts)}
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
{:ok, do_decode32(string, decode_case(case, &dec32/1))}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -518,27 +373,13 @@ defmodule Base do
|
||||
@doc """
|
||||
Decodes a base 32 encoded string into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - require the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignore padding from the input string
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI======")
|
||||
@@ -550,38 +391,20 @@ defmodule Base do
|
||||
iex> Base.decode32!("mzXW6ytBOi======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.decode32!("MZXW6YTBOI", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec decode32!(binary) :: binary
|
||||
@spec decode32!(binary, Keyword.t) :: binary
|
||||
def decode32!(string, opts \\ []) when is_binary(string) do
|
||||
def decode32!(string, opts \\ []) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_decode32(case, string, pad?)
|
||||
do_decode32(string, decode_case(case, &dec32/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a binary string into a base 32 encoded string with an
|
||||
extended hexadecimal alphabet.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to use when encoding
|
||||
* `:padding` - specifies whether to apply padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - use upper case characters (default)
|
||||
* `:lower` - use lower case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - pad the output string to the nearest multiple of 8 (default)
|
||||
* `false` - omit padding from the output string
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -591,39 +414,21 @@ defmodule Base do
|
||||
iex> Base.hex_encode32("foobar", case: :lower)
|
||||
"cpnmuoj1e8======"
|
||||
|
||||
iex> Base.hex_encode32("foobar", padding: false)
|
||||
"CPNMUOJ1E8"
|
||||
|
||||
"""
|
||||
@spec hex_encode32(binary) :: binary
|
||||
@spec hex_encode32(binary, Keyword.t) :: binary
|
||||
def hex_encode32(data, opts \\ []) when is_binary(data) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_hex_encode32(case, data, pad?)
|
||||
do_encode32(data, encode_case(case, &enc32hex/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - require the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignore padding from the input string
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -636,14 +441,12 @@ defmodule Base do
|
||||
iex> Base.hex_decode32("cpnMuOJ1E8======", case: :mixed)
|
||||
{:ok, "foobar"}
|
||||
|
||||
iex> Base.hex_decode32("CPNMUOJ1E8", padding: false)
|
||||
{:ok, "foobar"}
|
||||
|
||||
"""
|
||||
@spec hex_decode32(binary) :: {:ok, binary} | :error
|
||||
@spec hex_decode32(binary, Keyword.t) :: {:ok, binary} | :error
|
||||
def hex_decode32(string, opts \\ []) do
|
||||
{:ok, hex_decode32!(string, opts)}
|
||||
def hex_decode32(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
{:ok, do_decode32(string, decode_case(case, &dec32hex/1))}
|
||||
rescue
|
||||
ArgumentError -> :error
|
||||
end
|
||||
@@ -652,27 +455,13 @@ defmodule Base do
|
||||
Decodes a base 32 encoded string with extended hexadecimal alphabet
|
||||
into a binary string.
|
||||
|
||||
Accepts an atom `:upper` (default) for decoding from upper case characters or
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
An `ArgumentError` exception is raised if the padding is incorrect or
|
||||
a non-alphabet character is present in the string.
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:case` - specifies the character case to accept when decoding
|
||||
* `:padding` - specifies whether to require padding
|
||||
|
||||
The values for `:case` can be:
|
||||
|
||||
* `:upper` - only allow upper case characters (default)
|
||||
* `:lower` - only allow lower case characters
|
||||
* `:mixed` - allow mixed case characters
|
||||
|
||||
The values for `:padding` can be:
|
||||
|
||||
* `true` - require the input string to be padded to the nearest multiple of 8 (default)
|
||||
* `false` - ignore padding from the input string
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8======")
|
||||
@@ -684,261 +473,120 @@ defmodule Base do
|
||||
iex> Base.hex_decode32!("cpnMuOJ1E8======", case: :mixed)
|
||||
"foobar"
|
||||
|
||||
iex> Base.hex_decode32!("CPNMUOJ1E8", padding: false)
|
||||
"foobar"
|
||||
|
||||
"""
|
||||
@spec hex_decode32!(binary) :: binary
|
||||
@spec hex_decode32!(binary, Keyword.t) :: binary
|
||||
def hex_decode32!(string, opts \\ []) when is_binary(string) do
|
||||
case = Keyword.get(opts, :case, :upper)
|
||||
pad? = Keyword.get(opts, :padding, true)
|
||||
do_hex_decode32(case, string, pad?)
|
||||
do_decode32(string, decode_case(case, &dec32hex/1))
|
||||
end
|
||||
|
||||
defp remove_ignored(string, nil), do: string
|
||||
defp remove_ignored(string, :whitespace) do
|
||||
for <<c::8 <- string>>, not c in '\s\t\r\n', into: <<>>, do: <<c::8>>
|
||||
defp do_encode16(<<>>, _), do: <<>>
|
||||
defp do_encode16(data, enc) do
|
||||
for <<c::4 <- data>>, into: <<>>, do: <<enc.(c)::8>>
|
||||
end
|
||||
|
||||
defp do_encode16(_, <<>>), do: <<>>
|
||||
defp do_encode16(:upper, data) do
|
||||
for <<c::4 <- data>>, into: <<>>, do: <<enc16(c)::8>>
|
||||
end
|
||||
defp do_encode16(:lower, data) do
|
||||
for <<c::4 <- data>>, into: <<>>, do: <<to_lower(enc16(c))::8>>
|
||||
end
|
||||
|
||||
defp do_decode16(_, <<>>), do: <<>>
|
||||
defp do_decode16(:upper, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(c1)::4, dec16(c2)::4>>
|
||||
defp do_decode16(<<>>, _), do: <<>>
|
||||
defp do_decode16(string, dec) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec.(c1)::4, dec.(c2)::4>>
|
||||
end
|
||||
end
|
||||
defp do_decode16(:lower, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(from_lower(c1))::4, dec16(from_lower(c2))::4>>
|
||||
end
|
||||
end
|
||||
defp do_decode16(:mixed, string) when rem(byte_size(string), 2) == 0 do
|
||||
for <<c1::8, c2::8 <- string>>, into: <<>> do
|
||||
<<dec16(from_mixed(c1))::4, dec16(from_mixed(c2))::4>>
|
||||
end
|
||||
defp do_decode16(_, _) do
|
||||
raise ArgumentError, "odd-length string"
|
||||
end
|
||||
|
||||
defp do_encode64(<<>>, _), do: <<>>
|
||||
defp do_encode64(data, pad?) do
|
||||
defp do_encode64(data, enc) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64(c)::8>>
|
||||
tail = case rest do
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc.(c)::8>>
|
||||
case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64(c1)::8, enc64(c2)::8, enc64(bsl(c3, 2))::8>>
|
||||
<<main::binary, enc.(c1)::8, enc.(c2)::8, enc.(bsl(c3, 2))::8, ?=>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64(c1)::8, enc64(bsl(c2, 4))::8>>
|
||||
<<main::binary, enc.(c1)::8, enc.(bsl(c2, 4))::8, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64(<<>>, _), do: <<>>
|
||||
defp do_decode64(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64(true)
|
||||
end
|
||||
defp do_decode64(string, _pad?) when rem(byte_size(string), 4) == 0 do
|
||||
defp do_decode64(string, dec) when rem(byte_size(string), 4) == 0 do
|
||||
split = byte_size(string) - 4
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64(c)::6>>
|
||||
tail = case rest do
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec.(c)::6>>
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64(c1)::6, bsr(dec64(c2), 4)::2>>
|
||||
<<main::binary, dec.(c1)::6, bsr(dec.(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, bsr(dec64(c3), 2)::4>>
|
||||
<<main::binary, dec.(c1)::6, dec.(c2)::6, bsr(dec.(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64(c1)::6, dec64(c2)::6, dec64(c3)::6, dec64(c4)::6>>
|
||||
<<main::binary, dec.(c1)::6, dec.(c2)::6, dec.(c3)::6, dec.(c4)::6>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode64url(<<>>, _), do: <<>>
|
||||
defp do_encode64url(data, pad?) do
|
||||
split = 3 * div(byte_size(data), 3)
|
||||
defp do_encode32(<<>>, _), do: <<>>
|
||||
defp do_encode32(data, enc) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::6 <- main>>, into: <<>>, do: <<enc64url(c)::8>>
|
||||
tail = case rest do
|
||||
<<c1::6, c2::6, c3::4>> ->
|
||||
<<enc64url(c1)::8, enc64url(c2)::8, enc64url(bsl(c3, 2))::8>>
|
||||
<<c1::6, c2::2>> ->
|
||||
<<enc64url(c1)::8, enc64url(bsl(c2, 4))::8>>
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<enc.(c)::8>>
|
||||
case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<main::binary,
|
||||
enc.(c1)::8, enc.(c2)::8, enc.(c3)::8, enc.(c4)::8,
|
||||
enc.(c5)::8, enc.(c6)::8, enc.(bsl(c7, 3))::8, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<main::binary,
|
||||
enc.(c1)::8, enc.(c2)::8, enc.(c3)::8, enc.(c4)::8,
|
||||
enc.(bsl(c5, 1))::8, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<main::binary,
|
||||
enc.(c1)::8, enc.(c2)::8, enc.(c3)::8, enc.(bsl(c4, 4))::8,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<main::binary,
|
||||
enc.(c1)::8, enc.(bsl(c2, 2))::8, ?=, ?=,
|
||||
?=, ?=, ?=, ?=>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 4, "=")
|
||||
end
|
||||
|
||||
defp do_decode64url(<<>>, _), do: <<>>
|
||||
defp do_decode64url(string, false) do
|
||||
maybe_pad(string, true, 4, "=") |> do_decode64url(true)
|
||||
end
|
||||
defp do_decode64url(string, _pad?) when rem(byte_size(string), 4) == 0 do
|
||||
split = byte_size(string) - 4
|
||||
defp do_decode32(<<>>, _), do: <<>>
|
||||
defp do_decode32(string, dec) when rem(byte_size(string), 8) == 0 do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec64url(c)::6>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=>> ->
|
||||
<<dec64url(c1)::6, bsr(dec64url(c2), 4)::2>>
|
||||
<<c1::8, c2::8, c3::8, ?=>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, bsr(dec64url(c3), 2)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8>> ->
|
||||
<<dec64url(c1)::6, dec64url(c2)::6, dec64url(c3)::6, dec64url(c4)::6>>
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec.(c)::5>>
|
||||
case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<main::binary, dec.(c1)::5, bsr(dec.(c2), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<main::binary,
|
||||
dec.(c1)::5, dec.(c2)::5, dec.(c3)::5, bsr(dec.(c4), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<main::binary,
|
||||
dec.(c1)::5, dec.(c2)::5, dec.(c3)::5, dec.(c4)::5,
|
||||
bsr(dec.(c5), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<main::binary,
|
||||
dec.(c1)::5, dec.(c2)::5, dec.(c3)::5, dec.(c4)::5,
|
||||
dec.(c5)::5, dec.(c6)::5, bsr(dec.(c7), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<main::binary,
|
||||
dec.(c1)::5, dec.(c2)::5, dec.(c3)::5, dec.(c4)::5,
|
||||
dec.(c5)::5, dec.(c6)::5, dec.(c7)::5, dec.(c8)::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
main
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
defp do_decode64url(_, _) do
|
||||
defp do_decode32(_, _) do
|
||||
raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
defp do_encode32(_, <<>>, _), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_encode32(unquote(case), data, pad?) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32(c))::8>>
|
||||
tail = case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(c5))::8, unquote(fun)(enc32(c6))::8,
|
||||
unquote(fun)(enc32(bsl(c7, 3)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(c4))::8,
|
||||
unquote(fun)(enc32(bsl(c5, 1)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(c2))::8,
|
||||
unquote(fun)(enc32(c3))::8, unquote(fun)(enc32(bsl(c4, 4)))::8>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32(c1))::8, unquote(fun)(enc32(bsl(c2, 2)))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_decode32(case, string, false),
|
||||
do: do_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_decode32(unquote(case), string, _pad?) when rem(byte_size(string), 8) == 0 do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, bsr(dec32(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, bsr(dec32(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
bsr(dec32(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
bsr(dec32(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32(unquote(fun)(c1))::5, dec32(unquote(fun)(c2))::5,
|
||||
dec32(unquote(fun)(c3))::5, dec32(unquote(fun)(c4))::5,
|
||||
dec32(unquote(fun)(c5))::5, dec32(unquote(fun)(c6))::5,
|
||||
dec32(unquote(fun)(c7))::5, dec32(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
|
||||
defp do_hex_encode32(_, <<>>, _), do: <<>>
|
||||
|
||||
for {case, fun} <- [upper: :to_upper, lower: :to_lower] do
|
||||
defp do_hex_encode32(unquote(case), data, pad?) do
|
||||
split = 5 * div(byte_size(data), 5)
|
||||
<<main::size(split)-binary, rest::binary>> = data
|
||||
main = for <<c::5 <- main>>, into: <<>>, do: <<unquote(fun)(enc32hex(c))::8>>
|
||||
tail = case rest do
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::5, c6::5, c7::2>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(c5))::8, unquote(fun)(enc32hex(c6))::8,
|
||||
unquote(fun)(enc32hex(bsl(c7, 3)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::5, c5::4>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(c4))::8,
|
||||
unquote(fun)(enc32hex(bsl(c5, 1)))::8>>
|
||||
<<c1::5, c2::5, c3::5, c4::1>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(c2))::8,
|
||||
unquote(fun)(enc32hex(c3))::8, unquote(fun)(enc32hex(bsl(c4, 4)))::8>>
|
||||
<<c1::5, c2::3>> ->
|
||||
<<unquote(fun)(enc32hex(c1))::8, unquote(fun)(enc32hex(bsl(c2, 2)))::8>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> maybe_pad(tail, pad?, 8, "=")
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, <<>>, _), do: <<>>
|
||||
defp do_hex_decode32(case, string, false),
|
||||
do: do_hex_decode32(case, maybe_pad(string, true, 8, "="), true)
|
||||
|
||||
for {case, fun} <- [upper: :from_upper, lower: :from_lower, mixed: :from_mixed] do
|
||||
defp do_hex_decode32(unquote(case), string, _pad?) when rem(byte_size(string), 8) == 0 do
|
||||
split = byte_size(string) - 8
|
||||
<<main::size(split)-binary, rest::binary>> = string
|
||||
main = for <<c::8 <- main>>, into: <<>>, do: <<dec32hex(unquote(fun)(c))::5>>
|
||||
tail = case rest do
|
||||
<<c1::8, c2::8, ?=, ?=, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, bsr(dec32hex(unquote(fun)(c2)), 2)::3>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, ?=, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, bsr(dec32hex(unquote(fun)(c4)), 4)::1>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, ?=, ?=, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
bsr(dec32hex(unquote(fun)(c5)), 1)::4>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, ?=>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
bsr(dec32hex(unquote(fun)(c7)), 3)::2>>
|
||||
<<c1::8, c2::8, c3::8, c4::8, c5::8, c6::8, c7::8, c8::8>> ->
|
||||
<<dec32hex(unquote(fun)(c1))::5, dec32hex(unquote(fun)(c2))::5,
|
||||
dec32hex(unquote(fun)(c3))::5, dec32hex(unquote(fun)(c4))::5,
|
||||
dec32hex(unquote(fun)(c5))::5, dec32hex(unquote(fun)(c6))::5,
|
||||
dec32hex(unquote(fun)(c7))::5, dec32hex(unquote(fun)(c8))::5>>
|
||||
<<>> ->
|
||||
<<>>
|
||||
end
|
||||
main <> tail
|
||||
end
|
||||
end
|
||||
|
||||
defp do_hex_decode32(_, _, _),
|
||||
do: raise ArgumentError, "incorrect padding"
|
||||
end
|
||||
|
||||
@@ -7,7 +7,8 @@ defmodule Behaviour do
|
||||
Instead of `__behaviour__(:callbacks)`, one can simply use `behaviour_info(:callbacks)`.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate by 1.4
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
|
||||
@@ -1,35 +1,29 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
A set of macros that perform calculations on bits.
|
||||
|
||||
The macros in this module come in two flavors: named or
|
||||
operators. For example:
|
||||
This module provides macro-based operators that perform calculations
|
||||
on (sets of) bits. In general, you should `use` the Bitwise module
|
||||
as a whole:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> bnot 1 # named
|
||||
iex> bnot 1
|
||||
-2
|
||||
iex> 1 &&& 1 # operator
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
If you prefer to use only operators or skip them, you can
|
||||
pass the following options:
|
||||
When used, it accepts the following options:
|
||||
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
|
||||
For example:
|
||||
|
||||
iex> use Bitwise, only_operators: true
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
When invoked with no options, `use Bitwise` is equivalent
|
||||
to `import Bitwise`.
|
||||
|
||||
All bitwise macros can be used in guards:
|
||||
These macros can be used in guards:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> odd? = fn int when band(int, 1) == 1 -> true; _ -> false end
|
||||
iex> odd? = fn(int) when band(int, 1) == 1 -> true; (_) -> false end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
|
||||
@@ -42,8 +36,7 @@ defmodule Bitwise do
|
||||
[bnot: 1, band: 2, bor: 2, bxor: 2, bsl: 2, bsr: 2]
|
||||
Keyword.get(options, :skip_operators) ->
|
||||
[~~~: 1, &&&: 2, |||: 2, ^^^: 2, <<<: 2, >>>: 2]
|
||||
true ->
|
||||
[]
|
||||
true -> []
|
||||
end
|
||||
|
||||
quote do
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,189 +0,0 @@
|
||||
defmodule Calendar.ISO do
|
||||
@moduledoc """
|
||||
A calendar implementation that follows to ISO8601.
|
||||
|
||||
This calendar implements the proleptic Gregorian calendar and
|
||||
is therefore compatible with the calendar used in most countries
|
||||
today. The proleptic means the Gregorian rules for leap years are
|
||||
applied for all time, consequently the dates give different results
|
||||
before the year 1583 from when the Gregorian calendar was adopted.
|
||||
"""
|
||||
|
||||
@behaviour Calendar
|
||||
|
||||
@doc """
|
||||
Builds and validates an ISO date.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.date(2000, 1, 1)
|
||||
{:ok, ~D[2000-01-01]}
|
||||
iex> Calendar.ISO.date(2000, 13, 1)
|
||||
{:error, :invalid_date}
|
||||
iex> Calendar.ISO.date(2000, 2, 29)
|
||||
{:ok, ~D[2000-02-29]}
|
||||
|
||||
iex> Calendar.ISO.date(2000, 2, 30)
|
||||
{:error, :invalid_date}
|
||||
iex> Calendar.ISO.date(2001, 2, 29)
|
||||
{:error, :invalid_date}
|
||||
|
||||
"""
|
||||
def date(year, month, day) when is_integer(year) and is_integer(month) and is_integer(day) do
|
||||
if :calendar.valid_date(year, month, day) and year <= 9999 do
|
||||
{:ok, %Date{year: year, month: month, day: day}}
|
||||
else
|
||||
{:error, :invalid_date}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns if the given year is a leap year.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Calendar.ISO.leap_year?(2000)
|
||||
true
|
||||
iex> Calendar.ISO.leap_year?(2001)
|
||||
false
|
||||
iex> Calendar.ISO.leap_year?(2004)
|
||||
true
|
||||
iex> Calendar.ISO.leap_year?(1900)
|
||||
false
|
||||
|
||||
"""
|
||||
def leap_year?(year) when is_integer(year) and year >= 0 do
|
||||
rem(year, 4) === 0 and (rem(year, 100) > 0 or rem(year, 400) === 0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given structure into a string.
|
||||
|
||||
It uses the ISO8601 standard except for DateTime where the
|
||||
timezone information is added between brackets.
|
||||
"""
|
||||
def to_string(%Date{year: year, month: month, day: day}) do
|
||||
date_to_string(year, month, day)
|
||||
end
|
||||
|
||||
def to_string(%Time{hour: hour, minute: minute, second: second, microsecond: microsecond}) do
|
||||
time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
def to_string(%NaiveDateTime{year: year, month: month, day: day,
|
||||
hour: hour, minute: minute, second: second, microsecond: microsecond}) do
|
||||
date_to_string(year, month, day) <> " " <> time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
def to_string(%DateTime{year: year, month: month, day: day, zone_abbr: zone_abbr,
|
||||
hour: hour, minute: minute, second: second, microsecond: microsecond,
|
||||
utc_offset: utc_offset, std_offset: std_offset, time_zone: time_zone}) do
|
||||
date_to_string(year, month, day) <> " " <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone) <>
|
||||
zone_to_string(utc_offset, std_offset, zone_abbr, time_zone)
|
||||
end
|
||||
|
||||
defp date_to_string(year, month, day) do
|
||||
zero_pad(year, 4) <> "-" <> zero_pad(month, 2) <> "-" <> zero_pad(day, 2)
|
||||
end
|
||||
|
||||
defp time_to_string(hour, minute, second, 0) do
|
||||
zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2) <> ":" <> zero_pad(second, 2)
|
||||
end
|
||||
defp time_to_string(hour, minute, second, {_, 0}) do
|
||||
time_to_string(hour, minute, second, 0)
|
||||
end
|
||||
defp time_to_string(hour, minute, second, {microsecond, precision}) do
|
||||
time_to_string(hour, minute, second, 0) <> "." <>
|
||||
(microsecond |> zero_pad(6) |> binary_part(0, precision))
|
||||
end
|
||||
|
||||
defp offset_to_string(0, 0, "Etc/UTC"), do: "Z"
|
||||
defp offset_to_string(utc, std, _zone) do
|
||||
total = utc + std
|
||||
second = abs(total)
|
||||
minute = second |> rem(3600) |> div(60)
|
||||
hour = second |> div(3600)
|
||||
sign(total) <> zero_pad(hour, 2) <> ":" <> zero_pad(minute, 2)
|
||||
end
|
||||
|
||||
defp zone_to_string(0, 0, _abbr, "Etc/UTC"), do: ""
|
||||
defp zone_to_string(_, _, abbr, zone), do: " " <> abbr <> " " <> zone
|
||||
|
||||
defp sign(total) when total < 0, do: "-"
|
||||
defp sign(_), do: "+"
|
||||
|
||||
defp zero_pad(val, count) do
|
||||
num = Integer.to_string(val)
|
||||
:binary.copy("0", count - byte_size(num)) <> num
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
@doc false
|
||||
def to_iso8601(%Date{year: year, month: month, day: day}) do
|
||||
date_to_string(year, month, day)
|
||||
end
|
||||
|
||||
def to_iso8601(%Time{hour: hour, minute: minute, second: second, microsecond: microsecond}) do
|
||||
time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
def to_iso8601(%NaiveDateTime{year: year, month: month, day: day,
|
||||
hour: hour, minute: minute, second: second, microsecond: microsecond}) do
|
||||
date_to_string(year, month, day) <> "T" <> time_to_string(hour, minute, second, microsecond)
|
||||
end
|
||||
|
||||
def to_iso8601(%DateTime{year: year, month: month, day: day,
|
||||
hour: hour, minute: minute, second: second, microsecond: microsecond,
|
||||
utc_offset: utc_offset, std_offset: std_offset, time_zone: time_zone}) do
|
||||
date_to_string(year, month, day) <> "T" <>
|
||||
time_to_string(hour, minute, second, microsecond) <>
|
||||
offset_to_string(utc_offset, std_offset, time_zone)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def parse_microsecond("." <> rest) do
|
||||
case parse_microsecond(rest, 0, "") do
|
||||
{"", 0, _} ->
|
||||
:error
|
||||
{microsecond, precision, rest} when precision in 1..6 ->
|
||||
pad = String.duplicate("0", 6 - byte_size(microsecond))
|
||||
{{String.to_integer(microsecond <> pad), precision}, rest}
|
||||
{microsecond, _precision, rest} ->
|
||||
{{String.to_integer(binary_part(microsecond, 0, 6)), 6}, rest}
|
||||
end
|
||||
end
|
||||
def parse_microsecond(rest) do
|
||||
{{0, 0}, rest}
|
||||
end
|
||||
|
||||
defp parse_microsecond(<<h, t::binary>>, precision, acc) when h in ?0..?9,
|
||||
do: parse_microsecond(t, precision + 1, <<acc::binary, h>>)
|
||||
defp parse_microsecond(rest, precision, acc),
|
||||
do: {acc, precision, rest}
|
||||
|
||||
@doc false
|
||||
def parse_offset(""),
|
||||
do: {nil, ""}
|
||||
def parse_offset("Z"),
|
||||
do: {0, ""}
|
||||
def parse_offset("-00:00"),
|
||||
do: :error
|
||||
def parse_offset(<<?+, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(1, hour, min, rest)
|
||||
def parse_offset(<<?-, hour::2-bytes, ?:, min::2-bytes, rest::binary>>),
|
||||
do: parse_offset(-1, hour, min, rest)
|
||||
def parse_offset(_),
|
||||
do: :error
|
||||
|
||||
defp parse_offset(sign, hour, min, rest) do
|
||||
with {hour, ""} when hour < 24 <- Integer.parse(hour),
|
||||
{min, ""} when min < 60 <- Integer.parse(min) do
|
||||
{((hour * 60) + min) * 60 * sign, rest}
|
||||
else
|
||||
_ -> :error
|
||||
end
|
||||
end
|
||||
end
|
||||
+38
-35
@@ -56,11 +56,11 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def append_path(path) do
|
||||
:code.add_pathz(to_charlist(Path.expand path))
|
||||
:code.add_pathz(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prepends a path to the beginning of the Erlang VM code path list.
|
||||
Prepends a path to the begining of the Erlang VM code path list.
|
||||
|
||||
This is the list of directories the Erlang VM uses for finding
|
||||
module code.
|
||||
@@ -76,7 +76,7 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def prepend_path(path) do
|
||||
:code.add_patha(to_charlist(Path.expand path))
|
||||
:code.add_patha(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -95,7 +95,7 @@ defmodule Code do
|
||||
|
||||
"""
|
||||
def delete_path(path) do
|
||||
:code.del_path(to_charlist(Path.expand path))
|
||||
:code.del_path(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -126,7 +126,7 @@ defmodule Code do
|
||||
Notice that setting any of the values above overrides Elixir's default
|
||||
values. For example, setting `:requires` to `[]`, will no longer
|
||||
automatically require the `Kernel` module; in the same way setting
|
||||
`:macros` will no longer auto-import `Kernel` macros like `if/2`, `case/2`,
|
||||
`:macros` will no longer auto-import `Kernel` macros like `if`, `case`,
|
||||
etc.
|
||||
|
||||
Returns a tuple of the form `{value, binding}`,
|
||||
@@ -159,13 +159,13 @@ defmodule Code do
|
||||
def eval_string(string, binding \\ [], opts \\ [])
|
||||
|
||||
def eval_string(string, binding, %Macro.Env{} = env) do
|
||||
{value, binding, _env, _scope} = :elixir.eval to_charlist(string), binding, Map.to_list(env)
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, Map.to_list(env)
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
def eval_string(string, binding, opts) when is_list(opts) do
|
||||
validate_eval_opts(opts)
|
||||
{value, binding, _env, _scope} = :elixir.eval to_charlist(string), binding, opts
|
||||
{value, binding, _env, _scope} = :elixir.eval to_char_list(string), binding, opts
|
||||
{value, binding}
|
||||
end
|
||||
|
||||
@@ -263,7 +263,7 @@ defmodule Code do
|
||||
def string_to_quoted(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted(to_charlist(string), line, file, opts)
|
||||
:elixir.string_to_quoted(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -279,7 +279,7 @@ defmodule Code do
|
||||
def string_to_quoted!(string, opts \\ []) when is_list(opts) do
|
||||
file = Keyword.get opts, :file, "nofile"
|
||||
line = Keyword.get opts, :line, 1
|
||||
:elixir.string_to_quoted!(to_charlist(string), line, file, opts)
|
||||
:elixir.string_to_quoted!(to_char_list(string), line, file, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -311,7 +311,7 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
Code.load_file("eex_test.exs", "../eex/test") |> List.first
|
||||
Code.load_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
"""
|
||||
@@ -343,11 +343,11 @@ defmodule Code do
|
||||
|
||||
If the code is already loaded, it returns `nil`:
|
||||
|
||||
Code.require_file("eex_test.exs", "../eex/test") #=> nil
|
||||
Code.require_file("eex_test.exs","../eex/test") #=> nil
|
||||
|
||||
If the code is not loaded yet, it returns the same as `load_file/2`:
|
||||
|
||||
Code.require_file("eex_test.exs", "../eex/test") |> List.first
|
||||
Code.require_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
"""
|
||||
@@ -374,8 +374,7 @@ defmodule Code do
|
||||
## Examples
|
||||
|
||||
Code.compiler_options
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: false}
|
||||
#=> [debug_info: true, docs: true, warnings_as_errors: false]
|
||||
|
||||
"""
|
||||
def compiler_options do
|
||||
@@ -389,8 +388,8 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.available_compiler_options
|
||||
[:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
Code.available_compiler_options
|
||||
#=> [:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
def available_compiler_options do
|
||||
@@ -422,18 +421,17 @@ defmodule Code do
|
||||
## Examples
|
||||
|
||||
Code.compiler_options(debug_info: true)
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: false}
|
||||
#=> [debug_info: true, docs: true, warnings_as_errors: false]
|
||||
|
||||
"""
|
||||
def compiler_options(opts) do
|
||||
available = available_compiler_options()
|
||||
|
||||
for {k, _} <- opts,
|
||||
not k in available,
|
||||
do: raise "unknown compiler options: #{k}"
|
||||
|
||||
:elixir_config.update :compiler_options, &Enum.into(opts, &1)
|
||||
{opts, bad} = Keyword.split(opts, available_compiler_options)
|
||||
if bad != [] do
|
||||
bad = bad |> Keyword.keys |> Enum.join(", ")
|
||||
raise ArgumentError, message: "unknown compiler options: #{bad}"
|
||||
end
|
||||
update = &:orddict.merge(fn(_, _, value) -> value end, &1, opts)
|
||||
:elixir_config.update :compiler_options, update
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -445,7 +443,7 @@ defmodule Code do
|
||||
For compiling many files at once, check `Kernel.ParallelCompiler.files/2`.
|
||||
"""
|
||||
def compile_string(string, file \\ "nofile") when is_binary(file) do
|
||||
:elixir_compiler.string to_charlist(string), file
|
||||
:elixir_compiler.string to_char_list(string), file
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -542,11 +540,15 @@ defmodule Code do
|
||||
def ensure_compiled(module) when is_atom(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:error, :nofile} = error ->
|
||||
if is_pid(:erlang.get(:elixir_compiler_pid)) and
|
||||
Kernel.ErrorHandler.ensure_compiled(module, :module) do
|
||||
{:module, module}
|
||||
else
|
||||
error
|
||||
case :erlang.get(:elixir_ensure_compiled) do
|
||||
:undefined -> error
|
||||
_ ->
|
||||
try do
|
||||
module.__info__(:module)
|
||||
{:module, module}
|
||||
rescue
|
||||
UndefinedFunctionError -> error
|
||||
end
|
||||
end
|
||||
other -> other
|
||||
end
|
||||
@@ -593,10 +595,11 @@ defmodule Code do
|
||||
|
||||
## Examples
|
||||
|
||||
# Get the module documentation
|
||||
iex> {_line, text} = Code.get_docs(Atom, :moduledoc)
|
||||
# Get the documentation for the first function listed
|
||||
iex> [fun|_] = Code.get_docs(Atom, :docs) |> Enum.sort()
|
||||
iex> {{_function, _arity}, _line, _kind, _signature, text} = fun
|
||||
iex> String.split(text, "\n") |> Enum.at(0)
|
||||
"Convenience functions for working with atoms."
|
||||
"Converts an atom to a char list."
|
||||
|
||||
# Module doesn't exist
|
||||
iex> Code.get_docs(ModuleNotGood, :all)
|
||||
@@ -615,7 +618,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
def get_docs(binpath, kind) when is_binary(binpath) and kind in @doc_kinds do
|
||||
do_get_docs(String.to_charlist(binpath), kind)
|
||||
do_get_docs(String.to_char_list(binpath), kind)
|
||||
end
|
||||
|
||||
@docs_chunk 'ExDc'
|
||||
|
||||
@@ -13,7 +13,7 @@ defprotocol Collectable do
|
||||
The `Enumerable` protocol is useful to take values out of a collection.
|
||||
In order to support a wide range of values, the functions provided by
|
||||
the `Enumerable` protocol do not keep shape. For example, passing a
|
||||
map to `Enum.map/2` always returns a list.
|
||||
dictionary to `Enum.map/2` always returns a list.
|
||||
|
||||
This design is intentional. `Enumerable` was designed to support infinite
|
||||
collections, resources and other structures with fixed shape. For example,
|
||||
@@ -49,7 +49,7 @@ end
|
||||
defimpl Collectable, for: List do
|
||||
def into(original) do
|
||||
{[], fn
|
||||
list, {:cont, x} -> [x | list]
|
||||
list, {:cont, x} -> [x|list]
|
||||
list, :done -> original ++ :lists.reverse(list)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
@@ -59,7 +59,7 @@ end
|
||||
defimpl Collectable, for: BitString do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc | x]
|
||||
acc, {:cont, x} when is_bitstring(x) -> [acc|x]
|
||||
acc, :done -> IO.iodata_to_binary(acc)
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
|
||||
+537
-14
@@ -1,27 +1,153 @@
|
||||
defmodule Dict do
|
||||
@moduledoc ~S"""
|
||||
WARNING: this module is deprecated.
|
||||
This module specifies the Dict API expected to be
|
||||
implemented by different dictionaries.
|
||||
|
||||
If you need a general dictionary, use the `Map` module.
|
||||
If you need to manipulate keyword lists, use `Keyword`.
|
||||
It also provides functions that redirect to the underlying
|
||||
Dict, allowing a developer to work with different Dict
|
||||
implementations using one API.
|
||||
|
||||
To create a new dict, use the `new` functions defined
|
||||
by each dict type:
|
||||
|
||||
HashDict.new #=> creates an empty HashDict
|
||||
|
||||
In the examples below, `dict_impl` means a specific
|
||||
`Dict` implementation, for example `HashDict` or `Map`.
|
||||
|
||||
## Warning
|
||||
|
||||
Do not use this module if you expect a certain `Dict`
|
||||
implementation. For example, if you are working with
|
||||
maps and you don't need polymorphism, it is preferrable
|
||||
to use the `Map` module instead of the `Dict` one.
|
||||
|
||||
## Protocols
|
||||
|
||||
Besides implementing the functions in this module, all
|
||||
dictionaries are required to implement the `Access`
|
||||
protocol:
|
||||
|
||||
iex> dict = dict_impl.new
|
||||
iex> dict = Dict.put(dict, :hello, :world)
|
||||
iex> dict[:hello]
|
||||
:world
|
||||
|
||||
As well as the `Enumerable` and `Collectable` protocols.
|
||||
|
||||
## Match
|
||||
|
||||
Dictionaries are required to implement all operations
|
||||
using the match (`===`) operator.
|
||||
|
||||
## Default implementation
|
||||
|
||||
Default implementations for some functions in the `Dict` module
|
||||
are provided via `use Dict`.
|
||||
|
||||
For example:
|
||||
|
||||
defmodule MyDict do
|
||||
use Dict
|
||||
|
||||
# implement required functions (see below)
|
||||
# override default implementations if optimization
|
||||
# is needed
|
||||
end
|
||||
|
||||
The client module must contain the following functions:
|
||||
|
||||
* `delete/2`
|
||||
* `fetch/2`
|
||||
* `put/3`
|
||||
* `reduce/3`
|
||||
* `size/1`
|
||||
|
||||
All functions, except `reduce/3`, are required by the Dict behaviour.
|
||||
`reduce/3` must be implemented as per the Enumerable protocol.
|
||||
|
||||
Based on these functions, `Dict` generates default implementations
|
||||
for the following functions:
|
||||
|
||||
* `drop/2`
|
||||
* `equal?/2`
|
||||
* `fetch!/2`
|
||||
* `get/2`
|
||||
* `get/3`
|
||||
* `get_lazy/3`
|
||||
* `get_and_update/3`
|
||||
* `has_key?/2`
|
||||
* `keys/1`
|
||||
* `merge/2`
|
||||
* `merge/3`
|
||||
* `pop/2`
|
||||
* `pop/3`
|
||||
* `pop_lazy/3`
|
||||
* `put_new/3`
|
||||
* `put_new_lazy/3`
|
||||
* `split/2`
|
||||
* `take/2`
|
||||
* `to_list/1`
|
||||
* `update/4`
|
||||
* `update!/3`
|
||||
* `values/1`
|
||||
|
||||
All of these functions are defined as overridable, so you can provide
|
||||
your own implementation if needed.
|
||||
|
||||
Note you can also test your custom module via `Dict`'s doctests:
|
||||
|
||||
defmodule MyDict do
|
||||
# ...
|
||||
end
|
||||
|
||||
defmodule MyTests do
|
||||
use ExUnit.Case
|
||||
doctest Dict
|
||||
defp dict_impl, do: MyDict
|
||||
end
|
||||
|
||||
To convert maps into keywords and vice-versa, use the
|
||||
`new` function in the respective modules.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
@type t :: list | map
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
@callback new :: t
|
||||
@callback delete(t, key) :: t
|
||||
@callback drop(t, Enum.t) :: t
|
||||
@callback equal?(t, t) :: boolean
|
||||
@callback get(t, key) :: value
|
||||
@callback get(t, key, value) :: value
|
||||
@callback get_lazy(t, key, (() -> value)) :: value
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback fetch!(t, key) :: value | no_return
|
||||
@callback has_key?(t, key) :: boolean
|
||||
@callback keys(t) :: [key]
|
||||
@callback merge(t, t) :: t
|
||||
@callback merge(t, t, (key, value, value -> value)) :: t
|
||||
@callback pop(t, key) :: {value, t}
|
||||
@callback pop(t, key, value) :: {value, t}
|
||||
@callback pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
@callback put(t, key, value) :: t
|
||||
@callback put_new(t, key, value) :: t
|
||||
@callback put_new_lazy(t, key, (() -> value)) :: t
|
||||
@callback size(t) :: non_neg_integer()
|
||||
@callback split(t, Enum.t) :: {t, t}
|
||||
@callback take(t, Enum.t) :: t
|
||||
@callback to_list(t) :: list()
|
||||
@callback update(t, key, value, (value -> value)) :: t
|
||||
@callback update!(t, key, (value -> value)) :: t | no_return
|
||||
@callback values(t) :: list(value)
|
||||
|
||||
defmacro __using__(_) do
|
||||
# Use this import to guarantee proper code expansion
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
%{file: file, line: line} = __CALLER__
|
||||
:elixir_errors.warn(line, file, "the Dict module is deprecated")
|
||||
|
||||
quote do
|
||||
@behaviour Dict
|
||||
|
||||
def get(dict, key, default \\ nil) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -82,19 +208,19 @@ defmodule Dict do
|
||||
|
||||
def to_list(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
kv, acc -> {:cont, [kv | acc]}
|
||||
kv, acc -> {:cont, [kv|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
def keys(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{k, _}, acc -> {:cont, [k | acc]}
|
||||
{k, _}, acc -> {:cont, [k|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
def values(dict) do
|
||||
reduce(dict, {:cont, []}, fn
|
||||
{_, v}, acc -> {:cont, [v | acc]}
|
||||
{_, v}, acc -> {:cont, [v|acc]}
|
||||
end) |> elem(1) |> :lists.reverse
|
||||
end
|
||||
|
||||
@@ -199,71 +325,276 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of all keys in `dict`.
|
||||
The keys are not guaranteed to be in any order.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Enum.sort(Dict.keys(dict))
|
||||
[:a, :b]
|
||||
|
||||
"""
|
||||
@spec keys(t) :: [key]
|
||||
def keys(dict) do
|
||||
target(dict).keys(dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of all values in `dict`.
|
||||
The values are not guaranteed to be in any order.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Enum.sort(Dict.values(dict))
|
||||
[1, 2]
|
||||
|
||||
"""
|
||||
@spec values(t) :: [value]
|
||||
def values(dict) do
|
||||
target(dict).values(dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of elements in `dict`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> Dict.size(dict)
|
||||
2
|
||||
|
||||
"""
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(dict) do
|
||||
target(dict).size(dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether the given `key` exists in the given `dict`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.has_key?(dict, :a)
|
||||
true
|
||||
iex> Dict.has_key?(dict, :b)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec has_key?(t, key) :: boolean
|
||||
def has_key?(dict, key) do
|
||||
target(dict).has_key?(dict, key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, returns `default` (or `nil` if not provided).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.get(dict, :a)
|
||||
1
|
||||
iex> Dict.get(dict, :b)
|
||||
nil
|
||||
iex> Dict.get(dict, :b, 3)
|
||||
3
|
||||
"""
|
||||
@spec get(t, key, value) :: value
|
||||
def get(dict, key, default \\ nil) do
|
||||
target(dict).get(dict, key, default)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, it lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to set-up and tear-down again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> :result
|
||||
...> end
|
||||
iex> Dict.get_lazy(dict, :a, fun)
|
||||
1
|
||||
iex> Dict.get_lazy(dict, :b, fun)
|
||||
:result
|
||||
|
||||
"""
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
def get_lazy(dict, key, fun) do
|
||||
target(dict).get_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a value from `dict` and updates the value at `key` in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` in `dict` (or `nil` if `key`
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
value retrieved from the dict which can be operated on before being returned)
|
||||
and the new value to be stored under `key` in `dict`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a new
|
||||
dict with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {get, new_dict} = Dict.get_and_update dict, :a, fn(current_value) ->
|
||||
...> {current_value + 1, "foo"}
|
||||
...> end
|
||||
iex> get
|
||||
2
|
||||
iex> Dict.get(new_dict, :a)
|
||||
"foo"
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
def get_and_update(dict, key, fun) do
|
||||
target(dict).get_and_update(dict, key, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `{:ok, value}` associated with `key` in `dict`.
|
||||
If `dict` does not contain `key`, returns `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.fetch(dict, :a)
|
||||
{:ok, 1}
|
||||
iex> Dict.fetch(dict, :b)
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec fetch(t, key) :: value
|
||||
def fetch(dict, key) do
|
||||
target(dict).fetch(dict, key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict`. If `dict` does not
|
||||
contain `key`, it raises `KeyError`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> Dict.fetch!(dict, :a)
|
||||
1
|
||||
|
||||
"""
|
||||
@spec fetch!(t, key) :: value | no_return
|
||||
def fetch!(dict, key) do
|
||||
target(dict).fetch!(dict, key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the given `value` under `key` in `dict`.
|
||||
If `dict` already has `key`, the stored value is replaced by the new one.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.put(dict, :a, 3)
|
||||
iex> Dict.get(dict, :a)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec put(t, key, value) :: t
|
||||
def put(dict, key, val) do
|
||||
target(dict).put(dict, key, val)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` in `dict` unless `key` is already present.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.put_new(dict, :a, 3)
|
||||
iex> Dict.get(dict, :a)
|
||||
1
|
||||
|
||||
"""
|
||||
@spec put_new(t, key, value) :: t
|
||||
def put_new(dict, key, val) do
|
||||
target(dict).put_new(dict, key, val)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key` in `dict` unless `key`
|
||||
is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or generally
|
||||
difficult to set-up and tear-down again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 3
|
||||
...> end
|
||||
iex> dict = Dict.put_new_lazy(dict, :a, fun)
|
||||
iex> Dict.get(dict, :a)
|
||||
1
|
||||
iex> dict = Dict.put_new_lazy(dict, :c, fun)
|
||||
iex> Dict.get(dict, :c)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(t, key, (() -> value)) :: t
|
||||
def put_new_lazy(dict, key, fun) do
|
||||
target(dict).put_new_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the entry stored under the given `key` from `dict`.
|
||||
If `dict` does not contain `key`, returns the dictionary unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.delete(dict, :a)
|
||||
iex> Dict.get(dict, :a)
|
||||
nil
|
||||
|
||||
iex> dict = Enum.into([b: 2], dict_impl.new)
|
||||
iex> Dict.delete(dict, :a) == dict
|
||||
true
|
||||
|
||||
"""
|
||||
@spec delete(t, key) :: t
|
||||
def delete(dict, key) do
|
||||
target(dict).delete(dict, key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges the dict `dict2` into dict `dict1`.
|
||||
|
||||
If one of the `dict2` entries is found in `dict1`, the
|
||||
conflicting entries in `dict2` have higher precedence.
|
||||
|
||||
Notice this function is polymorphic as it merges dicts of any
|
||||
type. Each dict implementation also provides a `merge` function,
|
||||
but they can only merge dicts of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict1 = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict2 = Enum.into([a: 3, d: 4], dict_impl.new)
|
||||
iex> dict = Dict.merge(dict1, dict2)
|
||||
iex> [a: Dict.get(dict, :a), b: Dict.get(dict, :b), d: Dict.get(dict, :d)]
|
||||
[a: 3, b: 2, d: 4]
|
||||
|
||||
"""
|
||||
@spec merge(t, t) :: t
|
||||
def merge(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
@@ -276,6 +607,27 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges the dict `dict2` into dict `dict1`.
|
||||
|
||||
If one of the `dict2` entries is found in `dict1`, the function
|
||||
will be invoked to resolve the conflict.
|
||||
|
||||
Notice this function is polymorphic as it merges dicts of any
|
||||
type. Each dict implementation also provides a `merge` function,
|
||||
but they can only merge dicts of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict1 = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict2 = Enum.into([a: 3, d: 4], dict_impl.new)
|
||||
iex> dict = Dict.merge(dict1, dict2, fn(_k, v1, v2) ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
iex> [a: Dict.get(dict, :a), b: Dict.get(dict, :b), d: Dict.get(dict, :d)]
|
||||
[a: 4, b: 2, d: 4]
|
||||
|
||||
"""
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(dict1, dict2, fun) do
|
||||
target1 = target(dict1)
|
||||
@@ -294,46 +646,207 @@ defmodule Dict do
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict` as
|
||||
well as the `dict` without `key`.
|
||||
|
||||
If `key` is not present in `dict`, then the `dict` will
|
||||
be returned unmodified.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :a
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{1, []}
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :b
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{nil, [a: 1]}
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> {v, dict} = Dict.pop dict, :b, 3
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{3, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(dict, key, default \\ nil) do
|
||||
target(dict).pop(dict, key, default)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict` as
|
||||
well as the `dict` without `key`.
|
||||
|
||||
If `key` is not present in `dict`, then the `dict` will
|
||||
be returned unmodified, and it will lazily evaluate `fun`
|
||||
and return its result instead of the missing value.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to set-up and tear-down again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> :result
|
||||
...> end
|
||||
iex> {v, dict} = Dict.pop_lazy dict, :a, fun
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{1, []}
|
||||
|
||||
iex> dict = Enum.into([a: 1], dict_impl.new)
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> :result
|
||||
...> end
|
||||
iex> {v, dict} = Dict.pop_lazy dict, :b, fun
|
||||
iex> {v, Enum.sort(dict)}
|
||||
{:result, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(dict, key, fun) do
|
||||
target(dict).pop_lazy(dict, key, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a value in `dict` by calling `fun` on the value to get a new
|
||||
value. An exception is generated if `key` is not present in the dict.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.update!(dict, :a, fn(val) -> -val end)
|
||||
iex> Dict.get(dict, :a)
|
||||
-1
|
||||
|
||||
"""
|
||||
@spec update!(t, key, (value -> value)) :: t
|
||||
def update!(dict, key, fun) do
|
||||
target(dict).update!(dict, key, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a value in `dict` by calling `fun` on the value to get a new value. If
|
||||
`key` is not present in `dict` then `initial` will be stored as the first
|
||||
value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.update(dict, :c, 3, fn(val) -> -val end)
|
||||
iex> Dict.get(dict, :c)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(dict, key, initial, fun) do
|
||||
target(dict).update(dict, key, initial, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a tuple of two dicts, where the first dict contains only
|
||||
entries from `dict` with keys in `keys`, and the second dict
|
||||
contains only entries from `dict` with keys not in `keys`.
|
||||
|
||||
All non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2, c: 3, d: 4], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :c, :e])
|
||||
iex> {Dict.to_list(dict1) |> Enum.sort, Dict.to_list(dict2) |> Enum.sort}
|
||||
{[a: 1, c: 3], [b: 2, d: 4]}
|
||||
|
||||
iex> dict = Enum.into([], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :c])
|
||||
iex> {Dict.to_list(dict1), Dict.to_list(dict2)}
|
||||
{[], []}
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> {dict1, dict2} = Dict.split(dict, [:a, :b, :c])
|
||||
iex> {Dict.to_list(dict1) |> Enum.sort, Dict.to_list(dict2)}
|
||||
{[a: 1, b: 2], []}
|
||||
|
||||
"""
|
||||
@spec split(t, [key]) :: {t, t}
|
||||
def split(dict, keys) do
|
||||
target(dict).split(dict, keys)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a new dict where the given `keys` are removed from `dict`.
|
||||
All non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.drop(dict, [:a, :c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[b: 2]
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.drop(dict, [:c, :d])
|
||||
iex> Dict.to_list(dict) |> Enum.sort
|
||||
[a: 1, b: 2]
|
||||
|
||||
"""
|
||||
@spec drop(t, [key]) :: t
|
||||
def drop(dict, keys) do
|
||||
target(dict).drop(dict, keys)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a new dict where only the keys in `keys` from `dict` are included.
|
||||
|
||||
All non-member keys are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = Enum.into([a: 1, b: 2], dict_impl.new)
|
||||
iex> dict = Dict.take(dict, [:a, :c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[a: 1]
|
||||
iex> dict = Dict.take(dict, [:c, :d])
|
||||
iex> Dict.to_list(dict)
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec take(t, [key]) :: t
|
||||
def take(dict, keys) do
|
||||
target(dict).take(dict, keys)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec empty(t) :: t
|
||||
def empty(dict) do
|
||||
target(dict).empty(dict)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two dicts are equal using `===`.
|
||||
|
||||
Notice this function is polymorphic as it compares dicts of any
|
||||
type. Each dict implementation also provides an `equal?` function,
|
||||
but they can only compare dicts of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict1 = Enum.into([a: 2, b: 3, f: 5, c: 123], dict_impl.new)
|
||||
iex> dict2 = [a: 2, b: 3, f: 5, c: 123]
|
||||
iex> Dict.equal?(dict1, dict2)
|
||||
true
|
||||
|
||||
iex> dict1 = Enum.into([a: 2, b: 3, f: 5, c: 123], dict_impl.new)
|
||||
iex> dict2 = []
|
||||
iex> Dict.equal?(dict1, dict2)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
@@ -347,7 +860,7 @@ defmodule Dict do
|
||||
Enumerable.reduce(dict2, {:cont, true}, fn({k, v}, _acc) ->
|
||||
case target1.fetch(dict1, k) do
|
||||
{:ok, ^v} -> {:cont, true}
|
||||
_ -> {:halt, false}
|
||||
_ -> {:halt, false}
|
||||
end
|
||||
end) |> elem(1)
|
||||
|
||||
@@ -356,12 +869,22 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of key-value pairs stored in `dict`.
|
||||
No particular order is enforced.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> dict = dict_impl.new
|
||||
iex> dict = Dict.put(dict, :a, 1)
|
||||
iex> Dict.to_list(dict)
|
||||
[a: 1]
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(dict) do
|
||||
target(dict).to_list(dict)
|
||||
end
|
||||
|
||||
@spec unsupported_dict(t) :: no_return
|
||||
defp unsupported_dict(dict) do
|
||||
raise ArgumentError, "unsupported dict: #{inspect dict}"
|
||||
end
|
||||
|
||||
+864
-1089
File diff suppressed because it is too large
Load Diff
+56
-122
@@ -15,11 +15,7 @@ defmodule Exception do
|
||||
"""
|
||||
|
||||
@typedoc "The exception type"
|
||||
@type t :: %{
|
||||
required(:__struct__) => module,
|
||||
required(:__exception__) => true,
|
||||
atom => any
|
||||
}
|
||||
@type t :: %{__struct__: module, __exception__: true}
|
||||
|
||||
@typedoc "The kind handled by formatting functions"
|
||||
@type kind :: :error | :exit | :throw | {:EXIT, pid}
|
||||
@@ -46,7 +42,7 @@ defmodule Exception do
|
||||
def exception?(_), do: false
|
||||
|
||||
@doc """
|
||||
Gets the message for an `exception`.
|
||||
Gets the message for an exception.
|
||||
"""
|
||||
def message(%{__struct__: module, __exception__: true} = exception) when is_atom(module) do
|
||||
try do
|
||||
@@ -99,7 +95,7 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes and formats any throw/error/exit.
|
||||
Normalizes and formats any throw, error and exit.
|
||||
|
||||
The message is formatted and displayed in the same
|
||||
format as used by Elixir's CLI.
|
||||
@@ -132,7 +128,7 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes and formats throw/errors/exits and stacktraces.
|
||||
Normalizes and formats throw/errors/exits and stacktrace.
|
||||
|
||||
It relies on `format_banner/3` and `format_stacktrace/1`
|
||||
to generate the final format.
|
||||
@@ -159,7 +155,7 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats an exit. It returns a string.
|
||||
Formats an exit, returns a string.
|
||||
|
||||
Often there are errors/exceptions inside exits. Exits are often
|
||||
wrapped by the caller and provide stacktraces too. This function
|
||||
@@ -234,7 +230,6 @@ defmodule Exception do
|
||||
"shutdown: #{inspect(reason)}"
|
||||
end
|
||||
|
||||
defp format_exit_reason(:calling_self), do: "process attempted to call itself"
|
||||
defp format_exit_reason(:timeout), do: "time out"
|
||||
defp format_exit_reason(:killed), do: "killed"
|
||||
defp format_exit_reason(:noconnection), do: "no connection"
|
||||
@@ -370,10 +365,9 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
defp format_application(module) do
|
||||
# We cannot use Application due to bootstrap issues
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} -> "(" <> Atom.to_string(app) <> ") "
|
||||
:undefined -> ""
|
||||
:undefined -> ""
|
||||
end
|
||||
end
|
||||
|
||||
@@ -390,7 +384,7 @@ defmodule Exception do
|
||||
|
||||
case trace do
|
||||
[] -> "\n"
|
||||
_ -> " " <> Enum.map_join(trace, "\n ", &format_stacktrace_entry(&1)) <> "\n"
|
||||
s -> " " <> Enum.map_join(s, "\n ", &format_stacktrace_entry(&1)) <> "\n"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -400,7 +394,7 @@ defmodule Exception do
|
||||
|
||||
## Examples
|
||||
|
||||
Exception.format_fa(fn -> nil end, 1)
|
||||
Exception.format_fa(fn -> end, 1)
|
||||
#=> "#Function<...>/1"
|
||||
|
||||
"""
|
||||
@@ -453,7 +447,7 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the given `file` and `line` as shown in stacktraces.
|
||||
Formats the given file and line as shown in stacktraces.
|
||||
If any of the values are `nil`, they are omitted.
|
||||
|
||||
## Examples
|
||||
@@ -468,7 +462,11 @@ defmodule Exception do
|
||||
""
|
||||
|
||||
"""
|
||||
def format_file_line(file, line, suffix \\ "") do
|
||||
def format_file_line(file, line) do
|
||||
format_file_line(file, line, "")
|
||||
end
|
||||
|
||||
defp format_file_line(file, line, suffix) do
|
||||
if file do
|
||||
if line && line != 0 do
|
||||
"#{file}:#{line}:#{suffix}"
|
||||
@@ -499,7 +497,11 @@ defmodule ArgumentError do
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
defexception message: "bad argument in arithmetic expression"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"bad argument in arithmetic expression"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule SystemLimitError do
|
||||
@@ -522,18 +524,18 @@ end
|
||||
defmodule TokenMissingError do
|
||||
defexception [file: nil, line: nil, description: "expression is incomplete"]
|
||||
|
||||
def message(%{file: file, line: line, description: description}) do
|
||||
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <>
|
||||
" " <> description
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CompileError do
|
||||
defexception [file: nil, line: nil, description: "compile error"]
|
||||
|
||||
def message(%{file: file, line: line, description: description}) do
|
||||
Exception.format_file_line(file && Path.relative_to_cwd(file), line) <>
|
||||
" " <> description
|
||||
def message(exception) do
|
||||
Exception.format_file_line(Path.relative_to_cwd(exception.file), exception.line) <>
|
||||
" " <> exception.description
|
||||
end
|
||||
end
|
||||
|
||||
@@ -577,14 +579,6 @@ defmodule CaseClauseError do
|
||||
end
|
||||
end
|
||||
|
||||
defmodule WithClauseError do
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"no with clause matching: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule CondClauseError do
|
||||
defexception []
|
||||
|
||||
@@ -632,66 +626,18 @@ defmodule UndefinedFunctionError do
|
||||
end
|
||||
|
||||
def message(%{reason: :"module could not be loaded", module: module, function: function, arity: arity}) do
|
||||
"function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined (module #{inspect module} is not available)"
|
||||
"undefined function: " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (module #{inspect module} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not exported", module: module, function: function, arity: arity}) do
|
||||
"function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined or private" <> did_you_mean(module, function, arity)
|
||||
"undefined function: " <> Exception.format_mfa(module, function, arity)
|
||||
end
|
||||
|
||||
def message(%{reason: :"function not available", module: module, function: function, arity: arity}) do
|
||||
"nil." <> fa = Exception.format_mfa(nil, function, arity)
|
||||
"function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" is undefined (function #{fa} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: reason, module: module, function: function, arity: arity}) do
|
||||
"function " <> Exception.format_mfa(module, function, arity) <> " is undefined (#{reason})"
|
||||
end
|
||||
|
||||
@function_threshold 0.77
|
||||
@max_suggestions 5
|
||||
|
||||
defp did_you_mean(module, function, _arity) do
|
||||
exports = exports_for(module)
|
||||
|
||||
result =
|
||||
case Keyword.take(exports, [function]) do
|
||||
[] ->
|
||||
base = Atom.to_string(function)
|
||||
for {key, val} <- exports,
|
||||
dist = String.jaro_distance(base, Atom.to_string(key)),
|
||||
dist >= @function_threshold,
|
||||
do: {dist, key, val}
|
||||
arities ->
|
||||
for {key, val} <- arities, do: {1.0, key, val}
|
||||
end
|
||||
|> Enum.sort(&elem(&1, 0) >= elem(&2, 0))
|
||||
|> Enum.take(@max_suggestions)
|
||||
|> Enum.sort(&elem(&1, 1) <= elem(&2, 1))
|
||||
|
||||
case result do
|
||||
[] -> ""
|
||||
suggestions -> ". Did you mean one of:\n\n#{Enum.map(suggestions, &format_fa/1)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp format_fa({_dist, fun, arity}) do
|
||||
fun = with ":" <> fun <- inspect(fun), do: fun
|
||||
" * " <> fun <> "/" <> Integer.to_string(arity) <> "\n"
|
||||
end
|
||||
|
||||
defp exports_for(module) do
|
||||
if function_exported?(module, :__info__, 1) do
|
||||
module.__info__(:macros) ++ module.__info__(:functions)
|
||||
else
|
||||
module.module_info(:exports)
|
||||
end
|
||||
rescue
|
||||
# In case the module was removed while we are computing this
|
||||
UndefinedFunctionError -> []
|
||||
"undefined function: " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (function #{fa} is not available)"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -718,13 +664,14 @@ defmodule Code.LoadError do
|
||||
end
|
||||
|
||||
defmodule Protocol.UndefinedError do
|
||||
defexception [protocol: nil, value: nil, description: ""]
|
||||
defexception [protocol: nil, value: nil, description: nil]
|
||||
|
||||
def message(exception) do
|
||||
msg = "protocol #{inspect exception.protocol} not implemented for #{inspect exception.value}"
|
||||
case exception.description do
|
||||
"" -> msg
|
||||
descr -> msg <> ", " <> descr
|
||||
if exception.description do
|
||||
msg <> ", " <> exception.description
|
||||
else
|
||||
msg
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -756,21 +703,25 @@ defmodule UnicodeConversionError do
|
||||
"encoding starting at #{inspect rest}"
|
||||
end
|
||||
|
||||
defp detail([h | _]) when is_integer(h) do
|
||||
defp detail([h|_]) do
|
||||
"code point #{h}"
|
||||
end
|
||||
|
||||
defp detail([h | _]) do
|
||||
detail(h)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.OutOfBoundsError do
|
||||
defexception message: "out of bounds error"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"out of bounds error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule Enum.EmptyError do
|
||||
defexception message: "empty error"
|
||||
defexception []
|
||||
|
||||
def message(_) do
|
||||
"empty error"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.Error do
|
||||
@@ -778,25 +729,18 @@ defmodule File.Error do
|
||||
|
||||
def message(exception) do
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
"could not #{exception.action} #{inspect(exception.path)}: #{formatted}"
|
||||
"could not #{exception.action} #{exception.path}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule File.CopyError do
|
||||
defexception [reason: nil, source: nil, destination: nil, on: "", action: ""]
|
||||
defexception [reason: nil, action: "", source: nil, destination: nil, on: nil]
|
||||
|
||||
def message(exception) do
|
||||
formatted =
|
||||
IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
|
||||
location =
|
||||
case exception.on do
|
||||
"" -> ""
|
||||
on -> ". #{on}"
|
||||
end
|
||||
|
||||
"could not #{exception.action} from #{inspect(exception.source)} to " <>
|
||||
"#{inspect(exception.destination)}#{location}: #{formatted}"
|
||||
formatted = IO.iodata_to_binary(:file.format_error(exception.reason))
|
||||
location = if on = exception.on, do: ". #{on}", else: ""
|
||||
"could not #{exception.action} from #{exception.source} to " <>
|
||||
"#{exception.destination}#{location}: #{formatted}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -847,27 +791,17 @@ defmodule ErlangError do
|
||||
def normalize({:badkey, key}, stacktrace) do
|
||||
term =
|
||||
case stacktrace || :erlang.get_stacktrace do
|
||||
[{Map, :get_and_update!, [map, _, _], _} | _] -> map
|
||||
[{Map, :update!, [map, _, _], _} | _] -> map
|
||||
[{:maps, :update, [_, _, map], _} | _] -> map
|
||||
[{:maps, :get, [_, map], _} | _] -> map
|
||||
[{:maps, :update, [_, _, map], _}|_] -> map
|
||||
[{:maps, :get, [_, map], _}|_] -> map
|
||||
_ -> nil
|
||||
end
|
||||
%KeyError{key: key, term: term}
|
||||
end
|
||||
|
||||
def normalize({:badkey, key, map}, _stacktrace) do
|
||||
%KeyError{key: key, term: map}
|
||||
end
|
||||
|
||||
def normalize({:case_clause, term}, _stacktrace) do
|
||||
%CaseClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:with_clause, term}, _stacktrace) do
|
||||
%WithClauseError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:try_clause, term}, _stacktrace) do
|
||||
%TryClauseError{term: term}
|
||||
end
|
||||
@@ -891,11 +825,11 @@ defmodule ErlangError do
|
||||
%ErlangError{original: other}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, args, _} | _]) when is_list(args) do
|
||||
defp from_stacktrace([{module, function, args, _}|_]) when is_list(args) do
|
||||
{module, function, length(args)}
|
||||
end
|
||||
|
||||
defp from_stacktrace([{module, function, arity, _} | _]) do
|
||||
defp from_stacktrace([{module, function, arity, _}|_]) do
|
||||
{module, function, arity}
|
||||
end
|
||||
|
||||
|
||||
+87
-97
@@ -3,35 +3,35 @@ defmodule File do
|
||||
This module contains functions to manipulate files.
|
||||
|
||||
Some of those functions are low-level, allowing the user
|
||||
to interact with files or IO devices, like `open/2`,
|
||||
to interact with the file or IO devices, like `open/2`,
|
||||
`copy/3` and others. This module also provides higher
|
||||
level functions that work with filenames and have their naming
|
||||
based on UNIX variants. For example, one can copy a file
|
||||
via `cp/3` and remove files and directories recursively
|
||||
via `rm_rf/1`.
|
||||
via `rm_rf/1`
|
||||
|
||||
## Encoding
|
||||
|
||||
In order to write and read files, one must use the functions
|
||||
in the `IO` module. By default, a file is opened in binary mode,
|
||||
in the `IO` module. By default, a file is opened in binary mode
|
||||
which requires the functions `IO.binread/2` and `IO.binwrite/2`
|
||||
to interact with the file. A developer may pass `:utf8` as an
|
||||
option when opening the file, then the slower `IO.read/2` and
|
||||
`IO.write/2` functions must be used as they are responsible for
|
||||
doing the proper conversions and providing the proper data guarantees.
|
||||
doing the proper conversions and data guarantees.
|
||||
|
||||
Note that filenames when given as charlists in Elixir are
|
||||
Note that filenames when given as char lists in Elixir are
|
||||
always treated as UTF-8. In particular, we expect that the
|
||||
shell and the operating system are configured to use UTF-8
|
||||
encoding. Binary filenames are considered raw and passed
|
||||
shell and the operating system are configured to use UTF8
|
||||
encoding. Binary filenames are considering raw and passed
|
||||
to the OS as is.
|
||||
|
||||
## API
|
||||
|
||||
Most of the functions in this module return `:ok` or
|
||||
`{:ok, result}` in case of success, `{:error, reason}`
|
||||
otherwise. Those functions also have a variant
|
||||
that ends with `!` which returns the result (instead of the
|
||||
otherwise. Those function are also followed by a variant
|
||||
that ends with `!` which returns the result (without the
|
||||
`{:ok, result}` tuple) in case of success or raises an
|
||||
exception in case it fails. For example:
|
||||
|
||||
@@ -55,7 +55,7 @@ defmodule File do
|
||||
## Processes and raw files
|
||||
|
||||
Every time a file is opened, Elixir spawns a new process. Writing
|
||||
to a file is equivalent to sending messages to the process that
|
||||
to a file is equivalent to sending messages to that process that
|
||||
writes to the file descriptor.
|
||||
|
||||
This means files can be passed between nodes and message passing
|
||||
@@ -63,7 +63,7 @@ defmodule File do
|
||||
|
||||
However, you may not always want to pay the price for this abstraction.
|
||||
In such cases, a file can be opened in `:raw` mode. The options `:read_ahead`
|
||||
and `:delayed_write` are also useful when operating on large files or
|
||||
and `:delayed_write` are also useful when operating large files or
|
||||
working with files in tight loops.
|
||||
|
||||
Check [`:file.open/2`](http://www.erlang.org/doc/man/file.html#open-2) for more information
|
||||
@@ -75,9 +75,9 @@ defmodule File do
|
||||
@type posix :: :file.posix()
|
||||
@type io_device :: :file.io_device()
|
||||
@type stat_options :: [time: :local | :universal | :posix]
|
||||
@type mode :: :append | :binary | :charlist | :compressed | :delayed_write | :exclusive |
|
||||
:raw | :read | :read_ahead | :sync | :utf8 | :write |
|
||||
{:encoding, :latin1 | :unicode | :utf8 | :utf16 | :utf32 |
|
||||
@type mode :: :append | :binary | :compressed | :delayed_write | :exclusive |
|
||||
:raw | :read | :read_ahead | :sync | :write |
|
||||
{:encoding, :latin1 | :unicode | :utf16 | :utf32 | :utf8 |
|
||||
{:utf16, :big | :little} | {:utf32, :big | :little}} |
|
||||
{:read_ahead, pos_integer} |
|
||||
{:delayed_write, non_neg_integer, non_neg_integer}
|
||||
@@ -232,7 +232,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary with the contents of the given filename or raises
|
||||
Returns binary with the contents of the given filename or raises
|
||||
`File.Error` if an error occurs.
|
||||
"""
|
||||
@spec read!(Path.t) :: binary | no_return
|
||||
@@ -261,8 +261,7 @@ defmodule File do
|
||||
The values for `:time` can be:
|
||||
|
||||
* `:universal` - returns a `{date, time}` tuple in UTC (default)
|
||||
* `:local` - returns a `{date, time}` tuple using the same time zone as the
|
||||
machine
|
||||
* `:local` - returns a `{date, time}` tuple using the machine time
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
"""
|
||||
@@ -292,8 +291,8 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `path`. If the file is a symlink, sets
|
||||
the `type` to `:symlink` and returns a `File.Stat` struct for the link. For any
|
||||
Returns information about the `path`. If the file is a symlink sets
|
||||
the `type` to `:symlink` and returns `File.Stat` for the link. For any
|
||||
other file, returns exactly the same values as `stat/2`.
|
||||
|
||||
For more details, see [`:file.read_link_info/2`](http://www.erlang.org/doc/man/file.html#read_link_info-2).
|
||||
@@ -323,7 +322,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `lstat/2` but returns the `File.Stat` struct directly and
|
||||
Same as `lstat/2` but returns the `File.Stat` directly and
|
||||
throws `File.Error` if an error is returned.
|
||||
"""
|
||||
@spec lstat!(Path.t, stat_options) :: File.Stat.t | no_return
|
||||
@@ -364,7 +363,7 @@ defmodule File do
|
||||
Updates modification time (mtime) and access time (atime) of
|
||||
the given file.
|
||||
|
||||
The file is created if it doesn’t exist. Requires datetime in UTC.
|
||||
File is created if it doesn’t exist. Requires datetime in UTC.
|
||||
"""
|
||||
@spec touch(Path.t, :calendar.datetime) :: :ok | {:error, posix}
|
||||
def touch(path, time \\ :calendar.universal_time) do
|
||||
@@ -411,7 +410,7 @@ defmodule File do
|
||||
@doc """
|
||||
Copies the contents of `source` to `destination`.
|
||||
|
||||
Both parameters can be a filename or an IO device opened
|
||||
Both parameters can be a filename or an io device opened
|
||||
with `open/2`. `bytes_count` specifies the number of
|
||||
bytes to copy, the default being `:infinity`.
|
||||
|
||||
@@ -430,33 +429,32 @@ defmodule File do
|
||||
Typical error reasons are the same as in `open/2`,
|
||||
`read/1` and `write/3`.
|
||||
"""
|
||||
@spec copy(Path.t | io_device, Path.t | io_device, pos_integer | :infinity) :: {:ok, non_neg_integer} | {:error, posix}
|
||||
@spec copy(Path.t, Path.t, pos_integer | :infinity) :: {:ok, non_neg_integer} | {:error, posix}
|
||||
def copy(source, destination, bytes_count \\ :infinity) do
|
||||
F.copy(maybe_to_string(source), maybe_to_string(destination), bytes_count)
|
||||
F.copy(IO.chardata_to_string(source), IO.chardata_to_string(destination), bytes_count)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `copy/3` but raises an `File.CopyError` if it fails.
|
||||
Returns the `bytes_copied` otherwise.
|
||||
"""
|
||||
@spec copy!(Path.t | io_device, Path.t | io_device, pos_integer | :infinity) :: non_neg_integer | no_return
|
||||
@spec copy!(Path.t, Path.t, pos_integer | :infinity) :: non_neg_integer | no_return
|
||||
def copy!(source, destination, bytes_count \\ :infinity) do
|
||||
case copy(source, destination, bytes_count) do
|
||||
{:ok, bytes_count} -> bytes_count
|
||||
{:error, reason} ->
|
||||
raise File.CopyError, reason: reason, action: "copy",
|
||||
source: maybe_to_string(source),
|
||||
destination: maybe_to_string(destination)
|
||||
source: IO.chardata_to_string(source), destination: IO.chardata_to_string(destination)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renames the `source` file to `destination` file. It can be used to move files
|
||||
Renames the `source` file to `destination` file. If can be used to move files
|
||||
(and directories) between directories. If moving a file, you must fully
|
||||
specify the `destination` filename, it is not sufficient to simply specify
|
||||
its directory.
|
||||
it's directory.
|
||||
|
||||
It returns `:ok` in case of success, returns `{:error, reason}` otherwise.
|
||||
It returns `:ok` in case of success, returns `{:error, reason}` otherwise
|
||||
|
||||
Note: The command `mv` in Unix systems behaves differently depending
|
||||
if `source` is a file and the `destination` is an existing directory.
|
||||
@@ -480,12 +478,12 @@ defmodule File do
|
||||
|
||||
If a file already exists in the destination, it invokes a
|
||||
callback which should return `true` if the existing file
|
||||
should be overwritten, `false` otherwise. The callback defaults to return `true`.
|
||||
should be overwritten, `false` otherwise. It defaults to return `true`.
|
||||
|
||||
The function returns `:ok` in case of success, returns
|
||||
It returns `:ok` in case of success, returns
|
||||
`{:error, reason}` otherwise.
|
||||
|
||||
If you want to copy contents from an IO device to another device
|
||||
If you want to copy contents from an io device to another device
|
||||
or do a straight copy from a source to a destination without
|
||||
preserving modes, check `copy/3` instead.
|
||||
|
||||
@@ -536,20 +534,20 @@ defmodule File do
|
||||
If a file already exists in the destination,
|
||||
it invokes a callback which should return
|
||||
`true` if the existing file should be overwritten,
|
||||
`false` otherwise. The callback defaults to return `true`.
|
||||
`false` otherwise. It defaults to return `true`.
|
||||
|
||||
If a directory already exists in the destination
|
||||
where a file is meant to be (or vice versa), this
|
||||
where a file is meant to be (or otherwise), this
|
||||
function will fail.
|
||||
|
||||
This function may fail while copying files,
|
||||
in such cases, it will leave the destination
|
||||
directory in a dirty state, where file which have already been copied
|
||||
won't be removed.
|
||||
directory in a dirty state, where already
|
||||
copied files won't be removed.
|
||||
|
||||
The function returns `{:ok, files_and_directories}` in case of
|
||||
success, `files_and_directories` lists all files and directories copied in no
|
||||
specific order. It returns `{:error, reason, file}` otherwise.
|
||||
It returns `{:ok, files_and_directories}` in case of
|
||||
success with all files and directories copied in no
|
||||
specific order, `{:error, reason, file}` otherwise.
|
||||
|
||||
Note: The command `cp` in Unix systems behaves differently
|
||||
depending if `destination` is an existing directory or not.
|
||||
@@ -610,7 +608,7 @@ defmodule File do
|
||||
{:ok, files} ->
|
||||
case mkdir(dest) do
|
||||
success when success in [:ok, {:error, :eexist}] ->
|
||||
Enum.reduce(files, [dest | acc], fn(x, acc) ->
|
||||
Enum.reduce(files, [dest|acc], fn(x, acc) ->
|
||||
do_cp_r(Path.join(src, x), Path.join(dest, x), callback, acc)
|
||||
end)
|
||||
{:error, reason} -> {:error, reason, dest}
|
||||
@@ -637,13 +635,13 @@ defmodule File do
|
||||
case F.copy(src, {dest, [:exclusive]}) do
|
||||
{:ok, _} ->
|
||||
copy_file_mode!(src, dest)
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
case copy(src, dest) do
|
||||
{:ok, _} ->
|
||||
copy_file_mode!(src, dest)
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, reason} -> {:error, reason, src}
|
||||
end
|
||||
else
|
||||
@@ -657,13 +655,13 @@ defmodule File do
|
||||
defp do_cp_link(link, src, dest, callback, acc) do
|
||||
case F.make_symlink(link, dest) do
|
||||
:ok ->
|
||||
[dest | acc]
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
# If rm/1 fails, F.make_symlink/2 will fail
|
||||
_ = rm(dest)
|
||||
case F.make_symlink(link, dest) do
|
||||
:ok -> [dest | acc]
|
||||
:ok -> [dest|acc]
|
||||
{:error, reason} -> {:error, reason, src}
|
||||
end
|
||||
else
|
||||
@@ -684,13 +682,13 @@ defmodule File do
|
||||
and a new process is spawned to write to the file. For this reason, if you are
|
||||
doing multiple writes in a loop, opening the file via `File.open/2` and using
|
||||
the functions in `IO` to write to the file will yield much better performance
|
||||
than calling this function multiple times.
|
||||
then calling this function multiple times.
|
||||
|
||||
Typical error reasons are:
|
||||
|
||||
* `:enoent` - a component of the file name does not exist
|
||||
* `:enotdir` - a component of the file name is not a directory;
|
||||
on some platforms, `:enoent` is returned instead
|
||||
on some platforms, enoent is returned instead
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:eacces` - missing permission for writing the file or searching one of
|
||||
the parent directories
|
||||
@@ -700,7 +698,6 @@ defmodule File do
|
||||
"""
|
||||
@spec write(Path.t, iodata, [mode]) :: :ok | {:error, posix}
|
||||
def write(path, content, modes \\ []) do
|
||||
modes = normalize_modes(modes, false)
|
||||
F.write_file(IO.chardata_to_string(path), content, modes)
|
||||
end
|
||||
|
||||
@@ -709,7 +706,6 @@ defmodule File do
|
||||
"""
|
||||
@spec write!(Path.t, iodata, [mode]) :: :ok | no_return
|
||||
def write!(path, content, modes \\ []) do
|
||||
modes = normalize_modes(modes, false)
|
||||
case F.write_file(path, content, modes) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
@@ -731,7 +727,7 @@ defmodule File do
|
||||
* `:eacces` - missing permission for the file or one of its parents
|
||||
* `:eperm` - the file is a directory and user is not super-user
|
||||
* `:enotdir` - a component of the file name is not a directory;
|
||||
on some platforms, `:enoent` is returned instead
|
||||
on some platforms, enoent is returned instead
|
||||
* `:einval` - filename had an improper type, such as tuple
|
||||
|
||||
## Examples
|
||||
@@ -852,7 +848,7 @@ defmodule File do
|
||||
case res do
|
||||
{:ok, acc} ->
|
||||
case rmdir(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enoent} -> res
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
end
|
||||
@@ -872,19 +868,19 @@ defmodule File do
|
||||
|
||||
defp do_rm_regular(path, {:ok, acc} = entry) do
|
||||
case rm(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enoent} -> entry
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
end
|
||||
end
|
||||
|
||||
# On Windows, symlinks are treated as directory and must be removed
|
||||
# On windows, symlinks are treated as directory and must be removed
|
||||
# with rmdir/1. But on Unix, we remove them via rm/1. So we first try
|
||||
# to remove it as a directory and, if we get :enotdir, we fallback to
|
||||
# a file removal.
|
||||
defp do_rm_directory(path, {:ok, acc} = entry) do
|
||||
case rmdir(path) do
|
||||
:ok -> {:ok, [path | acc]}
|
||||
:ok -> {:ok, [path|acc]}
|
||||
{:error, :enotdir} -> do_rm_regular(path, entry)
|
||||
{:error, :enoent} -> entry
|
||||
{:error, reason} -> {:error, reason, path}
|
||||
@@ -922,10 +918,10 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Opens the given `path` according to the given list of `modes`.
|
||||
Opens the given `path` according to the given list of modes.
|
||||
|
||||
In order to write and read files, one must use the functions
|
||||
in the `IO` module. By default, a file is opened in `:binary` mode,
|
||||
in the `IO` module. By default, a file is opened in binary mode
|
||||
which requires the functions `IO.binread/2` and `IO.binwrite/2`
|
||||
to interact with the file. A developer may pass `:utf8` as an
|
||||
option when opening the file and then all other functions from
|
||||
@@ -933,9 +929,6 @@ defmodule File do
|
||||
|
||||
The allowed modes:
|
||||
|
||||
* `:binary` - opens the file in binary mode, disabling special handling of unicode sequences
|
||||
(default mode).
|
||||
|
||||
* `:read` - the file, which must exist, is opened for reading.
|
||||
|
||||
* `:write` - the file is opened for writing. It is created if it does not
|
||||
@@ -951,8 +944,8 @@ defmodule File do
|
||||
* `:exclusive` - the file, when opened for writing, is created if it does
|
||||
not exist. If the file exists, open will return `{:error, :eexist}`.
|
||||
|
||||
* `:charlist` - when this term is given, read operations on the file will
|
||||
return charlists rather than binaries.
|
||||
* `:char_list` - when this term is given, read operations on the file will
|
||||
return char lists rather than binaries.
|
||||
|
||||
* `:compressed` - makes it possible to read or write gzip compressed files.
|
||||
|
||||
@@ -962,16 +955,15 @@ defmodule File do
|
||||
|
||||
* `:utf8` - this option denotes how data is actually stored in the disk
|
||||
file and makes the file perform automatic translation of characters to
|
||||
and from UTF-8.
|
||||
and from utf-8.
|
||||
|
||||
If data is sent to a file in a format that cannot be converted to the
|
||||
UTF-8 or if data is read by a function that returns data in a format that
|
||||
utf-8 or if data is read by a function that returns data in a format that
|
||||
cannot cope with the character range of the data, an error occurs and the
|
||||
file will be closed.
|
||||
|
||||
* `:delayed_write`, `:raw`, `:ram`, `:read_ahead`, `:sync`, `{:encoding, ...}`,
|
||||
`{:read_ahead, pos_integer}`, `{:delayed_write, non_neg_integer, non_neg_integer}` -
|
||||
for more information about these options see [`:file.open/2`](http://www.erlang.org/doc/man/file.html#open-2).
|
||||
For more information about other options like `:read_ahead` and `:delayed_write`,
|
||||
see [`:file.open/2`](http://www.erlang.org/doc/man/file.html#open-2).
|
||||
|
||||
This function returns:
|
||||
|
||||
@@ -999,7 +991,7 @@ defmodule File do
|
||||
def open(path, modes \\ [])
|
||||
|
||||
def open(path, modes) when is_list(modes) do
|
||||
F.open(IO.chardata_to_string(path), normalize_modes(modes, true))
|
||||
F.open(IO.chardata_to_string(path), open_defaults(modes, true))
|
||||
end
|
||||
|
||||
def open(path, function) when is_function(function) do
|
||||
@@ -1007,9 +999,9 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `open/2` but expects a function as its last argument.
|
||||
Similar to `open/2` but expects a function as last argument.
|
||||
|
||||
The file is opened, given to the function as an argument and
|
||||
The file is opened, given to the function as argument and
|
||||
automatically closed after the function returns, regardless
|
||||
if there was an error when executing the function.
|
||||
|
||||
@@ -1073,7 +1065,7 @@ defmodule File do
|
||||
Gets the current working directory.
|
||||
|
||||
In rare circumstances, this function can fail on Unix. It may happen
|
||||
if read permissions do not exist for the parent directories of the
|
||||
if read permission does not exist for the parent directories of the
|
||||
current directory. For this reason, returns `{:ok, cwd}` in case
|
||||
of success, `{:error, reason}` otherwise.
|
||||
"""
|
||||
@@ -1131,8 +1123,8 @@ defmodule File do
|
||||
|
||||
@doc """
|
||||
Changes the current directory to the given `path`,
|
||||
executes the given function and then reverts back
|
||||
to the previous path regardless of whether there is an exception.
|
||||
executes the given function and then revert back
|
||||
to the previous path regardless if there is an exception.
|
||||
|
||||
Raises an error if retrieving or changing the current
|
||||
directory fails.
|
||||
@@ -1149,7 +1141,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the list of files in the given directory.
|
||||
Returns list of files in the given directory.
|
||||
|
||||
It returns `{:ok, [files]}` in case of success,
|
||||
`{:error, reason}` otherwise.
|
||||
@@ -1199,9 +1191,9 @@ defmodule File do
|
||||
streaming, by `:line` (default) or by a given number of bytes.
|
||||
|
||||
Operating the stream can fail on open for the same reasons as
|
||||
`File.open!/2`. Note that the file is automatically opened each time streaming
|
||||
begins. There is no need to pass `:read` and `:write` modes, as those are
|
||||
automatically set by Elixir.
|
||||
`File.open!/2`. Note that the file is automatically opened only and
|
||||
every time streaming begins. There is no need to pass `:read` and
|
||||
`:write` modes, as those are automatically set by Elixir.
|
||||
|
||||
## Raw files
|
||||
|
||||
@@ -1228,7 +1220,7 @@ defmodule File do
|
||||
|
||||
"""
|
||||
def stream!(path, modes \\ [], line_or_bytes \\ :line) do
|
||||
modes = normalize_modes(modes, true)
|
||||
modes = open_defaults(modes, true)
|
||||
File.Stream.__build__(IO.chardata_to_string(path), modes, line_or_bytes)
|
||||
end
|
||||
|
||||
@@ -1275,7 +1267,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the group given by the group id `gid`
|
||||
Changes the user group given by the group id `gid`
|
||||
for a given `file`. Returns `:ok` on success, or
|
||||
`{:error, reason}` on failure.
|
||||
"""
|
||||
@@ -1322,26 +1314,24 @@ defmodule File do
|
||||
|
||||
## Helpers
|
||||
|
||||
@read_ahead_size 64 * 1024
|
||||
@read_ahead 64*1024
|
||||
|
||||
defp normalize_modes([:utf8 | rest], binary?) do
|
||||
[encoding: :utf8] ++ normalize_modes(rest, binary?)
|
||||
defp open_defaults([:char_list|t], _add_binary) do
|
||||
open_defaults(t, false)
|
||||
end
|
||||
defp normalize_modes([:read_ahead | rest], binary?) do
|
||||
[read_ahead: @read_ahead_size] ++ normalize_modes(rest, binary?)
|
||||
end
|
||||
# TODO: Deprecate :char_list mode by v1.5
|
||||
defp normalize_modes([mode | rest], _binary?) when mode in [:charlist, :char_list] do
|
||||
normalize_modes(rest, false)
|
||||
end
|
||||
defp normalize_modes([mode | rest], binary?) do
|
||||
[mode | normalize_modes(rest, binary?)]
|
||||
end
|
||||
defp normalize_modes([], true), do: [:binary]
|
||||
defp normalize_modes([], false), do: []
|
||||
|
||||
defp maybe_to_string(path) when is_pid(path),
|
||||
do: path
|
||||
defp maybe_to_string(path),
|
||||
do: IO.chardata_to_string(path)
|
||||
defp open_defaults([:utf8|t], add_binary) do
|
||||
open_defaults([{:encoding, :utf8}|t], add_binary)
|
||||
end
|
||||
|
||||
defp open_defaults([:read_ahead|t], add_binary) do
|
||||
open_defaults([{:read_ahead, @read_ahead}|t], add_binary)
|
||||
end
|
||||
|
||||
defp open_defaults([h|t], add_binary) do
|
||||
[h|open_defaults(t, add_binary)]
|
||||
end
|
||||
|
||||
defp open_defaults([], true), do: [:binary]
|
||||
defp open_defaults([], false), do: []
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ require Record
|
||||
|
||||
defmodule File.Stat do
|
||||
@moduledoc """
|
||||
A struct that holds file information.
|
||||
A struct responsible to hold file information.
|
||||
|
||||
In Erlang, this struct is represented by a `:file_info` record.
|
||||
Therefore this module also provides functions for converting
|
||||
@@ -32,7 +32,7 @@ defmodule File.Stat do
|
||||
systems which have no concept of links.
|
||||
|
||||
* `major_device` - identifies the file system where the file is located.
|
||||
In Windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
In windows, the number indicates a drive as follows: 0 means A:, 1 means
|
||||
B:, and so on.
|
||||
|
||||
* `minor_device` - only valid for character devices on Unix. In all other
|
||||
@@ -41,11 +41,10 @@ defmodule File.Stat do
|
||||
* `inode` - gives the inode number. On non-Unix file systems, this field
|
||||
will be zero.
|
||||
|
||||
* `uid` - indicates the owner of the file. Will be zero for non-Unix file
|
||||
systems.
|
||||
* `uid` - indicates the owner of the file.
|
||||
|
||||
* `gid` - indicates the group that owns the file. Will be zero for
|
||||
non-Unix file systems.
|
||||
* `gid` - gives the group that the owner of the file belongs to. Will be
|
||||
zero for non-Unix file systems.
|
||||
|
||||
The time type returned in `atime`, `mtime`, and `ctime` is dependent on the
|
||||
time type set in options. `{:time, type}` where type can be `:local`,
|
||||
|
||||
@@ -20,15 +20,14 @@ defmodule File.Stream do
|
||||
raw = :lists.keyfind(:encoding, 1, modes) == false
|
||||
|
||||
modes =
|
||||
case raw do
|
||||
true ->
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw | modes]
|
||||
else
|
||||
[:raw, :read_ahead | modes]
|
||||
end
|
||||
false ->
|
||||
modes
|
||||
if raw do
|
||||
if :lists.keyfind(:read_ahead, 1, modes) == {:read_ahead, false} do
|
||||
[:raw|modes]
|
||||
else
|
||||
[:raw, :read_ahead|modes]
|
||||
end
|
||||
else
|
||||
modes
|
||||
end
|
||||
|
||||
%File.Stream{path: path, modes: modes, raw: raw, line_or_bytes: line_or_bytes}
|
||||
@@ -38,7 +37,7 @@ defmodule File.Stream do
|
||||
def into(%{path: path, modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, not mode in [:read], do: mode
|
||||
|
||||
case :file.open(path, [:write | modes]) do
|
||||
case :file.open(path, [:write|modes]) do
|
||||
{:ok, device} ->
|
||||
{:ok, into(device, stream, raw)}
|
||||
{:error, reason} ->
|
||||
|
||||
+63
-49
@@ -8,14 +8,10 @@ defmodule Float do
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
If successful, returns a tuple in the form of `{float, remainder_of_binary}`;
|
||||
when the binary cannot be coerced into a valid float, the atom `:error` is
|
||||
returned.
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`;
|
||||
otherwise, `:error`.
|
||||
|
||||
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
|
||||
the `ArgumentError` exception is raised.
|
||||
|
||||
If you want to convert a string-formatted float directly to a float,
|
||||
If a float formatted string wants to be directly converted to a float,
|
||||
`String.to_float/1` can be used instead.
|
||||
|
||||
## Examples
|
||||
@@ -55,16 +51,16 @@ defmodule Float do
|
||||
defp parse_unsigned(binary) when is_binary(binary), do:
|
||||
:error
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>, dot?, e?, acc) when digit in ?0..?9, do:
|
||||
defp parse_unsigned(<<digit, rest :: binary>>, dot?, e?, acc) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, dot?, e?, <<acc::binary, digit>>)
|
||||
|
||||
defp parse_unsigned(<<?., digit, rest::binary>>, false, false, acc) when digit in ?0..?9, do:
|
||||
defp parse_unsigned(<<?., digit, rest :: binary>>, false, false, acc) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, false, <<acc::binary, ?., digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, digit, rest::binary>>, dot?, false, acc) when exp_marker in 'eE' and digit in ?0..?9, do:
|
||||
defp parse_unsigned(<<exp_marker, digit, rest :: binary>>, dot?, false, acc) when exp_marker in 'eE' and digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, digit>>)
|
||||
|
||||
defp parse_unsigned(<<exp_marker, sign, digit, rest::binary>>, dot?, false, acc) when exp_marker in 'eE' and sign in '-+' and digit in ?0..?9, do:
|
||||
defp parse_unsigned(<<exp_marker, sign, digit, rest :: binary>>, dot?, false, acc) when exp_marker in 'eE' and sign in '-+' and digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, true, <<add_dot(acc, dot?)::binary, ?e, sign, digit>>)
|
||||
|
||||
defp parse_unsigned(rest, dot?, _e?, acc), do:
|
||||
@@ -90,7 +86,7 @@ defmodule Float do
|
||||
iex> Float.floor(-56.5)
|
||||
-57.0
|
||||
|
||||
iex> Float.floor(34.259, 2)
|
||||
iex> Float.floor(34.253, 2)
|
||||
34.25
|
||||
|
||||
"""
|
||||
@@ -104,7 +100,7 @@ defmodule Float do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a float to the smallest integer greater than or equal to `num`.
|
||||
Rounds a float to the largest integer greater than or equal to `num`.
|
||||
|
||||
`ceil/2` also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
@@ -120,7 +116,7 @@ defmodule Float do
|
||||
iex> Float.ceil(-56.5)
|
||||
-56.0
|
||||
|
||||
iex> Float.ceil(34.251, 2)
|
||||
iex> Float.ceil(34.253, 2)
|
||||
34.26
|
||||
|
||||
"""
|
||||
@@ -155,9 +151,6 @@ defmodule Float do
|
||||
iex> Float.round(-5.5675, 3)
|
||||
-5.568
|
||||
|
||||
iex> Float.round(-5.5675)
|
||||
-6.0
|
||||
|
||||
"""
|
||||
@spec round(float, 0..15) :: float
|
||||
def round(number, precision \\ 0) when is_float(number) and precision in 0..15 do
|
||||
@@ -171,63 +164,84 @@ defmodule Float do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation
|
||||
of the given float.
|
||||
Returns a char list which corresponds to the text representation of the given float.
|
||||
|
||||
It uses the shortest representation according to algorithm described
|
||||
in "Printing Floating-Point Numbers Quickly and Accurately" in
|
||||
Proceedings of the SIGPLAN '96 Conference on Programming Language
|
||||
Design and Implementation.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_charlist(7.0)
|
||||
'7.0'
|
||||
iex> Float.to_char_list(7.0)
|
||||
'7.00000000000000000000e+00'
|
||||
|
||||
"""
|
||||
@spec to_charlist(float) :: charlist
|
||||
def to_charlist(float) when is_float(float) do
|
||||
:io_lib_format.fwrite_g(float)
|
||||
@spec to_char_list(float) :: char_list
|
||||
def to_char_list(float) do
|
||||
:erlang.float_to_list(float)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
## Options
|
||||
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_char_list 7.1, [decimals: 2, compact: true]
|
||||
'7.1'
|
||||
|
||||
"""
|
||||
@spec to_char_list(float, list) :: char_list
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of the given float.
|
||||
|
||||
It uses the shortest representation according to algorithm described
|
||||
in "Printing Floating-Point Numbers Quickly and Accurately" in
|
||||
Proceedings of the SIGPLAN '96 Conference on Programming Language
|
||||
Design and Implementation.
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string(7.0)
|
||||
"7.0"
|
||||
"7.00000000000000000000e+00"
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t
|
||||
def to_string(float) when is_float(float) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(float))
|
||||
def to_string(float) do
|
||||
:erlang.float_to_binary(float)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
def to_char_list(float), do: Float.to_charlist(float)
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `float`.
|
||||
|
||||
# TODO: Deprecate by v1.4
|
||||
@doc false
|
||||
def to_char_list(float, options) do
|
||||
:erlang.float_to_list(float, expand_compact(options))
|
||||
end
|
||||
## Options
|
||||
|
||||
# TODO: Deprecate by v1.4
|
||||
@doc false
|
||||
* `:decimals` - number of decimal points to show
|
||||
* `:scientific` - number of decimal points to show, in scientific format
|
||||
* `:compact` - when `true`, use the most compact representation (ignored
|
||||
with the `scientific` option)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.to_string 7.1, [decimals: 2, compact: true]
|
||||
"7.1"
|
||||
|
||||
"""
|
||||
@spec to_string(float, list) :: String.t
|
||||
def to_string(float, options) do
|
||||
:erlang.float_to_binary(float, expand_compact(options))
|
||||
end
|
||||
|
||||
defp expand_compact([{:compact, false} | t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true} | t]), do: [:compact | expand_compact(t)]
|
||||
defp expand_compact([h | t]), do: [h | expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
defp expand_compact([{:compact, false}|t]), do: expand_compact(t)
|
||||
defp expand_compact([{:compact, true}|t]), do: [:compact|expand_compact(t)]
|
||||
defp expand_compact([h|t]), do: [h|expand_compact(t)]
|
||||
defp expand_compact([]), do: []
|
||||
end
|
||||
|
||||
+45
-56
@@ -14,7 +14,7 @@ defmodule GenEvent do
|
||||
|
||||
There are many use cases for event handlers. For example, a logging
|
||||
system can be built using event handlers where each log message is
|
||||
an event and different event handlers can be attached to handle the
|
||||
an event and different event handlers can be plugged to handle the
|
||||
log messages. One handler may print error messages on the terminal,
|
||||
another can write it to a file, while a third one can keep the
|
||||
messages in memory (like a buffer) until they are read.
|
||||
@@ -22,14 +22,13 @@ defmodule GenEvent do
|
||||
As an example, let's have a GenEvent that accumulates messages until
|
||||
they are collected by an explicit call.
|
||||
|
||||
# Define an Event Handler
|
||||
defmodule LoggerHandler do
|
||||
use GenEvent
|
||||
|
||||
# Callbacks
|
||||
|
||||
def handle_event({:log, x}, messages) do
|
||||
{:ok, [x | messages]}
|
||||
{:ok, [x|messages]}
|
||||
end
|
||||
|
||||
def handle_call(:messages, messages) do
|
||||
@@ -37,21 +36,17 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
# Start a new event manager.
|
||||
{:ok, pid} = GenEvent.start_link([])
|
||||
|
||||
# Attach an event handler to the event manager.
|
||||
GenEvent.add_handler(pid, LoggerHandler, [])
|
||||
#=> :ok
|
||||
|
||||
# Send some events to the event manager.
|
||||
GenEvent.notify(pid, {:log, 1})
|
||||
#=> :ok
|
||||
|
||||
GenEvent.notify(pid, {:log, 2})
|
||||
#=> :ok
|
||||
|
||||
# Call functions on specific handlers in the manager.
|
||||
GenEvent.call(pid, LoggerHandler, :messages)
|
||||
#=> [1, 2]
|
||||
|
||||
@@ -79,14 +74,14 @@ defmodule GenEvent do
|
||||
|
||||
## Modes
|
||||
|
||||
GenEvent supports three different notifications.
|
||||
GenEvent stream supports three different notifications.
|
||||
|
||||
On `GenEvent.ack_notify/2`, the manager acknowledges each event,
|
||||
providing backpressure, but processing of the message happens
|
||||
asynchronously.
|
||||
|
||||
On `GenEvent.sync_notify/2`, the manager acknowledges an event
|
||||
just after it is processed by all event handlers.
|
||||
just after it was processed by all event handlers.
|
||||
|
||||
On `GenEvent.notify/2`, all events are processed asynchronously and
|
||||
there is no ack (which means there is no backpressure).
|
||||
@@ -100,7 +95,7 @@ defmodule GenEvent do
|
||||
stream = GenEvent.stream(pid)
|
||||
|
||||
# Discard the next 3 events
|
||||
_ = Enum.take(stream, 3)
|
||||
_ = Enum.drop(stream, 3)
|
||||
|
||||
# Print all remaining events
|
||||
for event <- stream do
|
||||
@@ -114,9 +109,11 @@ defmodule GenEvent do
|
||||
|
||||
## Learn more and compatibility
|
||||
|
||||
If you wish to find out more about GenEvent, the documentation and links
|
||||
in Erlang can provide extra insight.
|
||||
If you wish to find out more about gen events, Elixir getting started
|
||||
guides provide a tutorial-like introduction. The documentation and links
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [Introduction to Mix – Elixir's Getting Started Guide](http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html)
|
||||
* [`:gen_event` module documentation](http://www.erlang.org/doc/man/gen_event.html)
|
||||
* [Event Handlers – Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/event-handlers)
|
||||
|
||||
@@ -128,13 +125,13 @@ defmodule GenEvent do
|
||||
too much kool aid" section of the "Learn you some Erlang" link above. Due
|
||||
to those changes, Elixir's GenEvent does not trap exits by default.
|
||||
|
||||
Furthermore, Elixir also normalizes the `{:error, _}` tuples returned
|
||||
Furthermore, Elixir's also normalizes the `{:error, _}` tuples returned
|
||||
by many functions, in order to be more consistent with themselves and
|
||||
the `GenServer` module.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Invoked when the handler is added to the `GenEvent` process. `add_handler/3`
|
||||
Invoked when the handler is added to the `GenEvent` process. `add_handler/3`,
|
||||
(and `add_mon_handler/3`) will block until it returns.
|
||||
|
||||
`args` is the argument term (third argument) passed to `add_handler/3`.
|
||||
@@ -195,7 +192,7 @@ defmodule GenEvent do
|
||||
`{:ok, reply, new_state}` except the process is hibernated. See
|
||||
`handle_event/2` for more information on hibernation.
|
||||
|
||||
Returning `{:remove_handler, reply}` sends `reply` as a response to the call,
|
||||
Returning `{:remove_handler, reply}` sends `reply` as a reponse to the call,
|
||||
removes the handler from the `GenEvent` loop and calls `terminate/2` with
|
||||
reason `:remove_handler` and state `state`.
|
||||
"""
|
||||
@@ -251,7 +248,7 @@ defmodule GenEvent do
|
||||
exits or its node is disconnected. Therefore it is not guaranteed that
|
||||
`terminate/2` is called when a `GenEvent` exits.
|
||||
|
||||
Care should be taken to cleanup because the `GenEvent` can continue to loop
|
||||
Care should be taken to cleanup because the `GenEvent` can continue is loop
|
||||
after removing the handler. This is different to most other OTP behaviours.
|
||||
For example if the handler controls a `port` (e.g. `:gen_tcp.socket`) or
|
||||
`File.io_device`, it will be need to be closed in `terminate/2` as the
|
||||
@@ -262,13 +259,13 @@ defmodule GenEvent do
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the handler when a different version of the
|
||||
handler's module is loaded (hot code swapping) and the state's term
|
||||
handler's module module is loaded (hot code swapping) and the state's term
|
||||
structure should be changed.
|
||||
|
||||
`old_vsn` is the previous version of the module (defined by the `@vsn`
|
||||
attribute) when upgrading. When downgrading the previous version is wrapped in
|
||||
a 2-tuple with first element `:down`. `state` is the current state of the
|
||||
handler and `extra` is any extra data required to change the state.
|
||||
handker and `extra` is any extra data required to change the state.
|
||||
|
||||
Returning `{:ok, new_state}` changes the state to `new_state` and the code
|
||||
change is successful.
|
||||
@@ -312,7 +309,7 @@ defmodule GenEvent do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
@@ -420,10 +417,6 @@ defmodule GenEvent do
|
||||
|
||||
If the given handler was previously installed at the manager, this
|
||||
function returns `{:error, :already_present}`.
|
||||
|
||||
For installing multiple instances of the same handler, `{Module, id}` instead
|
||||
of `Module` must be used. The handler could be then referenced with
|
||||
`{Module, id}` instead of just `Module`.
|
||||
"""
|
||||
@spec add_handler(manager, handler, term) :: :ok | {:error, term}
|
||||
def add_handler(manager, handler, args) do
|
||||
@@ -456,7 +449,7 @@ defmodule GenEvent do
|
||||
* `{:swapped, new_handler, pid}` - if the process pid has replaced the
|
||||
event handler by another
|
||||
|
||||
* `term` - if the event handler is removed due to an error. Which term
|
||||
* a term - if the event handler is removed due to an error. Which term
|
||||
depends on the error
|
||||
|
||||
Keep in mind that the `{:gen_event_EXIT, handler, reason}` message is not
|
||||
@@ -525,7 +518,7 @@ defmodule GenEvent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an ack event notification to the event `manager`.
|
||||
Sends a ack event notification to the event `manager`.
|
||||
|
||||
In other words, this function only returns `:ok` as soon as the
|
||||
event manager starts processing this event, but it does not wait
|
||||
@@ -614,24 +607,18 @@ defmodule GenEvent do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the manager with the given `reason`.
|
||||
Terminates the event `manager`.
|
||||
|
||||
Before terminating, the event manager will call
|
||||
`terminate(:stop, ...)` for each installed event handler.
|
||||
It returns `:ok` if the manager terminates with the given
|
||||
reason, if it terminates with another reason, the call will
|
||||
exit.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report will be logged.
|
||||
Before terminating, the event manager will call `terminate(:stop, ...)`
|
||||
for each installed event handler.
|
||||
"""
|
||||
@spec stop(manager, reason :: term, timeout) :: :ok
|
||||
def stop(manager, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(manager, reason, timeout)
|
||||
@spec stop(manager) :: :ok
|
||||
def stop(manager) do
|
||||
rpc(manager, :stop)
|
||||
end
|
||||
|
||||
defp rpc(module, cmd) do
|
||||
# TODO: Change the tag on OTP 18
|
||||
{:ok, reply} = :gen.call(module, self(), cmd, :infinity)
|
||||
reply
|
||||
end
|
||||
@@ -648,12 +635,7 @@ defmodule GenEvent do
|
||||
|
||||
def init_it(starter, parent, name, _, _, options) do
|
||||
Process.put(:"$initial_call", {__MODULE__, :init_it, 6})
|
||||
debug =
|
||||
if function_exported?(:gen, :debug_options, 2) do
|
||||
:gen.debug_options(name, options)
|
||||
else
|
||||
:gen.debug_options(options)
|
||||
end
|
||||
debug = :gen.debug_options(options)
|
||||
:proc_lib.init_ack(starter, {:ok, self()})
|
||||
loop(parent, name(name), [], debug, false)
|
||||
end
|
||||
@@ -745,6 +727,13 @@ defmodule GenEvent do
|
||||
{hib, reply, handlers} = server_swap_handler(handler1, args1, handler2, args2, handlers, mon, name)
|
||||
reply(tag, reply)
|
||||
loop(parent, name, handlers, debug, hib)
|
||||
{_from, tag, :stop} ->
|
||||
try do
|
||||
server_terminate(:normal, parent, handlers, name)
|
||||
catch
|
||||
:exit, :normal -> :ok
|
||||
end
|
||||
reply(tag, :ok)
|
||||
{_from, tag, :which_handlers} ->
|
||||
reply(tag, server_which_handlers(handlers))
|
||||
loop(parent, name, handlers, debug, false)
|
||||
@@ -904,13 +893,13 @@ defmodule GenEvent do
|
||||
{hib, server_collect_process_handlers(mode, event, streams, handlers, name)}
|
||||
end
|
||||
|
||||
defp server_split_process_handlers(mode, event, [handler | t], handlers, streams) do
|
||||
defp server_split_process_handlers(mode, event, [handler|t], handlers, streams) do
|
||||
case handler(handler, :id) do
|
||||
{pid, _ref} when is_pid(pid) ->
|
||||
server_process_notify(mode, event, handler)
|
||||
server_split_process_handlers(mode, event, t, handlers, [handler | streams])
|
||||
server_split_process_handlers(mode, event, t, handlers, [handler|streams])
|
||||
_ ->
|
||||
server_split_process_handlers(mode, event, t, [handler | handlers], streams)
|
||||
server_split_process_handlers(mode, event, t, [handler|handlers], streams)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -926,10 +915,10 @@ defmodule GenEvent do
|
||||
defp mode_to_tag(:sync), do: :sync_notify
|
||||
defp mode_to_tag(:async), do: :notify
|
||||
|
||||
defp server_notify(event, fun, [handler | t], name, handlers, acc, hib) do
|
||||
defp server_notify(event, fun, [handler|t], name, handlers, acc, hib) do
|
||||
case server_update(handler, fun, event, name, handlers) do
|
||||
{new_hib, handler} ->
|
||||
server_notify(event, fun, t, name, handlers, [handler | acc], hib or new_hib)
|
||||
server_notify(event, fun, t, name, handlers, [handler|acc], hib or new_hib)
|
||||
:error ->
|
||||
server_notify(event, fun, t, name, handlers, acc, hib)
|
||||
end
|
||||
@@ -963,16 +952,16 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
defp server_collect_process_handlers(:async, event, [handler | t], handlers, name) do
|
||||
server_collect_process_handlers(:async, event, t, [handler | handlers], name)
|
||||
defp server_collect_process_handlers(:async, event, [handler|t], handlers, name) do
|
||||
server_collect_process_handlers(:async, event, t, [handler|handlers], name)
|
||||
end
|
||||
|
||||
defp server_collect_process_handlers(mode, event, [handler | t], handlers, name) when mode in [:sync, :ack] do
|
||||
defp server_collect_process_handlers(mode, event, [handler|t], handlers, name) when mode in [:sync, :ack] do
|
||||
handler(ref: ref, id: id) = handler
|
||||
|
||||
receive do
|
||||
{^ref, :ok} ->
|
||||
server_collect_process_handlers(mode, event, t, [handler | handlers], name)
|
||||
server_collect_process_handlers(mode, event, t, [handler|handlers], name)
|
||||
{_from, tag, {:delete_handler, ^id, args}} ->
|
||||
do_terminate(handler, args, :remove, name, :normal)
|
||||
reply(tag, :ok)
|
||||
@@ -1062,9 +1051,9 @@ defmodule GenEvent do
|
||||
{:ok, res} ->
|
||||
case res do
|
||||
{:ok, state} ->
|
||||
{false, succ, [handler(handler, state: state) | handlers]}
|
||||
{false, succ, [handler(handler, state: state)|handlers]}
|
||||
{:ok, state, :hibernate} ->
|
||||
{true, succ, [handler(handler, state: state) | handlers]}
|
||||
{true, succ, [handler(handler, state: state)|handlers]}
|
||||
{:error, _} = error ->
|
||||
{false, error, handlers}
|
||||
other ->
|
||||
@@ -1127,7 +1116,7 @@ defmodule GenEvent do
|
||||
defp report_error(handler, reason, state, last_in, name) do
|
||||
reason =
|
||||
case reason do
|
||||
{:undef, [{m, f, a, _} | _]=mfas} ->
|
||||
{:undef, [{m, f, a, _}|_]=mfas} ->
|
||||
cond do
|
||||
:code.is_loaded(m) === false ->
|
||||
{:"module could not be loaded", mfas}
|
||||
|
||||
@@ -2,7 +2,7 @@ defmodule GenEvent.Stream do
|
||||
@moduledoc """
|
||||
Defines a `GenEvent` stream.
|
||||
|
||||
This is a struct returned by `GenEvent.stream/2`. The struct is public and
|
||||
This is a struct returned by `stream/2`. The struct is public and
|
||||
contains the following fields:
|
||||
|
||||
* `:manager` - the manager reference given to `GenEvent.stream/2`
|
||||
@@ -22,7 +22,7 @@ defmodule GenEvent.Stream do
|
||||
|
||||
@doc false
|
||||
def handle_event(event, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit({:bad_event, event})
|
||||
1 -> :remove_handler
|
||||
@@ -31,7 +31,7 @@ defmodule GenEvent.Stream do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
# We do this to trick dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_call, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
|
||||
+89
-179
@@ -2,7 +2,7 @@ defmodule GenServer do
|
||||
@moduledoc """
|
||||
A behaviour module for implementing the server of a client-server relation.
|
||||
|
||||
A GenServer is a process like any other Elixir process and it can be used
|
||||
A GenServer is a process as any other Elixir process and it can be used
|
||||
to keep state, execute code asynchronously and so on. The advantage of using
|
||||
a generic server process (GenServer) implemented using this module is that it
|
||||
will have a standard set of interface functions and include functionality for
|
||||
@@ -23,12 +23,12 @@ defmodule GenServer do
|
||||
|
||||
# Callbacks
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -76,11 +76,8 @@ defmodule GenServer do
|
||||
term using the functions in the `:global` module.
|
||||
|
||||
* `{:via, module, term}` - the GenServer is registered with the given
|
||||
mechanism and name. The `:via` option expects a module that exports
|
||||
`register_name/2`, `unregister_name/1`, `whereis_name/1` and `send/2`.
|
||||
One such example is the `:global` module which uses these functions
|
||||
for keeping the list of names of processes and their associated pid's
|
||||
that are available globally for a network of Erlang nodes.
|
||||
mechanism and name. The `:via` option expects a module name to control
|
||||
the registration mechanism alongside a name which can be any term.
|
||||
|
||||
For example, we could start and register our Stack server locally as follows:
|
||||
|
||||
@@ -129,7 +126,7 @@ defmodule GenServer do
|
||||
|
||||
# Server (callbacks)
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
@@ -139,7 +136,7 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
def handle_cast({:push, item}, state) do
|
||||
{:noreply, [item | state]}
|
||||
{:noreply, [item|state]}
|
||||
end
|
||||
|
||||
def handle_cast(request, state) do
|
||||
@@ -197,10 +194,10 @@ defmodule GenServer do
|
||||
`Supervisor.restart_child/2` as the child specification is saved in the parent
|
||||
supervisor. The main use cases for this are:
|
||||
|
||||
* The `GenServer` is disabled by configuration but might be enabled later.
|
||||
* An error occurred and it will be handled by a different mechanism than the
|
||||
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
|
||||
after a delay to attempt a restart.
|
||||
- The `GenServer` is disabled by configuration but might be enabled later.
|
||||
- An error occured and it will be handled by a different mechanism than the
|
||||
`Supervisor`. Likely this approach involves calling `Supervisor.restart_child/2`
|
||||
after a delay to attempt a restart.
|
||||
|
||||
Returning `{:stop, reason}` will cause `start_link/3` to return
|
||||
`{:error, reason}` and the process to exit with reason `reason` without
|
||||
@@ -225,14 +222,14 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:reply, reply, new_state, timeout}` is similar to
|
||||
`{:reply, reply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
called after `timeout` milliseconds if no messages are receved.
|
||||
|
||||
Returning `{:reply, reply, new_state, :hibernate}` is similar to
|
||||
`{:reply, reply, new_state}` except the process is hibernated and will
|
||||
continue the loop once a message is in its message queue. If a message is
|
||||
already in the message queue this will be immediately. Hibernating a
|
||||
`GenServer` causes garbage collection and leaves a continuous heap that
|
||||
minimises the memory used by the process.
|
||||
continue the loop once a message is its message queue. If a message is already
|
||||
in the message queue this will be immediately. Hibernating a `GenServer`
|
||||
causes garbage collection and leaves a continuous heap that minimises the
|
||||
memory used by the process.
|
||||
|
||||
Hibernating should not be used aggressively as too much time could be spent
|
||||
garbage collecting. Normally it should only be used when a message is not
|
||||
@@ -245,11 +242,11 @@ defmodule GenServer do
|
||||
|
||||
There are three main use cases for not replying using the return value:
|
||||
|
||||
* To reply before returning from the callback because the response is known
|
||||
before calling a slow function.
|
||||
* To reply after returning from the callback because the response is not yet
|
||||
available.
|
||||
* To reply from another process, such as a task.
|
||||
- To reply before returning from the callback because the response is known
|
||||
before calling a slow function.
|
||||
- To reply after returning from the callback because the response is not yet
|
||||
available.
|
||||
- To reply from another process, such as a task.
|
||||
|
||||
When replying from another process the `GenServer` should exit if the other
|
||||
process exits without replying as the caller will be blocking awaiting a
|
||||
@@ -265,9 +262,6 @@ defmodule GenServer do
|
||||
|
||||
Returning `{:stop, reason, new_state}` is similar to
|
||||
`{:stop, reason, reply, new_state}` except a reply is not sent.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:stop, {:bad_call, request}, state}`.
|
||||
"""
|
||||
@callback handle_call(request :: term, from, state :: term) ::
|
||||
{:reply, reply, new_state} |
|
||||
@@ -286,8 +280,8 @@ defmodule GenServer do
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{:noreply, new_state}` except `handle_info(:timeout, new_state)` will be
|
||||
called after `timeout` milliseconds if no messages are received.
|
||||
`{:noreply, reply, new_state}` except `handle_info(:timeout, new_state)` will
|
||||
be called after `timeout` milliseconds if no messages are received.
|
||||
|
||||
Returning `{:noreply, new_state, :hibernate}` is similar to
|
||||
`{:noreply, new_state}` except the process is hibernated before continuing the
|
||||
@@ -296,9 +290,6 @@ defmodule GenServer do
|
||||
Returning `{:stop, reason, new_state}` stops the loop and `terminate/2` is
|
||||
called with the reason `reason` and state `new_state`. The process exits with
|
||||
reason `reason`.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:stop, {:bad_cast, request}, state}`.
|
||||
"""
|
||||
@callback handle_cast(request :: term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
@@ -312,9 +303,6 @@ defmodule GenServer do
|
||||
a timeout occurs the message is `:timeout`.
|
||||
|
||||
Return values are the same as `handle_cast/2`.
|
||||
|
||||
If this callback is not implemented, the default implementation by
|
||||
`use GenServer` will return `{:noreply, state}`.
|
||||
"""
|
||||
@callback handle_info(msg :: :timeout | term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
@@ -382,28 +370,6 @@ defmodule GenServer do
|
||||
{:ok, new_state :: term} |
|
||||
{:error, reason :: term} when old_vsn: term | {:down, term}
|
||||
|
||||
@doc """
|
||||
Invoked in some cases to retrieve a formatted version of the `GenServer` status.
|
||||
|
||||
This callback can be useful to control the *appearance* of the status of the
|
||||
`GenServer`. For example, it can be used to return a compact representation of
|
||||
the `GenServers`'s state to avoid having large state terms printed.
|
||||
|
||||
* one of `:sys.get_status/1` or `:sys.get_status/2` is invoked to get the
|
||||
status of the `GenServer`; in such cases, `reason` is `:normal`
|
||||
|
||||
* the `GenServer` terminates abnormally and logs an error; in such cases,
|
||||
`reason` is `:terminate`
|
||||
|
||||
`pdict_and_state` is a two-elements list `[pdict, state]` where `pdict` is a
|
||||
list of `{key, value}` tuples representing the current process dictionary of
|
||||
the `GenServer` and `state` is the current state of the `GenServer`.
|
||||
"""
|
||||
@callback format_status(reason, pdict_and_state :: list) ::
|
||||
term when reason: :normal | :terminate
|
||||
|
||||
@optional_callbacks format_status: 2
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
|
||||
|
||||
@@ -436,7 +402,7 @@ defmodule GenServer do
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour GenServer
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc false
|
||||
def init(args) do
|
||||
@@ -488,41 +454,38 @@ defmodule GenServer do
|
||||
|
||||
This is often used to start the `GenServer` as part of a supervision tree.
|
||||
|
||||
Once the server is started, the `init/1` function of the given `module` is
|
||||
called with `args` as its arguments to initialize the server. To ensure a
|
||||
synchronized start-up procedure, this function does not return until `init/1`
|
||||
has returned.
|
||||
Once the server is started, it calls the `init/1` function in the given `module`
|
||||
passing the given `args` to initialize it. To ensure a synchronized start-up
|
||||
procedure, this function does not return until `init/1` has returned.
|
||||
|
||||
Note that a `GenServer` started with `start_link/3` is linked to the
|
||||
parent process and will exit in case of crashes from the parent. The GenServer
|
||||
will also exit due to the `:normal` reasons in case it is configured to trap
|
||||
exits in the `init/1` callback.
|
||||
parent process and will exit in case of crashes. The GenServer will also
|
||||
exit due to the `:normal` reasons in case it is configured to trap exits
|
||||
in the `init/1` callback.
|
||||
|
||||
## Options
|
||||
|
||||
* `:name` - used for name registration as described in the "Name
|
||||
registration" section of the module documentation
|
||||
The `:name` option is used for name registration as described in the module
|
||||
documentation. If the option `:timeout` option is present, the server is
|
||||
allowed to spend the given milliseconds initializing or it will be
|
||||
terminated and the start function will return `{:error, :timeout}`.
|
||||
|
||||
* `:timeout` - if present, the server is allowed to spend the given amount of
|
||||
milliseconds initializing or it will be terminated and the start function
|
||||
will return `{:error, :timeout}`
|
||||
If the `:debug` option is present, the corresponding function in the
|
||||
[`:sys` module](http://www.erlang.org/doc/man/sys.html) will be invoked.
|
||||
|
||||
* `:debug` - if present, the corresponding function in the [`:sys`
|
||||
module](http://www.erlang.org/doc/man/sys.html) is invoked
|
||||
|
||||
* `:spawn_opt` - if present, its value is passed as options to the
|
||||
underlying process as in `Process.spawn/4`
|
||||
If the `:spawn_opt` option is present, its value will be passed as options
|
||||
to the underlying process as in `Process.spawn/4`.
|
||||
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, this function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If a process with the
|
||||
specified server name already exists, this function returns
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where pid is the pid of the server. If a process with the
|
||||
specified server name already exists, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the `init/1` callback fails with `reason`, this function returns
|
||||
If the `init/1` callback fails with `reason`, the function returns
|
||||
`{:error, reason}`. Otherwise, if it returns `{:stop, reason}`
|
||||
or `:ignore`, the process is terminated and this function returns
|
||||
or `:ignore`, the process is terminated and the function returns
|
||||
`{:error, reason}` or `:ignore`, respectively.
|
||||
"""
|
||||
@spec start_link(module, any, options) :: on_start
|
||||
@@ -551,22 +514,6 @@ defmodule GenServer do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the server with the given `reason`.
|
||||
|
||||
The `terminate/2` callback of the given `server` will be invoked before
|
||||
exiting. This function returns `:ok` if the server terminates with the
|
||||
given reason; if it terminates with another reason, the call exits.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report is logged.
|
||||
"""
|
||||
@spec stop(server, reason :: term, timeout) :: :ok
|
||||
def stop(server, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(server, reason, timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes a synchronous call to the `server` and waits for its reply.
|
||||
|
||||
@@ -574,47 +521,45 @@ defmodule GenServer do
|
||||
arrives or a timeout occurs. `handle_call/3` will be called on the server
|
||||
to handle the request.
|
||||
|
||||
`server` can be any of the values described in the "Name registration"
|
||||
section of the documentation for this module.
|
||||
The server can be any of the values described in the `Name Registration`
|
||||
section of the module documentation.
|
||||
|
||||
## Timeouts
|
||||
|
||||
`timeout` is an integer greater than zero which specifies how many
|
||||
The `timeout` is an integer greater than zero which specifies how many
|
||||
milliseconds to wait for a reply, or the atom `:infinity` to wait
|
||||
indefinitely. The default value is `5000`. If no reply is received within
|
||||
the specified time, the function call fails and the caller exits. If the
|
||||
caller catches the failure and continues running, and the server is just late
|
||||
with the reply, it may arrive at any time later into the caller's message
|
||||
queue. The caller must in this case be prepared for this and discard any such
|
||||
garbage messages that are two-element tuples with a reference as the first
|
||||
element.
|
||||
indefinitely. The default value is 5000. If no reply is received within
|
||||
the specified time, the function call fails. If the caller catches the
|
||||
failure and continues running, and the server is just late with the reply,
|
||||
it may arrive at any time later into the caller's message queue. The caller
|
||||
must in this case be prepared for this and discard any such garbage messages
|
||||
that are two element tuples with a reference as the first element.
|
||||
"""
|
||||
@spec call(server, term, timeout) :: term
|
||||
def call(server, request, timeout \\ 5000) do
|
||||
case whereis(server) do
|
||||
nil ->
|
||||
exit({:noproc, {__MODULE__, :call, [server, request, timeout]}})
|
||||
pid when pid == self() ->
|
||||
exit({:calling_self, {__MODULE__, :call, [server, request, timeout]}})
|
||||
pid ->
|
||||
try do
|
||||
:gen.call(pid, :"$gen_call", request, timeout)
|
||||
catch
|
||||
:exit, reason ->
|
||||
exit({reason, {__MODULE__, :call, [server, request, timeout]}})
|
||||
else
|
||||
{:ok, res} -> res
|
||||
end
|
||||
try do
|
||||
:gen.call(server, :"$gen_call", request, timeout)
|
||||
catch
|
||||
:exit, reason ->
|
||||
exit({reason, {__MODULE__, :call, [server, request, timeout]}})
|
||||
else
|
||||
{:ok, res} -> res
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an asynchronous request to the `server`.
|
||||
|
||||
This function always returns `:ok` regardless of whether
|
||||
the destination `server` (or node) exists. Therefore it
|
||||
This function returns `:ok` without waiting for the
|
||||
destination `server` to handle the message. Therefore it
|
||||
is unknown whether the destination `server` successfully
|
||||
handled the message.
|
||||
handled the message. If the `server` is an atom without
|
||||
an associated process an `ArgumentError` is raised. In
|
||||
all other cases the function returns `:ok` regardless of
|
||||
whether the destination `server` (or node) exists. Note
|
||||
that `{name, node()}` can be used when an exception is
|
||||
not desired if no process is locally associated with the
|
||||
atom `name`.
|
||||
|
||||
`handle_cast/2` will be called on the server to handle
|
||||
the request. In case the `server` is on a node which is
|
||||
@@ -654,7 +599,7 @@ defmodule GenServer do
|
||||
@doc """
|
||||
Casts all servers locally registered as `name` at the specified nodes.
|
||||
|
||||
This function returns immediately and ignores nodes that do not exist, or where the
|
||||
The function returns immediately and ignores nodes that do not exist, or where the
|
||||
server name does not exist.
|
||||
|
||||
See `multi_call/4` for more information.
|
||||
@@ -671,42 +616,25 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
defp do_send(dest, msg) do
|
||||
try do
|
||||
send(dest, msg)
|
||||
:ok
|
||||
catch
|
||||
_, _ -> :ok
|
||||
end
|
||||
send(dest, msg)
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calls all servers locally registered as `name` at the specified `nodes`.
|
||||
|
||||
First, the `request` is sent to every node in `nodes`; then, the caller waits
|
||||
for the replies. This function returns a two-element tuple `{replies,
|
||||
bad_nodes}` where:
|
||||
The `request` is first sent to every node and then we wait for the
|
||||
replies. This function returns a tuple containing the node and its reply
|
||||
as first element and all bad nodes as second element. The bad nodes is a
|
||||
list of nodes that either did not exist, or where a server with the given
|
||||
`name` did not exist or did not reply.
|
||||
|
||||
* `replies` - is a list of `{node, reply}` tuples where `node` is the node
|
||||
that replied and `reply` is its reply
|
||||
* `bad_nodes` - is a list of nodes that either did not exist or where a
|
||||
server with the given `name` did not exist or did not reply
|
||||
|
||||
`nodes` is a list of node names to which the request is sent. The default
|
||||
value is the list of all known nodes (including this node).
|
||||
Nodes is a list of node names to which the request is sent. The default
|
||||
value is the list of all known nodes.
|
||||
|
||||
To avoid that late answers (after the timeout) pollute the caller's message
|
||||
queue, a middleman process is used to do the actual calls. Late answers will
|
||||
then be discarded when they arrive to a terminated process.
|
||||
|
||||
## Examples
|
||||
|
||||
Assuming the `Stack` GenServer mentioned in the docs for the `GenServer`
|
||||
module is registered as `Stack` in the `:"foo@my-machine"` and
|
||||
`:"bar@my-machine"` nodes:
|
||||
|
||||
GenServer.multi_call(Stack, :pop)
|
||||
#=> {[{:"foo@my-machine", :hello}, {:"bar@my-machine", :world}], []}
|
||||
|
||||
"""
|
||||
@spec multi_call([node], name :: atom, term, timeout) ::
|
||||
{replies :: [{node, term}], bad_nodes :: [node]}
|
||||
@@ -717,36 +645,20 @@ defmodule GenServer do
|
||||
@doc """
|
||||
Replies to a client.
|
||||
|
||||
This function can be used to explicitely send a reply to a client that called
|
||||
`call/3` or `multi_call/4` when the reply cannot be specified in the return
|
||||
value of `handle_call/3`.
|
||||
This function can be used by a server to explicitly send a reply to a
|
||||
client that called `call/3` or `multi_call/4`. When the reply cannot be
|
||||
defined in the return value of `handle_call/3`.
|
||||
|
||||
`client` must be the `from` argument (the second argument) accepted by
|
||||
`handle_call/3` callbacks. `reply` is an arbitrary term which will be given
|
||||
back to the client as the return value of the call.
|
||||
|
||||
Note that `reply/2` can be called from any process, not just the GenServer
|
||||
that originally received the call (as long as that GenServer communicated the
|
||||
`from` argument somehow).
|
||||
The `client` must be the `from` argument (the second argument) received
|
||||
in `handle_call/3` callbacks. Reply is an arbitrary term which will be
|
||||
given back to the client as the return value of the call.
|
||||
|
||||
This function always returns `:ok`.
|
||||
|
||||
## Examples
|
||||
|
||||
def handle_call(:reply_in_one_second, from, state) do
|
||||
Process.send_after(self(), {:reply, from}, 1_000)
|
||||
{:noreply, state}
|
||||
end
|
||||
|
||||
def handle_info({:reply, from}, state) do
|
||||
GenServer.reply(from, :one_second_has_passed)
|
||||
end
|
||||
|
||||
"""
|
||||
@spec reply(from, term) :: :ok
|
||||
def reply(client, reply)
|
||||
|
||||
def reply({to, tag}, reply) when is_pid(to) do
|
||||
def reply({to, tag}, reply) do
|
||||
try do
|
||||
send(to, {tag, reply})
|
||||
:ok
|
||||
@@ -756,12 +668,10 @@ defmodule GenServer do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `pid` or `{name, node}` of a GenServer process, or `nil` if
|
||||
no process is associated with the given name.
|
||||
Returns the `pid` or `{name, node}` of a GenServer process.
|
||||
Returns `nil` if no process is associated with the given name.
|
||||
|
||||
## Examples
|
||||
|
||||
For example, to lookup a server process, monitor it and send a cast to it:
|
||||
For example, to lookup a server process, monitor it and send a cast:
|
||||
|
||||
process = GenServer.whereis(server)
|
||||
monitor = Process.monitor(process)
|
||||
@@ -795,6 +705,6 @@ defmodule GenServer do
|
||||
@compile {:inline, [nodes: 0]}
|
||||
|
||||
defp nodes do
|
||||
[node() | :erlang.nodes()]
|
||||
[node()|:erlang.nodes()]
|
||||
end
|
||||
end
|
||||
|
||||
+29
-22
@@ -1,12 +1,19 @@
|
||||
defmodule HashDict do
|
||||
@moduledoc """
|
||||
WARNING: this module is deprecated.
|
||||
A key-value store.
|
||||
|
||||
Use the `Map` module instead.
|
||||
The `HashDict` is represented internally as a struct, therefore
|
||||
`%HashDict{}` can be used whenever there is a need to match
|
||||
on any `HashDict`. Note though the struct fields are private and
|
||||
must not be accessed directly. Instead, use the functions on this
|
||||
or in the `Dict` module.
|
||||
|
||||
Implementation-wise, `HashDict` is implemented using tries, which
|
||||
grows in space as the number of keys grows, working well with both
|
||||
small and large set of keys. For more information about the
|
||||
functions and their APIs, please consult the `Dict` module.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
|
||||
use Dict
|
||||
|
||||
@node_bitmap 0b111
|
||||
@@ -92,7 +99,7 @@ defmodule HashDict do
|
||||
defp do_fetch(node, key, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[^key | v] -> {:ok, v}
|
||||
[^key|v] -> {:ok, v}
|
||||
{^key, v, _} -> {:ok, v}
|
||||
{_, _, n} -> do_fetch(n, key, key_shift(hash))
|
||||
_ -> :error
|
||||
@@ -103,11 +110,11 @@ defmodule HashDict do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | value]), 1}
|
||||
[^key | _] ->
|
||||
{put_elem(node, index, [key | value]), 0}
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | value])
|
||||
{put_elem(node, index, [key|value]), 1}
|
||||
[^key|_] ->
|
||||
{put_elem(node, index, [key|value]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|value])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, _, n} ->
|
||||
{put_elem(node, index, {key, value, n}), 0}
|
||||
@@ -121,11 +128,11 @@ defmodule HashDict do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [key | initial.()]), 1}
|
||||
[^key | value] ->
|
||||
{put_elem(node, index, [key | fun.(value)]), 0}
|
||||
[k | v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key | initial.()])
|
||||
{put_elem(node, index, [key|initial.()]), 1}
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, [key|fun.(value)]), 0}
|
||||
[k|v] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [key|initial.()])
|
||||
{put_elem(node, index, {k, v, n}), 1}
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, {key, fun.(value), n}), 0}
|
||||
@@ -140,16 +147,16 @@ defmodule HashDict do
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
:error
|
||||
[^key | value] ->
|
||||
[^key|value] ->
|
||||
{put_elem(node, index, []), value}
|
||||
[_ | _] ->
|
||||
[_|_] ->
|
||||
:error
|
||||
{^key, value, n} ->
|
||||
{put_elem(node, index, do_compact_node(n)), value}
|
||||
{k, v, n} ->
|
||||
case do_delete(n, key, key_shift(hash)) do
|
||||
{@node_template, value} ->
|
||||
{put_elem(node, index, [k | v]), value}
|
||||
{put_elem(node, index, [k|v]), value}
|
||||
{n, value} ->
|
||||
{put_elem(node, index, {k, v, n}), value}
|
||||
:error ->
|
||||
@@ -161,9 +168,9 @@ defmodule HashDict do
|
||||
Enum.each 0..(@node_size - 1), fn index ->
|
||||
defp do_compact_node(node) when elem(node, unquote(index)) != [] do
|
||||
case elem(node, unquote(index)) do
|
||||
[k | v] ->
|
||||
[k|v] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [k | v]
|
||||
@node_template -> [k|v]
|
||||
n -> {k, v, n}
|
||||
end
|
||||
{k, v, n} ->
|
||||
@@ -186,7 +193,7 @@ defmodule HashDict do
|
||||
next.(acc)
|
||||
end
|
||||
|
||||
defp do_reduce_each([k | v], {:cont, acc}, fun, next) do
|
||||
defp do_reduce_each([k|v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k, v}, acc))
|
||||
end
|
||||
|
||||
@@ -229,7 +236,7 @@ end
|
||||
defimpl Collectable, for: HashDict do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
dict, {:cont, {k, v}} -> HashDict.put(dict, k, v)
|
||||
dict, {:cont, {k, v}} -> Dict.put(dict, k, v)
|
||||
dict, :done -> dict
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
|
||||
+34
-22
@@ -1,11 +1,20 @@
|
||||
defmodule HashSet do
|
||||
@moduledoc """
|
||||
WARNING: this module is deprecated.
|
||||
A set store.
|
||||
|
||||
Use the `MapSet` module instead.
|
||||
The `HashSet` is represented internally as a struct, therefore
|
||||
`%HashSet{}` can be used whenever there is a need to match
|
||||
on any `HashSet`. Note though the struct fields are private and
|
||||
must not be accessed directly. Instead, use the functions on this
|
||||
or in the `Set` module.
|
||||
|
||||
The `HashSet` is implemented using tries, which grows in
|
||||
space as the number of keys grows, working well with both
|
||||
small and large set of keys. For more information about the
|
||||
functions and their APIs, please consult the `Set` module.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
@behaviour Set
|
||||
|
||||
@node_bitmap 0b111
|
||||
@node_shift 3
|
||||
@@ -20,6 +29,9 @@ defmodule HashSet do
|
||||
@compile :inline_list_funcs
|
||||
@compile {:inline, key_hash: 1, key_mask: 1, key_shift: 1}
|
||||
|
||||
@doc """
|
||||
Creates a new empty set.
|
||||
"""
|
||||
@spec new :: Set.t
|
||||
def new do
|
||||
%HashSet{}
|
||||
@@ -44,7 +56,7 @@ defmodule HashSet do
|
||||
end
|
||||
|
||||
def to_list(set) do
|
||||
set_fold(set, [], &[&1 | &2]) |> :lists.reverse
|
||||
set_fold(set, [], &[&1|&2]) |> :lists.reverse
|
||||
end
|
||||
|
||||
def equal?(%HashSet{size: size1} = set1, %HashSet{size: size2} = set2) do
|
||||
@@ -112,10 +124,10 @@ defmodule HashSet do
|
||||
defp do_member?(node, term, hash) do
|
||||
index = key_mask(hash)
|
||||
case elem(node, index) do
|
||||
[] -> false
|
||||
[^term | _] -> true
|
||||
[_] -> false
|
||||
[_ | n] -> do_member?(n, term, key_shift(hash))
|
||||
[] -> false
|
||||
[^term|_] -> true
|
||||
[_] -> false
|
||||
[_|n] -> do_member?(n, term, key_shift(hash))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -124,14 +136,14 @@ defmodule HashSet do
|
||||
case elem(node, index) do
|
||||
[] ->
|
||||
{put_elem(node, index, [term]), 1}
|
||||
[^term | _] ->
|
||||
[^term|_] ->
|
||||
{node, 0}
|
||||
[t] ->
|
||||
n = put_elem(@node_template, key_mask(key_shift(hash)), [term])
|
||||
{put_elem(node, index, [t | n]), 1}
|
||||
[t | n] ->
|
||||
{put_elem(node, index, [t|n]), 1}
|
||||
[t|n] ->
|
||||
{n, counter} = do_put(n, term, key_shift(hash))
|
||||
{put_elem(node, index, [t | n]), counter}
|
||||
{put_elem(node, index, [t|n]), counter}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -144,14 +156,14 @@ defmodule HashSet do
|
||||
{:ok, put_elem(node, index, [])}
|
||||
[_] ->
|
||||
:error
|
||||
[^term | n] ->
|
||||
[^term|n] ->
|
||||
{:ok, put_elem(node, index, do_compact_node(n))}
|
||||
[t | n] ->
|
||||
[t|n] ->
|
||||
case do_delete(n, term, key_shift(hash)) do
|
||||
{:ok, @node_template} ->
|
||||
{:ok, put_elem(node, index, [t])}
|
||||
{:ok, n} ->
|
||||
{:ok, put_elem(node, index, [t | n])}
|
||||
{:ok, put_elem(node, index, [t|n])}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
@@ -164,19 +176,19 @@ defmodule HashSet do
|
||||
[t] ->
|
||||
case put_elem(node, unquote(index), []) do
|
||||
@node_template -> [t]
|
||||
n -> [t | n]
|
||||
n -> [t|n]
|
||||
end
|
||||
[t | n] ->
|
||||
[t | put_elem(node, unquote(index), do_compact_node(n))]
|
||||
[t|n] ->
|
||||
[t|put_elem(node, unquote(index), do_compact_node(n))]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
## Set fold
|
||||
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t | n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
defp do_fold_each([], acc, _fun), do: acc
|
||||
defp do_fold_each([t], acc, fun), do: fun.(t, acc)
|
||||
defp do_fold_each([t|n], acc, fun), do: do_fold(n, fun.(t, acc), fun, @node_size)
|
||||
|
||||
defp do_fold(node, acc, fun, count) when count > 0 do
|
||||
acc = do_fold_each(:erlang.element(count, node), acc, fun)
|
||||
@@ -205,7 +217,7 @@ defmodule HashSet do
|
||||
next.(fun.(t, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each([t | n], {:cont, acc}, fun, next) do
|
||||
defp do_reduce_each([t|n], {:cont, acc}, fun, next) do
|
||||
do_reduce(n, fun.(t, acc), fun, @node_size, next)
|
||||
end
|
||||
|
||||
|
||||
+88
-109
@@ -16,20 +16,20 @@ defprotocol Inspect do
|
||||
## Examples
|
||||
|
||||
Many times, inspecting a structure can be implemented in function
|
||||
of existing entities. For example, here is `MapSet`'s `inspect`
|
||||
of existing entities. For example, here is `HashSet`'s `inspect`
|
||||
implementation:
|
||||
|
||||
defimpl Inspect, for: MapSet do
|
||||
defimpl Inspect, for: HashSet do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(dict, opts) do
|
||||
concat ["#MapSet<", to_doc(MapSet.to_list(dict), opts), ">"]
|
||||
concat ["#HashSet<", to_doc(HashSet.to_list(dict), opts), ">"]
|
||||
end
|
||||
end
|
||||
|
||||
The `concat` function comes from `Inspect.Algebra` and it
|
||||
concatenates algebra documents together. In the example above,
|
||||
it is concatenating the string `"MapSet<"` (all strings are
|
||||
it is concatenating the string `"HashSet<"` (all strings are
|
||||
valid algebra documents that keep their formatting when pretty
|
||||
printed), the document returned by `Inspect.Algebra.to_doc/2` and the
|
||||
other string `">"`.
|
||||
@@ -45,17 +45,17 @@ defprotocol Inspect do
|
||||
to a raw representation for printing the structure.
|
||||
|
||||
You can however access the underlying error by invoking the Inspect
|
||||
implementation directly. For example, to test Inspect.MapSet above,
|
||||
implementation directly. For example, to test Inspect.HashSet above,
|
||||
you can invoke it as:
|
||||
|
||||
Inspect.MapSet.inspect(MapSet.new, %Inspect.Opts{})
|
||||
Inspect.HashSet.inspect(HashSet.new, Inspect.Opts.new)
|
||||
|
||||
"""
|
||||
|
||||
# Handle structs in Any
|
||||
@fallback_to_any true
|
||||
|
||||
def inspect(term, opts)
|
||||
def inspect(thing, opts)
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Atom do
|
||||
@@ -88,7 +88,7 @@ defimpl Inspect, for: Atom do
|
||||
atom in Macro.binary_ops or atom in Macro.unary_ops ->
|
||||
":" <> binary
|
||||
true ->
|
||||
<<?:, ?", Inspect.BitString.escape(binary, ?")::binary, ?">>
|
||||
<< ?:, ?", Inspect.BitString.escape(binary, ?") :: binary, ?" >>
|
||||
end
|
||||
end
|
||||
|
||||
@@ -104,7 +104,7 @@ defimpl Inspect, for: Atom do
|
||||
|
||||
defp valid_ref_identifier?(_), do: false
|
||||
|
||||
defp valid_ref_piece?(<<?., h, t::binary>>) when h in ?A..?Z do
|
||||
defp valid_ref_piece?(<<?., h, t :: binary>>) when h in ?A..?Z do
|
||||
valid_ref_piece? valid_identifier?(t)
|
||||
end
|
||||
|
||||
@@ -113,7 +113,7 @@ defimpl Inspect, for: Atom do
|
||||
|
||||
# Detect if atom
|
||||
|
||||
defp valid_atom_identifier?(<<h, t::binary>>) when h in ?a..?z or h in ?A..?Z or h == ?_ do
|
||||
defp valid_atom_identifier?(<<h, t :: binary>>) when h in ?a..?z or h in ?A..?Z or h == ?_ do
|
||||
valid_atom_piece?(t)
|
||||
end
|
||||
|
||||
@@ -129,7 +129,7 @@ defimpl Inspect, for: Atom do
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_identifier?(<<h, t::binary>>)
|
||||
defp valid_identifier?(<<h, t :: binary>>)
|
||||
when h in ?a..?z
|
||||
when h in ?A..?Z
|
||||
when h in ?0..?9
|
||||
@@ -141,16 +141,16 @@ defimpl Inspect, for: Atom do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: BitString do
|
||||
def inspect(term, %Inspect.Opts{binaries: bins, base: base} = opts) when is_binary(term) do
|
||||
if base == :decimal and (bins == :as_strings or (bins == :infer and String.printable?(term))) do
|
||||
<<?", escape(term, ?")::binary, ?">>
|
||||
def inspect(thing, %Inspect.Opts{binaries: bins} = opts) when is_binary(thing) do
|
||||
if bins == :as_strings or (bins == :infer and String.printable?(thing)) do
|
||||
<<?", escape(thing, ?") :: binary, ?">>
|
||||
else
|
||||
inspect_bitstring(term, opts)
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
end
|
||||
|
||||
def inspect(term, opts) do
|
||||
inspect_bitstring(term, opts)
|
||||
def inspect(thing, opts) do
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
|
||||
## Escaping
|
||||
@@ -160,54 +160,54 @@ defimpl Inspect, for: BitString do
|
||||
escape(other, char, <<>>)
|
||||
end
|
||||
|
||||
defp escape(<<char, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, char>>)
|
||||
defp escape(<< char, t :: binary >>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, char >>)
|
||||
end
|
||||
defp escape(<<?#, ?{, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?#, ?{>>)
|
||||
defp escape(<<?#, ?{, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?#, ?{>>)
|
||||
end
|
||||
defp escape(<<?\a, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?a>>)
|
||||
defp escape(<<?\a, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?a >>)
|
||||
end
|
||||
defp escape(<<?\b, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?b>>)
|
||||
defp escape(<<?\b, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?b >>)
|
||||
end
|
||||
defp escape(<<?\d, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?d>>)
|
||||
defp escape(<<?\d, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?d >>)
|
||||
end
|
||||
defp escape(<<?\e, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?e>>)
|
||||
defp escape(<<?\e, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?e >>)
|
||||
end
|
||||
defp escape(<<?\f, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?f>>)
|
||||
defp escape(<<?\f, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?f >>)
|
||||
end
|
||||
defp escape(<<?\n, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?n>>)
|
||||
defp escape(<<?\n, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?n >>)
|
||||
end
|
||||
defp escape(<<?\r, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?r>>)
|
||||
defp escape(<<?\r, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?r >>)
|
||||
end
|
||||
defp escape(<<?\\, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?\\>>)
|
||||
defp escape(<<?\\, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?\\ >>)
|
||||
end
|
||||
defp escape(<<?\t, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?t>>)
|
||||
defp escape(<<?\t, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?t >>)
|
||||
end
|
||||
defp escape(<<?\v, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, ?\\, ?v>>)
|
||||
defp escape(<<?\v, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, ?\\, ?v >>)
|
||||
end
|
||||
defp escape(<<h::utf8, t::binary>>, char, binary) do
|
||||
head = <<h::utf8>>
|
||||
defp escape(<<h :: utf8, t :: binary>>, char, binary) do
|
||||
head = << h :: utf8 >>
|
||||
if String.printable?(head) do
|
||||
escape(t, char, append(head, binary))
|
||||
else
|
||||
<<byte::8, h::binary>> = head
|
||||
t = <<h::binary, t::binary>>
|
||||
escape(t, char, <<binary::binary, escape_char(byte)::binary>>)
|
||||
<< byte :: size(8), h :: binary >> = head
|
||||
t = << h :: binary, t :: binary >>
|
||||
escape(t, char, << binary :: binary, escape_char(byte) :: binary >>)
|
||||
end
|
||||
end
|
||||
defp escape(<<h, t::binary>>, char, binary) do
|
||||
escape(t, char, <<binary::binary, escape_char(h)::binary>>)
|
||||
defp escape(<<h, t :: binary>>, char, binary) do
|
||||
escape(t, char, << binary :: binary, escape_char(h) :: binary >>)
|
||||
end
|
||||
defp escape(<<>>, _char, binary), do: binary
|
||||
|
||||
@@ -218,17 +218,17 @@ defimpl Inspect, for: BitString do
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x100 do
|
||||
<<a::4, b::4>> = <<char::8>>
|
||||
<<a::4, b::4>> = <<char::size(8)>>
|
||||
<<?\\, ?x, to_hex(a), to_hex(b)>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x10000 do
|
||||
<<a::4, b::4, c::4, d::4>> = <<char::16>>
|
||||
<<a::4, b::4, c::4, d::4>> = <<char::size(16)>>
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}>>
|
||||
end
|
||||
|
||||
def escape_char(char) when char < 0x1000000 do
|
||||
<<a::4, b::4, c::4, d::4, e::4, f::4>> = <<char::24>>
|
||||
<<a::4, b::4, c::4, d::4, e::4, f::4>> = <<char::size(24)>>
|
||||
<<?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c),
|
||||
to_hex(d), to_hex(e), to_hex(f), ?}>>
|
||||
end
|
||||
@@ -236,40 +236,35 @@ defimpl Inspect, for: BitString do
|
||||
defp to_hex(c) when c in 0..9, do: ?0+c
|
||||
defp to_hex(c) when c in 10..15, do: ?A+c-10
|
||||
|
||||
defp append(<<h, t::binary>>, binary), do: append(t, <<binary::binary, h>>)
|
||||
defp append(<<h, t :: binary>>, binary), do: append(t, << binary :: binary, h >>)
|
||||
defp append(<<>>, binary), do: binary
|
||||
|
||||
## Bitstrings
|
||||
|
||||
defp inspect_bitstring("", _opts) do
|
||||
"<<>>"
|
||||
end
|
||||
|
||||
defp inspect_bitstring(bitstring, opts) do
|
||||
nest surround("<<", each_bit(bitstring, opts.limit, opts), ">>"), 1
|
||||
each_bit(bitstring, opts.limit, "<<") <> ">>"
|
||||
end
|
||||
|
||||
defp each_bit(_, 0, _) do
|
||||
"..."
|
||||
defp each_bit(_, 0, acc) do
|
||||
acc <> "..."
|
||||
end
|
||||
|
||||
defp each_bit(<<>>, _counter, _opts) do
|
||||
:doc_nil
|
||||
defp each_bit(<<h, t :: bitstring>>, counter, acc) when t != <<>> do
|
||||
each_bit(t, decrement(counter), acc <> Integer.to_string(h) <> ", ")
|
||||
end
|
||||
|
||||
defp each_bit(<<h::8>>, _counter, opts) do
|
||||
Inspect.Integer.inspect(h, opts)
|
||||
defp each_bit(<<h :: size(8)>>, _counter, acc) do
|
||||
acc <> Integer.to_string(h)
|
||||
end
|
||||
|
||||
defp each_bit(<<h, t::bitstring>>, counter, opts) do
|
||||
glue(concat(Inspect.Integer.inspect(h, opts), ","),
|
||||
each_bit(t, decrement(counter), opts))
|
||||
defp each_bit(<<>>, _counter, acc) do
|
||||
acc
|
||||
end
|
||||
|
||||
defp each_bit(bitstring, _counter, opts) do
|
||||
defp each_bit(bitstring, _counter, acc) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
Inspect.Integer.inspect(h, opts) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
<<h :: size(size)>> = bitstring
|
||||
acc <> Integer.to_string(h) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
@@ -279,31 +274,17 @@ end
|
||||
defimpl Inspect, for: List do
|
||||
def inspect([], _opts), do: "[]"
|
||||
|
||||
# TODO: Deprecate :char_lists and :as_char_lists keys in v1.5
|
||||
def inspect(term, %Inspect.Opts{charlists: lists, char_lists: lists_deprecated} = opts) do
|
||||
lists =
|
||||
if lists == :infer and lists_deprecated != :infer do
|
||||
case lists_deprecated do
|
||||
:as_char_lists ->
|
||||
:as_charlists
|
||||
_ ->
|
||||
lists_deprecated
|
||||
end
|
||||
else
|
||||
lists
|
||||
end
|
||||
|
||||
def inspect(thing, %Inspect.Opts{char_lists: lists} = opts) do
|
||||
cond do
|
||||
lists == :as_charlists or (lists == :infer and printable?(term)) ->
|
||||
<<?', Inspect.BitString.escape(IO.chardata_to_string(term), ?')::binary, ?'>>
|
||||
keyword?(term) ->
|
||||
surround_many("[", term, "]", opts, &keyword/2)
|
||||
lists == :as_char_lists or (lists == :infer and printable?(thing)) ->
|
||||
<< ?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?') :: binary, ?' >>
|
||||
keyword?(thing) ->
|
||||
surround_many("[", thing, "]", opts, &keyword/2)
|
||||
true ->
|
||||
surround_many("[", term, "]", opts, &to_doc/2)
|
||||
surround_many("[", thing, "]", opts, &to_doc/2)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword({key, value}, opts) do
|
||||
concat(
|
||||
key_to_binary(key) <> ": ",
|
||||
@@ -311,9 +292,8 @@ defimpl Inspect, for: List do
|
||||
)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_charlist(key) do
|
||||
case Atom.to_char_list(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
_ -> keyword?(rest)
|
||||
end
|
||||
@@ -322,19 +302,6 @@ defimpl Inspect, for: List do
|
||||
def keyword?([]), do: true
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc false
|
||||
def printable?([c | cs]) when c in 32..126, do: printable?(cs)
|
||||
def printable?([?\n | cs]), do: printable?(cs)
|
||||
def printable?([?\r | cs]), do: printable?(cs)
|
||||
def printable?([?\t | cs]), do: printable?(cs)
|
||||
def printable?([?\v | cs]), do: printable?(cs)
|
||||
def printable?([?\b | cs]), do: printable?(cs)
|
||||
def printable?([?\f | cs]), do: printable?(cs)
|
||||
def printable?([?\e | cs]), do: printable?(cs)
|
||||
def printable?([?\a | cs]), do: printable?(cs)
|
||||
def printable?([]), do: true
|
||||
def printable?(_), do: false
|
||||
|
||||
## Private
|
||||
|
||||
defp key_to_binary(key) do
|
||||
@@ -343,6 +310,18 @@ defimpl Inspect, for: List do
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp printable?([c|cs]) when is_integer(c) and c in 32..126, do: printable?(cs)
|
||||
defp printable?([?\n|cs]), do: printable?(cs)
|
||||
defp printable?([?\r|cs]), do: printable?(cs)
|
||||
defp printable?([?\t|cs]), do: printable?(cs)
|
||||
defp printable?([?\v|cs]), do: printable?(cs)
|
||||
defp printable?([?\b|cs]), do: printable?(cs)
|
||||
defp printable?([?\f|cs]), do: printable?(cs)
|
||||
defp printable?([?\e|cs]), do: printable?(cs)
|
||||
defp printable?([?\a|cs]), do: printable?(cs)
|
||||
defp printable?([]), do: true
|
||||
defp printable?(_), do: false
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Tuple do
|
||||
@@ -380,8 +359,8 @@ defimpl Inspect, for: Map do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Integer do
|
||||
def inspect(term, %Inspect.Opts{base: base}) do
|
||||
Integer.to_string(term, base_to_value(base))
|
||||
def inspect(thing, %Inspect.Opts{base: base}) do
|
||||
Integer.to_string(thing, base_to_value(base))
|
||||
|> prepend_prefix(base)
|
||||
end
|
||||
|
||||
@@ -406,8 +385,8 @@ defimpl Inspect, for: Integer do
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Float do
|
||||
def inspect(term, _opts) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
|
||||
def inspect(thing, _opts) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -472,7 +451,7 @@ defimpl Inspect, for: Function do
|
||||
if fun_info[:type] == :external and fun_info[:env] == [] do
|
||||
"&#{Inspect.Atom.inspect(mod)}.#{fun_info[:name]}/#{fun_info[:arity]}"
|
||||
else
|
||||
case Atom.to_charlist(mod) do
|
||||
case Atom.to_char_list(mod) do
|
||||
'elixir_compiler_' ++ _ ->
|
||||
if function_exported?(mod, :__RELATIVE__, 0) do
|
||||
"#Function<#{uniq(fun_info)} in file:#{mod.__RELATIVE__}>"
|
||||
|
||||
@@ -15,39 +15,35 @@ defmodule Inspect.Opts do
|
||||
When the default `:infer`, the binary will be printed as a string if it
|
||||
is printable, otherwise in bit syntax.
|
||||
|
||||
* `:charlists` - when `:as_charlists` all lists will be printed as char
|
||||
* `:char_lists` - when `:as_char_lists` all lists will be printed as char
|
||||
lists, non-printable elements will be escaped.
|
||||
|
||||
When `:as_lists` all lists will be printed as lists.
|
||||
|
||||
When the default `:infer`, the list will be printed as a charlist if it
|
||||
When the default `:infer`, the list will be printed as a char list if it
|
||||
is printable, otherwise as list.
|
||||
|
||||
* `:limit` - limits the number of items that are printed for tuples,
|
||||
bitstrings, and lists, does not apply to strings nor charlists, defaults
|
||||
bitstrings, and lists, does not apply to strings nor char lists, defaults
|
||||
to 50.
|
||||
|
||||
* `:pretty` - if set to `true` enables pretty printing, defaults to `false`.
|
||||
|
||||
* `:width` - defaults to 80 characters, used when pretty is `true` or when
|
||||
printing to IO devices. Set to 0 to force each item to be printed on its
|
||||
own line.
|
||||
* `:width` - defaults to the 80 characters, used when pretty is `true` or
|
||||
when printing to IO devices.
|
||||
|
||||
* `:base` - print integers as :binary, :octal, :decimal, or :hex, defaults
|
||||
to :decimal. When inspecting binaries any `:base` other than `:decimal`
|
||||
implies `binaries: :as_binaries`.
|
||||
to :decimal
|
||||
|
||||
* `:safe` - when `false`, failures while inspecting structs will be raised
|
||||
as errors instead of being wrapped in the `Inspect.Error` exception. This
|
||||
as errors instead of being wrapped in the Inspect.Error exception. This
|
||||
is useful when debugging failures and crashes for custom inspect
|
||||
implementations
|
||||
|
||||
"""
|
||||
|
||||
# TODO: Deprecate char_lists key by v1.5
|
||||
defstruct structs: true,
|
||||
binaries: :infer,
|
||||
charlists: :infer,
|
||||
char_lists: :infer,
|
||||
limit: 50,
|
||||
width: 80,
|
||||
@@ -55,11 +51,9 @@ defmodule Inspect.Opts do
|
||||
pretty: false,
|
||||
safe: true
|
||||
|
||||
# TODO: Deprecate char_lists key and :as_char_lists value by v1.5
|
||||
@type t :: %__MODULE__{
|
||||
structs: boolean,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
charlists: :infer | :as_lists | :as_charlists,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
limit: pos_integer | :infinity,
|
||||
width: pos_integer | :infinity,
|
||||
@@ -83,7 +77,7 @@ defmodule Inspect.Algebra do
|
||||
This module implements the functionality described in
|
||||
["Strictly Pretty" (2000) by Christian Lindig][0] with small
|
||||
additions, like support for String nodes, and a custom
|
||||
rendering function that maximises horizontal space use.
|
||||
rendering function that maximises horizontal space use.
|
||||
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
@@ -126,7 +120,7 @@ defmodule Inspect.Algebra do
|
||||
`:flat` (breaks as spaces) and `:break` (breaks as newlines).
|
||||
Implementing the same logic in a strict language such as Elixir leads
|
||||
to an exponential growth of possible documents, unless document groups
|
||||
are encoded explicitly as `:flat` or `:break`. Those groups are then reduced
|
||||
are encoded explictly as `:flat` or `:break`. Those groups are then reduced
|
||||
to a simple document, where the layout is already decided, per [Lindig][0].
|
||||
|
||||
This implementation slightly changes the semantic of Lindig's algorithm
|
||||
@@ -422,7 +416,7 @@ defmodule Inspect.Algebra do
|
||||
def fold_doc(list, fun)
|
||||
def fold_doc([], _), do: empty
|
||||
def fold_doc([doc], _), do: doc
|
||||
def fold_doc([d | ds], fun), do: fun.(d, fold_doc(ds, fun))
|
||||
def fold_doc([d|ds], fun), do: fun.(d, fold_doc(ds, fun))
|
||||
|
||||
# Elixir conveniences
|
||||
|
||||
@@ -494,14 +488,14 @@ defmodule Inspect.Algebra do
|
||||
fun.(h, %{opts | limit: limit})
|
||||
end
|
||||
|
||||
defp do_surround_many([h | t], limit, opts, fun, sep) when is_list(t) do
|
||||
defp do_surround_many([h|t], limit, opts, fun, sep) when is_list(t) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = do_surround_many(t, limit, opts, fun, sep)
|
||||
do_join(h, t, sep)
|
||||
end
|
||||
|
||||
defp do_surround_many([h | t], limit, opts, fun, _sep) do
|
||||
defp do_surround_many([h|t], limit, opts, fun, _sep) do
|
||||
limit = decrement(limit)
|
||||
h = fun.(h, %{opts | limit: limit})
|
||||
t = fun.(t, %{opts | limit: limit})
|
||||
@@ -524,7 +518,7 @@ defmodule Inspect.Algebra do
|
||||
document to fit in the given width.
|
||||
"""
|
||||
@spec format(t, non_neg_integer | :infinity) :: iodata
|
||||
def format(d, w) when w == :infinity or w >= 0 do
|
||||
def format(d, w) do
|
||||
format(w, 0, [{0, default_mode(w), doc_group(d)}])
|
||||
end
|
||||
|
||||
|
||||
+60
-149
@@ -6,134 +6,88 @@ defmodule Integer do
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Determines if `integer` is odd.
|
||||
Determines if an integer is odd.
|
||||
|
||||
Returns `true` if the given `integer` is an odd number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an odd number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_odd(5)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(6)
|
||||
false
|
||||
|
||||
iex> Integer.is_odd(-5)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(0)
|
||||
false
|
||||
|
||||
"""
|
||||
defmacro is_odd(integer) do
|
||||
quote do: (unquote(integer) &&& 1) == 1
|
||||
defmacro is_odd(n) do
|
||||
quote do: (unquote(n) &&& 1) == 1
|
||||
end
|
||||
|
||||
@doc """
|
||||
Determines if an `integer` is even.
|
||||
Determines if an integer is even.
|
||||
|
||||
Returns `true` if the given `integer` is an even number,
|
||||
otherwise it returns `false`.
|
||||
Returns `true` if `n` is an even number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_even(10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(5)
|
||||
false
|
||||
|
||||
iex> Integer.is_even(-10)
|
||||
true
|
||||
|
||||
iex> Integer.is_even(0)
|
||||
true
|
||||
|
||||
"""
|
||||
defmacro is_even(integer) do
|
||||
quote do: (unquote(integer) &&& 1) == 0
|
||||
defmacro is_even(n) do
|
||||
quote do: (unquote(n) &&& 1) == 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the ordered digits for the given non-negative `integer`.
|
||||
Returns the ordered digits for the given non-negative integer.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the returned
|
||||
digits. This one can be an integer >= 2.
|
||||
An optional base value may be provided representing the radix for the returned
|
||||
digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.digits(101)
|
||||
[1, 0, 1]
|
||||
|
||||
iex> Integer.digits(170, 2)
|
||||
[1, 0, 1, 0, 1, 0, 1, 0]
|
||||
iex> Integer.digits(58127, 2)
|
||||
[1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]
|
||||
|
||||
"""
|
||||
@spec digits(non_neg_integer, pos_integer) :: [non_neg_integer, ...]
|
||||
def digits(integer, base \\ 10)
|
||||
when is_integer(integer) and integer >= 0 and is_integer(base) and base >= 2 do
|
||||
do_digits(integer, base, [])
|
||||
@spec digits(non_neg_integer, pos_integer) :: [non_neg_integer]
|
||||
def digits(n, base \\ 10) when is_integer(n) and n >= 0
|
||||
and is_integer(base) and base >= 2 do
|
||||
do_digits(n, base, [])
|
||||
end
|
||||
|
||||
defp do_digits(0, _base, []), do: [0]
|
||||
defp do_digits(1, _base, []), do: [1]
|
||||
defp do_digits(base, base, []), do: [1, 0]
|
||||
defp do_digits(0, _base, []), do: [0]
|
||||
defp do_digits(0, _base, acc), do: acc
|
||||
defp do_digits(integer, base, acc) do
|
||||
do_digits div(integer, base), base, [rem(integer, base) | acc]
|
||||
defp do_digits(n, base, acc) do
|
||||
do_digits div(n, base), base, [rem(n, base) | acc]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the integer represented by the ordered `digits`.
|
||||
Returns the integer represented by the ordered digits.
|
||||
|
||||
An optional `base` value may be provided representing the radix for the `digits`.
|
||||
This one can be an integer >= 2.
|
||||
An optional base value may be provided representing the radix for the digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.undigits([1, 2, 3])
|
||||
123
|
||||
iex> Integer.undigits([1, 0, 1])
|
||||
101
|
||||
|
||||
iex> Integer.undigits([1, 4], 16)
|
||||
20
|
||||
|
||||
iex> Integer.undigits([])
|
||||
0
|
||||
|
||||
"""
|
||||
@spec undigits([integer], integer) :: integer
|
||||
def undigits(digits, base \\ 10) when is_list(digits) and is_integer(base) and base >= 2 do
|
||||
def undigits(digits, base \\ 10) when is_integer(base) do
|
||||
do_undigits(digits, base, 0)
|
||||
end
|
||||
|
||||
defp do_undigits([], _base, []), do: 0
|
||||
defp do_undigits([0], _base, []), do: 0
|
||||
defp do_undigits([1], _base, []), do: 1
|
||||
defp do_undigits([1, 0], base, []), do: base
|
||||
defp do_undigits([], _base, acc), do: acc
|
||||
defp do_undigits([digit | tail], base, acc) do
|
||||
do_undigits(tail, base, acc * base + digit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a text representation of an integer.
|
||||
Converts a binary from a text representation of an integer
|
||||
in an optional base `base` to the corresponding integer.
|
||||
|
||||
An optional `base` to the corresponding integer can be provided.
|
||||
If `base` is not given, 10 will be used.
|
||||
If the base `base` is not given, base 10 will be used.
|
||||
|
||||
If successful, returns a tuple in the form of `{integer, remainder_of_binary}`.
|
||||
If successful, returns a tuple of the form `{integer, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
Raises an error if `base` is less than 2 or more than 36.
|
||||
|
||||
If you want to convert a string-formatted integer directly to a integer,
|
||||
`String.to_integer/1` or `String.to_integer/2` can be used instead.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.parse("34")
|
||||
@@ -164,49 +118,44 @@ defmodule Integer do
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error | no_return
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse("", base) when base in 2..36,
|
||||
do: :error
|
||||
|
||||
def parse(binary, base) when is_binary(binary) and base in 2..36 do
|
||||
def parse(binary, base) when is_integer(base) and base in 2..36 do
|
||||
parse_in_base(binary, base)
|
||||
end
|
||||
|
||||
def parse(binary, base) when is_binary(binary) do
|
||||
def parse(_, base) do
|
||||
raise ArgumentError, "invalid base #{base}"
|
||||
end
|
||||
|
||||
defp parse_in_base("-" <> bin, base) do
|
||||
defp parse_in_base(<< ?-, bin :: binary >>, base) do
|
||||
case do_parse(bin, base) do
|
||||
{number, remainder} -> {-number, remainder}
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_in_base("+" <> bin, base) do
|
||||
defp parse_in_base(<< ?+, bin :: binary >>, base) do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp parse_in_base(binary, base) when is_binary(binary) do
|
||||
do_parse(binary, base)
|
||||
defp parse_in_base(bin, base) when is_binary(bin) do
|
||||
do_parse(bin, base)
|
||||
end
|
||||
|
||||
defp do_parse(<<char, rest::binary>>, base) do
|
||||
defp do_parse(<< char, bin :: binary >>, base) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, parse_digit(char, base))
|
||||
do_parse(bin, base, parse_digit(char, base))
|
||||
else
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse(_, _) do
|
||||
:error
|
||||
end
|
||||
defp do_parse(_, _), do: :error
|
||||
|
||||
defp do_parse(<<char, rest::binary>> = bin, base, acc) do
|
||||
defp do_parse(<< char, rest :: binary >>, base, acc) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, base * acc + parse_digit(char, base))
|
||||
else
|
||||
{acc, bin}
|
||||
{acc, << char, rest :: binary >>}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -232,7 +181,7 @@ defmodule Integer do
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer`.
|
||||
of `some_integer`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -241,26 +190,15 @@ defmodule Integer do
|
||||
iex> Integer.to_string(123)
|
||||
"123"
|
||||
|
||||
iex> Integer.to_string(+456)
|
||||
"456"
|
||||
|
||||
iex> Integer.to_string(-789)
|
||||
"-789"
|
||||
|
||||
iex> Integer.to_string(0123)
|
||||
"123"
|
||||
|
||||
"""
|
||||
@spec to_string(integer) :: String.t
|
||||
def to_string(integer) do
|
||||
:erlang.integer_to_binary(integer)
|
||||
def to_string(some_integer) do
|
||||
:erlang.integer_to_binary(some_integer)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
of `some_integer` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -269,69 +207,42 @@ defmodule Integer do
|
||||
iex> Integer.to_string(100, 16)
|
||||
"64"
|
||||
|
||||
iex> Integer.to_string(-100, 16)
|
||||
"-64"
|
||||
|
||||
iex> Integer.to_string(882681651, 36)
|
||||
"ELIXIR"
|
||||
|
||||
"""
|
||||
@spec to_string(integer, 2..36) :: String.t
|
||||
def to_string(integer, base) do
|
||||
:erlang.integer_to_binary(integer, base)
|
||||
def to_string(some_integer, base) do
|
||||
:erlang.integer_to_binary(some_integer, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of the given `integer`.
|
||||
Returns a char list which corresponds to the text representation of the given integer.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(123)
|
||||
'123'
|
||||
|
||||
iex> Integer.to_charlist(+456)
|
||||
'456'
|
||||
|
||||
iex> Integer.to_charlist(-789)
|
||||
'-789'
|
||||
|
||||
iex> Integer.to_charlist(0123)
|
||||
'123'
|
||||
iex> Integer.to_char_list(7)
|
||||
'7'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer) :: charlist
|
||||
def to_charlist(integer) do
|
||||
:erlang.integer_to_list(integer)
|
||||
@spec to_char_list(integer) :: char_list
|
||||
def to_char_list(number) do
|
||||
:erlang.integer_to_list(number)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a charlist which corresponds to the text representation of `integer` in the given `base`.
|
||||
|
||||
`base` can be an integer between 2 and 36.
|
||||
Returns a char list which corresponds to the text representation of the
|
||||
given integer in the given base.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.to_charlist(100, 16)
|
||||
'64'
|
||||
|
||||
iex> Integer.to_charlist(-100, 16)
|
||||
'-64'
|
||||
|
||||
iex> Integer.to_charlist(882681651, 36)
|
||||
'ELIXIR'
|
||||
iex> Integer.to_char_list(1023, 16)
|
||||
'3FF'
|
||||
|
||||
"""
|
||||
@spec to_charlist(integer, 2..36) :: charlist
|
||||
def to_charlist(integer, base) do
|
||||
:erlang.integer_to_list(integer, base)
|
||||
@spec to_char_list(integer, 2..36) :: char_list
|
||||
def to_char_list(number, base) do
|
||||
:erlang.integer_to_list(number, base)
|
||||
end
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
@spec to_char_list(integer) :: charlist
|
||||
def to_char_list(integer), do: Integer.to_charlist(integer)
|
||||
end
|
||||
|
||||
+80
-151
@@ -1,25 +1,25 @@
|
||||
defmodule IO do
|
||||
@moduledoc """
|
||||
Functions handling input/output (IO).
|
||||
Functions handling IO.
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
An IO device must be a pid or an atom representing a process.
|
||||
For convenience, Elixir provides `:stdio` and `:stderr` as
|
||||
shortcuts to Erlang's `:standard_io` and `:standard_error`.
|
||||
|
||||
The majority of the functions expect chardata, i.e. strings or
|
||||
The majority of the functions expect char data, i.e. strings or
|
||||
lists of characters and strings. In case another type is given,
|
||||
functions will convert to string via the `String.Chars` protocol
|
||||
it will do a conversion to string via the `String.Chars` protocol
|
||||
(as shown in typespecs).
|
||||
|
||||
The functions starting with `bin` expect iodata as an argument,
|
||||
The functions starting with `bin*` expect iodata as an argument,
|
||||
i.e. binaries or lists of bytes and binaries.
|
||||
|
||||
## IO devices
|
||||
|
||||
An IO device may be an atom or a pid. In case it is an atom,
|
||||
the atom must be the name of a registered process. In addition,
|
||||
Elixir provides two shortcuts:
|
||||
Elixir provides two shorcuts:
|
||||
|
||||
* `:stdio` - a shortcut for `:standard_io`, which maps to
|
||||
the current `Process.group_leader/0` in Erlang
|
||||
@@ -27,11 +27,6 @@ defmodule IO do
|
||||
* `:stderr` - a shortcut for the named process `:standard_error`
|
||||
provided in Erlang
|
||||
|
||||
IO devices maintain their position, that means subsequent calls to any
|
||||
reading or writing functions will start from the place when the device
|
||||
was last accessed. Position of files can be changed using the
|
||||
`:file.position/2` function.
|
||||
|
||||
"""
|
||||
|
||||
@type device :: atom | pid
|
||||
@@ -47,11 +42,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`.
|
||||
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -67,7 +59,7 @@ defmodule IO do
|
||||
empty string in case the device has reached EOF.
|
||||
"""
|
||||
@spec read(device, :all | :line | non_neg_integer) :: chardata | nodata
|
||||
def read(device \\ group_leader(), line_or_chars)
|
||||
def read(device \\ group_leader, chars_or_line)
|
||||
|
||||
def read(device, :all) do
|
||||
do_read_all(map_dev(device), "")
|
||||
@@ -77,7 +69,7 @@ defmodule IO do
|
||||
:io.get_line(map_dev(device), '')
|
||||
end
|
||||
|
||||
def read(device, count) when is_integer(count) and count >= 0 do
|
||||
def read(device, count) when count >= 0 do
|
||||
:io.get_chars(map_dev(device), '', count)
|
||||
end
|
||||
|
||||
@@ -90,11 +82,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads from the IO `device`. The operation is Unicode unsafe.
|
||||
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
Alternatively, if `:all` is given, then whole `device` is returned.
|
||||
Reads `count` characters from the IO device, a whole
|
||||
`:line` or the whole device with `:all`.
|
||||
|
||||
It returns:
|
||||
|
||||
@@ -109,11 +98,11 @@ defmodule IO do
|
||||
If `:all` is given, `:eof` is never returned, but an
|
||||
empty string in case the device has reached EOF.
|
||||
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binread(device, :all | :line | non_neg_integer) :: iodata | nodata
|
||||
def binread(device \\ group_leader(), line_or_chars)
|
||||
def binread(device \\ group_leader, chars_or_line)
|
||||
|
||||
def binread(device, :all) do
|
||||
do_binread_all(map_dev(device), "")
|
||||
@@ -126,7 +115,7 @@ defmodule IO do
|
||||
end
|
||||
end
|
||||
|
||||
def binread(device, count) when is_integer(count) and count >= 0 do
|
||||
def binread(device, count) when count >= 0 do
|
||||
case :file.read(map_dev(device), count) do
|
||||
{:ok, data} -> data
|
||||
other -> other
|
||||
@@ -143,18 +132,18 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` to the given `device`.
|
||||
Writes the given argument to the given device.
|
||||
|
||||
By default the `device` is the standard output.
|
||||
By default the device is the standard output.
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.write "sample"
|
||||
#=> sample
|
||||
#=> "sample"
|
||||
|
||||
IO.write :stderr, "error"
|
||||
#=> error
|
||||
#=> "error"
|
||||
|
||||
"""
|
||||
@spec write(device, chardata | String.Chars.t) :: :ok
|
||||
@@ -163,13 +152,12 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` as a binary to the given `device`.
|
||||
No Unicode conversion happens.
|
||||
The operation is Unicode unsafe.
|
||||
Writes the given argument to the given device
|
||||
as a binary, no unicode conversion happens.
|
||||
|
||||
Check `write/2` for more information.
|
||||
|
||||
Note: do not use this function on IO devices in Unicode mode
|
||||
Note: do not use this function on IO devices in unicode mode
|
||||
as it will return the wrong result.
|
||||
"""
|
||||
@spec binwrite(device, iodata) :: :ok | {:error, term}
|
||||
@@ -178,60 +166,18 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes `item` to the given `device`, similar to `write/2`,
|
||||
but adds a newline at the end.
|
||||
Writes the argument to the device, similar to `write/2`,
|
||||
but adds a newline at the end. The argument is expected
|
||||
to be a chardata.
|
||||
"""
|
||||
@spec puts(device, chardata | String.Chars.t) :: :ok
|
||||
def puts(device \\ group_leader(), item) do
|
||||
:io.put_chars map_dev(device), [to_chardata(item), ?\n]
|
||||
erl_dev = map_dev(device)
|
||||
:io.put_chars erl_dev, [to_chardata(item), ?\n]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes a `message` to stderr, along with the given `stacktrace`.
|
||||
|
||||
This function also notifies the compiler a warning was printed
|
||||
(in case --warnings-as-errors was enabled). It returns `:ok`
|
||||
if it succeeds.
|
||||
|
||||
An empty list can be passed to avoid stacktrace printing.
|
||||
|
||||
## Examples
|
||||
|
||||
stacktrace = [{MyApp, :main, 1, [file: 'my_app.ex', line: 4]}]
|
||||
IO.warn "variable bar is unused", stacktrace
|
||||
#=> warning: variable bar is unused
|
||||
#=> my_app.ex:4: MyApp.main/1
|
||||
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t, Exception.stacktrace) :: :ok
|
||||
def warn(message, []) do
|
||||
:elixir_errors.warn([to_chardata(message), ?\n])
|
||||
end
|
||||
def warn(message, stacktrace) when is_list(stacktrace) do
|
||||
formatted = Enum.map_join(stacktrace, "\n ", &Exception.format_stacktrace_entry(&1))
|
||||
:elixir_errors.warn([to_chardata(message), ?\n, " ", formatted, ?\n])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Writes a `message` to stderr, along with the current stacktrace.
|
||||
|
||||
It returns `:ok` if it succeeds.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.warn "variable bar is unused"
|
||||
#=> warning: variable bar is unused
|
||||
#=> (iex) evaluator.ex:108: IEx.Evaluator.eval/4
|
||||
|
||||
"""
|
||||
@spec warn(chardata | String.Chars.t) :: :ok
|
||||
def warn(message) do
|
||||
{:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace)
|
||||
warn(message, Enum.drop(stacktrace, 2))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects and writes the given `item` to the device.
|
||||
Inspects and writes the given argument to the device.
|
||||
|
||||
It enables pretty printing by default with width of
|
||||
80 characters. The width can be changed by explicitly
|
||||
@@ -250,7 +196,7 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects `item` according to the given options using the IO `device`.
|
||||
Inspects the item with options using the given device.
|
||||
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
"""
|
||||
@@ -263,34 +209,10 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from IO device `:stdio`.
|
||||
|
||||
If `:stdio` is a Unicode device, `count` implies
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
See `IO.getn/3` for a description of return values.
|
||||
|
||||
"""
|
||||
@spec getn(chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
@spec getn(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, count) when is_integer(count) and count > 0 do
|
||||
getn(group_leader, prompt, count)
|
||||
end
|
||||
|
||||
def getn(device, prompt) when not is_integer(prompt) do
|
||||
getn(device, prompt, 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the IO `device`.
|
||||
|
||||
If the IO `device` is a Unicode device, `count` implies
|
||||
the number of Unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
|
||||
It returns:
|
||||
|
||||
* `data` - the input characters
|
||||
@@ -300,17 +222,32 @@ defmodule IO do
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
"""
|
||||
@spec getn(chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
@spec getn(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def getn(prompt, count \\ 1)
|
||||
|
||||
def getn(prompt, count) when is_integer(count) do
|
||||
getn(group_leader, prompt, count)
|
||||
end
|
||||
|
||||
def getn(device, prompt) do
|
||||
getn(device, prompt, 1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a number of bytes from the io device. If the
|
||||
io device is a unicode device, `count` implies
|
||||
the number of unicode codepoints to be retrieved.
|
||||
Otherwise, `count` is the number of raw bytes to be retrieved.
|
||||
"""
|
||||
@spec getn(device, chardata | String.Chars.t, pos_integer) :: chardata | nodata
|
||||
def getn(device, prompt, count) when is_integer(count) and count > 0 do
|
||||
def getn(device, prompt, count) do
|
||||
:io.get_chars(map_dev(device), to_chardata(prompt), count)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads a line from the IO `device`.
|
||||
|
||||
It returns:
|
||||
Reads a line from the IO device. It returns:
|
||||
|
||||
* `data` - the characters in the line terminated
|
||||
by a line-feed (LF) or end of file (EOF)
|
||||
@@ -320,13 +257,6 @@ defmodule IO do
|
||||
* `{:error, reason}` - other (rare) error condition;
|
||||
for instance, `{:error, :estale}` if reading from an
|
||||
NFS volume
|
||||
|
||||
## Examples
|
||||
|
||||
To display "What is your name?" as a prompt and await user input:
|
||||
|
||||
IO.gets "What is your name?\n"
|
||||
|
||||
"""
|
||||
@spec gets(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def gets(device \\ group_leader(), prompt) do
|
||||
@@ -334,16 +264,16 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`.
|
||||
Converts the io device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of characters or line by line if
|
||||
`:line` is given.
|
||||
The device is iterated line by line if `:line` is given or
|
||||
by a given number of codepoints.
|
||||
|
||||
This reads from the IO as utf-8. Check out
|
||||
This reads the IO as utf-8. Check out
|
||||
`IO.binstream/2` to handle the IO as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
@@ -358,34 +288,28 @@ defmodule IO do
|
||||
|
||||
"""
|
||||
@spec stream(device, :line | pos_integer) :: Enumerable.t
|
||||
def stream(device, line_or_codepoints)
|
||||
when line_or_codepoints == :line
|
||||
when is_integer(line_or_codepoints) and line_or_codepoints > 0 do
|
||||
def stream(device, line_or_codepoints) do
|
||||
IO.Stream.__build__(map_dev(device), false, line_or_codepoints)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the IO `device` into an `IO.Stream`. The operation is Unicode unsafe.
|
||||
Converts the IO device into a `IO.Stream`.
|
||||
|
||||
An `IO.Stream` implements both `Enumerable` and
|
||||
`Collectable`, allowing it to be used for both read
|
||||
and write.
|
||||
|
||||
The `device` is iterated by the given number of bytes or line by line if
|
||||
`:line` is given.
|
||||
This reads from the IO device as a raw binary.
|
||||
The device is iterated line by line or by a number of bytes.
|
||||
This reads the IO device as a raw binary.
|
||||
|
||||
Note that an IO stream has side effects and every time
|
||||
you go over the stream you may get different results.
|
||||
|
||||
Finally, do not use this function on IO devices in Unicode
|
||||
Finally, do not use this function on IO devices in unicode
|
||||
mode as it will return the wrong result.
|
||||
|
||||
"""
|
||||
@spec binstream(device, :line | pos_integer) :: Enumerable.t
|
||||
def binstream(device, line_or_bytes)
|
||||
when line_or_bytes == :line
|
||||
when is_integer(line_or_bytes) and line_or_bytes > 0 do
|
||||
def binstream(device, line_or_bytes) do
|
||||
IO.Stream.__build__(map_dev(device), true, line_or_bytes)
|
||||
end
|
||||
|
||||
@@ -393,8 +317,8 @@ defmodule IO do
|
||||
Converts chardata (a list of integers representing codepoints,
|
||||
lists and strings) into a string.
|
||||
|
||||
In case the conversion fails, it raises an `UnicodeConversionError`.
|
||||
If a string is given, it returns the string itself.
|
||||
In case the conversion fails, it raises a `UnicodeConversionError`.
|
||||
If a string is given, returns the string itself.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -404,9 +328,6 @@ defmodule IO do
|
||||
iex> IO.chardata_to_string([0x0061, "bc"])
|
||||
"abc"
|
||||
|
||||
iex> IO.chardata_to_string("string")
|
||||
"string"
|
||||
|
||||
"""
|
||||
@spec chardata_to_string(chardata) :: String.t | no_return
|
||||
def chardata_to_string(string) when is_binary(string) do
|
||||
@@ -414,17 +335,25 @@ defmodule IO do
|
||||
end
|
||||
|
||||
def chardata_to_string(list) when is_list(list) do
|
||||
List.to_string(list)
|
||||
case :unicode.characters_to_binary(list) do
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
{:error, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :invalid
|
||||
|
||||
{:incomplete, encoded, rest} ->
|
||||
raise UnicodeConversionError, encoded: encoded, rest: rest, kind: :incomplete
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts iodata (a list of integers representing bytes, lists
|
||||
and binaries) into a binary.
|
||||
The operation is Unicode unsafe.
|
||||
|
||||
Notice that this function treats lists of integers as raw bytes
|
||||
and does not perform any kind of encoding conversion. If you want
|
||||
to convert from a charlist to a string (UTF-8 encoded), please
|
||||
to convert from a char list to a string (UTF-8 encoded), please
|
||||
use `chardata_to_string/1` instead.
|
||||
|
||||
If this function receives a binary, the same binary is returned.
|
||||
@@ -436,7 +365,7 @@ defmodule IO do
|
||||
iex> bin1 = <<1, 2, 3>>
|
||||
iex> bin2 = <<4, 5>>
|
||||
iex> bin3 = <<6>>
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4 | bin3])
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4|bin3])
|
||||
<<1, 2, 3, 1, 2, 3, 4, 5, 4, 6>>
|
||||
|
||||
iex> bin = <<1, 2, 3>>
|
||||
@@ -456,7 +385,7 @@ defmodule IO do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> IO.iodata_length([1, 2 | <<3, 4>>])
|
||||
iex> IO.iodata_length([1, 2|<<3, 4>>])
|
||||
4
|
||||
|
||||
"""
|
||||
@@ -466,8 +395,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_stream(device, line_or_codepoints) do
|
||||
case read(device, line_or_codepoints) do
|
||||
def each_stream(device, what) do
|
||||
case read(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
@@ -478,8 +407,8 @@ defmodule IO do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def each_binstream(device, line_or_chars) do
|
||||
case binread(device, line_or_chars) do
|
||||
def each_binstream(device, what) do
|
||||
case binread(device, what) do
|
||||
:eof ->
|
||||
{:halt, device}
|
||||
{:error, reason} ->
|
||||
@@ -491,7 +420,7 @@ defmodule IO do
|
||||
|
||||
@compile {:inline, map_dev: 1, to_chardata: 1}
|
||||
|
||||
# Map the Elixir names for standard IO and error to Erlang names
|
||||
# Map the Elixir names for standard io and error to Erlang names
|
||||
defp map_dev(:stdio), do: :standard_io
|
||||
defp map_dev(:stderr), do: :standard_error
|
||||
defp map_dev(other) when is_atom(other) or is_pid(other) or is_tuple(other), do: other
|
||||
|
||||
+14
-42
@@ -25,9 +25,9 @@ defmodule IO.ANSI do
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
|
||||
@typep ansicode :: atom
|
||||
@typep ansilist :: maybe_improper_list(char | ansicode | binary | ansilist, binary | ansicode | [])
|
||||
@type ansidata :: ansilist | ansicode | binary
|
||||
@typep ansicode :: atom()
|
||||
@typep ansilist :: maybe_improper_list(char() | ansicode() | binary() | ansilist(), binary() | ansicode() | [])
|
||||
@type ansidata :: ansilist() | ansicode() | binary()
|
||||
|
||||
@doc """
|
||||
Checks if ANSI coloring is supported and enabled on this machine.
|
||||
@@ -42,34 +42,6 @@ defmodule IO.ANSI do
|
||||
Application.get_env(:elixir, :ansi_enabled, false)
|
||||
end
|
||||
|
||||
@doc "Sets foreground color"
|
||||
@spec color(0..255) :: String.t
|
||||
def color(code) when code in 0..255, do: "\e[38;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the foreground color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color(0..5, 0..5, 0..5) :: String.t
|
||||
def color(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Sets background color"
|
||||
@spec color_background(0..255) :: String.t
|
||||
def color_background(code) when code in 0..255, do: "\e[48;5;#{code}m"
|
||||
|
||||
@doc ~S"""
|
||||
Sets the background color from individual RGB values.
|
||||
|
||||
Valid values for each color are in the range 0 to 5.
|
||||
"""
|
||||
@spec color_background(0..5, 0..5, 0..5) :: String.t
|
||||
def color_background(r, g, b) when r in 0..5 and g in 0..5 and b in 0..5 do
|
||||
color_background(16 + (36 * r) + (6 * g) + b)
|
||||
end
|
||||
|
||||
@doc "Resets all attributes"
|
||||
defsequence :reset, 0
|
||||
|
||||
@@ -186,8 +158,8 @@ defmodule IO.ANSI do
|
||||
[[[[[[], "Hello, "] | "\e[31m"] | "\e[1m"], "world!"] | "\e[0m"]
|
||||
|
||||
"""
|
||||
def format(chardata, emit? \\ enabled?) when is_boolean(emit?) do
|
||||
do_format(chardata, [], [], emit?, :maybe)
|
||||
def format(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, :maybe)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -206,12 +178,12 @@ defmodule IO.ANSI do
|
||||
[[[[[[] | "\e[1m"], 87], 111], 114], 100]
|
||||
|
||||
"""
|
||||
def format_fragment(chardata, emit? \\ enabled?) when is_boolean(emit?) do
|
||||
do_format(chardata, [], [], emit?, false)
|
||||
def format_fragment(chardata, emit \\ enabled?) when is_boolean(emit) do
|
||||
do_format(chardata, [], [], emit, false)
|
||||
end
|
||||
|
||||
defp do_format([term | rest], rem, acc, emit?, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit?, append_reset)
|
||||
defp do_format([term | rest], rem, acc, emit, append_reset) do
|
||||
do_format(term, [rest | rem], acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, true, append_reset) when is_atom(term) do
|
||||
@@ -222,19 +194,19 @@ defmodule IO.ANSI do
|
||||
do_format([], rem, acc, false, append_reset)
|
||||
end
|
||||
|
||||
defp do_format(term, rem, acc, emit?, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc, term], emit?, append_reset)
|
||||
defp do_format(term, rem, acc, emit, append_reset) when not is_list(term) do
|
||||
do_format([], rem, [acc | [term]], emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [next | rest], acc, emit?, append_reset) do
|
||||
do_format(next, rest, acc, emit?, append_reset)
|
||||
defp do_format([], [next | rest], acc, emit, append_reset) do
|
||||
do_format(next, rest, acc, emit, append_reset)
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, true, true) do
|
||||
[acc | IO.ANSI.reset]
|
||||
end
|
||||
|
||||
defp do_format([], [], acc, _emit?, _append_reset) do
|
||||
defp do_format([], [], acc, _emit, _append_reset) do
|
||||
acc
|
||||
end
|
||||
end
|
||||
|
||||
@@ -43,7 +43,7 @@ defmodule IO.ANSI.Docs do
|
||||
options = Keyword.merge(default_options, options)
|
||||
width = options[:width]
|
||||
padding = div(width + String.length(heading), 2)
|
||||
heading = heading |> String.pad_leading(padding) |> String.pad_trailing(width)
|
||||
heading = heading |> String.rjust(padding) |> String.ljust(width)
|
||||
write(:doc_title, heading, options)
|
||||
newline_after_block
|
||||
end
|
||||
@@ -58,7 +58,7 @@ defmodule IO.ANSI.Docs do
|
||||
options = Keyword.merge(default_options, options)
|
||||
doc
|
||||
|> String.split(["\r\n", "\n"], trim: false)
|
||||
|> Enum.map(&String.trim_trailing/1)
|
||||
|> Enum.map(&String.rstrip/1)
|
||||
|> process([], "", options)
|
||||
end
|
||||
|
||||
@@ -68,19 +68,19 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp process(["# " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h1(String.trim(heading), options)
|
||||
write_h1(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["## " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h2(String.trim(heading), options)
|
||||
write_h2(String.strip(heading), options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
defp process(["### " <> heading | rest], text, indent, options) do
|
||||
write_text(text, indent, options)
|
||||
write_h3(String.trim(heading), indent, options)
|
||||
write_h3(String.strip(heading), indent, options)
|
||||
process(rest, [], "", options)
|
||||
end
|
||||
|
||||
@@ -104,15 +104,23 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp process(all=[line | rest], text, indent, options) do
|
||||
{stripped, count} = strip_spaces(line, 0, :infinity)
|
||||
cond do
|
||||
link_label?(stripped, count) ->
|
||||
write_text([line], indent, options, true)
|
||||
process(rest, text, indent, options)
|
||||
table_line?(stripped) and rest != [] and table_line?(hd(rest)) ->
|
||||
write_text(text, indent, options)
|
||||
process_table(all, indent, options)
|
||||
true ->
|
||||
process_rest(stripped, rest, count, text, indent, options)
|
||||
if is_table_line?(stripped) and rest != [] and is_table_line?(hd(rest)) do
|
||||
write_text(text, indent, options)
|
||||
process_table(all, indent, options)
|
||||
else
|
||||
case stripped do
|
||||
<<bullet, ?\s, item :: binary>> when bullet in @bullets ->
|
||||
write_text(text, indent, options)
|
||||
process_list("• ", item, rest, count, indent, options)
|
||||
<<d1, ?., ?\s, item :: binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, ?., ?\s>>, item, rest, count, indent, options)
|
||||
<<d1, d2, ?., ?\s, item :: binary>> when d1 in ?0..?9 and d2 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, d2, ?., ?\s>>, item, rest, count, indent, options)
|
||||
_ ->
|
||||
process(rest, [stripped | text], indent, options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -135,25 +143,9 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
## Lists
|
||||
|
||||
defp process_rest(stripped, rest, count, text, indent, options) do
|
||||
case stripped do
|
||||
<<bullet, ?\s, item::binary>> when bullet in @bullets ->
|
||||
write_text(text, indent, options)
|
||||
process_list("• ", item, rest, count, indent, options)
|
||||
<<d1, ?., ?\s, item::binary>> when d1 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, ?., ?\s>>, item, rest, count, indent, options)
|
||||
<<d1, d2, ?., ?\s, item::binary>> when d1 in ?0..?9 and d2 in ?0..?9 ->
|
||||
write_text(text, indent, options)
|
||||
process_list(<<d1, d2, ?., ?\s>>, item, rest, count, indent, options)
|
||||
_ ->
|
||||
process(rest, [stripped | text], indent, options)
|
||||
end
|
||||
end
|
||||
|
||||
defp process_list(entry, line, rest, count, indent, options) do
|
||||
# The first list always win some extra padding
|
||||
entry = if indent == "", do: " " <> entry, else: entry
|
||||
if indent == "", do: entry = " " <> entry
|
||||
new_indent = indent <> String.duplicate(" ", String.length(entry))
|
||||
|
||||
{contents, rest, done} = process_list_next(rest, count, byte_size(new_indent), [])
|
||||
@@ -178,11 +170,11 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp process_list_next_kind(stripped, rest, count, next_count) do
|
||||
case {stripped, rest} do
|
||||
{<<bullet, ?\s, _::binary>>, _} when bullet in @bullets and next_count <= count ->
|
||||
{<<bullet, ?\s, _ :: binary>>, _} when bullet in @bullets and next_count <= count ->
|
||||
:list
|
||||
{<<d1, ?., ?\s, _::binary>>, _} when d1 in ?0..?9 and next_count <= count ->
|
||||
{<<d1, ?., ?\s, _ :: binary>>, _} when d1 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
{<<d1, d2, ?., ?\s, _::binary>>, _} when d1 in ?0..?9 and d2 in ?0..?9 and next_count <= count ->
|
||||
{<<d1, d2, ?., ?\s, _ :: binary>>, _} when d1 in ?0..?9 and d2 in ?0..?9 and next_count <= count ->
|
||||
:list
|
||||
{"", [" " <> _ | _]} ->
|
||||
:next
|
||||
@@ -197,7 +189,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp write_text(text, indent, options) do
|
||||
case Enum.reverse(text) do
|
||||
[:no_wrap | rest] -> write_text(rest, indent, options, true)
|
||||
[:no_wrap|rest] -> write_text(rest, indent, options, true)
|
||||
rest -> write_text(rest, indent, options, false)
|
||||
end
|
||||
end
|
||||
@@ -229,7 +221,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp process_code([" " <> line | rest], code, indent, options) do
|
||||
process_code(rest, [line | code], indent, options)
|
||||
process_code(rest, [line|code], indent, options)
|
||||
end
|
||||
|
||||
defp process_code(rest, code, indent, options) do
|
||||
@@ -250,7 +242,7 @@ defmodule IO.ANSI.Docs do
|
||||
if line === delimiter do
|
||||
process_code(rest, code, indent, options)
|
||||
else
|
||||
process_fenced_code(rest, [line | code], indent, options, delimiter)
|
||||
process_fenced_code(rest, [line|code], indent, options, delimiter)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -262,7 +254,7 @@ defmodule IO.ANSI.Docs do
|
||||
## Tables
|
||||
|
||||
defp process_table(lines, indent, options) do
|
||||
{table, rest} = Enum.split_while(lines, &table_line?/1)
|
||||
{table, rest} = Enum.split_while(lines, &is_table_line?/1)
|
||||
table_lines(table, options)
|
||||
newline_after_block
|
||||
process(rest, [], indent, options)
|
||||
@@ -285,8 +277,8 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
defp split_into_columns(line, options) do
|
||||
line
|
||||
|> String.trim("|")
|
||||
|> String.trim()
|
||||
|> String.strip(?|)
|
||||
|> String.strip()
|
||||
|> String.split(~r/\s\|\s/)
|
||||
|> Enum.map(&render_column(&1, options))
|
||||
end
|
||||
@@ -342,7 +334,7 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
end
|
||||
|
||||
defp table_line?(line) do
|
||||
defp is_table_line?(line) do
|
||||
Regex.match?(~r'''
|
||||
( ^ \s{0,3} \| (?: [^|]+ \|)+ \s* $ )
|
||||
|
|
||||
@@ -352,14 +344,6 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
## Helpers
|
||||
|
||||
defp link_label?("[" <> rest, count) when count <= 3, do: link_label?(rest)
|
||||
defp link_label?(_, _), do: false
|
||||
|
||||
defp link_label?("]: " <> _), do: true
|
||||
defp link_label?("]" <> _), do: false
|
||||
defp link_label?(""), do: false
|
||||
defp link_label?(<<_>> <> rest), do: link_label?(rest)
|
||||
|
||||
defp strip_spaces(" " <> line, acc, max) when acc < max,
|
||||
do: strip_spaces(line, acc + 1, max)
|
||||
defp strip_spaces(rest, acc, _max),
|
||||
@@ -379,13 +363,13 @@ defmodule IO.ANSI.Docs do
|
||||
write_with_wrap(rest, available, indent, false)
|
||||
end
|
||||
|
||||
defp take_words([word | words], available, acc) do
|
||||
defp take_words([word|words], available, acc) do
|
||||
available = available - length_without_escape(word, 0)
|
||||
|
||||
cond do
|
||||
# It fits, take one for space and continue decreasing
|
||||
available > 0 ->
|
||||
take_words(words, available - 1, [word | acc])
|
||||
take_words(words, available - 1, [word|acc])
|
||||
|
||||
# No space but we got no words
|
||||
acc == [] ->
|
||||
@@ -393,7 +377,7 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Otherwise
|
||||
true ->
|
||||
{Enum.reverse(acc), [word | words]}
|
||||
{Enum.reverse(acc), [word|words]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -401,11 +385,11 @@ defmodule IO.ANSI.Docs do
|
||||
{Enum.reverse(acc), []}
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, _, ?m>> <> rest, count) do
|
||||
defp length_without_escape(<< ?\e, ?[, _, _, ?m, rest :: binary >>, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
defp length_without_escape(<<?\e, ?[, _, ?m>> <> rest, count) do
|
||||
defp length_without_escape(<< ?\e, ?[, _, ?m, rest :: binary >>, count) do
|
||||
length_without_escape(rest, count)
|
||||
end
|
||||
|
||||
@@ -423,7 +407,10 @@ defmodule IO.ANSI.Docs do
|
||||
end
|
||||
|
||||
defp escape_underlines_in_link(text) do
|
||||
Regex.replace(~r{https?\S*}, text, &String.replace(&1, "_", "\\_"))
|
||||
case Regex.match?(~r{.*(https?\S*)}, text) do
|
||||
true -> Regex.replace(~r{_}, text, "\\\\_")
|
||||
_ -> text
|
||||
end
|
||||
end
|
||||
|
||||
defp remove_square_brackets_in_link(text) do
|
||||
@@ -450,11 +437,11 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Inline start
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
|
||||
defp handle_inline(<<?*, ?*, rest :: binary>>, options) do
|
||||
handle_inline(rest, ?d, ["**"], [], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
|
||||
defp handle_inline(<<mark, rest :: binary>>, options) when mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [], options)
|
||||
end
|
||||
|
||||
@@ -464,82 +451,82 @@ defmodule IO.ANSI.Docs do
|
||||
|
||||
# Inline delimiters
|
||||
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
defp handle_inline(<<delimiter, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters do
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?d, ["**"], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<delimiter, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
defp handle_inline(<<delimiter, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and delimiter in @delimiters and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, mark, [<<mark>>], [delimiter, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, nil, buffer, acc, options)
|
||||
defp handle_inline(<<?`, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest::binary>>, nil, buffer, acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, mark, rest :: binary>>, nil, buffer, acc, options)
|
||||
when rest != "" and mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer) | acc], options)
|
||||
handle_inline(rest, mark, [<<mark>>], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, rest::binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\ | buffer], acc, options)
|
||||
defp handle_inline(<<?\\, ?\\, rest :: binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?\\|buffer], acc, options)
|
||||
end
|
||||
|
||||
# An escape is not valid inside `
|
||||
defp handle_inline(<<?\\, mark, rest::binary>>, limit, buffer, acc, options)
|
||||
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
|
||||
when not(mark == limit and mark == ?`) do
|
||||
handle_inline(rest, limit, [mark | buffer], acc, options)
|
||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Inline end
|
||||
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest::binary>>, ?d, buffer, acc, options)
|
||||
defp handle_inline(<<?*, ?*, delimiter, rest :: binary>>, ?d, buffer, acc, options)
|
||||
when delimiter in @delimiters do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, delimiter, rest::binary>>, mark, buffer, acc, options)
|
||||
defp handle_inline(<<mark, delimiter, rest :: binary>>, mark, buffer, acc, options)
|
||||
when delimiter in @delimiters and mark in @single do
|
||||
handle_inline(<<delimiter, rest::binary>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<delimiter, rest :: binary>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
defp handle_inline(<<?*, ?*, rest:: binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, mark, buffer, acc, options)
|
||||
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options)
|
||||
when rest == "" and mark in @single do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options) | acc], options)
|
||||
defp handle_inline(<<?`, rest :: binary>>, ?`, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
# Catch all
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char | buffer], acc, options)
|
||||
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<>>, _mark, buffer, acc, _options) do
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer) | acc])
|
||||
IO.iodata_to_binary Enum.reverse([Enum.reverse(buffer)|acc])
|
||||
end
|
||||
|
||||
defp inline_buffer(buffer, options) do
|
||||
[h | t] = Enum.reverse([IO.ANSI.reset | buffer])
|
||||
[color_for(h, options) | t]
|
||||
[h|t] = Enum.reverse([IO.ANSI.reset|buffer])
|
||||
[color_for(h, options)|t]
|
||||
end
|
||||
|
||||
defp color_for(mark, colors) do
|
||||
|
||||
@@ -10,7 +10,7 @@ end
|
||||
|
||||
defmodule IO.Stream do
|
||||
@moduledoc """
|
||||
Defines an `IO.Stream` struct returned by `IO.stream/2` and `IO.binstream/2`.
|
||||
Defines a `IO.Stream` struct returned by `IO.stream/2` and `IO.binstream/2`.
|
||||
|
||||
The following fields are public:
|
||||
|
||||
@@ -18,9 +18,6 @@ defmodule IO.Stream do
|
||||
* `raw` - a boolean indicating if bin functions should be used
|
||||
* `line_or_bytes` - if reading should read lines or a given amount of bytes
|
||||
|
||||
It is worth noting that an IO stream has side effects and every time you go
|
||||
over the stream you may get different results.
|
||||
|
||||
"""
|
||||
|
||||
defstruct device: nil, raw: true, line_or_bytes: :line
|
||||
|
||||
+264
-626
File diff suppressed because it is too large
Load Diff
@@ -111,7 +111,7 @@ defmodule Kernel.CLI do
|
||||
|
||||
defp shared_option?(list, config, callback) do
|
||||
case parse_shared(list, config) do
|
||||
{[h | hs], _} when h == hd(list) ->
|
||||
{[h|hs], _} when h == hd(list) ->
|
||||
new_config = %{config | errors: ["#{h} : Unknown option" | config.errors]}
|
||||
callback.(hs, new_config)
|
||||
{new_list, new_config} ->
|
||||
@@ -125,18 +125,18 @@ defmodule Kernel.CLI do
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def, :elixir_map]
|
||||
:elixir_def]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _} | _]) do
|
||||
defp prune_stacktrace([{__MODULE__, :wrapper, 1, _}|_]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h | t]) do
|
||||
[h | prune_stacktrace(t)]
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
@@ -145,54 +145,48 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse shared options
|
||||
|
||||
defp parse_shared([opt | _t], _config) when opt in ["-v", "--version"] do
|
||||
if function_exported?(IEx, :started?, 0) and IEx.started? do
|
||||
IO.puts "IEx " <> System.build_info[:build]
|
||||
else
|
||||
IO.puts :erlang.system_info(:system_version)
|
||||
IO.puts "Elixir " <> System.build_info[:build]
|
||||
end
|
||||
|
||||
defp parse_shared([opt|_t], _config) when opt in ["-v", "--version"] do
|
||||
IO.puts "Elixir #{System.version}"
|
||||
System.halt 0
|
||||
end
|
||||
|
||||
defp parse_shared(["-pa", h | t], config) do
|
||||
defp parse_shared(["-pa", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_patha/1)
|
||||
parse_shared t, %{config | pa: config.pa ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pz", h | t], config) do
|
||||
defp parse_shared(["-pz", h|t], config) do
|
||||
paths = expand_code_path(h)
|
||||
Enum.each(paths, &:code.add_pathz/1)
|
||||
parse_shared t, %{config | pz: config.pz ++ paths}
|
||||
end
|
||||
|
||||
defp parse_shared(["--app", h | t], config) do
|
||||
defp parse_shared(["--app", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:app, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["--no-halt" | t], config) do
|
||||
defp parse_shared(["--no-halt"|t], config) do
|
||||
parse_shared t, %{config | halt: false}
|
||||
end
|
||||
|
||||
defp parse_shared(["-e", h | t], config) do
|
||||
defp parse_shared(["-e", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:eval, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-r", h | t], config) do
|
||||
defp parse_shared(["-r", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared(["-pr", h | t], config) do
|
||||
defp parse_shared(["-pr", h|t], config) do
|
||||
parse_shared t, %{config | commands: [{:parallel_require, h} | config.commands]}
|
||||
end
|
||||
|
||||
defp parse_shared([erl, _ | t], config) when erl in ["--erl", "--sname", "--name", "--cookie", "--logger-otp-reports", "--logger-sasl-reports"] do
|
||||
defp parse_shared([erl, _|t], config) when erl in ["--erl", "--sname", "--name", "--cookie"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
defp parse_shared([erl | t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
defp parse_shared([erl|t], config) when erl in ["--detached", "--hidden", "--werl"] do
|
||||
parse_shared t, config
|
||||
end
|
||||
|
||||
@@ -203,30 +197,30 @@ defmodule Kernel.CLI do
|
||||
defp expand_code_path(path) do
|
||||
path = Path.expand(path)
|
||||
case Path.wildcard(path) do
|
||||
[] -> [to_charlist(path)]
|
||||
list -> Enum.map(list, &to_charlist/1)
|
||||
[] -> [to_char_list(path)]
|
||||
list -> Enum.map(list, &to_char_list/1)
|
||||
end
|
||||
end
|
||||
|
||||
# Process init options
|
||||
|
||||
defp parse_argv(["--" | t], config) do
|
||||
defp parse_argv(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_argv(["+elixirc" | t], config) do
|
||||
defp parse_argv(["+elixirc"|t], config) do
|
||||
parse_compiler t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["+iex" | t], config) do
|
||||
defp parse_argv(["+iex"|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_argv(["-S", h | t], config) do
|
||||
defp parse_argv(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_argv([h | t] = list, config) do
|
||||
defp parse_argv([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_argv(&1, &2)
|
||||
@@ -245,35 +239,35 @@ defmodule Kernel.CLI do
|
||||
|
||||
# Parse compiler options
|
||||
|
||||
defp parse_compiler(["--" | t], config) do
|
||||
defp parse_compiler(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
defp parse_compiler(["-o", h | t], config) do
|
||||
defp parse_compiler(["-o", h|t], config) do
|
||||
parse_compiler t, %{config | output: h}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-docs" | t], config) do
|
||||
defp parse_compiler(["--no-docs"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:docs, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--no-debug-info" | t], config) do
|
||||
defp parse_compiler(["--no-debug-info"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:debug_info, false} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--ignore-module-conflict" | t], config) do
|
||||
defp parse_compiler(["--ignore-module-conflict"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:ignore_module_conflict, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--warnings-as-errors" | t], config) do
|
||||
defp parse_compiler(["--warnings-as-errors"|t], config) do
|
||||
parse_compiler t, %{config | compiler_options: [{:warnings_as_errors, true} | config.compiler_options]}
|
||||
end
|
||||
|
||||
defp parse_compiler(["--verbose" | t], config) do
|
||||
defp parse_compiler(["--verbose"|t], config) do
|
||||
parse_compiler t, %{config | verbose_compile: true}
|
||||
end
|
||||
|
||||
defp parse_compiler([h | t] = list, config) do
|
||||
defp parse_compiler([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_compiler(&1, &2)
|
||||
@@ -284,30 +278,30 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp parse_compiler([], config) do
|
||||
{%{config | commands: [{:compile, config.compile} | config.commands]}, []}
|
||||
{%{config | commands: [{:compile, config.compile}|config.commands]}, []}
|
||||
end
|
||||
|
||||
# Parse IEx options
|
||||
# Parse iex options
|
||||
|
||||
defp parse_iex(["--" | t], config) do
|
||||
defp parse_iex(["--"|t], config) do
|
||||
{config, t}
|
||||
end
|
||||
|
||||
# This clause is here so that Kernel.CLI does not
|
||||
# error out with "unknown option"
|
||||
defp parse_iex(["--dot-iex", _ | t], config) do
|
||||
defp parse_iex(["--dot-iex", _|t], config) do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex([opt, _ | t], config) when opt in ["--remsh"] do
|
||||
defp parse_iex([opt, _|t], config) when opt in ["--remsh"] do
|
||||
parse_iex t, config
|
||||
end
|
||||
|
||||
defp parse_iex(["-S", h | t], config) do
|
||||
defp parse_iex(["-S", h|t], config) do
|
||||
{%{config | commands: [{:script, h} | config.commands]}, t}
|
||||
end
|
||||
|
||||
defp parse_iex([h | t] = list, config) do
|
||||
defp parse_iex([h|t] = list, config) do
|
||||
case h do
|
||||
"-" <> _ ->
|
||||
shared_option? list, config, &parse_iex(&1, &2)
|
||||
@@ -390,13 +384,8 @@ defmodule Kernel.CLI do
|
||||
{:ok, files} ->
|
||||
wrapper fn ->
|
||||
Code.compiler_options(config.compiler_options)
|
||||
opts =
|
||||
if config.verbose_compile do
|
||||
[each_long_compilation: &IO.puts("Compiling #{&1} (it's taking more than 5s)")]
|
||||
else
|
||||
[]
|
||||
end
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output, opts)
|
||||
Kernel.ParallelCompiler.files_to_path(files, config.output,
|
||||
each_file: fn file -> if config.verbose_compile do IO.puts "Compiled #{file}" end end)
|
||||
end
|
||||
{:missing, missing} ->
|
||||
{:error, "No files matched pattern(s) #{Enum.join(missing, ",")}"}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
defmodule Kernel.Def do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Callback invoked at compile time for `defdelegate`.
|
||||
"""
|
||||
def delegate(fun, opts) do
|
||||
append_first = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
as_args =
|
||||
case append_first and args != [] do
|
||||
true -> tl(args) ++ [hd(args)]
|
||||
false -> args
|
||||
end
|
||||
|
||||
as = Keyword.get(opts, :as, name)
|
||||
{name, args, as, as_args}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback invoked at compile time for `defstruct`.
|
||||
"""
|
||||
def struct(module, fields) do
|
||||
case fields do
|
||||
fs when is_list(fs) -> :ok
|
||||
other ->
|
||||
raise ArgumentError, "struct fields definition must be list, got: #{inspect other}"
|
||||
end
|
||||
|
||||
fields = :lists.map(fn
|
||||
{key, val} when is_atom(key) ->
|
||||
try do
|
||||
Macro.escape(val)
|
||||
rescue
|
||||
e in [ArgumentError] ->
|
||||
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
|
||||
else
|
||||
_ -> {key, val}
|
||||
end
|
||||
key when is_atom(key) ->
|
||||
{key, nil}
|
||||
other ->
|
||||
raise ArgumentError, "struct field names must be atoms, got: #{inspect other}"
|
||||
end, fields)
|
||||
|
||||
:maps.put(:__struct__, module, :maps.from_list(fields))
|
||||
end
|
||||
end
|
||||
@@ -4,34 +4,37 @@ defmodule Kernel.ErrorHandler do
|
||||
@moduledoc false
|
||||
|
||||
def undefined_function(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_function(module, fun, args)
|
||||
end
|
||||
|
||||
def undefined_lambda(module, fun, args) do
|
||||
ensure_loaded(module) or ensure_compiled(module, :module)
|
||||
ensure_loaded(module)
|
||||
:error_handler.undefined_lambda(module, fun, args)
|
||||
end
|
||||
|
||||
def ensure_loaded(module) do
|
||||
case :code.ensure_loaded(module) do
|
||||
{:module, _} -> true
|
||||
{:error, _} -> false
|
||||
end
|
||||
def release() do
|
||||
# On release, no further allow elixir_ensure_compiled
|
||||
# directives and revert to the original error handler.
|
||||
# Note we should not delete the elixir_compiler_pid though,
|
||||
# as we still want to send notifications to the compiler.
|
||||
:erlang.erase(:elixir_ensure_compiled)
|
||||
:erlang.process_flag(:error_handler, :error_handler)
|
||||
:ok
|
||||
end
|
||||
|
||||
# Never wait on nil because it should never be defined.
|
||||
def ensure_compiled(nil, _kind) do
|
||||
false
|
||||
end
|
||||
def ensure_compiled(module, kind) do
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, kind, self(), ref, module, :elixir_module.compiler_modules()}
|
||||
:erlang.garbage_collect(self)
|
||||
receive do
|
||||
{^ref, :found} -> true
|
||||
{^ref, :not_found} -> false
|
||||
defp ensure_loaded(module) do
|
||||
case Code.ensure_loaded(module) do
|
||||
{:module, _} -> :ok
|
||||
{:error, _} ->
|
||||
parent = :erlang.get(:elixir_compiler_pid)
|
||||
ref = :erlang.make_ref
|
||||
send parent, {:waiting, :module, self(), ref, module}
|
||||
:erlang.garbage_collect(self)
|
||||
receive do
|
||||
{^ref, :ready} -> :ok
|
||||
{^ref, :release} -> release()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
# This is an Elixir module responsible for tracking
|
||||
# This is a module Elixir responsible for tracking
|
||||
# the usage of aliases, imports and requires in the Elixir scope.
|
||||
#
|
||||
# The implementation simply stores dispatch information in an
|
||||
# ETS table and then consults this table once compilation is done.
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
defmodule Kernel.LexicalTracker do
|
||||
@@ -9,18 +12,20 @@ defmodule Kernel.LexicalTracker do
|
||||
@behaviour :gen_server
|
||||
|
||||
@doc """
|
||||
Returns all remotes referenced in this lexical scope.
|
||||
Returns all remotes linked to in this lexical scope.
|
||||
"""
|
||||
def remote_references(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_references, @timeout)
|
||||
def remotes(arg) do
|
||||
:gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
|> :ets.match({{:mode, :'$1'}, :'$2'})
|
||||
|> partition([], [])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all remote dispatches in this lexical scope.
|
||||
"""
|
||||
def remote_dispatches(arg) do
|
||||
:gen_server.call(to_pid(arg), :remote_dispatches, @timeout)
|
||||
end
|
||||
defp partition([[remote, :compile]|t], compile, runtime),
|
||||
do: partition(t, [remote|compile], runtime)
|
||||
defp partition([[remote, :runtime]|t], compile, runtime),
|
||||
do: partition(t, compile, [remote|runtime])
|
||||
defp partition([], compile, runtime),
|
||||
do: {compile, runtime}
|
||||
|
||||
@doc """
|
||||
Gets the destination the lexical scope is meant to
|
||||
@@ -51,8 +56,8 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def add_import(pid, module, fas, line, warn) do
|
||||
:gen_server.cast(pid, {:add_import, module, fas, line, warn})
|
||||
def add_import(pid, module, line, warn) do
|
||||
:gen_server.cast(pid, {:add_import, module, line, warn})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -61,18 +66,13 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_reference(pid, module, mode) do
|
||||
:gen_server.cast(pid, {:remote_reference, module, mode})
|
||||
def remote_dispatch(pid, module, mode) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, fa, line, mode) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, fa, line, mode})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def import_dispatch(pid, module, fa, line, mode) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module, fa, line, mode})
|
||||
def import_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:import_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -91,87 +91,61 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
defp unused(pid, tag) do
|
||||
:gen_server.call(pid, {:unused, tag}, @timeout)
|
||||
:gen_server.call(pid, :ets, @timeout)
|
||||
|> :ets.select([{{{tag, :"$1"}, :"$2"}, [is_integer: :"$2"], [{{:"$1", :"$2"}}]}])
|
||||
|> Enum.sort
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init(dest) do
|
||||
{:ok, %{directives: %{}, references: %{}, compile: %{},
|
||||
runtime: %{}, dest: dest}}
|
||||
{:ok, {:ets.new(__MODULE__, [:protected]), dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call({:unused, tag}, _from, state) do
|
||||
directives =
|
||||
for {{^tag, module_or_mfa}, marker} <- state.directives,
|
||||
is_integer(marker),
|
||||
do: {module_or_mfa, marker}
|
||||
|
||||
{:reply, Enum.sort(directives), state}
|
||||
def handle_call(:ets, _from, {d, dest}) do
|
||||
{:reply, d, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:remote_references, _from, state) do
|
||||
{:reply, partition(Enum.to_list(state.references), [], []), state}
|
||||
def handle_call(:dest, _from, {d, dest}) do
|
||||
{:reply, dest, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:remote_dispatches, _from, state) do
|
||||
{:reply, {state.compile, state.runtime}, state}
|
||||
def handle_cast({:remote_dispatch, module, mode}, {d, dest}) do
|
||||
add_compile(d, module, mode)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_call(:dest, _from, state) do
|
||||
{:reply, state.dest, state}
|
||||
def handle_cast({:import_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :import)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
add_compile(d, module, :compile)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_reference, module, mode}, state) do
|
||||
{:noreply, %{state | references: add_reference(state.references, module, mode)}}
|
||||
def handle_cast({:alias_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, fa, line, mode}, state) do
|
||||
references = add_reference(state.references, module, mode)
|
||||
state = add_remote_dispatch(state, module, fa, line, mode)
|
||||
{:noreply, %{state | references: references}}
|
||||
def handle_cast({:add_import, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:import_dispatch, module, {function, arity} = fa, line, mode}, state) do
|
||||
state =
|
||||
state
|
||||
|> add_import_dispatch(module, function, arity)
|
||||
|> add_remote_dispatch(module, fa, line, mode)
|
||||
|
||||
{:noreply, state}
|
||||
def handle_cast({:add_alias, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:alias_dispatch, module}, state) do
|
||||
{:noreply, %{state | directives: add_dispatch(state.directives, module, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, fas, line, warn}, state) when is_atom(module) do
|
||||
directives =
|
||||
state.directives
|
||||
|> Enum.reject(&match?({{:import, {^module, _, _}}, _}, &1))
|
||||
|> :maps.from_list
|
||||
|> add_directive(module, line, warn, :import)
|
||||
|
||||
directives =
|
||||
Enum.reduce(fas, directives, fn {function, arity}, directives ->
|
||||
add_directive(directives, {module, function, arity}, line, warn, :import)
|
||||
end)
|
||||
|
||||
{:noreply, %{state | directives: directives}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_alias, module, line, warn}, state) do
|
||||
{:noreply, %{state | directives: add_directive(state.directives, module, line, warn, :alias)}}
|
||||
end
|
||||
|
||||
def handle_cast(:stop, state) do
|
||||
{:stop, :normal, state}
|
||||
def handle_cast(:stop, {d, dest}) do
|
||||
{:stop, :normal, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
def handle_info(_msg, {d, dest}) do
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -184,62 +158,21 @@ defmodule Kernel.LexicalTracker do
|
||||
{:ok, state}
|
||||
end
|
||||
|
||||
defp partition([{remote, :compile} | t], compile, runtime),
|
||||
do: partition(t, [remote | compile], runtime)
|
||||
defp partition([{remote, :runtime} | t], compile, runtime),
|
||||
do: partition(t, compile, [remote | runtime])
|
||||
defp partition([], compile, runtime),
|
||||
do: {compile, runtime}
|
||||
|
||||
# Callbacks helpers
|
||||
|
||||
defp add_reference(references, module, :runtime) when is_atom(module),
|
||||
do: map_put_new(module, :runtime, references)
|
||||
defp add_reference(references, module, :compile) when is_atom(module),
|
||||
do: :maps.put(module, :compile, references)
|
||||
|
||||
defp add_remote_dispatch(state, module, fa, line, mode) when is_atom(module) do
|
||||
map_update mode, %{module => %{fa => [line]}}, state, fn mode_dispatches ->
|
||||
map_update module, %{fa => [line]}, mode_dispatches, fn module_dispatches ->
|
||||
map_update fa, [line], module_dispatches, &[line | List.delete(&1, line)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp add_import_dispatch(state, module, function, arity) do
|
||||
directives =
|
||||
add_dispatch(state.directives, module, :import)
|
||||
|> add_dispatch({module, function, arity}, :import)
|
||||
# Always compile time because we depend
|
||||
# on the module at compile time
|
||||
references = add_reference(state.references, module, :compile)
|
||||
|
||||
%{state | directives: directives, references: references}
|
||||
end
|
||||
|
||||
# In the map we keep imports and aliases.
|
||||
# In the table we keep imports and aliases.
|
||||
# If the value is false, it was not imported/aliased
|
||||
# If the value is a line, it was imported/aliased and has a pending warning
|
||||
# If the value is true, it was imported/aliased and used
|
||||
defp add_directive(directives, module_or_mfa, line, warn, tag) do
|
||||
defp add_dispatch(d, module, tag) do
|
||||
:ets.insert(d, {{tag, module}, true})
|
||||
end
|
||||
|
||||
defp add_compile(d, module, :runtime), do: :ets.insert_new(d, {{:mode, module}, :runtime})
|
||||
defp add_compile(d, module, :compile), do: :ets.insert(d, {{:mode, module}, :compile})
|
||||
|
||||
defp add_directive(d, module, line, warn, tag) do
|
||||
marker = if warn, do: line, else: true
|
||||
:maps.put({tag, module_or_mfa}, marker, directives)
|
||||
end
|
||||
|
||||
defp add_dispatch(directives, module_or_mfa, tag) do
|
||||
:maps.put({tag, module_or_mfa}, true, directives)
|
||||
end
|
||||
|
||||
defp map_update(key, initial, map, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, val} -> :maps.put(key, fun.(val), map)
|
||||
:error -> :maps.put(key, initial, map)
|
||||
end
|
||||
end
|
||||
|
||||
defp map_put_new(key, value, map) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, _} -> map
|
||||
:error -> :maps.put(key, value, map)
|
||||
end
|
||||
:ets.insert(d, {{tag, module}, marker})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,13 +20,6 @@ defmodule Kernel.ParallelCompiler do
|
||||
* `:each_file` - for each file compiled, invokes the callback passing the
|
||||
file
|
||||
|
||||
* `:each_long_compilation` - for each file that takes more than a given
|
||||
timeout (see the `:long_compilation_threshold` option) to compile, invoke
|
||||
this callback passing the file as its argument
|
||||
|
||||
* `:long_compilation_threshold` - the timeout (in seconds) after the
|
||||
`:each_long_compilation` callback is invoked; defaults to `10`
|
||||
|
||||
* `:each_module` - for each module compiled, invokes the callback passing
|
||||
the file, module and the module bytecode
|
||||
|
||||
@@ -59,21 +52,12 @@ defmodule Kernel.ParallelCompiler do
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
|
||||
result = spawn_compilers(%{
|
||||
entries: files,
|
||||
original: files,
|
||||
output: path,
|
||||
options: options,
|
||||
waiting: [],
|
||||
queued: [],
|
||||
schedulers: schedulers,
|
||||
result: [],
|
||||
})
|
||||
result = spawn_compilers(files, files, path, options, [], [], schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
# compilation status will be set to error and we fail with CompileError
|
||||
case :elixir_code_server.call({:compilation_status, compiler_pid}) do
|
||||
:ok ->
|
||||
:ok ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
@@ -81,113 +65,81 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
# We already have n=schedulers currently running, don't spawn new ones
|
||||
defp spawn_compilers(%{queued: queued, waiting: waiting, schedulers: schedulers} = state)
|
||||
when length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(state)
|
||||
# We already have 4 currently running, don't spawn new ones
|
||||
defp spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result) when
|
||||
length(queued) - length(waiting) >= schedulers do
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# Release waiting processes
|
||||
defp spawn_compilers(%{entries: [{ref, found} | t], waiting: waiting} = state) do
|
||||
waiting =
|
||||
case List.keytake(waiting, ref, 2) do
|
||||
{{_kind, pid, ^ref, _on, _defining}, waiting} ->
|
||||
send pid, {ref, found}
|
||||
waiting
|
||||
nil ->
|
||||
waiting
|
||||
end
|
||||
spawn_compilers(%{state | entries: t, waiting: waiting})
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) when is_pid(h) do
|
||||
{_kind, ^h, ref, _module} = List.keyfind(waiting, h, 1)
|
||||
send h, {ref, :ready}
|
||||
waiting = List.keydelete(waiting, h, 1)
|
||||
spawn_compilers(t, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_compilers(%{entries: [file | files], queued: queued, output: output, options: options} = state) do
|
||||
# Spawn a compiler for each file in the list until we reach the limit
|
||||
defp spawn_compilers([h|t], original, output, options, waiting, queued, schedulers, result) do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
:erlang.spawn_monitor fn ->
|
||||
# Notify Code.ensure_compiled/2 that we should
|
||||
# attempt to compile the module by doing a dispatch.
|
||||
:erlang.put(:elixir_ensure_compiled, true)
|
||||
|
||||
# Set the elixir_compiler_pid used by our custom Kernel.ErrorHandler.
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
|
||||
|
||||
exit(try do
|
||||
_ = if output do
|
||||
:elixir_compiler.file_to_path(file, output)
|
||||
:elixir_compiler.file_to_path(h, output)
|
||||
else
|
||||
:elixir_compiler.file(file, Keyword.get(options, :dest))
|
||||
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
||||
end
|
||||
{:shutdown, file}
|
||||
{:shutdown, h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
end
|
||||
|
||||
timeout = Keyword.get(options, :long_compilation_threshold, 10) * 1_000
|
||||
timer_ref = Process.send_after(self(), {:timed_out, pid}, timeout)
|
||||
|
||||
new_queued = [{pid, ref, file, timer_ref} | queued]
|
||||
spawn_compilers(%{state | entries: files, queued: new_queued})
|
||||
spawn_compilers(t, original, output, options, waiting,
|
||||
[{pid, ref, h}|queued], schedulers, result)
|
||||
end
|
||||
|
||||
# No more files, nothing waiting, queue is empty, we are done
|
||||
defp spawn_compilers(%{entries: [], waiting: [], queued: [], result: result}) do
|
||||
defp spawn_compilers([], _original, _output, _options, [], [], _schedulers, result) do
|
||||
for {:module, mod} <- result, do: mod
|
||||
end
|
||||
|
||||
# Queued x, waiting for x: POSSIBLE ERROR! Release processes so we get the failures
|
||||
defp spawn_compilers(%{entries: [], waiting: waiting, queued: queued} = state) when length(waiting) == length(queued) do
|
||||
entries = for {pid, _, _, _} <- queued,
|
||||
entry = waiting_on_without_definition(waiting, pid),
|
||||
{_, _, ref, on, _} = entry,
|
||||
do: {on, {ref, :not_found}}
|
||||
|
||||
# Instead of releasing all files at once, we release them in groups
|
||||
# based on the module they are waiting on. We pick the module being
|
||||
# depended on with less edges, as it is the mostly likely source of
|
||||
# error (for example, someone made a type). This may not always be
|
||||
# true though: for example, if there is a macro injecting code into
|
||||
# multiple modules and such code becomes faulty, now multiple modules
|
||||
# are waiting on the same module required by the faulty code. However,
|
||||
# since we need to pick something to be first, the one with fewer edges
|
||||
# sounds like a sane choice.
|
||||
entries =
|
||||
entries
|
||||
|> Enum.group_by(&elem(&1, 0), &elem(&1, 1))
|
||||
|> Enum.sort_by(&length(elem(&1, 1)))
|
||||
|> Enum.find_value([], &elem(&1, 1))
|
||||
|
||||
case entries do
|
||||
[] -> handle_deadlock(waiting, queued)
|
||||
_ -> spawn_compilers(%{state | entries: entries})
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) when length(waiting) == length(queued) do
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
{_kind, ^child, ref, _module} = List.keyfind(waiting, child, 1)
|
||||
send child, {ref, :release}
|
||||
end
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# No more files, but queue and waiting are not full or do not match
|
||||
defp spawn_compilers(%{entries: []} = state) do
|
||||
wait_for_messages(state)
|
||||
end
|
||||
|
||||
defp waiting_on_without_definition(waiting, pid) do
|
||||
{_, ^pid, _, on, _} = entry = List.keyfind(waiting, pid, 1)
|
||||
if Enum.any?(waiting, fn {_, _, _, _, defining} -> on in defining end) do
|
||||
nil
|
||||
else
|
||||
entry
|
||||
end
|
||||
defp spawn_compilers([], original, output, options, waiting, queued, schedulers, result) do
|
||||
wait_for_messages([], original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
|
||||
# Wait for messages from child processes
|
||||
defp wait_for_messages(state) do
|
||||
%{entries: entries, options: options, waiting: waiting, queued: queued, result: result} = state
|
||||
|
||||
defp wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result) do
|
||||
receive do
|
||||
{:struct_available, module} ->
|
||||
available = for {:struct, _, ref, waiting_module, _defining} <- waiting,
|
||||
available = for {:struct, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
spawn_compilers(%{state | entries: available ++ entries, result: [{:struct, module} | result]})
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:struct, module}|result])
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
if callback = Keyword.get(options, :each_module) do
|
||||
@@ -197,104 +149,63 @@ defmodule Kernel.ParallelCompiler do
|
||||
# Release the module loader which is waiting for an ack
|
||||
send child, {ref, :ack}
|
||||
|
||||
available = for {:module, _, ref, waiting_module, _defining} <- waiting,
|
||||
available = for {_kind, pid, _, waiting_module} <- waiting,
|
||||
module == waiting_module,
|
||||
do: {ref, :found}
|
||||
not pid in entries,
|
||||
do: pid
|
||||
|
||||
cancel_waiting_timer(queued, child)
|
||||
spawn_compilers(available ++ entries, original, output, options,
|
||||
waiting, queued, schedulers, [{:module, module}|result])
|
||||
|
||||
spawn_compilers(%{state | entries: available ++ entries, result: [{:module, module} | result]})
|
||||
{:waiting, kind, child, ref, on} ->
|
||||
defined = fn {k, m} -> on == m and k in [kind, :module] end
|
||||
|
||||
{:waiting, kind, child, ref, on, defining} ->
|
||||
# Oops, we already got it, do not put it on waiting.
|
||||
# OR
|
||||
# We're waiting on ourselves, send :found so that we can crash with a better error
|
||||
waiting =
|
||||
if :lists.any(&match?({^kind, ^on}, &1), result) or on in defining do
|
||||
send child, {ref, :found}
|
||||
waiting
|
||||
else
|
||||
[{kind, child, ref, on, defining} | waiting]
|
||||
end
|
||||
|
||||
spawn_compilers(%{state | waiting: waiting})
|
||||
|
||||
{:timed_out, child} ->
|
||||
callback = Keyword.get(options, :each_long_compilation)
|
||||
case List.keyfind(queued, child, 0) do
|
||||
{^child, _, file, _} when not is_nil(callback) ->
|
||||
callback.(file)
|
||||
_ ->
|
||||
:ok
|
||||
if :lists.any(defined, result) do
|
||||
send child, {ref, :ready}
|
||||
else
|
||||
waiting = [{kind, child, ref, on}|waiting]
|
||||
end
|
||||
spawn_compilers(state)
|
||||
|
||||
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
|
||||
{:DOWN, _down_ref, :process, down_pid, {:shutdown, file}} ->
|
||||
if callback = Keyword.get(options, :each_file) do
|
||||
callback.(file)
|
||||
end
|
||||
|
||||
cancel_waiting_timer(queued, down_pid)
|
||||
|
||||
# Sometimes we may have spurious entries in the waiting
|
||||
# list because someone invoked try/rescue UndefinedFunctionError
|
||||
new_entries = List.delete(entries, down_pid)
|
||||
new_queued = List.keydelete(queued, down_pid, 0)
|
||||
new_waiting = List.keydelete(waiting, down_pid, 1)
|
||||
spawn_compilers(%{state | entries: new_entries, waiting: new_waiting, queued: new_queued})
|
||||
spawn_compilers(new_entries, original, output, options, new_waiting, new_queued, schedulers, result)
|
||||
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
handle_failure(down_ref, reason, queued)
|
||||
wait_for_messages(state)
|
||||
handle_failure(down_ref, reason, entries, waiting, queued)
|
||||
wait_for_messages(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
end
|
||||
end
|
||||
|
||||
defp handle_deadlock(waiting, queued) do
|
||||
deadlock =
|
||||
for {pid, _, file, _} <- queued do
|
||||
{:current_stacktrace, stacktrace} = Process.info(pid, :current_stacktrace)
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
{_kind, ^pid, _, on, _} = List.keyfind(waiting, pid, 1)
|
||||
error = CompileError.exception(description: "deadlocked waiting on module #{inspect on}",
|
||||
file: nil, line: nil)
|
||||
print_failure(file, {:failure, :error, error, stacktrace})
|
||||
|
||||
{file, on}
|
||||
end
|
||||
|
||||
IO.puts """
|
||||
|
||||
Compilation failed because of a deadlock between files.
|
||||
The following files depended on the following modules:
|
||||
"""
|
||||
|
||||
max =
|
||||
deadlock
|
||||
|> Enum.map(& &1 |> elem(0) |> String.length)
|
||||
|> Enum.max
|
||||
|
||||
for {file, mod} <- deadlock do
|
||||
IO.puts [" ", String.pad_leading(file, max), " => " | inspect(mod)]
|
||||
end
|
||||
|
||||
IO.puts ""
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
|
||||
defp handle_failure(ref, reason, queued) do
|
||||
defp handle_failure(ref, reason, entries, waiting, queued) do
|
||||
if file = find_failure(ref, queued) do
|
||||
print_failure(file, reason)
|
||||
for {pid, _, _, _} <- queued do
|
||||
Process.exit(pid, :kill)
|
||||
|
||||
if all_missing?(entries, waiting, queued) do
|
||||
collect_failures(queued, length(queued) - 1)
|
||||
end
|
||||
|
||||
Enum.each queued, fn {child, _, _} ->
|
||||
Process.exit(child, :kill)
|
||||
end
|
||||
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
end
|
||||
|
||||
defp find_failure(ref, queued) do
|
||||
case List.keyfind(queued, ref, 1) do
|
||||
{_child, ^ref, file, _timer_ref} -> file
|
||||
{_child, ^ref, file} -> file
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
@@ -315,33 +226,39 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def, :elixir_map, Kernel.ErrorHandler]
|
||||
:elixir_def]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _} | t]) when mod in @elixir_internals do
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
end
|
||||
|
||||
defp prune_stacktrace([h | t]) do
|
||||
[h | prune_stacktrace(t)]
|
||||
defp prune_stacktrace([h|t]) do
|
||||
[h|prune_stacktrace(t)]
|
||||
end
|
||||
|
||||
defp prune_stacktrace([]) do
|
||||
[]
|
||||
end
|
||||
|
||||
defp cancel_waiting_timer(queued, child_pid) do
|
||||
case List.keyfind(queued, child_pid, 0) do
|
||||
{^child_pid, _ref, _file, timer_ref} ->
|
||||
Process.cancel_timer(timer_ref)
|
||||
# Let's flush the message in case it arrived before we canceled the
|
||||
# timeout.
|
||||
receive do
|
||||
{:timed_out, ^child_pid} -> :ok
|
||||
after
|
||||
0 -> :ok
|
||||
defp all_missing?(entries, waiting, queued) do
|
||||
entries == [] and waiting != [] and
|
||||
length(waiting) == length(queued)
|
||||
end
|
||||
|
||||
defp collect_failures(_queued, 0), do: :ok
|
||||
|
||||
defp collect_failures(queued, remaining) do
|
||||
receive do
|
||||
{:DOWN, down_ref, :process, _down_pid, reason} ->
|
||||
if file = find_failure(down_ref, queued) do
|
||||
print_failure(file, reason)
|
||||
collect_failures(queued, remaining - 1)
|
||||
else
|
||||
collect_failures(queued, remaining)
|
||||
end
|
||||
nil ->
|
||||
:ok
|
||||
after
|
||||
# Give up if no failure appears in 5 seconds
|
||||
5000 -> :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -3,107 +3,78 @@ defmodule Kernel.ParallelRequire do
|
||||
A module responsible for requiring files in parallel.
|
||||
"""
|
||||
|
||||
defmacrop default_callback, do: quote(do: fn x -> x end)
|
||||
|
||||
@doc """
|
||||
Requires the given files.
|
||||
|
||||
A callback that will be invoked with each file, or a keyword list of `callbacks` can be provided:
|
||||
|
||||
* `:each_file` - invoked with each file
|
||||
|
||||
* `:each_module` - invoked with file, module name, and binary code
|
||||
A callback that is invoked every time a file is required
|
||||
can be optionally given as argument.
|
||||
|
||||
Returns the modules generated by each required file.
|
||||
"""
|
||||
def files(files, callbacks \\ [])
|
||||
|
||||
def files(files, callback) when is_function(callback, 1) do
|
||||
files(files, [each_file: callback])
|
||||
end
|
||||
|
||||
def files(files, callbacks) when is_list(callbacks) do
|
||||
compiler_pid = self()
|
||||
:elixir_code_server.cast({:reset_warnings, compiler_pid})
|
||||
def files(files, callback \\ default_callback) do
|
||||
schedulers = max(:erlang.system_info(:schedulers_online), 2)
|
||||
result = spawn_requires(files, [], callbacks, schedulers, [])
|
||||
|
||||
# In case --warning-as-errors is enabled and there was a warning,
|
||||
# compilation status will be set to error.
|
||||
case :elixir_code_server.call({:compilation_status, compiler_pid}) do
|
||||
:ok ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "\nExecution failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
spawn_requires(files, [], callback, schedulers, [])
|
||||
end
|
||||
|
||||
defp spawn_requires([], [], _callbacks, _schedulers, result), do: result
|
||||
defp spawn_requires([], [], _callback, _schedulers, result), do: result
|
||||
|
||||
defp spawn_requires([], waiting, callbacks, schedulers, result) do
|
||||
wait_for_messages([], waiting, callbacks, schedulers, result)
|
||||
defp spawn_requires([], waiting, callback, schedulers, result) do
|
||||
wait_for_messages([], waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires(files, waiting, callbacks, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callbacks, schedulers, result)
|
||||
defp spawn_requires(files, waiting, callback, schedulers, result) when length(waiting) >= schedulers do
|
||||
wait_for_messages(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp spawn_requires([file | files], waiting, callbacks, schedulers, result) do
|
||||
parent = self()
|
||||
defp spawn_requires([h|t], waiting, callback, schedulers, result) do
|
||||
parent = self
|
||||
|
||||
compiler_pid = :erlang.get(:elixir_compiler_pid)
|
||||
ensure_compiled = :erlang.get(:elixir_ensure_compiled)
|
||||
{:error_handler, handler} = :erlang.process_info(parent, :error_handler)
|
||||
|
||||
{pid, ref} = :erlang.spawn_monitor fn ->
|
||||
:erlang.put(:elixir_compiler_pid, parent)
|
||||
:erlang.put(:elixir_compiler_file, file)
|
||||
if compiler_pid != :undefined do
|
||||
:erlang.put(:elixir_compiler_pid, compiler_pid)
|
||||
end
|
||||
|
||||
if ensure_compiled != :undefined do
|
||||
:erlang.put(:elixir_ensure_compiled, ensure_compiled)
|
||||
end
|
||||
|
||||
:erlang.process_flag(:error_handler, handler)
|
||||
|
||||
exit(try do
|
||||
new = Code.require_file(file) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), file}
|
||||
new = Code.require_file(h) || []
|
||||
{:required, Enum.map(new, &elem(&1, 0)), h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
end)
|
||||
end
|
||||
|
||||
spawn_requires(files, [{pid, ref} | waiting], callbacks, schedulers, result)
|
||||
spawn_requires(t, [{pid, ref}|waiting], callback, schedulers, result)
|
||||
end
|
||||
|
||||
defp wait_for_messages(files, waiting, callbacks, schedulers, result) do
|
||||
defp wait_for_messages(files, waiting, callback, schedulers, result) do
|
||||
receive do
|
||||
{:DOWN, ref, :process, pid, status} ->
|
||||
tuple = {pid, ref}
|
||||
if tuple in waiting do
|
||||
waiting = List.delete(waiting, tuple)
|
||||
|
||||
case status do
|
||||
{:required, mods, file} ->
|
||||
if each_file_callback = callbacks[:each_file] do
|
||||
each_file_callback.(file)
|
||||
end
|
||||
|
||||
spawn_requires(files, waiting, callbacks, schedulers, mods ++ result)
|
||||
|
||||
callback.(file)
|
||||
result = mods ++ result
|
||||
waiting = List.delete(waiting, tuple)
|
||||
{:failure, kind, reason, stacktrace} ->
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
|
||||
other ->
|
||||
:erlang.raise(:exit, other, [])
|
||||
end
|
||||
else
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
end
|
||||
|
||||
{:module_available, child, ref, file, module, binary} ->
|
||||
if each_module_callback = callbacks[:each_module] do
|
||||
each_module_callback.(file, module, binary)
|
||||
end
|
||||
|
||||
send(child, {ref, :ack})
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
|
||||
{:struct_available, _} ->
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
|
||||
{:waiting, _, child, ref, _, _} ->
|
||||
send(child, {ref, :not_found})
|
||||
spawn_requires(files, waiting, callbacks, schedulers, result)
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
+263
-146
@@ -1,5 +1,173 @@
|
||||
defmodule Kernel.Typespec do
|
||||
@moduledoc false
|
||||
@moduledoc ~S"""
|
||||
Provides macros and functions for working with typespecs.
|
||||
|
||||
Elixir comes with a notation for declaring types and specifications. Elixir is
|
||||
dynamically typed, as such typespecs are never used by the compiler to
|
||||
optimize or modify code. Still, using typespecs is useful as documentation and
|
||||
tools such as [Dialyzer](http://www.erlang.org/doc/man/dialyzer.html) can
|
||||
analyze the code with typespecs to find bugs.
|
||||
|
||||
The attributes `@type`, `@opaque`, `@typep`, `@spec`, `@callback` and
|
||||
`@macrocallback` available in modules are handled by the equivalent macros
|
||||
defined by this module. See sub-sections "Defining a type" and "Defining a
|
||||
specification" below.
|
||||
|
||||
## Types and their syntax
|
||||
|
||||
The type syntax provided by Elixir is fairly similar to [the one in
|
||||
Erlang](http://www.erlang.org/doc/reference_manual/typespec.html).
|
||||
|
||||
Most of the built-in types provided in Erlang (for example, `pid()`) are
|
||||
expressed the same way: `pid()` or simply `pid`. Parameterized types are also
|
||||
supported (`list(integer)`) and so are remote types (`Enum.t`).
|
||||
|
||||
Integers and atom literals are allowed as types (ex. `1`, `:atom` or
|
||||
`false`). All other types are built of unions of predefined types. Certain
|
||||
shorthands are allowed, such as `[...]`, `<<>>` and `{...}`.
|
||||
|
||||
### Basic types
|
||||
|
||||
type :: any() # the top type, the set of all terms
|
||||
| none() # the bottom type, contains no terms
|
||||
| pid()
|
||||
| port()
|
||||
| reference()
|
||||
| tuple()
|
||||
| atom()
|
||||
| integer()
|
||||
| non_neg_integer() # 0, 1, 2, 3, ...
|
||||
| pos_integer() # 1, 2, 3, ...
|
||||
| neg_integer() # ..., -3, -2, -1
|
||||
| float()
|
||||
| map()
|
||||
| struct()
|
||||
| list(type)
|
||||
| nonempty_list(type)
|
||||
| improper_list(type1, type2)
|
||||
| maybe_improper_list(type1, type2)
|
||||
| Literals # Described in section "Literals"
|
||||
| Builtin # Described in section "Builtin-types"
|
||||
| Remotes # Described in section "Remotes"
|
||||
|
||||
### Literals
|
||||
|
||||
The following literals are also supported in typespecs:
|
||||
|
||||
type :: :atom ## Atoms
|
||||
| 1 ## Integers
|
||||
| 1..10 ## Integers from 1 to 10
|
||||
| 1.0 ## Floats
|
||||
|
||||
| <<>> ## Bitstrings
|
||||
| <<_ :: size>> # size is 0 or a positive integer
|
||||
| <<_ :: _ * unit>> # unit is an integer from 1 to 256
|
||||
| <<_ :: size * unit>>
|
||||
|
||||
| [type] ## Lists
|
||||
| [] # empty list
|
||||
| [...] # shorthand for nonempty_list(any())
|
||||
| [type, ...] # shorthand for nonempty_list(type)
|
||||
| [key: type] # keyword lists
|
||||
|
||||
| (... -> type) ## Functions
|
||||
| (... -> type) # any arity, returns type
|
||||
| (() -> type) # 0-arity, returns type
|
||||
| (type1, type2 -> type) # 2-arity, returns type
|
||||
|
||||
| %{} ## Maps
|
||||
| %{key: type} # map with key :key with value of type
|
||||
| %{type1 => type2} # map with keys of type1 with values of type2
|
||||
| %SomeStruct{}
|
||||
| %SomeStruct{key: type}
|
||||
|
||||
| {} ## Tuples
|
||||
| {:ok, type} # two element tuple with an atom and any type
|
||||
|
||||
### Built-in types
|
||||
|
||||
Those types are also provided by Elixir as shortcuts on top of the
|
||||
basic and literal types.
|
||||
|
||||
Built-in type | Defined as
|
||||
:---------------------- | :---------
|
||||
`term()` | `any()`
|
||||
`binary()` | `<< _ :: _ * 8 >>`
|
||||
`bitstring()` | `<< _ :: _ * 1 >>`
|
||||
`boolean()` | `false` \| `true`
|
||||
`byte()` | `0..255`
|
||||
`char()` | `0..0x10ffff`
|
||||
`number()` | `integer()` \| `float()`
|
||||
`char_list()` | `[char()]`
|
||||
`list()` | `[any()]`
|
||||
`maybe_improper_list()` | `maybe_improper_list(any(), any())`
|
||||
`nonempty_list()` | `nonempty_list(any())`
|
||||
`iodata()` | `iolist()` \| `binary()`
|
||||
`iolist()` | `maybe_improper_list(byte()` \| `binary()` \| `iolist(), binary()` \| `[])`
|
||||
`module()` | `atom()` \| `tuple()`
|
||||
`arity()` | `0..255`
|
||||
`mfa()` | `{atom(), atom(), arity()}`
|
||||
`node()` | `atom()`
|
||||
`timeout()` | `:infinity` \| `non_neg_integer()`
|
||||
`no_return()` | `none()`
|
||||
`fun()` | `(... -> any)`
|
||||
`struct()` | `%{__struct__: atom()}`
|
||||
|
||||
### Remote types
|
||||
|
||||
Any module is also able to define their own type and the modules in
|
||||
Elixir are no exception. For example, a string is `String.t`, a
|
||||
range is `Range.t`, any enumerable can be `Enum.t` and so on.
|
||||
|
||||
## Defining a type
|
||||
|
||||
@type type_name :: type
|
||||
@typep type_name :: type
|
||||
@opaque type_name :: type
|
||||
|
||||
A type defined with `@typep` is private. An opaque type, defined with
|
||||
`@opaque` is a type where the internal structure of the type will not be
|
||||
visible, but the type is still public.
|
||||
|
||||
Types can be parameterized by defining variables as parameters, these variables
|
||||
can then be used to define the type.
|
||||
|
||||
@type dict(key, value) :: [{key, value}]
|
||||
|
||||
## Defining a specification
|
||||
|
||||
@spec function_name(type1, type2) :: return_type
|
||||
@callback function_name(type1, type2) :: return_type
|
||||
@macrocallback macro_name(type1, type2) :: Macro.t
|
||||
|
||||
Callbacks are used to define the callbacks functions of behaviours (see
|
||||
`Behaviour`).
|
||||
|
||||
Guards can be used to restrict type variables given as arguments to the
|
||||
function.
|
||||
|
||||
@spec function(arg) :: [arg] when arg: atom
|
||||
|
||||
Type variables with no restriction can also be defined.
|
||||
|
||||
@spec function(arg) :: [arg] when arg: var
|
||||
|
||||
Specifications can be overloaded just like ordinary functions.
|
||||
|
||||
@spec function(integer) :: atom
|
||||
@spec function(atom) :: integer
|
||||
|
||||
## Notes
|
||||
|
||||
Elixir discourages the use of type `string` as it might be confused with
|
||||
binaries which are referred to as "strings" in Elixir (as opposed to character
|
||||
lists). In order to use the type that is called `string` in Erlang, one has to
|
||||
use the `char_list` type which is a synonym for `string`. If you use `string`,
|
||||
you'll get a warning from the compiler.
|
||||
|
||||
If you want to refer to the "string" type (the one operated on by functions in
|
||||
the `String` module), use `String.t` type instead.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Defines a type.
|
||||
@@ -92,12 +260,6 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro defoptional_callbacks(callbacks) do
|
||||
quote do
|
||||
Module.store_typespec(__ENV__.module, :optional_callbacks, {__ENV__.line, unquote(callbacks)})
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a `type`, `typep` or `opaque` by receiving a typespec expression.
|
||||
"""
|
||||
@@ -202,7 +364,7 @@ defmodule Kernel.Typespec do
|
||||
def type_to_ast({{:record, record}, fields, args}) when is_atom(record) do
|
||||
fields = for field <- fields, do: typespec_to_ast(field)
|
||||
args = for arg <- args, do: typespec_to_ast(arg)
|
||||
type = {:{}, [], [record | fields]}
|
||||
type = {:{}, [], [record|fields]}
|
||||
quote do: unquote(record)(unquote_splicing(args)) :: unquote(type)
|
||||
end
|
||||
|
||||
@@ -211,11 +373,10 @@ defmodule Kernel.Typespec do
|
||||
quote do: unquote(name)(unquote_splicing(args)) :: unquote(typespec_to_ast(type))
|
||||
end
|
||||
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
@doc false
|
||||
# TODO: Remove on v2.0
|
||||
def beam_typedocs(module) when is_atom(module) or is_binary(module) do
|
||||
IO.write :stderr, "Kernel.Typespec.beam_typedocs/1 is deprecated, please use Code.get_docs/2 instead\n" <>
|
||||
Exception.format_stacktrace
|
||||
if docs = Code.get_docs(module, :type_docs) do
|
||||
for {tuple, _, _, doc} <- docs, do: {tuple, doc}
|
||||
end
|
||||
@@ -347,9 +508,13 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp get_doc_info(table, attr, caller) do
|
||||
case :ets.take(table, attr) do
|
||||
[{^attr, {line, doc}}] -> {line, doc}
|
||||
[] -> {caller.line, nil}
|
||||
# TODO: Use :ets.take/2 with Erlang 18
|
||||
case :ets.lookup(table, attr) do
|
||||
[{^attr, {line, doc}}] ->
|
||||
:ets.delete(table, attr)
|
||||
{line, doc}
|
||||
[] ->
|
||||
{caller.line, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -414,11 +579,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp elixir_builtin_type?(:as_boolean, 1), do: true
|
||||
defp elixir_builtin_type?(:struct, 0), do: true
|
||||
defp elixir_builtin_type?(:charlist, 0), do: true
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp elixir_builtin_type?(:char_list, 0), do: true
|
||||
defp elixir_builtin_type?(:keyword, 0), do: true
|
||||
defp elixir_builtin_type?(:keyword, 1), do: true
|
||||
defp elixir_builtin_type?(_, _), do: false
|
||||
|
||||
@doc false
|
||||
@@ -430,9 +591,32 @@ defmodule Kernel.Typespec do
|
||||
translate_spec(kind, spec, [], caller)
|
||||
end
|
||||
|
||||
defp translate_spec(kind, {:::, meta, [{name, _, args}, return]}, guard, caller)
|
||||
when is_atom(name) and name != ::: do
|
||||
translate_spec(kind, meta, name, args, return, guard, caller)
|
||||
defp translate_spec(kind, {:::, meta, [{name, _, args}, return]}, guard, caller) when is_atom(name) and name != ::: do
|
||||
if is_atom(args), do: args = []
|
||||
|
||||
if kind == :macrocallback do
|
||||
kind = :callback
|
||||
name = :"MACRO-#{name}"
|
||||
args = [quote(do: env :: Macro.Env.t)|args]
|
||||
end
|
||||
|
||||
ensure_no_defaults! args
|
||||
|
||||
unless Keyword.keyword?(guard) do
|
||||
guard = Macro.to_string(guard)
|
||||
compile_error caller, "expected keywords as guard in type specification, got: #{guard}"
|
||||
end
|
||||
|
||||
vars = Keyword.keys(guard)
|
||||
constraints = guard_to_constraints(guard, vars, meta, caller)
|
||||
|
||||
spec = {:type, line(meta), :fun, fn_args(meta, args, return, vars, caller)}
|
||||
if constraints != [] do
|
||||
spec = {:type, line(meta), :bounded_fun, [spec, constraints]}
|
||||
end
|
||||
|
||||
arity = length(args)
|
||||
{{kind, {name, arity}, spec}, caller.line}
|
||||
end
|
||||
|
||||
defp translate_spec(_kind, {name, _meta, _args} = spec, _guard, caller) when is_atom(name) and name != ::: do
|
||||
@@ -445,35 +629,6 @@ defmodule Kernel.Typespec do
|
||||
compile_error caller, "invalid type specification: #{spec}"
|
||||
end
|
||||
|
||||
defp translate_spec(kind, meta, name, args, return, guard, caller) when is_atom(args),
|
||||
do: translate_spec(kind, meta, name, [], return, guard, caller)
|
||||
defp translate_spec(:macrocallback, meta, name, args, return, guard, caller),
|
||||
do: translate_spec(:callback, meta, :"MACRO-#{name}", macro_args(args), return, guard, caller)
|
||||
defp translate_spec(kind, meta, name, args, return, guard, caller) do
|
||||
ensure_no_defaults!(args)
|
||||
|
||||
unless Keyword.keyword?(guard) do
|
||||
compile_error caller, "expected keywords as guard in type specification, " <>
|
||||
"got: #{Macro.to_string(guard)}"
|
||||
end
|
||||
|
||||
vars = Keyword.keys(guard)
|
||||
spec = {:type, line(meta), :fun, fn_args(meta, args, return, vars, caller)}
|
||||
|
||||
spec =
|
||||
case guard_to_constraints(guard, vars, meta, caller) do
|
||||
[] -> spec
|
||||
constraints -> {:type, line(meta), :bounded_fun, [spec, constraints]}
|
||||
end
|
||||
|
||||
arity = length(args)
|
||||
{{kind, {name, arity}, spec}, caller.line}
|
||||
end
|
||||
|
||||
defp macro_args(args) do
|
||||
[quote(do: {line :: Macro.Env.line, env :: Macro.Env.t}) | args]
|
||||
end
|
||||
|
||||
defp ensure_no_defaults!(args) do
|
||||
:lists.foreach fn
|
||||
{:::, _, [left, right]} ->
|
||||
@@ -501,7 +656,7 @@ defmodule Kernel.Typespec do
|
||||
{name, type}, acc ->
|
||||
constraint = [{:atom, line, :is_subtype}, [{:var, line, name}, typespec(type, vars, caller)]]
|
||||
type = {:type, line, :constraint, constraint}
|
||||
[type | acc]
|
||||
[type|acc]
|
||||
end, [], guard) |> :lists.reverse
|
||||
end
|
||||
|
||||
@@ -577,14 +732,12 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec_to_ast({:type, line, :map, fields}) do
|
||||
fields = Enum.map fields, fn
|
||||
{:type, _, :map_field_assoc, :any} ->
|
||||
{:..., [line: line], nil}
|
||||
{:type, _, :map_field_exact, [{:atom, _, k}, v]} ->
|
||||
{k, typespec_to_ast(v)}
|
||||
{:type, _, :map_field_exact, [k, v]} ->
|
||||
{{:required, [], [typespec_to_ast(k)]}, typespec_to_ast(v)}
|
||||
# OTP 18
|
||||
{:type, _, :map_field_assoc, [k, v]} ->
|
||||
{{:optional, [], [typespec_to_ast(k)]}, typespec_to_ast(v)}
|
||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||
# OTP 17
|
||||
{:type, _, :map_field_assoc, k, v} ->
|
||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||
end
|
||||
|
||||
{struct, fields} = Keyword.pop(fields, :__struct__)
|
||||
@@ -599,13 +752,13 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec_to_ast({:type, line, :binary, [arg1, arg2]}) do
|
||||
[arg1, arg2] = for arg <- [arg1, arg2], do: typespec_to_ast(arg)
|
||||
case {typespec_to_ast(arg1), typespec_to_ast(arg2)} do
|
||||
{arg1, 0} ->
|
||||
cond do
|
||||
arg2 == 0 ->
|
||||
quote line: line, do: <<_ :: unquote(arg1)>>
|
||||
{0, arg2} ->
|
||||
arg1 == 0 ->
|
||||
quote line: line, do: <<_ :: _ * unquote(arg2)>>
|
||||
{arg1, arg2} ->
|
||||
quote line: line, do: <<_ :: unquote(arg1), _ :: _ * unquote(arg2)>>
|
||||
true ->
|
||||
quote line: line, do: <<_ :: unquote(arg1) * unquote(arg2)>>
|
||||
end
|
||||
end
|
||||
|
||||
@@ -649,10 +802,8 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
# Special shortcut(s)
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, type}, []]})
|
||||
when type in [:charlist, :char_list] do
|
||||
typespec_to_ast({:type, line, :charlist, []})
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :char_list}, []]}) do
|
||||
typespec_to_ast({:type, line, :char_list, []})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :struct}, []]}) do
|
||||
@@ -663,10 +814,6 @@ defmodule Kernel.Typespec do
|
||||
typespec_to_ast({:type, line, :as_boolean, [arg]})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :keyword}, args]}) do
|
||||
typespec_to_ast({:type, line, :keyword, args})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [mod, name, args]}) do
|
||||
args = for arg <- args, do: typespec_to_ast(arg)
|
||||
dot = {:., [line: line], [typespec_to_ast(mod), typespec_to_ast(name)]}
|
||||
@@ -699,9 +846,9 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp erl_to_ex_var(var) do
|
||||
case Atom.to_string(var) do
|
||||
<<"_", c::binary-1, rest::binary>> ->
|
||||
<<"_", c :: binary-size(1), rest :: binary>> ->
|
||||
String.to_atom("_#{String.downcase(c)}#{rest}")
|
||||
<<c::binary-1, rest::binary>> ->
|
||||
<<c :: binary-size(1), rest :: binary>> ->
|
||||
String.to_atom("#{String.downcase(c)}#{rest}")
|
||||
end
|
||||
end
|
||||
@@ -732,53 +879,38 @@ defmodule Kernel.Typespec do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta), 0}, {:integer, line(unit_meta), unit}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, shared_meta, [{:_, _, ctx}, {:*, _, [size, unit]}]}]}, _, _)
|
||||
when is_atom(ctx) and is_integer(unit) and is_integer(size) do
|
||||
{:type, line(meta), :binary, [{:integer, line(shared_meta), size}, {:integer, line(shared_meta), unit}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, size_meta, [{:_, _, ctx}, size]}]}, _, _)
|
||||
when is_atom(ctx) and is_integer(size) do
|
||||
{:type, line(meta), :binary, [{:integer, line(size_meta), size}, {:integer, line(meta), 0}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, size_meta, [{:_, _, ctx1}, size]}, {:::, unit_meta, [{:_, _, ctx2}, {:*, _, [{:_, _, ctx3}, unit]}]}]}, _, _)
|
||||
when is_atom(ctx1) and is_atom(ctx2) and is_atom(ctx3) and is_integer(size) and is_integer(unit) do
|
||||
{:type, line(meta), :binary, [{:integer, line(size_meta), size}, {:integer, line(unit_meta), unit}]}
|
||||
end
|
||||
|
||||
## Handle maps and structs
|
||||
defp typespec({:map, meta, args}, _vars, _caller) when args == [] or is_atom(args) do
|
||||
{:type, line(meta), :map, :any}
|
||||
end
|
||||
|
||||
defp typespec({:%{}, meta, fields} = map, vars, caller) do
|
||||
defp typespec({:%{}, meta, fields}, vars, caller) do
|
||||
fields =
|
||||
:lists.map(fn
|
||||
:... ->
|
||||
{:type, line(meta), :map_field_assoc, :any}
|
||||
{k, v} when is_atom(k) ->
|
||||
{:type, line(meta), :map_field_exact, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{{:required, meta2, [k]}, v} ->
|
||||
{:type, line(meta2), :map_field_exact, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{{:optional, meta2, [k]}, v} ->
|
||||
{:type, line(meta2), :map_field_assoc, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{k, v} ->
|
||||
# TODO: Emit warnings on v1.5
|
||||
# :elixir_errors.warn(caller.line, caller.file,
|
||||
# "invalid map specification. %{foo => bar} is deprecated in favor of " <>
|
||||
# "%{required(foo) => bar} and %{optional(foo) => bar}. required/1 is an " <>
|
||||
# "OTP 19 only feature, if you are targeting OTP 18 use optional/1.")
|
||||
# TODO: Remove else once we support only OTP >18
|
||||
if :erlang.system_info(:otp_release) >= '18' do
|
||||
:lists.map(fn {k, v} ->
|
||||
{:type, line(meta), :map_field_assoc, [typespec(k, vars, caller), typespec(v, vars, caller)]}
|
||||
{:|, _, [_, _]} ->
|
||||
compile_error(caller,
|
||||
"invalid map specification. When using the | operator in the map key, " <>
|
||||
"make sure to wrap the key type in parentheses: #{Macro.to_string(map)}")
|
||||
_ ->
|
||||
compile_error(caller, "invalid map specification: #{Macro.to_string(map)}")
|
||||
end, fields)
|
||||
end, fields)
|
||||
else
|
||||
:lists.map(fn {k, v} ->
|
||||
{:type, line(meta), :map_field_assoc, typespec(k, vars, caller), typespec(v, vars, caller)}
|
||||
end, fields)
|
||||
end
|
||||
|
||||
{:type, line(meta), :map, fields}
|
||||
end
|
||||
|
||||
defp typespec({:%, _, [name, {:%{}, meta, fields}]}, vars, caller) do
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency, because for structs it actually is one.
|
||||
module = Macro.expand(name, caller)
|
||||
|
||||
struct =
|
||||
@@ -789,16 +921,14 @@ defmodule Kernel.Typespec do
|
||||
module.__struct__
|
||||
end
|
||||
|
||||
struct = struct |> Map.from_struct |> Map.to_list
|
||||
|
||||
unless Keyword.keyword?(fields) do
|
||||
compile_error(caller, "expected key-value pairs in struct #{Macro.to_string(name)}")
|
||||
end
|
||||
|
||||
types =
|
||||
struct =
|
||||
:lists.map(fn {field, _} ->
|
||||
{field, Keyword.get(fields, field, quote(do: term()))}
|
||||
end, struct)
|
||||
{field, quote do: term()}
|
||||
end, Map.to_list(struct))
|
||||
|
||||
:lists.foreach(fn {field, _} ->
|
||||
unless Keyword.has_key?(struct, field) do
|
||||
@@ -806,7 +936,8 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end, fields)
|
||||
|
||||
typespec({:%{}, meta, [__struct__: module] ++ types}, vars, caller)
|
||||
fields = Keyword.merge(struct, [__struct__: module] ++ fields)
|
||||
typespec({:%{}, meta, fields}, vars, caller)
|
||||
end
|
||||
|
||||
# Handle records
|
||||
@@ -815,13 +946,11 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:record, meta, [atom, fields]}, vars, caller) do
|
||||
# We cannot set a function name to avoid tracking
|
||||
# as a compile time dependency because for records it actually is one.
|
||||
case Macro.expand({atom, [], [{atom, [], []}]}, caller) do
|
||||
keyword when is_list(keyword) ->
|
||||
types =
|
||||
keyword =
|
||||
:lists.map(fn {field, _} ->
|
||||
Keyword.get(fields, field, quote(do: term()))
|
||||
{field, quote do: term()}
|
||||
end, keyword)
|
||||
|
||||
:lists.foreach(fn {field, _} ->
|
||||
@@ -830,7 +959,10 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end, fields)
|
||||
|
||||
typespec({:{}, meta, [atom | types]}, vars, caller)
|
||||
fields = Keyword.merge(keyword, fields)
|
||||
types = Keyword.values(fields)
|
||||
|
||||
typespec({:{}, meta, [atom|types]}, vars, caller)
|
||||
_ ->
|
||||
compile_error(caller, "unknown record #{inspect atom}")
|
||||
end
|
||||
@@ -862,7 +994,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle type operator
|
||||
defp typespec({:::, meta, [var, expr]}, vars, caller) do
|
||||
left = typespec(var, [elem(var, 0) | vars], caller)
|
||||
left = typespec(var, [elem(var, 0)|vars], caller)
|
||||
right = typespec(expr, vars, caller)
|
||||
{:ann_type, line(meta), [left, right]}
|
||||
end
|
||||
@@ -872,24 +1004,9 @@ defmodule Kernel.Typespec do
|
||||
{:op, line(meta), op, {:integer, line(meta), integer}}
|
||||
end
|
||||
|
||||
# Handle remote calls in the form of @module_attribute.type.
|
||||
# These are not handled by the general remote type clause as calling
|
||||
# Macro.expand/2 on the remote does not expand module attributes (but expands
|
||||
# things like __MODULE__).
|
||||
defp typespec({{:., meta, [{:@, _, [{attr, _, _}]}, name]}, _, args} = orig, vars, caller) do
|
||||
remote = Module.get_attribute(caller.module, attr)
|
||||
unless is_atom(remote) and remote != nil do
|
||||
message = "invalid remote in typespec: #{Macro.to_string(orig)} (@#{attr} is #{inspect remote})"
|
||||
compile_error(caller, message)
|
||||
end
|
||||
remote_type({typespec(remote, vars, caller), meta, typespec(name, vars, caller), args}, vars, caller)
|
||||
end
|
||||
|
||||
# Handle remote calls
|
||||
defp typespec({{:., meta, [remote, name]}, _, args} = orig, vars, caller) do
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
remote = Macro.expand remote, %{caller | function: {:typespec, 0}}
|
||||
remote = Macro.expand remote, caller
|
||||
unless is_atom(remote) do
|
||||
compile_error(caller, "invalid remote in typespec: #{Macro.to_string(orig)}")
|
||||
end
|
||||
@@ -927,14 +1044,13 @@ defmodule Kernel.Typespec do
|
||||
# Handle local calls
|
||||
defp typespec({type, meta, arguments}, vars, caller) when type in [:string, :nonempty_string] do
|
||||
:elixir_errors.warn caller.line, caller.file, "#{type}() type use is discouraged. For character lists, use " <>
|
||||
"charlist() type, for strings, String.t()\n#{Exception.format_stacktrace(Macro.Env.stacktrace(caller))}"
|
||||
"char_list() type, for strings, String.t()\n#{Exception.format_stacktrace(Macro.Env.stacktrace(caller))}"
|
||||
arguments = for arg <- arguments, do: typespec(arg, vars, caller)
|
||||
{:type, line(meta), type, arguments}
|
||||
end
|
||||
|
||||
# TODO: Deprecate char_list type by v1.5
|
||||
defp typespec({type, _meta, []}, vars, caller) when type in [:charlist, :char_list] do
|
||||
typespec((quote do: :elixir.charlist()), vars, caller)
|
||||
defp typespec({:char_list, _meta, []}, vars, caller) do
|
||||
typespec((quote do: :elixir.char_list()), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:struct, _meta, []}, vars, caller) do
|
||||
@@ -945,10 +1061,6 @@ defmodule Kernel.Typespec do
|
||||
typespec((quote do: :elixir.as_boolean(unquote(arg))), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:keyword, _meta, args}, vars, caller) when length(args) <= 1 do
|
||||
typespec((quote do: :elixir.keyword(unquote_splicing(args))), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:fun, meta, args}, vars, caller) do
|
||||
args = for arg <- args, do: typespec(arg, vars, caller)
|
||||
{:type, line(meta), :fun, args}
|
||||
@@ -956,9 +1068,14 @@ defmodule Kernel.Typespec do
|
||||
|
||||
defp typespec({name, meta, arguments}, vars, caller) do
|
||||
arguments = for arg <- arguments, do: typespec(arg, vars, caller)
|
||||
arity = length(arguments)
|
||||
type = if :erl_internal.is_type(name, arity), do: :type, else: :user_type
|
||||
{type, line(meta), name, arguments}
|
||||
|
||||
if :erlang.system_info(:otp_release) >= '18' do
|
||||
arity = length(arguments)
|
||||
type = if :erl_internal.is_type(name, arity), do: :type, else: :user_type
|
||||
{type, line(meta), name, arguments}
|
||||
else
|
||||
{:type, line(meta), name, arguments}
|
||||
end
|
||||
end
|
||||
|
||||
# Handle literals
|
||||
@@ -987,7 +1104,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec(list, vars, caller) when is_list(list) do
|
||||
[h | t] = :lists.reverse(list)
|
||||
[h|t] = :lists.reverse(list)
|
||||
union = :lists.foldl(fn(x, acc) ->
|
||||
{:|, [], [validate_kw(x, list, caller), acc]}
|
||||
end, validate_kw(h, list, caller), t)
|
||||
@@ -1009,7 +1126,7 @@ defmodule Kernel.Typespec do
|
||||
{:remote_type, line(meta), [ remote, name, arguments ]}
|
||||
end
|
||||
|
||||
defp collect_union({:|, _, [a, b]}), do: [a | collect_union(b)]
|
||||
defp collect_union({:|, _, [a, b]}), do: [a|collect_union(b)]
|
||||
defp collect_union(v), do: [v]
|
||||
|
||||
defp validate_kw({key, _} = t, _, _caller) when is_atom(key), do: t
|
||||
@@ -1037,8 +1154,8 @@ defmodule Kernel.Typespec do
|
||||
{:var, line(meta), name}
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]} | t], acc) do
|
||||
unpack_typespec_kw(t, [{atom, typespec_to_ast(type)} | acc])
|
||||
defp unpack_typespec_kw([{:type, _, :tuple, [{:atom, _, atom}, type]}|t], acc) do
|
||||
unpack_typespec_kw(t, [{atom, typespec_to_ast(type)}|acc])
|
||||
end
|
||||
|
||||
defp unpack_typespec_kw([], acc) do
|
||||
|
||||
@@ -1,123 +0,0 @@
|
||||
import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
|
||||
|
||||
defmodule Kernel.Utils do
|
||||
@moduledoc false
|
||||
|
||||
@doc """
|
||||
Callback for destructure.
|
||||
"""
|
||||
def destructure(list, count) when is_list(list), do: destructure_list(list, count)
|
||||
def destructure(nil, count), do: destructure_nil(count)
|
||||
|
||||
defp destructure_list(_, 0), do: []
|
||||
defp destructure_list([], count), do: destructure_nil(count)
|
||||
defp destructure_list([h | t], count), do: [h | destructure_list(t, count - 1)]
|
||||
|
||||
defp destructure_nil(0), do: []
|
||||
defp destructure_nil(count), do: [nil | destructure_nil(count - 1)]
|
||||
|
||||
@doc """
|
||||
Callback for defdelegate.
|
||||
"""
|
||||
def defdelegate(fun, opts) do
|
||||
append_first = Keyword.get(opts, :append_first, false)
|
||||
|
||||
{name, args} =
|
||||
case Macro.decompose_call(fun) do
|
||||
{_, _} = pair -> pair
|
||||
_ -> raise ArgumentError, "invalid syntax in defdelegate #{Macro.to_string(fun)}"
|
||||
end
|
||||
|
||||
as_args_list = normalize_args(args)
|
||||
as = Keyword.get(opts, :as, name)
|
||||
:lists.map(fn as_args ->
|
||||
formal_args = make_formal_args(as_args)
|
||||
as_args = case append_first do
|
||||
true -> tl(as_args) ++ [hd(as_args)]
|
||||
false -> as_args
|
||||
end
|
||||
{name, formal_args, as, as_args}
|
||||
end, as_args_list)
|
||||
end
|
||||
|
||||
defp make_formal_args(args) do
|
||||
fun = &match?({name, _, mod} when is_atom(name) and is_atom(mod), &1)
|
||||
:lists.filter(fun, args)
|
||||
end
|
||||
|
||||
defp normalize_args(raw_args) do
|
||||
:lists.foldr(fn
|
||||
({:\\, _, [arg, default_arg]}, [as_args | _] = as_args_list) ->
|
||||
new_as_args = [default_arg | as_args]
|
||||
[new_as_args | add_arg(as_args_list, arg)]
|
||||
(arg, as_args_list) ->
|
||||
add_arg(as_args_list, arg)
|
||||
end, [[]], raw_args)
|
||||
end
|
||||
|
||||
defp add_arg(as_args_list, {name, _, mod} = arg) when is_atom(name) and is_atom(mod),
|
||||
do: :lists.map(&([arg | &1]), as_args_list)
|
||||
defp add_arg(_, code) do
|
||||
raise ArgumentError,
|
||||
"defdelegate/2 only accepts function parameters, got: #{Macro.to_string(code)}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback for defstruct.
|
||||
"""
|
||||
def defstruct(module, fields) do
|
||||
case fields do
|
||||
fs when is_list(fs) ->
|
||||
:ok
|
||||
other ->
|
||||
raise ArgumentError, "struct fields definition must be list, got: #{inspect other}"
|
||||
end
|
||||
|
||||
fields = :lists.map(fn
|
||||
{key, val} when is_atom(key) ->
|
||||
try do
|
||||
Macro.escape(val)
|
||||
rescue
|
||||
e in [ArgumentError] ->
|
||||
raise ArgumentError, "invalid value for struct field #{key}, " <> Exception.message(e)
|
||||
else
|
||||
_ -> {key, val}
|
||||
end
|
||||
key when is_atom(key) ->
|
||||
{key, nil}
|
||||
other ->
|
||||
raise ArgumentError, "struct field names must be atoms, got: #{inspect other}"
|
||||
end, fields)
|
||||
|
||||
{:maps.put(:__struct__, module, :maps.from_list(fields)),
|
||||
List.wrap(Module.get_attribute(module, :enforce_keys)),
|
||||
Module.get_attribute(module, :derive)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Announcing callback for defstruct.
|
||||
"""
|
||||
def announce_struct(module) do
|
||||
case :erlang.get(:elixir_compiler_pid) do
|
||||
:undefined -> :ok
|
||||
pid -> send(pid, {:struct_available, module})
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback for raise.
|
||||
"""
|
||||
def raise(msg) when is_binary(msg) do
|
||||
RuntimeError.exception(msg)
|
||||
end
|
||||
def raise(atom) when is_atom(atom) do
|
||||
atom.exception([])
|
||||
end
|
||||
def raise(%{__struct__: struct, __exception__: true} = exception) when is_atom(struct) do
|
||||
exception
|
||||
end
|
||||
def raise(other) do
|
||||
ArgumentError.exception("raise/1 expects an alias, string or exception as " <>
|
||||
"the first argument, got: #{inspect other}")
|
||||
end
|
||||
end
|
||||
+127
-247
@@ -1,15 +1,13 @@
|
||||
defmodule Keyword do
|
||||
@moduledoc """
|
||||
A set of functions for working with keywords.
|
||||
|
||||
A keyword is a list of two-element tuples where the first
|
||||
element of the tuple is an atom and the second element
|
||||
can be any value.
|
||||
A keyword is a list of tuples where the first element
|
||||
of the tuple is an atom and the second element can be
|
||||
any value.
|
||||
|
||||
A keyword may have duplicated keys so it is not strictly
|
||||
a key-value store. However most of the functions in this module
|
||||
behave exactly as a dictionary so they work similarly to
|
||||
the functions you would find in the `Map` module.
|
||||
a dictionary. However most of the functions in this module
|
||||
behave exactly as a dictionary and mimic the API defined
|
||||
by the `Dict` behaviour.
|
||||
|
||||
For example, `Keyword.get/3` will get the first entry matching
|
||||
the given key, regardless if duplicated entries exist.
|
||||
@@ -29,6 +27,7 @@ defmodule Keyword do
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@behaviour Dict
|
||||
|
||||
@type key :: atom
|
||||
@type value :: any
|
||||
@@ -38,53 +37,33 @@ defmodule Keyword do
|
||||
|
||||
@doc """
|
||||
Returns `true` if `term` is a keyword list; otherwise returns `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.keyword?([])
|
||||
true
|
||||
iex> Keyword.keyword?([a: 1])
|
||||
true
|
||||
iex> Keyword.keyword?([{Foo, 1}])
|
||||
true
|
||||
iex> Keyword.keyword?([{}])
|
||||
false
|
||||
iex> Keyword.keyword?([:key])
|
||||
false
|
||||
iex> Keyword.keyword?(%{})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec keyword?(term) :: boolean
|
||||
def keyword?(term)
|
||||
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key), do: keyword?(rest)
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key),
|
||||
do: keyword?(rest)
|
||||
|
||||
def keyword?([]), do: true
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc """
|
||||
Returns an empty keyword list, i.e. an empty list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new()
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec new :: []
|
||||
@spec new :: t
|
||||
def new, do: []
|
||||
|
||||
@doc """
|
||||
Creates a keyword from an enumerable.
|
||||
|
||||
Duplicated entries are removed, the latest one prevails.
|
||||
Unlike `Enum.into(enumerable, [])`, `Keyword.new(enumerable)`
|
||||
guarantees the keys are unique.
|
||||
Unlike `Enum.into(enumerable, [])`,
|
||||
`Keyword.new(enumerable)` guarantees the keys are unique.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new([{:b, 1}, {:a, 2}])
|
||||
[b: 1, a: 2]
|
||||
[a: 2, b: 1]
|
||||
|
||||
iex> Keyword.new([{:a, 1}, {:a, 2}, {:a, 3}])
|
||||
[a: 3]
|
||||
@@ -104,7 +83,7 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.new([:a, :b], fn(x) -> {x, x} end)
|
||||
iex> Keyword.new([:a, :b], fn (x) -> {x, x} end) |> Enum.sort
|
||||
[a: :a, b: :b]
|
||||
|
||||
"""
|
||||
@@ -114,36 +93,29 @@ defmodule Keyword do
|
||||
{k, v} = transform.(el)
|
||||
put_new(acc, k, v)
|
||||
end
|
||||
:lists.foldl(fun, [], Enum.reverse(pairs))
|
||||
keywords = :lists.foldl(fun, [], Enum.reverse(pairs))
|
||||
:lists.reverse(keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
If `key` does not exist, return the default value (`nil` if no default value).
|
||||
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get([], :a)
|
||||
nil
|
||||
iex> Keyword.get([a: 1], :a)
|
||||
1
|
||||
|
||||
iex> Keyword.get([a: 1], :b)
|
||||
nil
|
||||
|
||||
iex> Keyword.get([a: 1], :b, 3)
|
||||
3
|
||||
|
||||
With duplicated keys:
|
||||
|
||||
iex> Keyword.get([a: 1, a: 2], :a, 3)
|
||||
1
|
||||
iex> Keyword.get([a: 1, a: 2], :b, 3)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec get(t, key) :: value
|
||||
@spec get(t, key, value) :: value
|
||||
@@ -160,7 +132,7 @@ defmodule Keyword do
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
generally difficult to set-up and tear-down again.
|
||||
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
@@ -170,12 +142,12 @@ defmodule Keyword do
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> :result
|
||||
...> end
|
||||
iex> Keyword.get_lazy(keyword, :a, fun)
|
||||
1
|
||||
iex> Keyword.get_lazy(keyword, :b, fun)
|
||||
13
|
||||
:result
|
||||
|
||||
"""
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
@@ -191,105 +163,37 @@ defmodule Keyword do
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed from the
|
||||
keyword list and returned.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new keyword list with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :a, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, [a: "new value!"]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, [b: "new value!", a: 1]}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :a, fn _ -> :pop end)
|
||||
{1, []}
|
||||
|
||||
iex> Keyword.get_and_update([a: 1], :b, fn _ -> :pop end)
|
||||
{nil, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {get, value} | :pop)) :: {get, t} when get: term
|
||||
def get_and_update(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key),
|
||||
do: get_and_update(keywords, [], key, fun)
|
||||
|
||||
defp get_and_update([{key, current} | t], acc, key, fun) do
|
||||
case fun.(current) do
|
||||
{get, value} -> {get, :lists.reverse(acc, [{key, value} | t])}
|
||||
:pop -> {current, :lists.reverse(acc, t)}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_and_update([h | t], acc, key, fun),
|
||||
do: get_and_update(t, [h | acc], key, fun)
|
||||
|
||||
defp get_and_update([], acc, key, fun) do
|
||||
case fun.(nil) do
|
||||
{get, update} -> {get, [{key, update} | :lists.reverse(acc)]}
|
||||
:pop -> {nil, :lists.reverse(acc)}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-element tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
is not present) and must return a two-elements tuple: the "get" value (the
|
||||
retrieved value, which can be operated on before being returned) and the new
|
||||
value to be stored under `key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a new
|
||||
keyword list with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn current_value ->
|
||||
iex> Keyword.get_and_update [a: 1], :a, fn(current_value) ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
...> end
|
||||
{1, [a: "new value!"]}
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
iex> Keyword.get_and_update!([a: 1], :a, fn _ ->
|
||||
...> :pop
|
||||
...> end)
|
||||
{1, []}
|
||||
|
||||
"""
|
||||
@spec get_and_update!(t, key, (value -> {get, value})) :: {get, t} | no_return when get: term
|
||||
def get_and_update!(keywords, key, fun) do
|
||||
get_and_update!(keywords, key, fun, [])
|
||||
@spec get_and_update(t, key, (value -> {value, value})) :: {value, t}
|
||||
def get_and_update(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key),
|
||||
do: get_and_update(keywords, [], key, fun)
|
||||
|
||||
defp get_and_update([{key, value}|t], acc, key, fun) do
|
||||
{get, new_value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, new_value}|t])}
|
||||
end
|
||||
|
||||
defp get_and_update!([{key, value} | keywords], key, fun, acc) do
|
||||
case fun.(value) do
|
||||
{get, value} ->
|
||||
{get, :lists.reverse(acc, [{key, value} | delete(keywords, key)])}
|
||||
:pop ->
|
||||
{value, :lists.reverse(acc, keywords)}
|
||||
end
|
||||
end
|
||||
defp get_and_update([h|t], acc, key, fun),
|
||||
do: get_and_update(t, [h|acc], key, fun)
|
||||
|
||||
defp get_and_update!([{_, _} = e | keywords], key, fun, acc) do
|
||||
get_and_update!(keywords, key, fun, [e | acc])
|
||||
end
|
||||
|
||||
defp get_and_update!([], key, _fun, acc) when is_atom(key) do
|
||||
raise(KeyError, key: key, term: acc)
|
||||
defp get_and_update([], acc, key, fun) do
|
||||
{get, update} = fun.(nil)
|
||||
{get, [{key, update}|:lists.reverse(acc)]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -301,6 +205,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.fetch([a: 1], :a)
|
||||
{:ok, 1}
|
||||
|
||||
iex> Keyword.fetch([a: 1], :b)
|
||||
:error
|
||||
|
||||
@@ -322,6 +227,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.fetch!([a: 1], :a)
|
||||
1
|
||||
|
||||
iex> Keyword.fetch!([a: 1], :b)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
@@ -339,10 +245,6 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.get_values([], :a)
|
||||
[]
|
||||
iex> Keyword.get_values([a: 1], :a)
|
||||
[1]
|
||||
iex> Keyword.get_values([a: 1, a: 2], :a)
|
||||
[1, 2]
|
||||
|
||||
@@ -350,9 +252,10 @@ defmodule Keyword do
|
||||
@spec get_values(t, key) :: [value]
|
||||
def get_values(keywords, key) when is_list(keywords) and is_atom(key) do
|
||||
fun = fn
|
||||
{^key, val} -> {true, val}
|
||||
{k, v} when k === key -> {true, v}
|
||||
{_, _} -> false
|
||||
end
|
||||
|
||||
:lists.filtermap(fun, keywords)
|
||||
end
|
||||
|
||||
@@ -365,6 +268,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.keys([a: 1, b: 2])
|
||||
[:a, :b]
|
||||
|
||||
iex> Keyword.keys([a: 1, b: 2, a: 3])
|
||||
[:a, :b, :a]
|
||||
|
||||
@@ -377,14 +281,10 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Returns all values from the keyword list.
|
||||
|
||||
Values from duplicated keys will be kept in the final list of values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.values([a: 1, b: 2])
|
||||
[1, 2]
|
||||
iex> Keyword.values([a: 1, b: 2, a: 3])
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec values(t) :: [value]
|
||||
@@ -401,12 +301,12 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2], :a, 1)
|
||||
[b: 2]
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2, a: 3], :a, 3)
|
||||
[a: 1, b: 2]
|
||||
iex> Keyword.delete([a: 1], :a, 5)
|
||||
[a: 1]
|
||||
iex> Keyword.delete([a: 1], :b, 5)
|
||||
[a: 1]
|
||||
|
||||
iex> Keyword.delete([b: 2], :a, 5)
|
||||
[b: 2]
|
||||
|
||||
"""
|
||||
@spec delete(t, key, value) :: t
|
||||
@@ -425,8 +325,10 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2], :a)
|
||||
[b: 2]
|
||||
|
||||
iex> Keyword.delete([a: 1, b: 2, a: 3], :a)
|
||||
[b: 2]
|
||||
|
||||
iex> Keyword.delete([b: 2], :a)
|
||||
[b: 2]
|
||||
|
||||
@@ -445,6 +347,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.delete_first([a: 1, b: 2, a: 3], :a)
|
||||
[b: 2, a: 3]
|
||||
|
||||
iex> Keyword.delete_first([b: 2], :a)
|
||||
[b: 2]
|
||||
|
||||
@@ -462,25 +365,24 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.put([a: 1], :b, 2)
|
||||
[b: 2, a: 1]
|
||||
iex> Keyword.put([a: 1, b: 2], :a, 3)
|
||||
[a: 3, b: 2]
|
||||
|
||||
iex> Keyword.put([a: 1, b: 2, a: 4], :a, 3)
|
||||
[a: 3, b: 2]
|
||||
|
||||
"""
|
||||
@spec put(t, key, value) :: t
|
||||
def put(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
[{key, value} | delete(keywords, key)]
|
||||
[{key, value}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key`
|
||||
in keyword list unless `key` is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
This is useful if the value is very expensive to calculate or generally
|
||||
difficult to set-up and tear-down again.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -500,7 +402,7 @@ defmodule Keyword do
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, fun.()} | keywords]
|
||||
false -> [{key, fun.()}|keywords]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -512,6 +414,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.put_new([a: 1], :b, 2)
|
||||
[b: 2, a: 1]
|
||||
|
||||
iex> Keyword.put_new([a: 1, b: 2], :a, 3)
|
||||
[a: 1, b: 2]
|
||||
|
||||
@@ -520,7 +423,7 @@ defmodule Keyword do
|
||||
def put_new(keywords, key, value) when is_list(keywords) and is_atom(key) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, _} -> keywords
|
||||
false -> [{key, value} | keywords]
|
||||
false -> [{key, value}|keywords]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -534,10 +437,6 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.equal?([a: 1, b: 2], [b: 2, a: 1])
|
||||
true
|
||||
iex> Keyword.equal?([a: 1, b: 2], [b: 1, a: 2])
|
||||
false
|
||||
iex> Keyword.equal?([a: 1, b: 2, a: 3], [b: 2, a: 3, a: 1])
|
||||
true
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
@@ -548,72 +447,44 @@ defmodule Keyword do
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`, overriding any existing one.
|
||||
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
If they have duplicated keys, the one given in the second argument wins.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4])
|
||||
[b: 2, a: 3, d: 4]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5])
|
||||
[b: 2, a: 3, d: 4, a: 5]
|
||||
[a: 3, d: 4, b: 2]
|
||||
|
||||
"""
|
||||
@spec merge(t, t) :: t
|
||||
def merge(keywords1, keywords2) when is_list(keywords1) and is_list(keywords2) do
|
||||
fun = fn {k, _v} -> not has_key?(keywords2, k) end
|
||||
:lists.filter(fun, keywords1) ++ keywords2
|
||||
keywords2 ++ :lists.filter(fun, keywords1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Merges two keyword lists into one.
|
||||
|
||||
All keys, including duplicated keys, given in `keywords2` will be added
|
||||
to `keywords1`. The given function will be invoked to solve conflicts.
|
||||
|
||||
If `keywords2` has duplicate keys, the given function will be invoked
|
||||
for each matching pair in `keywords1`.
|
||||
|
||||
There are no guarantees about the order of keys in the returned keyword.
|
||||
If they have duplicated keys, the given function is invoked to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4], fn _k, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4], fn (_k, v1, v2) ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4, a: 5]
|
||||
|
||||
iex> Keyword.merge([a: 1, b: 2, a: 3], [a: 3, d: 4, a: 5], fn :a, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
[b: 2, a: 4, d: 4, a: 8]
|
||||
[a: 4, b: 2, d: 4]
|
||||
|
||||
"""
|
||||
@spec merge(t, t, (key, value, value -> value)) :: t
|
||||
def merge(keywords1, keywords2, fun) when is_list(keywords1) and is_list(keywords2) do
|
||||
do_merge(keywords2, [], keywords1, keywords1, fun)
|
||||
do_merge(keywords2, keywords1, fun)
|
||||
end
|
||||
|
||||
defp do_merge([{k, v2} | t], acc, rest, original, fun) do
|
||||
case :lists.keyfind(k, 1, original) do
|
||||
{^k, v1} ->
|
||||
do_merge(t, [{k, fun.(k, v1, v2)} | acc],
|
||||
delete(rest, k), :lists.keydelete(k, 1, original), fun)
|
||||
false ->
|
||||
do_merge(t, [{k, v2} | acc], rest, original, fun)
|
||||
end
|
||||
defp do_merge([{k, v2}|t], acc, fun) do
|
||||
do_merge t, update(acc, k, v2, fn(v1) -> fun.(k, v1, v2) end), fun
|
||||
end
|
||||
|
||||
defp do_merge([], acc, rest, _original, _fun) do
|
||||
rest ++ :lists.reverse(acc)
|
||||
defp do_merge([], acc, _fun) do
|
||||
acc
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -623,6 +494,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.has_key?([a: 1], :a)
|
||||
true
|
||||
|
||||
iex> Keyword.has_key?([a: 1], :b)
|
||||
false
|
||||
|
||||
@@ -644,8 +516,6 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.update!([a: 1], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update!([a: 1, a: 2], :a, &(&1 * 2))
|
||||
[a: 2]
|
||||
|
||||
iex> Keyword.update!([a: 1], :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
@@ -656,12 +526,12 @@ defmodule Keyword do
|
||||
update!(keywords, key, fun, keywords)
|
||||
end
|
||||
|
||||
defp update!([{key, value} | keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
defp update!([{key, value}|keywords], key, fun, _dict) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
defp update!([{_, _} = e | keywords], key, fun, dict) do
|
||||
[e | update!(keywords, key, fun, dict)]
|
||||
defp update!([{_, _} = e|keywords], key, fun, dict) do
|
||||
[e|update!(keywords, key, fun, dict)]
|
||||
end
|
||||
|
||||
defp update!([], key, _fun, dict) when is_atom(key) do
|
||||
@@ -680,8 +550,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.update([a: 1], :a, 13, &(&1 * 2))
|
||||
[a: 2]
|
||||
iex> Keyword.update([a: 1, a: 2], :a, 13, &(&1 * 2))
|
||||
[a: 2]
|
||||
|
||||
iex> Keyword.update([a: 1], :b, 11, &(&1 * 2))
|
||||
[a: 1, b: 11]
|
||||
|
||||
@@ -689,12 +558,12 @@ defmodule Keyword do
|
||||
@spec update(t, key, value, (value -> value)) :: t
|
||||
def update(keywords, key, initial, fun)
|
||||
|
||||
def update([{key, value} | keywords], key, _initial, fun) do
|
||||
[{key, fun.(value)} | delete(keywords, key)]
|
||||
def update([{key, value}|keywords], key, _initial, fun) do
|
||||
[{key, fun.(value)}|delete(keywords, key)]
|
||||
end
|
||||
|
||||
def update([{_, _} = e | keywords], key, initial, fun) do
|
||||
[e | update(keywords, key, initial, fun)]
|
||||
def update([{_, _} = e|keywords], key, initial, fun) do
|
||||
[e|update(keywords, key, initial, fun)]
|
||||
end
|
||||
|
||||
def update([], key, initial, _fun) when is_atom(key) do
|
||||
@@ -707,23 +576,26 @@ defmodule Keyword do
|
||||
|
||||
Returns a tuple with the new list and the old list with removed keys.
|
||||
|
||||
Keys for which there are no entries in the keyword list are ignored.
|
||||
Keys for which there are no entires in the keyword list are ignored.
|
||||
|
||||
Entries with duplicated keys end up in the same keyword list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.split([a: 1, b: 2, c: 3], [:a, :c, :e])
|
||||
{[a: 1, c: 3], [b: 2]}
|
||||
iex> Keyword.split([a: 1, b: 2, c: 3, a: 4], [:a, :c, :e])
|
||||
{[a: 1, c: 3, a: 4], [b: 2]}
|
||||
iex> d = [a: 1, b: 2, c: 3, d: 4]
|
||||
iex> Keyword.split(d, [:a, :c, :e])
|
||||
{[a: 1, c: 3], [b: 2, d: 4]}
|
||||
|
||||
iex> d = [a: 1, b: 2, c: 3, d: 4, a: 5]
|
||||
iex> Keyword.split(d, [:a, :c, :e])
|
||||
{[a: 1, c: 3, a: 5], [b: 2, d: 4]}
|
||||
|
||||
"""
|
||||
def split(keywords, keys) when is_list(keywords) do
|
||||
fun = fn {k, v}, {take, drop} ->
|
||||
case k in keys do
|
||||
true -> {[{k, v} | take], drop}
|
||||
false -> {take, [{k, v} | drop]}
|
||||
true -> {[{k, v}|take], drop}
|
||||
false -> {take, [{k, v}|drop]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -740,9 +612,12 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.take([a: 1, b: 2, c: 3], [:a, :c, :e])
|
||||
iex> d = [a: 1, b: 2, c: 3, d: 4]
|
||||
iex> Keyword.take(d, [:a, :c, :e])
|
||||
[a: 1, c: 3]
|
||||
iex> Keyword.take([a: 1, b: 2, c: 3, a: 5], [:a, :c, :e])
|
||||
|
||||
iex> d = [a: 1, b: 2, c: 3, d: 4, a: 5]
|
||||
iex> Keyword.take(d, [:a, :c, :e])
|
||||
[a: 1, c: 3, a: 5]
|
||||
|
||||
"""
|
||||
@@ -757,9 +632,12 @@ defmodule Keyword do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.drop([a: 1, b: 2, c: 3], [:b, :d])
|
||||
iex> d = [a: 1, b: 2, c: 3, d: 4]
|
||||
iex> Keyword.drop(d, [:b, :d])
|
||||
[a: 1, c: 3]
|
||||
iex> Keyword.drop([a: 1, b: 2, b: 3, c: 3, a: 5], [:b, :d])
|
||||
|
||||
iex> d = [a: 1, b: 2, b: 3, c: 3, d: 4, a: 5]
|
||||
iex> Keyword.drop(d, [:b, :d])
|
||||
[a: 1, c: 3, a: 5]
|
||||
|
||||
"""
|
||||
@@ -768,20 +646,24 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes all values associated with `key` in the keyword list.
|
||||
Returns the first value associated with `key` in the keyword
|
||||
list as well as the keyword list without `key`.
|
||||
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop([a: 1], :a)
|
||||
iex> Keyword.pop [a: 1], :a
|
||||
{1, []}
|
||||
iex> Keyword.pop([a: 1], :b)
|
||||
|
||||
iex> Keyword.pop [a: 1], :b
|
||||
{nil, [a: 1]}
|
||||
iex> Keyword.pop([a: 1], :b, 3)
|
||||
|
||||
iex> Keyword.pop [a: 1], :b, 3
|
||||
{3, [a: 1]}
|
||||
iex> Keyword.pop([a: 1, a: 2], :a)
|
||||
|
||||
iex> Keyword.pop [a: 1, a: 2], :a
|
||||
{1, []}
|
||||
|
||||
"""
|
||||
@@ -796,10 +678,11 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the keyword list.
|
||||
Returns the first value associated with `key` in the keyword
|
||||
list as well as the keyword list without `key`.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
generally difficult to set-up and tear-down again.
|
||||
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
@@ -809,12 +692,12 @@ defmodule Keyword do
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> :result
|
||||
...> end
|
||||
iex> Keyword.pop_lazy(keyword, :a, fun)
|
||||
{1, []}
|
||||
iex> Keyword.pop_lazy(keyword, :b, fun)
|
||||
{13, [a: 1]}
|
||||
{:result, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
@@ -829,7 +712,9 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the first value associated with `key` in the keyword list.
|
||||
Returns the first value associated with `key` in the keyword
|
||||
list as well as the keyword list without that particular occurrence
|
||||
of `key`.
|
||||
|
||||
Duplicated keys are not removed.
|
||||
|
||||
@@ -837,10 +722,13 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :a
|
||||
{1, []}
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :b
|
||||
{nil, [a: 1]}
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :b, 3
|
||||
{3, [a: 1]}
|
||||
|
||||
iex> Keyword.pop_first [a: 1, a: 2], :a
|
||||
{1, [a: 2]}
|
||||
|
||||
@@ -853,23 +741,15 @@ defmodule Keyword do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the keyword list itself.
|
||||
# Dict callbacks
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.to_list([a: 1])
|
||||
[a: 1]
|
||||
|
||||
"""
|
||||
def to_list(keyword) when is_list(keyword) do
|
||||
keyword
|
||||
@doc false
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def size(keyword) do
|
||||
IO.warn "Keyword.size/1 is deprecated, please use Kernel.length/1"
|
||||
length(keyword)
|
||||
def to_list(keyword) do
|
||||
keyword
|
||||
end
|
||||
end
|
||||
|
||||
+38
-81
@@ -1,45 +1,19 @@
|
||||
defmodule List do
|
||||
@moduledoc """
|
||||
Specialized functions that only work on lists.
|
||||
Implements functions that only make sense for lists
|
||||
and cannot be part of the Enum protocol. In general,
|
||||
favor using the Enum API instead of List.
|
||||
|
||||
In general, favor using the `Enum` API instead of `List`.
|
||||
|
||||
Index access for list is linear. Negative indexes are also
|
||||
Some functions in this module expect an index. Index
|
||||
access for list is linear. Negative indexes are also
|
||||
supported but they imply the list will be iterated twice,
|
||||
one to calculate the proper index and another to perform the
|
||||
one to calculate the proper index and another to the
|
||||
operation.
|
||||
|
||||
A decision was taken to delegate most functions to
|
||||
Erlang's standard library but follow Elixir's convention
|
||||
of receiving the subject (in this case, a list) as the
|
||||
of receiving the target (in this case, a list) as the
|
||||
first argument.
|
||||
|
||||
## Charlists
|
||||
|
||||
If a list is made of non-negative integers, it can also
|
||||
be called as a charlist. Elixir uses single quotes to
|
||||
define charlists:
|
||||
|
||||
iex> 'héllo'
|
||||
[104, 233, 108, 108, 111]
|
||||
|
||||
In particular, charlists may be printed back in single
|
||||
quotes if they contain only ASCII-printable codepoints:
|
||||
|
||||
iex> 'abc'
|
||||
'abc'
|
||||
|
||||
The rationale behind this behaviour is to better support
|
||||
Erlang libraries which may return text as charlists
|
||||
instead of Elixir strings. One example of such functions
|
||||
is `Application.loaded_applications`:
|
||||
|
||||
Application.loaded_applications
|
||||
#=> [{:stdlib, 'ERTS CXC 138 10', '2.6'},
|
||||
{:compiler, 'ERTS CXC 138 10', '6.0.1'},
|
||||
{:elixir, 'elixir', '1.0.0'},
|
||||
{:kernel, 'ERTS CXC 138 10', '4.1'},
|
||||
{:logger, 'logger', '1.0.0'}]
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@@ -117,10 +91,10 @@ defmodule List do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldl([5, 5], 10, fn(x, acc) -> x + acc end)
|
||||
iex> List.foldl([5, 5], 10, fn (x, acc) -> x + acc end)
|
||||
20
|
||||
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
|
||||
iex> List.foldl([1, 2, 3, 4], 0, fn (x, acc) -> x - acc end)
|
||||
2
|
||||
|
||||
"""
|
||||
@@ -135,7 +109,7 @@ defmodule List do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn(x, acc) -> x - acc end)
|
||||
iex> List.foldr([1, 2, 3, 4], 0, fn (x, acc) -> x - acc end)
|
||||
-2
|
||||
|
||||
"""
|
||||
@@ -160,8 +134,8 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec first([elem]) :: nil | elem when elem: var
|
||||
def first([]), do: nil
|
||||
def first([h | _]), do: h
|
||||
def first([]), do: nil
|
||||
def first([h|_]), do: h
|
||||
|
||||
@doc """
|
||||
Returns the last element in `list` or `nil` if `list` is empty.
|
||||
@@ -179,9 +153,9 @@ defmodule List do
|
||||
|
||||
"""
|
||||
@spec last([elem]) :: nil | elem when elem: var
|
||||
def last([]), do: nil
|
||||
def last([h]), do: h
|
||||
def last([_ | t]), do: last(t)
|
||||
def last([]), do: nil
|
||||
def last([h]), do: h
|
||||
def last([_|t]), do: last(t)
|
||||
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
@@ -492,9 +466,9 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a charlist to an atom.
|
||||
Converts a char list to an atom.
|
||||
|
||||
Currently Elixir does not support conversions from charlists
|
||||
Currently Elixir does not support conversions from char lists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -505,16 +479,16 @@ defmodule List do
|
||||
:elixir
|
||||
|
||||
"""
|
||||
@spec to_atom(charlist) :: atom
|
||||
def to_atom(charlist) do
|
||||
:erlang.list_to_atom(charlist)
|
||||
@spec to_atom(char_list) :: atom
|
||||
def to_atom(char_list) do
|
||||
:erlang.list_to_atom(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a charlist to an existing atom. Raises an `ArgumentError`
|
||||
Converts a char list to an existing atom. Raises an `ArguementError`
|
||||
if the atom does not exist.
|
||||
|
||||
Currently Elixir does not support conversions from charlists
|
||||
Currently Elixir does not support conversions from char lists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -529,13 +503,13 @@ defmodule List do
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(charlist) :: atom
|
||||
def to_existing_atom(charlist) do
|
||||
:erlang.list_to_existing_atom(charlist)
|
||||
@spec to_existing_atom(char_list) :: atom
|
||||
def to_existing_atom(char_list) do
|
||||
:erlang.list_to_existing_atom(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the float whose text representation is `charlist`.
|
||||
Returns the float whose text representation is `char_list`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -545,13 +519,13 @@ defmodule List do
|
||||
2.2017764
|
||||
|
||||
"""
|
||||
@spec to_float(charlist) :: float
|
||||
def to_float(charlist) do
|
||||
:erlang.list_to_float(charlist)
|
||||
@spec to_float(char_list) :: float
|
||||
def to_float(char_list) do
|
||||
:erlang.list_to_float(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an integer whose text representation is `charlist`.
|
||||
Returns an integer whose text representation is `char_list`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -561,13 +535,13 @@ defmodule List do
|
||||
123
|
||||
|
||||
"""
|
||||
@spec to_integer(charlist) :: integer
|
||||
def to_integer(charlist) do
|
||||
:erlang.list_to_integer(charlist)
|
||||
@spec to_integer(char_list) :: integer
|
||||
def to_integer(char_list) do
|
||||
:erlang.list_to_integer(char_list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an integer whose text representation is `charlist` in base `base`.
|
||||
Returns an integer whose text representation is `char_list` in base `base`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -577,9 +551,9 @@ defmodule List do
|
||||
1023
|
||||
|
||||
"""
|
||||
@spec to_integer(charlist, 2..36) :: integer
|
||||
def to_integer(charlist, base) do
|
||||
:erlang.list_to_integer(charlist, base)
|
||||
@spec to_integer(char_list, 2..36) :: integer
|
||||
def to_integer(char_list, base) do
|
||||
:erlang.list_to_integer(char_list, base)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -617,24 +591,7 @@ defmodule List do
|
||||
"""
|
||||
@spec to_string(:unicode.charlist) :: String.t
|
||||
def to_string(list) when is_list(list) do
|
||||
try do
|
||||
:unicode.characters_to_binary(list)
|
||||
rescue
|
||||
ArgumentError ->
|
||||
raise ArgumentError, """
|
||||
cannot convert the given list to a string.
|
||||
|
||||
To be converted to a string, a list must contain only:
|
||||
|
||||
* strings
|
||||
* integers representing Unicode codepoints
|
||||
* or a list containing one of these three elements
|
||||
|
||||
Please check the given list or call inspect/1 to get the list representation, got:
|
||||
|
||||
#{inspect list}
|
||||
"""
|
||||
else
|
||||
case :unicode.characters_to_binary(list) do
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
|
||||
@@ -3,54 +3,54 @@ defprotocol List.Chars do
|
||||
The List.Chars protocol is responsible for
|
||||
converting a structure to a list (only if applicable).
|
||||
The only function required to be implemented is
|
||||
`to_charlist` which does the conversion.
|
||||
`to_char_list` which does the conversion.
|
||||
|
||||
The `to_charlist` function automatically imported
|
||||
The `to_char_list` function automatically imported
|
||||
by Kernel invokes this protocol.
|
||||
"""
|
||||
|
||||
def to_charlist(term)
|
||||
|
||||
# TODO: Deprecate by v1.5
|
||||
@doc false
|
||||
Kernel.def to_char_list(term) do
|
||||
__MODULE__.to_charlist(term)
|
||||
end
|
||||
def to_char_list(thing)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Atom do
|
||||
def to_charlist(atom), do: Atom.to_charlist(atom)
|
||||
def to_char_list(atom), do: Atom.to_char_list(atom)
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: BitString do
|
||||
@doc """
|
||||
Returns the given binary `term` converted to a charlist.
|
||||
Returns the given binary converted to a char list.
|
||||
"""
|
||||
def to_charlist(term) when is_binary(term) do
|
||||
String.to_charlist(term)
|
||||
def to_char_list(thing) when is_binary(thing) do
|
||||
String.to_char_list(thing)
|
||||
end
|
||||
|
||||
def to_charlist(term) do
|
||||
def to_char_list(thing) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: term,
|
||||
description: "cannot convert a bitstring to a charlist"
|
||||
value: thing,
|
||||
description: "cannot convert a bitstring to a char list"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: List do
|
||||
# Note that same inlining is used for the rewrite rule.
|
||||
def to_charlist(list), do: list
|
||||
def to_char_list(list), do: list
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Integer do
|
||||
def to_charlist(term) do
|
||||
Integer.to_charlist(term)
|
||||
def to_char_list(thing) do
|
||||
Integer.to_char_list(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl List.Chars, for: Float do
|
||||
def to_charlist(term) do
|
||||
:io_lib_format.fwrite_g(term)
|
||||
@digits 20
|
||||
@limit :math.pow(10, @digits)
|
||||
|
||||
def to_char_list(thing) when thing > @limit do
|
||||
Float.to_char_list(thing, scientific: @digits)
|
||||
end
|
||||
|
||||
def to_char_list(thing) do
|
||||
Float.to_char_list(thing, compact: true, decimals: @digits)
|
||||
end
|
||||
end
|
||||
|
||||
+120
-372
@@ -1,62 +1,27 @@
|
||||
import Kernel, except: [to_string: 1]
|
||||
|
||||
defmodule Macro do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
Conveniences for working with macros.
|
||||
|
||||
## Custom Sigils
|
||||
|
||||
To create a custom sigil, define a function with the name
|
||||
`sigil_{identifier}` that takes two arguments. The first argument will be
|
||||
the string, the second will be a charlist containing any modifiers. If the
|
||||
sigil is lower case (such as `sigil_x`) then the string argument will allow
|
||||
interpolation. If the sigil is upper case (such as `sigil_X`) then the string
|
||||
will not be interpolated.
|
||||
the interpolated string, the second will be a char list containing any
|
||||
modifiers.
|
||||
|
||||
Valid modifiers include only lower and upper case letters. Other characters
|
||||
will cause a syntax error.
|
||||
|
||||
The module containing the custom sigil must be imported before the sigil
|
||||
syntax can be used.
|
||||
|
||||
### Examples
|
||||
|
||||
defmodule MySigils do
|
||||
defmacro sigil_x(term, [?r]) do
|
||||
quote do
|
||||
unquote(term) |> String.reverse()
|
||||
end
|
||||
end
|
||||
defmacro sigil_x(term, _modifiers) do
|
||||
term
|
||||
end
|
||||
defmacro sigil_X(term, [?r]) do
|
||||
quote do
|
||||
unquote(term) |> String.reverse()
|
||||
end
|
||||
end
|
||||
defmacro sigil_X(term, _modifiers) do
|
||||
term
|
||||
end
|
||||
end
|
||||
|
||||
import MySigils
|
||||
|
||||
~x(with #{"inter" <> "polation"})
|
||||
#=>"with interpolation"
|
||||
|
||||
~x(with #{"inter" <> "polation"})r
|
||||
#=>"noitalopretni htiw"
|
||||
|
||||
~X(without #{"interpolation"})
|
||||
#=>"without \#{"interpolation"}"
|
||||
|
||||
~X(without #{"interpolation"})r
|
||||
#=>"}\"noitalopretni\"{# tuohtiw"
|
||||
"""
|
||||
|
||||
@typedoc "Abstract Syntax Tree (AST)"
|
||||
@type t :: expr | {t, t} | atom | number | binary | pid | fun | [t]
|
||||
|
||||
@typedoc "Expr node (remaining ones are literals)"
|
||||
@type expr :: {expr | atom, Keyword.t, atom | [t]}
|
||||
|
||||
@binary_ops [:===, :!==,
|
||||
@@ -101,30 +66,7 @@ defmodule Macro do
|
||||
@doc """
|
||||
Breaks a pipeline expression into a list.
|
||||
|
||||
The AST for a pipeline (a sequence of applications of `|>`) is similar to the
|
||||
AST of a sequence of binary operators or function applications: the top-level
|
||||
expression is the right-most `:|>` (which is the last one to be executed), and
|
||||
its left-hand and right-hand sides are its arguments:
|
||||
|
||||
quote do: 100 |> div(5) |> div(2)
|
||||
#=> {:|>, _, [arg1, arg2]}
|
||||
|
||||
In the example above, the `|>` pipe is the right-most pipe; `arg1` is the AST
|
||||
for `100 |> div(5)`, and `arg2` is the AST for `div(2)`.
|
||||
|
||||
It's often useful to have the AST for such a pipeline as a list of function
|
||||
applications. This function does exactly that:
|
||||
|
||||
Macro.unpipe(quote do: 100 |> div(5) |> div(2))
|
||||
#=> [{100, 0}, {{:div, [], [5]}, 0}, {{:div, [], [2]}, 0}]
|
||||
|
||||
We get a list that follows the pipeline directly: first the `100`, then the
|
||||
`div(5)` (more precisely, its AST), then `div(2)`. The `0` as the second
|
||||
element of the tuples is the position of the previous element in the pipeline
|
||||
inside the current function application: `{{:div, [], [5]}, 0}` means that the
|
||||
previous element (`100`) will be inserted as the 0th (first) argument to the
|
||||
`div/2` function, so that the AST for that function will become `{:div, [],
|
||||
[100, 5]}` (`div(100, 5)`).
|
||||
Raises if the pipeline is ill-formed.
|
||||
"""
|
||||
@spec unpipe(Macro.t) :: [Macro.t]
|
||||
def unpipe(expr) do
|
||||
@@ -136,7 +78,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
defp unpipe(other, acc) do
|
||||
[{other, 0} | acc]
|
||||
[{other, 0}|acc]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -146,32 +88,13 @@ defmodule Macro do
|
||||
def pipe(expr, call_args, position)
|
||||
|
||||
def pipe(expr, {:&, _, _} = call_args, _integer) do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
def pipe(expr, {tuple_or_map, _, _} = call_args, _integer) when tuple_or_map in [:{}, :%{}] do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
# Without this, `Macro |> Env == Macro.Env`.
|
||||
def pipe(expr, {:__aliases__, _, _} = call_args, _integer) do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
def pipe(expr, {call, _, [_, _]} = call_args, _integer)
|
||||
when call in unquote(@binary_ops) do
|
||||
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}, " <>
|
||||
"the #{to_string call} operator can only take two arguments"
|
||||
end
|
||||
|
||||
# {:fn, _, _} is what we get when we pipe into an anonymous function without
|
||||
# calling it, e.g., `:foo |> (fn x -> x end)`.
|
||||
def pipe(expr, {:fn, _, _}, _integer) do
|
||||
expr_str = to_string(expr)
|
||||
raise ArgumentError,
|
||||
"cannot pipe #{expr_str} into an anonymous function without" <>
|
||||
" calling the function; use something like (fn ... end).() or" <>
|
||||
" define the anonymous function as a regular private function"
|
||||
"the #{to_string call} operator can only take two arguments"
|
||||
end
|
||||
|
||||
def pipe(expr, {call, line, atom}, integer) when is_atom(atom) do
|
||||
@@ -183,20 +106,14 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
def pipe(expr, call_args, _integer) do
|
||||
raise ArgumentError, bad_pipe(expr, call_args)
|
||||
bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
defp bad_pipe(expr, call_args) do
|
||||
"cannot pipe #{to_string expr} into #{to_string call_args}, " <>
|
||||
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous functions calls foo.()"
|
||||
raise ArgumentError, "cannot pipe #{to_string expr} into #{to_string call_args}, " <>
|
||||
"can only pipe into local calls foo(), remote calls Foo.bar() or anonymous functions calls foo.()"
|
||||
end
|
||||
|
||||
@doc false
|
||||
def pipe_warning({call, _, _}) when call in unquote(@unary_ops) do
|
||||
"piping into a unary operator is deprecated. You could use e.g. Kernel.+(5) instead of +5"
|
||||
end
|
||||
def pipe_warning(_), do: nil
|
||||
|
||||
@doc """
|
||||
Applies the given function to the node metadata if it contains one.
|
||||
|
||||
@@ -248,56 +165,6 @@ defmodule Macro do
|
||||
{var, [], context}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first traversal of quoted expressions
|
||||
using an accumulator.
|
||||
"""
|
||||
@spec traverse(t, any, (t, any -> {t, any}), (t, any -> {t, any})) :: {t, any}
|
||||
def traverse(ast, acc, pre, post) when is_function(pre, 2) and is_function(post, 2) do
|
||||
{ast, acc} = pre.(ast, acc)
|
||||
do_traverse(ast, acc, pre, post)
|
||||
end
|
||||
|
||||
defp do_traverse({form, meta, args}, acc, pre, post) when is_atom(form) do
|
||||
{args, acc} = do_traverse_args(args, acc, pre, post)
|
||||
post.({form, meta, args}, acc)
|
||||
end
|
||||
|
||||
defp do_traverse({form, meta, args}, acc, pre, post) do
|
||||
{form, acc} = pre.(form, acc)
|
||||
{form, acc} = do_traverse(form, acc, pre, post)
|
||||
{args, acc} = do_traverse_args(args, acc, pre, post)
|
||||
post.({form, meta, args}, acc)
|
||||
end
|
||||
|
||||
defp do_traverse({left, right}, acc, pre, post) do
|
||||
{left, acc} = pre.(left, acc)
|
||||
{left, acc} = do_traverse(left, acc, pre, post)
|
||||
{right, acc} = pre.(right, acc)
|
||||
{right, acc} = do_traverse(right, acc, pre, post)
|
||||
post.({left, right}, acc)
|
||||
end
|
||||
|
||||
defp do_traverse(list, acc, pre, post) when is_list(list) do
|
||||
{list, acc} = do_traverse_args(list, acc, pre, post)
|
||||
post.(list, acc)
|
||||
end
|
||||
|
||||
defp do_traverse(x, acc, _pre, post) do
|
||||
post.(x, acc)
|
||||
end
|
||||
|
||||
defp do_traverse_args(args, acc, _pre, _post) when is_atom(args) do
|
||||
{args, acc}
|
||||
end
|
||||
|
||||
defp do_traverse_args(args, acc, pre, post) when is_list(args) do
|
||||
Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first, pre-order traversal of quoted expressions.
|
||||
"""
|
||||
@@ -312,7 +179,43 @@ defmodule Macro do
|
||||
"""
|
||||
@spec prewalk(t, any, (t, any -> {t, any})) :: {t, any}
|
||||
def prewalk(ast, acc, fun) when is_function(fun, 2) do
|
||||
traverse(ast, acc, fun, fn x, a -> {x, a} end)
|
||||
{ast, acc} = fun.(ast, acc)
|
||||
do_prewalk(ast, acc, fun)
|
||||
end
|
||||
|
||||
defp do_prewalk({form, meta, args}, acc, fun) do
|
||||
unless is_atom(form) do
|
||||
{form, acc} = fun.(form, acc)
|
||||
{form, acc} = do_prewalk(form, acc, fun)
|
||||
end
|
||||
|
||||
unless is_atom(args) do
|
||||
{args, acc} = Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = fun.(x, acc)
|
||||
do_prewalk(x, acc, fun)
|
||||
end)
|
||||
end
|
||||
|
||||
{{form, meta, args}, acc}
|
||||
end
|
||||
|
||||
defp do_prewalk({left, right}, acc, fun) do
|
||||
{left, acc} = fun.(left, acc)
|
||||
{left, acc} = do_prewalk(left, acc, fun)
|
||||
{right, acc} = fun.(right, acc)
|
||||
{right, acc} = do_prewalk(right, acc, fun)
|
||||
{{left, right}, acc}
|
||||
end
|
||||
|
||||
defp do_prewalk(list, acc, fun) when is_list(list) do
|
||||
Enum.map_reduce(list, acc, fn x, acc ->
|
||||
{x, acc} = fun.(x, acc)
|
||||
do_prewalk(x, acc, fun)
|
||||
end)
|
||||
end
|
||||
|
||||
defp do_prewalk(x, acc, _fun) do
|
||||
{x, acc}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -329,7 +232,34 @@ defmodule Macro do
|
||||
"""
|
||||
@spec postwalk(t, any, (t, any -> {t, any})) :: {t, any}
|
||||
def postwalk(ast, acc, fun) when is_function(fun, 2) do
|
||||
traverse(ast, acc, fn x, a -> {x, a} end, fun)
|
||||
do_postwalk(ast, acc, fun)
|
||||
end
|
||||
|
||||
defp do_postwalk({form, meta, args}, acc, fun) do
|
||||
unless is_atom(form) do
|
||||
{form, acc} = do_postwalk(form, acc, fun)
|
||||
end
|
||||
|
||||
unless is_atom(args) do
|
||||
{args, acc} = Enum.map_reduce(args, acc, &do_postwalk(&1, &2, fun))
|
||||
end
|
||||
|
||||
fun.({form, meta, args}, acc)
|
||||
end
|
||||
|
||||
defp do_postwalk({left, right}, acc, fun) do
|
||||
{left, acc} = do_postwalk(left, acc, fun)
|
||||
{right, acc} = do_postwalk(right, acc, fun)
|
||||
fun.({left, right}, acc)
|
||||
end
|
||||
|
||||
defp do_postwalk(list, acc, fun) when is_list(list) do
|
||||
{list, acc} = Enum.map_reduce(list, acc, &do_postwalk(&1, &2, fun))
|
||||
fun.(list, acc)
|
||||
end
|
||||
|
||||
defp do_postwalk(x, acc, fun) do
|
||||
fun.(x, acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -340,19 +270,19 @@ defmodule Macro do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo))
|
||||
iex> Macro.decompose_call(quote do: foo)
|
||||
{:foo, []}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo()))
|
||||
iex> Macro.decompose_call(quote do: foo())
|
||||
{:foo, []}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: foo(1, 2, 3)))
|
||||
iex> Macro.decompose_call(quote do: foo(1, 2, 3))
|
||||
{:foo, [1, 2, 3]}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: Elixir.M.foo(1, 2, 3)))
|
||||
iex> Macro.decompose_call(quote do: Elixir.M.foo(1, 2, 3))
|
||||
{{:__aliases__, [], [:Elixir, :M]}, :foo, [1, 2, 3]}
|
||||
|
||||
iex> Macro.decompose_call(quote(do: 42))
|
||||
iex> Macro.decompose_call(quote do: 42)
|
||||
:error
|
||||
|
||||
"""
|
||||
@@ -376,7 +306,7 @@ defmodule Macro do
|
||||
into a syntax tree.
|
||||
|
||||
One may pass `unquote: true` to `escape/2`
|
||||
which leaves `unquote/1` statements unescaped, effectively
|
||||
which leaves `unquote` statements unescaped, effectively
|
||||
unquoting the contents on escape.
|
||||
|
||||
## Examples
|
||||
@@ -400,24 +330,8 @@ defmodule Macro do
|
||||
@doc """
|
||||
Validates the given expressions are valid quoted expressions.
|
||||
|
||||
Checks the `type:Macro.t` for the specification of a valid
|
||||
Check the `type:Macro.t` for the specification of a valid
|
||||
quoted expression.
|
||||
|
||||
It returns `:ok` if the expression is valid. Otherwise it returns a tuple in the form of
|
||||
`{:error, remainder}` where `remainder` is the invalid part of the quoted expression.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.validate({:two_element, :tuple})
|
||||
:ok
|
||||
iex> Macro.validate({:three, :element, :tuple})
|
||||
{:error, {:three, :element, :tuple}}
|
||||
|
||||
iex> Macro.validate([1, 2, 3])
|
||||
:ok
|
||||
iex> Macro.validate([1, 2, 3, {4}])
|
||||
{:error, {4}}
|
||||
|
||||
"""
|
||||
@spec validate(term) :: :ok | {:error, term}
|
||||
def validate(expr) do
|
||||
@@ -455,9 +369,8 @@ defmodule Macro do
|
||||
for information on how to customize the escaping map.
|
||||
|
||||
In this setup, Elixir will escape the following: `\0`, `\a`, `\b`,
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Bytes can be
|
||||
given as hexadecimals via `\xNN` and Unicode Codepoints as
|
||||
`\uNNNN` escapes.
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Unicode codepoints
|
||||
can be given as hexadecimals via `\xNN` and `\x{NN...}` escapes.
|
||||
|
||||
This function is commonly used on sigil implementations
|
||||
(like `~r`, `~s` and others) which receive a raw, unescaped
|
||||
@@ -500,15 +413,13 @@ defmodule Macro do
|
||||
def unescape_map(?t), do: ?\t
|
||||
def unescape_map(?v), do: ?\v
|
||||
def unescape_map(?x), do: true
|
||||
def unescape_map(?u), do: true
|
||||
def unescape_map(e), do: e
|
||||
|
||||
If the `unescape_map` function returns `false`. The char is
|
||||
not escaped and `\` is kept in the charlist.
|
||||
not escaped and `\` is kept in the char list.
|
||||
|
||||
Hexadecimals and Unicode codepoints will be escaped if the map
|
||||
function returns `true` for `?x`. Unicode codepoints if the map
|
||||
function returns `true` for `?u`.
|
||||
Hexadecimals will be escaped if the map function returns `true`
|
||||
for `?x`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -553,7 +464,7 @@ defmodule Macro do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.to_string(quote(do: foo.bar(1, 2, 3)))
|
||||
iex> Macro.to_string(quote do: foo.bar(1, 2, 3))
|
||||
"foo.bar(1, 2, 3)"
|
||||
|
||||
"""
|
||||
@@ -582,19 +493,14 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# Bits containers
|
||||
def to_string({:<<>>, _, parts} = ast, fun) do
|
||||
def to_string({:<<>>, _, args} = ast, fun) do
|
||||
if interpolated?(ast) do
|
||||
fun.(ast, interpolate(ast, fun))
|
||||
else
|
||||
result = Enum.map_join(parts, ", ", fn(part) ->
|
||||
str = bitpart_to_string(part, fun)
|
||||
if :binary.first(str) == ?< or :binary.last(str) == ?> do
|
||||
"(" <> str <> ")"
|
||||
else
|
||||
str
|
||||
end
|
||||
fun.(ast, case Enum.map_join(args, ", ", &to_string(&1, fun)) do
|
||||
"<" <> rest -> "<< <" <> rest <> " >>"
|
||||
rest -> "<<" <> rest <> ">>"
|
||||
end)
|
||||
fun.(ast, "<<" <> result <> ">>")
|
||||
end
|
||||
end
|
||||
|
||||
@@ -631,25 +537,18 @@ defmodule Macro do
|
||||
fun.(ast, "fn\n " <> block <> "\nend")
|
||||
end
|
||||
|
||||
# Ranges
|
||||
def to_string({:.., _, args} = ast, fun) do
|
||||
range = Enum.map_join(args, "..", &to_string(&1, fun))
|
||||
fun.(ast, range)
|
||||
end
|
||||
|
||||
# left -> right
|
||||
def to_string([{:->, _, _} | _] = ast, fun) do
|
||||
def to_string([{:->, _, _}|_] = ast, fun) do
|
||||
fun.(ast, "(" <> arrow_to_string(ast, fun, true) <> ")")
|
||||
end
|
||||
|
||||
# left when right
|
||||
def to_string({:when, _, [left, right]} = ast, fun) do
|
||||
right =
|
||||
if right != [] and Keyword.keyword?(right) do
|
||||
kw_list_to_string(right, fun)
|
||||
else
|
||||
fun.(ast, op_to_string(right, fun, :when, :right))
|
||||
end
|
||||
if right != [] and Keyword.keyword?(right) do
|
||||
right = kw_list_to_string(right, fun)
|
||||
else
|
||||
right = fun.(ast, op_to_string(right, fun, :when, :right))
|
||||
end
|
||||
|
||||
fun.(ast, op_to_string(left, fun, :when, :left) <> " when " <> right)
|
||||
end
|
||||
@@ -676,10 +575,6 @@ defmodule Macro do
|
||||
fun.(ast, "&" <> to_string(mod, fun) <> "." <> Atom.to_string(name) <> "/" <> to_string(arity, fun))
|
||||
end
|
||||
|
||||
def to_string({:&, _, [arg]} = ast, fun) when not is_integer(arg) do
|
||||
fun.(ast, "&(" <> to_string(arg, fun) <> ")")
|
||||
end
|
||||
|
||||
# Unary ops
|
||||
def to_string({unary, _, [{binary, _, [_, _]} = arg]} = ast, fun)
|
||||
when unary in unquote(@unary_ops) and binary in unquote(@binary_ops) do
|
||||
@@ -695,11 +590,6 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# Access
|
||||
def to_string({{:., _, [Access, :get]}, _, [{op, _, _} = left, right]} = ast, fun)
|
||||
when op in unquote(@binary_ops) do
|
||||
fun.(ast, "(" <> to_string(left, fun) <> ")" <> to_string([right], fun))
|
||||
end
|
||||
|
||||
def to_string({{:., _, [Access, :get]}, _, [left, right]} = ast, fun) do
|
||||
fun.(ast, to_string(left, fun) <> to_string([right], fun))
|
||||
end
|
||||
@@ -717,7 +607,7 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
# Two-element tuples
|
||||
# Two-item tuples
|
||||
def to_string({left, right}, fun) do
|
||||
to_string({:{}, [], [left, right]}, fun)
|
||||
end
|
||||
@@ -739,40 +629,16 @@ defmodule Macro do
|
||||
# All other structures
|
||||
def to_string(other, fun), do: fun.(other, inspect(other, []))
|
||||
|
||||
defp bitpart_to_string({:::, _, [left, right]} = ast, fun) do
|
||||
result =
|
||||
op_to_string(left, fun, :::, :left) <>
|
||||
"::" <>
|
||||
bitmods_to_string(right, fun, :::, :right)
|
||||
fun.(ast, result)
|
||||
end
|
||||
|
||||
defp bitpart_to_string(ast, fun) do
|
||||
to_string(ast, fun)
|
||||
end
|
||||
|
||||
defp bitmods_to_string({:-, _, [left, right]} = ast, fun, _, _) do
|
||||
result =
|
||||
bitmods_to_string(left, fun, :-, :left) <>
|
||||
"-" <>
|
||||
bitmods_to_string(right, fun, :-, :right)
|
||||
fun.(ast, result)
|
||||
end
|
||||
|
||||
defp bitmods_to_string(other, fun, parent_op, side) do
|
||||
op_to_string(other, fun, parent_op, side)
|
||||
end
|
||||
|
||||
# Block keywords
|
||||
@kw_keywords [:do, :catch, :rescue, :after, :else]
|
||||
|
||||
defp kw_blocks?([{:do, _} | _] = kw) do
|
||||
defp kw_blocks?([_|_] = kw) do
|
||||
Enum.all?(kw, &match?({x, _} when x in unquote(@kw_keywords), &1))
|
||||
end
|
||||
defp kw_blocks?(_), do: false
|
||||
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_ | _] = parts}) do
|
||||
defp interpolated?({:<<>>, _, [_|_] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]},
|
||||
{:binary, _, _}]} -> true
|
||||
@@ -800,7 +666,7 @@ defmodule Macro do
|
||||
defp module_to_string(atom, _fun) when is_atom(atom), do: inspect(atom, [])
|
||||
defp module_to_string(other, fun), do: call_to_string(other, fun)
|
||||
|
||||
defp sigil_call({func, _, [{:<<>>, _, _} = bin, args]} = ast, fun) when is_atom(func) and is_list(args) do
|
||||
defp sigil_call({func, _, [{:<<>>, _, _} = bin, args]} = ast, fun) when is_list(args) do
|
||||
sigil =
|
||||
case Atom.to_string(func) do
|
||||
<<"sigil_", name>> ->
|
||||
@@ -822,8 +688,6 @@ defmodule Macro do
|
||||
|
||||
defp call_to_string(atom, _fun) when is_atom(atom),
|
||||
do: Atom.to_string(atom)
|
||||
defp call_to_string({:., _, [{:&, _, [val]} = arg]}, fun) when not is_integer(val),
|
||||
do: "(" <> module_to_string(arg, fun) <> ")."
|
||||
defp call_to_string({:., _, [arg]}, fun),
|
||||
do: module_to_string(arg, fun) <> "."
|
||||
defp call_to_string({:., _, [left, right]}, fun),
|
||||
@@ -841,12 +705,9 @@ defmodule Macro do
|
||||
{list, last} = :elixir_utils.split_last(args)
|
||||
|
||||
if last != [] and Keyword.keyword?(last) do
|
||||
prefix =
|
||||
case list do
|
||||
[] -> ""
|
||||
_ -> Enum.map_join(list, ", ", &to_string(&1, fun)) <> ", "
|
||||
end
|
||||
prefix <> kw_list_to_string(last, fun)
|
||||
args = Enum.map_join(list, ", ", &to_string(&1, fun))
|
||||
if list != [], do: args = args <> ", "
|
||||
args <> kw_list_to_string(last, fun)
|
||||
else
|
||||
Enum.map_join(args, ", ", &to_string(&1, fun))
|
||||
end
|
||||
@@ -866,7 +727,7 @@ defmodule Macro do
|
||||
Atom.to_string(key) <> "\n " <> block <> "\n"
|
||||
end
|
||||
|
||||
defp block_to_string([{:->, _, _} | _] = block, fun) do
|
||||
defp block_to_string([{:->, _, _}|_] = block, fun) do
|
||||
Enum.map_join(block, "\n", fn({:->, _, [left, right]}) ->
|
||||
left = comma_join_or_empty_paren(left, fun, false)
|
||||
left <> "->\n " <> adjust_new_lines block_to_string(right, fun), "\n "
|
||||
@@ -976,7 +837,7 @@ defmodule Macro do
|
||||
Consider the implementation below:
|
||||
|
||||
defmacro defmodule_with_length(name, do: block) do
|
||||
length = length(Atom.to_charlist(name))
|
||||
length = length(Atom.to_char_list(name))
|
||||
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@@ -1016,7 +877,7 @@ defmodule Macro do
|
||||
|
||||
defmacro defmodule_with_length(name, do: block) do
|
||||
expanded = Macro.expand(name, __CALLER__)
|
||||
length = length(Atom.to_charlist(expanded))
|
||||
length = length(Atom.to_char_list(expanded))
|
||||
|
||||
quote do
|
||||
defmodule unquote(name) do
|
||||
@@ -1050,6 +911,14 @@ defmodule Macro do
|
||||
end
|
||||
end
|
||||
|
||||
# Expand @ calls
|
||||
defp do_expand_once({:@, _, [{name, _, args}]} = original, env) when is_atom(args) or args == [] do
|
||||
case (module = env.module) && Module.open?(module) do
|
||||
true -> {escape(Module.get_attribute(module, name)), true}
|
||||
false -> {original, false}
|
||||
end
|
||||
end
|
||||
|
||||
# Expand pseudo-variables
|
||||
defp do_expand_once({:__MODULE__, _, atom}, env) when is_atom(atom),
|
||||
do: {env.module, true}
|
||||
@@ -1098,7 +967,7 @@ defmodule Macro do
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
next = :erlang.unique_integer()
|
||||
next = :elixir_counter.next
|
||||
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
|
||||
{:ok, _receiver, _name, _args} ->
|
||||
{original, false}
|
||||
@@ -1119,7 +988,7 @@ defmodule Macro do
|
||||
|
||||
case expand do
|
||||
{:ok, receiver, quoted} ->
|
||||
next = :erlang.unique_integer()
|
||||
next = :elixir_counter.next
|
||||
{:elixir_quote.linify_with_context_counter(0, {receiver, next}, quoted), true}
|
||||
:error ->
|
||||
{original, false}
|
||||
@@ -1148,125 +1017,4 @@ defmodule Macro do
|
||||
defp expand_until({tree, false}, _env) do
|
||||
tree
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given atom or binary to underscore format.
|
||||
|
||||
If an atom is given, it is assumed to be an Elixir module,
|
||||
so it is converted to a binary and then processed.
|
||||
|
||||
This function was designed to underscore language identifiers/tokens,
|
||||
that's why it belongs to the `Macro` module. Do not use it as a general
|
||||
mechanism for underscoring strings as it does not support Unicode or
|
||||
characters that are not valid in Elixir identifiers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.underscore "FooBar"
|
||||
"foo_bar"
|
||||
|
||||
iex> Macro.underscore "Foo.Bar"
|
||||
"foo/bar"
|
||||
|
||||
iex> Macro.underscore Foo.Bar
|
||||
"foo/bar"
|
||||
|
||||
In general, `underscore` can be thought of as the reverse of
|
||||
`camelize`, however, in some cases formatting may be lost:
|
||||
|
||||
iex> Macro.underscore "SAPExample"
|
||||
"sap_example"
|
||||
|
||||
iex> Macro.camelize "sap_example"
|
||||
"SapExample"
|
||||
|
||||
iex> Macro.camelize "hello_10"
|
||||
"Hello10"
|
||||
|
||||
"""
|
||||
def underscore(atom) when is_atom(atom) do
|
||||
"Elixir." <> rest = Atom.to_string(atom)
|
||||
underscore(rest)
|
||||
end
|
||||
|
||||
def underscore(""), do: ""
|
||||
|
||||
def underscore(<<h, t::binary>>) do
|
||||
<<to_lower_char(h)>> <> do_underscore(t, h)
|
||||
end
|
||||
|
||||
defp do_underscore(<<h, t, rest::binary>>, _)
|
||||
when (h >= ?A and h <= ?Z) and not (t >= ?A and t <= ?Z) and t != ?. do
|
||||
<<?_, to_lower_char(h), t>> <> do_underscore(rest, t)
|
||||
end
|
||||
|
||||
defp do_underscore(<<h, t::binary>>, prev)
|
||||
when (h >= ?A and h <= ?Z) and not (prev >= ?A and prev <= ?Z) do
|
||||
<<?_, to_lower_char(h)>> <> do_underscore(t, h)
|
||||
end
|
||||
|
||||
defp do_underscore(<<?., t::binary>>, _) do
|
||||
<<?/>> <> underscore(t)
|
||||
end
|
||||
|
||||
defp do_underscore(<<h, t::binary>>, _) do
|
||||
<<to_lower_char(h)>> <> do_underscore(t, h)
|
||||
end
|
||||
|
||||
defp do_underscore(<<>>, _) do
|
||||
<<>>
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to CamelCase format.
|
||||
|
||||
This function was designed to camelize language identifiers/tokens,
|
||||
that's why it belongs to the `Macro` module. Do not use it as a general
|
||||
mechanism for camelizing strings as it does not support Unicode or
|
||||
characters that are not valid in Elixir identifiers.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Macro.camelize "foo_bar"
|
||||
"FooBar"
|
||||
|
||||
"""
|
||||
@spec camelize(String.t) :: String.t
|
||||
def camelize(string)
|
||||
|
||||
def camelize(""),
|
||||
do: ""
|
||||
|
||||
def camelize(<<?_, t::binary>>),
|
||||
do: camelize(t)
|
||||
|
||||
def camelize(<<h, t::binary>>),
|
||||
do: <<to_upper_char(h)>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<?_, ?_, t::binary>>),
|
||||
do: do_camelize(<<?_, t::binary >>)
|
||||
|
||||
defp do_camelize(<<?_, h, t::binary>>) when h >= ?a and h <= ?z,
|
||||
do: <<to_upper_char(h)>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<?_, h, t::binary>>) when h >= ?0 and h <= ?9,
|
||||
do: <<h>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<?_>>),
|
||||
do: <<>>
|
||||
|
||||
defp do_camelize(<<?/, t::binary>>),
|
||||
do: <<?.>> <> camelize(t)
|
||||
|
||||
defp do_camelize(<<h, t::binary>>),
|
||||
do: <<h>> <> do_camelize(t)
|
||||
|
||||
defp do_camelize(<<>>),
|
||||
do: <<>>
|
||||
|
||||
defp to_upper_char(char) when char >= ?a and char <= ?z, do: char - 32
|
||||
defp to_upper_char(char), do: char
|
||||
|
||||
defp to_lower_char(char) when char >= ?A and char <= ?Z, do: char + 32
|
||||
defp to_lower_char(char), do: char
|
||||
end
|
||||
|
||||
@@ -24,13 +24,13 @@ defmodule Macro.Env do
|
||||
* `module` - the current module name
|
||||
* `file` - the current file name as a binary
|
||||
* `line` - the current line as an integer
|
||||
* `function` - a tuple as `{atom, integer}`, where the first
|
||||
element is the function name and the second its arity; returns
|
||||
* `function` - a tuple as `{atom, integer`}, where the first
|
||||
element is the function name and the seconds its arity; returns
|
||||
`nil` if not inside a function
|
||||
* `context` - the context of the environment; it can be `nil`
|
||||
(default context), inside a guard or inside a match
|
||||
* `aliases` - a list of two-element tuples, where the first
|
||||
element is the aliased name and the second one the actual name
|
||||
(default context), inside a guard or inside an assign
|
||||
* `aliases` - a list of two item tuples, where the first
|
||||
item is the aliased name and the second the actual name
|
||||
* `requires` - the list of required modules
|
||||
* `functions` - a list of functions imported from each module
|
||||
* `macros` - a list of macros imported from each module
|
||||
@@ -94,10 +94,6 @@ defmodule Macro.Env do
|
||||
lexical_tracker: nil}
|
||||
end
|
||||
|
||||
def __struct__(kv) do
|
||||
Enum.reduce kv, __struct__(), fn {k, v}, acc -> :maps.update(k, v, acc) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list containing the file and line
|
||||
information as keys.
|
||||
|
||||
+25
-575
@@ -1,366 +1,37 @@
|
||||
defmodule Map do
|
||||
@moduledoc """
|
||||
A set of functions for working with maps.
|
||||
A Dict implementation that works on maps.
|
||||
|
||||
Maps are key-value stores where keys can be any value and
|
||||
are compared using the match operator (`===`). Maps can be
|
||||
created with the `%{}` special form defined in the
|
||||
`Kernel.SpecialForms` module.
|
||||
Maps are key-value stores where keys are compared using
|
||||
the match operator (`===`). Maps can be created with
|
||||
the `%{}` special form defined in the `Kernel.SpecialForms`
|
||||
module.
|
||||
|
||||
For more information about the functions in this module and
|
||||
their APIs, please consult the `Dict` module.
|
||||
"""
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
use Dict
|
||||
|
||||
defdelegate [keys(map), values(map), size(map), merge(map1, map2), to_list(map)], to: :maps
|
||||
|
||||
@compile {:inline, fetch: 2, put: 3, delete: 2, has_key?: 2}
|
||||
|
||||
@doc """
|
||||
Returns all keys from `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.keys(%{a: 1, b: 2})
|
||||
[:a, :b]
|
||||
|
||||
"""
|
||||
@spec keys(map) :: [key]
|
||||
defdelegate keys(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Returns all values from `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.values(%{a: 1, b: 2})
|
||||
[1, 2]
|
||||
|
||||
"""
|
||||
@spec values(map) :: [value]
|
||||
defdelegate values(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Converts `map` to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.to_list(%{a: 1})
|
||||
[a: 1]
|
||||
iex> Map.to_list(%{1 => 2})
|
||||
[{1, 2}]
|
||||
|
||||
"""
|
||||
@spec to_list(map) :: [{term, term}]
|
||||
defdelegate to_list(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Returns a new empty map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new
|
||||
%{}
|
||||
|
||||
"""
|
||||
@spec new :: map
|
||||
def new, do: %{}
|
||||
|
||||
@doc """
|
||||
Creates a map from an `enumerable`.
|
||||
|
||||
Duplicated keys are removed; the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new([{:b, 1}, {:a, 2}])
|
||||
%{a: 2, b: 1}
|
||||
iex> Map.new([a: 1, a: 2, a: 3])
|
||||
%{a: 3}
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: map
|
||||
def new(enumerable)
|
||||
def new(%{__struct__: _} = struct), do: new_from_enum(struct)
|
||||
def new(%{} = map), do: map
|
||||
def new(enum), do: new_from_enum(enum)
|
||||
|
||||
defp new_from_enum(enumerable) do
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> :maps.from_list
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a map from an `enumerable` via the transformation function.
|
||||
|
||||
Duplicated keys are removed; the latest one prevails.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.new([:a, :b], fn x -> {x, x} end)
|
||||
%{a: :a, b: :b}
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> {key, value})) :: map
|
||||
def new(enumerable, transform) do
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> do_new_transform(transform, [])
|
||||
end
|
||||
|
||||
defp do_new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
defp do_new_transform([item | rest], fun, acc) do
|
||||
do_new_transform(rest, fun, [fun.(item) | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns whether a given `key` exists in the given `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.has_key?(%{a: 1}, :a)
|
||||
true
|
||||
iex> Map.has_key?(%{a: 1}, :b)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec has_key?(map, key) :: boolean
|
||||
def has_key?(map, key), do: :maps.is_key(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
If the `key` does not exist, returns `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.fetch(%{a: 1}, :a)
|
||||
{:ok, 1}
|
||||
iex> Map.fetch(%{a: 1}, :b)
|
||||
:error
|
||||
|
||||
"""
|
||||
@spec fetch(map, key) :: {:ok, value} | :error
|
||||
def fetch(map, key), do: :maps.find(key, map)
|
||||
|
||||
@doc """
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.fetch!(%{a: 1}, :a)
|
||||
1
|
||||
iex> Map.fetch!(%{a: 1}, :b)
|
||||
** (KeyError) key :b not found in: %{a: 1}
|
||||
|
||||
"""
|
||||
@spec fetch!(map, key) :: value | no_return
|
||||
def fetch!(map, key) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.put_new(%{a: 1}, :b, 2)
|
||||
%{b: 2, a: 1}
|
||||
iex> Map.put_new(%{a: 1, b: 2}, :a, 3)
|
||||
%{a: 1, b: 2}
|
||||
|
||||
"""
|
||||
@spec put_new(map, key, value) :: map
|
||||
def put_new(map, key, value) do
|
||||
case has_key?(map, key) do
|
||||
true -> map
|
||||
false -> put(map, key, value)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates `fun` and puts the result under `key`
|
||||
in map unless `key` is already present.
|
||||
|
||||
This is useful if the value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 3
|
||||
...> end
|
||||
iex> Map.put_new_lazy(map, :a, fun)
|
||||
%{a: 1}
|
||||
iex> Map.put_new_lazy(map, :b, fun)
|
||||
%{a: 1, b: 3}
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(map, key, (() -> value)) :: map
|
||||
def put_new_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case has_key?(map, key) do
|
||||
true -> map
|
||||
false -> put(map, key, fun.())
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and
|
||||
returns them in a new map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.take(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec take(map, Enumerable.t) :: map
|
||||
def take(map, keys) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> do_take(map, [])
|
||||
end
|
||||
|
||||
defp do_take([], _map, acc), do: :maps.from_list(acc)
|
||||
defp do_take([key | rest], map, acc) do
|
||||
acc = case fetch(map, key) do
|
||||
{:ok, value} -> [{key, value} | acc]
|
||||
:error -> acc
|
||||
end
|
||||
do_take(rest, map, acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, return the default value
|
||||
(`nil` if no default value).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get(%{}, :a)
|
||||
nil
|
||||
iex> Map.get(%{a: 1}, :a)
|
||||
1
|
||||
iex> Map.get(%{a: 1}, :b)
|
||||
nil
|
||||
iex> Map.get(%{a: 1}, :b, 3)
|
||||
3
|
||||
|
||||
"""
|
||||
@spec get(map, key) :: value
|
||||
@spec get(map, key, value) :: value
|
||||
def get(map, key, default \\ nil) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the value for a specific `key`.
|
||||
|
||||
If `key` does not exist, lazily evaluates `fun` and returns its result.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Map.get_lazy(map, :a, fun)
|
||||
1
|
||||
iex> Map.get_lazy(map, :b, fun)
|
||||
13
|
||||
|
||||
"""
|
||||
@spec get_lazy(map, key, (() -> value)) :: value
|
||||
def get_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> fun.()
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.put(%{a: 1}, :b, 2)
|
||||
%{a: 1, b: 2}
|
||||
iex> Map.put(%{a: 1, b: 2}, :a, 3)
|
||||
%{a: 3, b: 2}
|
||||
|
||||
"""
|
||||
@spec put(map, key, value) :: map
|
||||
def put(map, key, val) do
|
||||
:maps.put(key, val, map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the entries in `map` for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns `map` unchanged.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.delete(%{a: 1, b: 2}, :a)
|
||||
%{b: 2}
|
||||
iex> Map.delete(%{b: 2}, :a)
|
||||
%{b: 2}
|
||||
|
||||
"""
|
||||
@spec delete(map, key) :: map
|
||||
def delete(map, key), do: :maps.remove(key, map)
|
||||
|
||||
@doc """
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`, overriding any existing one.
|
||||
|
||||
If you have a struct and you would like to merge a set of keys into the
|
||||
struct, do not use this function, as it would merge all keys on the right
|
||||
side into the struct, even if the key is not part of the struct. Instead,
|
||||
use `Kernel.struct/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})
|
||||
%{a: 3, b: 2, d: 4}
|
||||
|
||||
"""
|
||||
@spec merge(map, map) :: map
|
||||
defdelegate merge(map1, map2), to: :maps
|
||||
|
||||
@doc """
|
||||
Merges two maps into one.
|
||||
|
||||
All keys in `map2` will be added to `map1`. The given function will
|
||||
be invoked with the key, value1 and value2 to solve conflicts.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4}, fn _k, v1, v2 ->
|
||||
...> v1 + v2
|
||||
...> end)
|
||||
%{a: 4, b: 2, d: 4}
|
||||
|
||||
"""
|
||||
@spec merge(map, map, (key, value, value -> value)) :: map
|
||||
def merge(map1, map2, callback) do
|
||||
:maps.fold fn k, v2, acc ->
|
||||
update(acc, k, v2, fn(v1) -> callback.(k, v1, v2) end)
|
||||
@@ -368,145 +39,8 @@ defmodule Map do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` in `map` with the given function.
|
||||
|
||||
If the `key` does not exist, inserts the given `initial` value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.update(%{a: 1}, :a, 13, &(&1 * 2))
|
||||
%{a: 2}
|
||||
iex> Map.update(%{a: 1}, :b, 11, &(&1 * 2))
|
||||
%{a: 1, b: 11}
|
||||
|
||||
Updates the value in the map with the given function.
|
||||
"""
|
||||
@spec update(map, key, value, (value -> value)) :: map
|
||||
def update(map, key, initial, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
put(map, key, fun.(value))
|
||||
:error ->
|
||||
put(map, key, initial)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns and removes the value associated with `key` in `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.pop(%{a: 1}, :a)
|
||||
{1, %{}}
|
||||
iex> Map.pop(%{a: 1}, :b)
|
||||
{nil, %{a: 1}}
|
||||
iex> Map.pop(%{a: 1}, :b, 3)
|
||||
{3, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop(map, key, value) :: {value, map}
|
||||
def pop(map, key, default \\ nil) do
|
||||
case map do
|
||||
%{^key => value} -> {value, delete(map, key)}
|
||||
%{} -> {default, map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes the value associated with `key` in `map`.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> map = %{a: 1}
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Map.pop_lazy(map, :a, fun)
|
||||
{1, %{}}
|
||||
iex> Map.pop_lazy(map, :b, fun)
|
||||
{13, %{a: 1}}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(map, key, (() -> value)) :: {value, map}
|
||||
def pop_lazy(map, key, fun) when is_function(fun, 0) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> {value, delete(map, key)}
|
||||
:error -> {fun.(), map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Drops the given `keys` from `map`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.drop(%{a: 1, b: 2, c: 3}, [:b, :d])
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, Enumerable.t) :: map
|
||||
def drop(map, keys) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> drop_list(map)
|
||||
end
|
||||
|
||||
defp drop_list([], acc), do: acc
|
||||
defp drop_list([key | rest], acc) do
|
||||
drop_list(rest, Map.delete(acc, key))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given `keys` and extracts them into a
|
||||
separate `map`.
|
||||
|
||||
Returns a tuple with the new map and the old map with removed keys.
|
||||
|
||||
Keys for which there are no entries in `map` are ignored.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.split(%{a: 1, b: 2, c: 3}, [:a, :c, :e])
|
||||
{%{a: 1, c: 3}, %{b: 2}}
|
||||
|
||||
"""
|
||||
@spec split(map, Enumerable.t) :: {map, map}
|
||||
def split(map, keys) do
|
||||
keys
|
||||
|> Enum.to_list
|
||||
|> do_split([], map)
|
||||
end
|
||||
|
||||
defp do_split([], inc, exc) do
|
||||
{:maps.from_list(inc), exc}
|
||||
end
|
||||
defp do_split([key | rest], inc, exc) do
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
do_split(rest, [{key, value} | inc], delete(exc, key))
|
||||
:error ->
|
||||
do_split(rest, inc, exc)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` with the given function.
|
||||
|
||||
If the `key` does not exist, raises `KeyError`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.update!(%{a: 1}, :a, &(&1 * 2))
|
||||
%{a: 2}
|
||||
|
||||
iex> Map.update!(%{a: 1}, :b, &(&1 * 2))
|
||||
** (KeyError) key :b not found
|
||||
|
||||
"""
|
||||
@spec update!(map, key, (value -> value)) :: map | no_return
|
||||
def update!(%{} = map, key, fun) do
|
||||
case fetch(map, key) do
|
||||
{:ok, value} ->
|
||||
@@ -519,90 +53,28 @@ defmodule Map do
|
||||
def update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it, all in one pass.
|
||||
|
||||
This `fun` argument receives the value of `key` (or `nil` if `key`
|
||||
is not present) and must return a two-element tuple: the "get" value
|
||||
(the retrieved value, which can be operated on before being returned)
|
||||
and the new value to be stored under `key`. The `fun` may also
|
||||
return `:pop`, implying the current value shall be removed
|
||||
from `map` and returned.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by
|
||||
`fun` and a new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, %{a: "new value!"}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{nil, %{b: "new value!", a: 1}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :a, fn _ -> :pop end)
|
||||
{1, %{}}
|
||||
|
||||
iex> Map.get_and_update(%{a: 1}, :b, fn _ -> :pop end)
|
||||
{nil, %{a: 1}}
|
||||
|
||||
Gets a value and updates a map in one operation.
|
||||
"""
|
||||
@spec get_and_update(map, key, (value -> {get, value} | :pop)) :: {get, map} when get: term
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current =
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
case fun.(current) do
|
||||
{get, update} -> {get, :maps.put(key, update, map)}
|
||||
:pop -> {current, :maps.remove(key, map)}
|
||||
current_value = case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Gets the value from `key` and updates it. Raises if there is no `key`.
|
||||
|
||||
This `fun` argument receives the value of `key` and must return a
|
||||
two-element tuple: the "get" value (the retrieved value, which can be
|
||||
operated on before being returned) and the new value to be stored under
|
||||
`key`.
|
||||
|
||||
The returned value is a tuple with the "get" value returned by `fun` and a
|
||||
new map with the updated value under `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
{1, %{a: "new value!"}}
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :b, fn current_value ->
|
||||
...> {current_value, "new value!"}
|
||||
...> end)
|
||||
** (KeyError) key :b not found
|
||||
|
||||
iex> Map.get_and_update!(%{a: 1}, :a, fn _ ->
|
||||
...> :pop
|
||||
...> end)
|
||||
{1, %{}}
|
||||
|
||||
Gets a value and updates a map only if the key exists in one operation.
|
||||
"""
|
||||
@spec get_and_update!(map, key, (value -> {get, value})) :: {get, map} | no_return when get: term
|
||||
def get_and_update!(%{} = map, key, fun) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} ->
|
||||
case fun.(value) do
|
||||
{get, update} -> {get, :maps.put(key, update, map)}
|
||||
:pop -> {value, :maps.remove(key, map)}
|
||||
end
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
:error ->
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
@@ -611,7 +83,7 @@ defmodule Map do
|
||||
def get_and_update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Converts a `struct` to map.
|
||||
Converts a struct to map.
|
||||
|
||||
It accepts the struct module or a struct itself and
|
||||
simply removes the `__struct__` field from the struct.
|
||||
@@ -629,7 +101,6 @@ defmodule Map do
|
||||
#=> %{name: "john"}
|
||||
|
||||
"""
|
||||
@spec from_struct(atom | struct) :: map
|
||||
def from_struct(struct) when is_atom(struct) do
|
||||
:maps.remove(:__struct__, struct.__struct__)
|
||||
end
|
||||
@@ -638,27 +109,6 @@ defmodule Map do
|
||||
:maps.remove(:__struct__, struct)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two maps are equal.
|
||||
|
||||
Two maps are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 2, a: 1})
|
||||
true
|
||||
iex> Map.equal?(%{a: 1, b: 2}, %{b: 1, a: 2})
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(map, map) :: boolean
|
||||
def equal?(map1, map2)
|
||||
def equal?(%{} = map1, %{} = map2), do: map1 === map2
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def size(map) do
|
||||
IO.warn "Map.size/1 is deprecated, please use Kernel.map_size/1"
|
||||
map_size(map)
|
||||
end
|
||||
end
|
||||
|
||||
+46
-276
@@ -1,327 +1,97 @@
|
||||
defmodule MapSet do
|
||||
@moduledoc """
|
||||
A set of functions for working with sets.
|
||||
A set store.
|
||||
|
||||
The `MapSet` is represented internally as a struct,
|
||||
therefore `%MapSet{}` can be used whenever there is a
|
||||
need to match on any `MapSet`. Note though the struct
|
||||
fields are private and must not be accessed directly.
|
||||
Instead, use the functions in this module.
|
||||
The `MapSet` is represented internally as a struct, therefore
|
||||
`%MapSet{}` can be used whenever there is a need to match
|
||||
on any `MapSet`. Note though the struct fields are private and
|
||||
must not be accessed directly. Instead, use the functions on this
|
||||
or in the `Set` module.
|
||||
|
||||
The `MapSet` is implemented using `Map` data type.
|
||||
For more information about the functions
|
||||
and their APIs, please consult the `Set` module.
|
||||
"""
|
||||
|
||||
@opaque t :: %__MODULE__{map: map}
|
||||
@type value :: term
|
||||
@behaviour Set
|
||||
|
||||
defstruct map: %{}
|
||||
|
||||
@doc """
|
||||
Returns a new set.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@spec new :: t
|
||||
def new(), do: %MapSet{}
|
||||
|
||||
@doc """
|
||||
Creates a set from an enumerable.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new([:b, :a, 3])
|
||||
#MapSet<[3, :a, :b]>
|
||||
iex> MapSet.new([3, 3, 3, 2, 2, 1])
|
||||
#MapSet<[1, 2, 3]>
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: t
|
||||
def new(%__MODULE__{} = mapset), do: mapset
|
||||
def new(enumerable) do
|
||||
map =
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> do_new([])
|
||||
|
||||
%MapSet{map: map}
|
||||
def delete(%MapSet{map: map} = set, term) do
|
||||
%{set | map: Map.delete(map, term)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a mapset from an enumerable via the transformation function.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
|
||||
#MapSet<[2, 4]>
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> term)) :: t
|
||||
def new(enumerable, transform) do
|
||||
map =
|
||||
enumerable
|
||||
|> Enum.to_list
|
||||
|> do_new_transform(transform, [])
|
||||
|
||||
%MapSet{map: map}
|
||||
def difference(%MapSet{} = set1, %MapSet{} = set2) do
|
||||
reduce(set2, {:cont, set1}, fn value, acc ->
|
||||
{:cont, delete(acc, value)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
defp do_new([], acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
defp do_new([item | rest], acc) do
|
||||
do_new(rest, [{item, true} | acc])
|
||||
def disjoint?(%MapSet{} = set1, %MapSet{} = set2) do
|
||||
if size(set1) > size(set2), do: {set1, set2} = {set2, set1}
|
||||
reduce(set1, {:cont, true}, fn value, _ ->
|
||||
if member?(set2, value) do
|
||||
{:halt, false}
|
||||
else
|
||||
{:cont, true}
|
||||
end
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
defp do_new_transform([], _fun, acc) do
|
||||
acc
|
||||
|> :lists.reverse
|
||||
|> :maps.from_list
|
||||
end
|
||||
defp do_new_transform([item | rest], fun, acc) do
|
||||
do_new_transform(rest, fun, [{fun.(item), true} | acc])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes `value` from `set`.
|
||||
|
||||
Returns a new set which is a copy of `set` but without `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> set = MapSet.new([1, 2, 3])
|
||||
iex> MapSet.delete(set, 4)
|
||||
#MapSet<[1, 2, 3]>
|
||||
iex> MapSet.delete(set, 2)
|
||||
#MapSet<[1, 3]>
|
||||
|
||||
"""
|
||||
@spec delete(t, value) :: t
|
||||
def delete(%MapSet{map: map} = set, value) do
|
||||
%{set | map: Map.delete(map, value)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set that is `set1` without the members of `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[1]>
|
||||
|
||||
"""
|
||||
@spec difference(t, t) :: t
|
||||
# If the first set is less than twice the size of the second map,
|
||||
# it is fastest to re-accumulate items in the first set that are not
|
||||
# present in the second set.
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2})
|
||||
when map_size(map1) < map_size(map2) * 2 do
|
||||
map = map1
|
||||
|> Map.keys
|
||||
|> filter_not_in(map2)
|
||||
|
||||
%MapSet{map: map}
|
||||
end
|
||||
|
||||
# If the second set is less than half the size of the first set, it's fastest
|
||||
# to simply iterate through each item in the second set, deleting them from
|
||||
# the first set.
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
%MapSet{map: Map.drop(map1, Map.keys(map2))}
|
||||
end
|
||||
|
||||
defp filter_not_in(keys, map2, acc \\ [])
|
||||
defp filter_not_in([], _map2, acc), do: :maps.from_list(acc)
|
||||
defp filter_not_in([key | rest], map2, acc) do
|
||||
acc = if Map.has_key?(map2, key) do
|
||||
acc
|
||||
else
|
||||
[{key, true} | acc]
|
||||
end
|
||||
filter_not_in(rest, map2, acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1` and `set2` have no members in common.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
true
|
||||
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([2, 3]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
{map1, map2} = order_by_size(map1, map2)
|
||||
|
||||
map1
|
||||
|> Map.keys
|
||||
|> none_in?(map2)
|
||||
end
|
||||
|
||||
defp none_in?([], _) do
|
||||
true
|
||||
end
|
||||
defp none_in?([key | rest], map2) do
|
||||
case Map.has_key?(map2, key) do
|
||||
true -> false
|
||||
false -> none_in?(rest, map2)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two sets are equal.
|
||||
|
||||
The comparison between elements must be done using `===`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([2, 1, 1]))
|
||||
true
|
||||
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
Map.equal?(map1, map2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing only members that `set1` and `set2` have in common.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[2]>
|
||||
|
||||
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))
|
||||
#MapSet<[]>
|
||||
|
||||
"""
|
||||
@spec intersection(t, t) :: t
|
||||
def intersection(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
{map1, map2} = order_by_size(map1, map2)
|
||||
|
||||
%MapSet{map: Map.take(map2, Map.keys(map1))}
|
||||
def intersection(%MapSet{} = set1, %MapSet{} = set2) do
|
||||
if size(set1) > size(set2), do: {set1, set2} = {set2, set1}
|
||||
reduce(set1, {:cont, new}, fn value, acc ->
|
||||
if member?(set2, value) do
|
||||
{:cont, put(acc, value)}
|
||||
else
|
||||
{:cont, acc}
|
||||
end
|
||||
end) |> elem(1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set` contains `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.member?(MapSet.new([1, 2, 3]), 2)
|
||||
true
|
||||
iex> MapSet.member?(MapSet.new([1, 2, 3]), 4)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec member?(t, value) :: boolean
|
||||
def member?(%MapSet{map: map}, value) do
|
||||
Map.has_key?(map, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts `value` into `set` if `set` doesn't already contain it.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.put(MapSet.new([1, 2, 3]), 3)
|
||||
#MapSet<[1, 2, 3]>
|
||||
iex> MapSet.put(MapSet.new([1, 2, 3]), 4)
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec put(t, value) :: t
|
||||
def put(%MapSet{map: map} = set, value) do
|
||||
%{set | map: Map.put(map, value, true)}
|
||||
%{set | map: Map.put(map, value, nil)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of elements in `set`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.size(MapSet.new([1, 2, 3]))
|
||||
3
|
||||
|
||||
"""
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(%MapSet{map: map}) do
|
||||
map_size(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1`'s members are all contained in `set2`.
|
||||
|
||||
This function checks if `set1` is a subset of `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.subset?(MapSet.new([1, 2]), MapSet.new([1, 2, 3]))
|
||||
true
|
||||
iex> MapSet.subset?(MapSet.new([1, 2, 3]), MapSet.new([1, 2]))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec subset?(t, t) :: boolean
|
||||
def subset?(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
if map_size(map1) <= map_size(map2) do
|
||||
map1
|
||||
|> Map.keys
|
||||
|> do_subset?(map2)
|
||||
def subset?(%MapSet{} = set1, %MapSet{} = set2) do
|
||||
if size(set1) <= size(set2) do
|
||||
reduce(set1, {:cont, true}, fn value, _ ->
|
||||
if member?(set2, value), do: {:cont, true}, else: {:halt, false}
|
||||
end) |> elem(1)
|
||||
else
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp do_subset?([], _), do: true
|
||||
defp do_subset?([key | rest], map2) do
|
||||
if Map.has_key?(map2, key) do
|
||||
do_subset?(rest, map2)
|
||||
else
|
||||
false
|
||||
end
|
||||
@doc false
|
||||
def reduce(%MapSet{} = set, acc, fun) do
|
||||
Enumerable.List.reduce(to_list(set), acc, fun)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `set` to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.to_list(MapSet.new([1, 2, 3]))
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(%MapSet{map: map}) do
|
||||
Map.keys(map)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing all members of `set1` and `set2`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
|
||||
#MapSet<[1, 2, 3, 4]>
|
||||
|
||||
"""
|
||||
@spec union(t, t) :: t
|
||||
def union(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
%MapSet{map: Map.merge(map1, map2)}
|
||||
end
|
||||
|
||||
defp order_by_size(map1, map2) when map_size(map1) > map_size(map2), do: {map2, map1}
|
||||
defp order_by_size(map1, map2), do: {map1, map2}
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(set, acc, fun), do: Enumerable.List.reduce(MapSet.to_list(set), acc, fun)
|
||||
def reduce(set, acc, fun), do: MapSet.reduce(set, acc, fun)
|
||||
def member?(set, val), do: {:ok, MapSet.member?(set, val)}
|
||||
def count(set), do: {:ok, MapSet.size(set)}
|
||||
end
|
||||
|
||||
+288
-383
@@ -1,9 +1,8 @@
|
||||
defmodule Module do
|
||||
@moduledoc ~S'''
|
||||
Provides functions to deal with modules during compilation time.
|
||||
|
||||
It allows a developer to dynamically add, delete and register
|
||||
attributes, attach documentation and so forth.
|
||||
This module provides many functions to deal with modules during
|
||||
compilation time. It allows a developer to dynamically attach
|
||||
documentation, add, delete and register attributes and so forth.
|
||||
|
||||
After a module is compiled, using many of the functions in
|
||||
this module will raise errors, since it is out of their scope
|
||||
@@ -18,77 +17,86 @@ defmodule Module do
|
||||
* `@after_compile`
|
||||
|
||||
A hook that will be invoked right after the current module is compiled.
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`.
|
||||
See the "Compile callbacks" section below.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`. The function
|
||||
must take two arguments: the module environment and its bytecode.
|
||||
When just a module is provided, the function is assumed to be
|
||||
`__after_compile__/2`.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@after_compile __MODULE__
|
||||
|
||||
def __after_compile__(env, _bytecode) do
|
||||
IO.inspect env
|
||||
end
|
||||
end
|
||||
|
||||
* `@before_compile`
|
||||
|
||||
A hook that will be invoked before the module is compiled.
|
||||
Accepts a module or a tuple `{<module>, <function/macro atom>}`.
|
||||
See the "Compile callbacks" section below.
|
||||
|
||||
* `@behaviour` (notice the British spelling)
|
||||
Accepts a module or a tuple `{<module>, <function/macro atom>}`. The
|
||||
function/macro must take one argument: the module environment. If it's a
|
||||
macro, its returned value will be injected at the end of the module definition
|
||||
before the compilation starts.
|
||||
|
||||
Behaviours can be referenced by modules to ensure they implement
|
||||
required specific function signatures defined by `@callback`.
|
||||
When just a module is provided, the function/macro is assumed to be
|
||||
`__before_compile__/1`.
|
||||
|
||||
For example, you can specify the URI.Parser behaviour as follows:
|
||||
Note: unlike `@after_compile`, the callback function/macro must
|
||||
be placed in a separate module (because when the callback is invoked,
|
||||
the current module does not yet exist).
|
||||
|
||||
defmodule URI.Parser do
|
||||
@doc "Parses the given URL"
|
||||
@callback parse(uri_info :: URI.t) :: URI.t
|
||||
### Example
|
||||
|
||||
@doc "Defines a default port"
|
||||
@callback default_port() :: integer
|
||||
defmodule A do
|
||||
defmacro __before_compile__(_env) do
|
||||
quote do
|
||||
def hello, do: "world"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
And then a module may use it as:
|
||||
|
||||
defmodule URI.HTTP do
|
||||
@behaviour URI.Parser
|
||||
def default_port(), do: 80
|
||||
def parse(info), do: info
|
||||
defmodule B do
|
||||
@before_compile A
|
||||
end
|
||||
|
||||
If the behaviour changes or URI.HTTP does not implement one of the
|
||||
callbacks, a warning will be raised.
|
||||
* `@behaviour` (notice the British spelling)
|
||||
|
||||
Specifies an OTP or user-defined behaviour.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@behaviour :gen_event
|
||||
@behaviour gen_event
|
||||
|
||||
# ...
|
||||
end
|
||||
|
||||
* `@callback`, `@macrocallback`, and `@optional_callbacks`
|
||||
|
||||
These attributes are used to define a behaviour (as shown in the
|
||||
documentation for `@behaviour` above). `@callback` defines a function
|
||||
callback, `@macrocallback` defines a macro callback, and
|
||||
`@optional_callbacks` specifies which callbacks and macrocallbacks are
|
||||
optional.
|
||||
|
||||
* `@compile`
|
||||
|
||||
Defines options for module compilation. This is used to configure
|
||||
both Elixir and Erlang compilers, as any other compilation pass
|
||||
added by external tools.
|
||||
Defines options for module compilation that are passed to the Erlang
|
||||
compiler.
|
||||
|
||||
Multiple uses of `@compile` will accumulate instead of overriding
|
||||
previous ones. See the "Compile options" section below.
|
||||
Accepts an atom, a tuple, or a list of atoms and tuples.
|
||||
|
||||
For the list of supported options, see Erlang's
|
||||
[`:compile` module](http://www.erlang.org/doc/man/compile.html).
|
||||
|
||||
Several uses of `@compile` will accumulate instead of overriding
|
||||
previous ones.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@compile {:inline, myfun: 1}
|
||||
defmodule M do
|
||||
@compile {:inline, myfun: 1}
|
||||
|
||||
def myfun(arg) do
|
||||
to_string(arg)
|
||||
def myfun(arg) do
|
||||
to_string(arg)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
* `@doc`
|
||||
|
||||
@@ -103,42 +111,144 @@ defmodule Module do
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
def hello do
|
||||
"world"
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sums `a` to `b`.
|
||||
"""
|
||||
def sum(a, b) do
|
||||
a + b
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sums `a` to `b`.
|
||||
"""
|
||||
def sum(a, b) do
|
||||
a + b
|
||||
end
|
||||
end
|
||||
* `@file`
|
||||
|
||||
* `@dialyzer`
|
||||
Changes the filename used in stacktraces for the function or macro that
|
||||
follows the attribute.
|
||||
|
||||
Defines warnings to request or suppress when using a version of
|
||||
`:dialyzer` that supports module attributes.
|
||||
|
||||
Accepts an atom, a tuple, or a list of atoms and tuples.
|
||||
|
||||
For the list of supported warnings, see
|
||||
[`:dialyzer` module](http://www.erlang.org/doc/man/dialyzer.html).
|
||||
|
||||
Multiple uses of `@dialyzer` will accumulate instead of overriding
|
||||
previous ones.
|
||||
Accepts a string. Can be used more than once.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@dialyzer {:nowarn_function, myfun: 1}
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
@file "hello.ex"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
end
|
||||
|
||||
def myfun(arg) do
|
||||
M.not_a_function(arg)
|
||||
* `@moduledoc`
|
||||
|
||||
Provides documentation for the current module.
|
||||
|
||||
Accepts a string (which is often a heredoc) or `false` where
|
||||
`@moduledoc false` will make the module invisible to the
|
||||
documentation extraction tools like ExDoc.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@moduledoc """
|
||||
A very useful module
|
||||
"""
|
||||
end
|
||||
|
||||
|
||||
* `@on_definition`
|
||||
|
||||
A hook that will be invoked when each function or macro in the current
|
||||
module is defined. Useful when annotating functions.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`. The function
|
||||
must take 6 arguments:
|
||||
|
||||
- the module environment
|
||||
- kind: `:def`, `:defp`, `:defmacro`, or `:defmacrop`
|
||||
- function/macro name
|
||||
- list of quoted arguments
|
||||
- list of quoted guards
|
||||
- quoted function body
|
||||
|
||||
Note the hook receives the quoted arguments and it is invoked before
|
||||
the function is stored in the module. So `Module.defines?/2` will return
|
||||
`false` for the first clause of every function.
|
||||
|
||||
If the function/macro being defined has multiple clauses, the hook will
|
||||
be called for each clause.
|
||||
|
||||
Unlike other hooks, `@on_definition` will only invoke functions
|
||||
and never macros. This is because the hook is invoked inside the context
|
||||
of the function (and nested function definitions are not allowed in
|
||||
Elixir).
|
||||
|
||||
When just a module is provided, the function is assumed to be
|
||||
`__on_definition__/6`.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule H do
|
||||
def on_def(_env, kind, name, args, guards, body) do
|
||||
IO.puts "Defining #{kind} named #{name} with args:"
|
||||
IO.inspect args
|
||||
IO.puts "and guards"
|
||||
IO.inspect guards
|
||||
IO.puts "and body"
|
||||
IO.puts Macro.to_string(body)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule M do
|
||||
@on_definition {H, :on_def}
|
||||
|
||||
def hello(arg) when is_binary(arg) or is_list(arg) do
|
||||
"Hello" <> to_string(arg)
|
||||
end
|
||||
|
||||
def hello(_) do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
* `@on_load`
|
||||
|
||||
A hook that will be invoked whenever the module is loaded.
|
||||
|
||||
Accepts a function atom of a function in the current module. The function
|
||||
must have arity 0 (no arguments) and has to return `:ok`, otherwise the
|
||||
loading of the module will be aborted.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@on_load :load_check
|
||||
|
||||
def load_check do
|
||||
if some_condition() do
|
||||
:ok
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def some_condition do
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
* `@vsn`
|
||||
|
||||
Specify the module version. Accepts any valid Elixir value.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@vsn "1.0"
|
||||
end
|
||||
end
|
||||
|
||||
* `@external_resource`
|
||||
|
||||
@@ -151,236 +261,54 @@ defmodule Module do
|
||||
Tools like Mix may use this information to ensure the module is
|
||||
recompiled in case any of the external resources change.
|
||||
|
||||
* `@file`
|
||||
* `@dialyzer`
|
||||
|
||||
Changes the filename used in stacktraces for the function or macro that
|
||||
follows the attribute.
|
||||
Defines warnings to request or suppress when using a version of
|
||||
`:dialyzer` that supports module attributes.
|
||||
|
||||
Accepts a string. Can be used more than once.
|
||||
Accepts an atom, a tuple, or a list of atoms and tuples.
|
||||
|
||||
For the list of supported warnings, see
|
||||
[`:dialyzer` module](http://www.erlang.org/doc/man/dialyzer.html).
|
||||
|
||||
Several uses of `@dialyzer` will accumulate instead of overriding
|
||||
previous ones.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
@file "hello.ex"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
end
|
||||
defmodule M do
|
||||
@dialyzer {:nowarn_function, myfun: 1}
|
||||
|
||||
* `@moduledoc`
|
||||
|
||||
Provides documentation for the current module.
|
||||
|
||||
Accepts a string (which is often a heredoc) or `false` where
|
||||
`@moduledoc false` will make the module invisible to the
|
||||
documentation extraction tools like ExDoc.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@moduledoc """
|
||||
A very useful module
|
||||
"""
|
||||
end
|
||||
|
||||
* `@on_definition`
|
||||
|
||||
A hook that will be invoked when each function or macro in the current
|
||||
module is defined. Useful when annotating functions.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`. See the
|
||||
"Compile callbacks" section below.
|
||||
|
||||
* `@on_load`
|
||||
|
||||
A hook that will be invoked whenever the module is loaded.
|
||||
|
||||
Accepts a function atom of a function in the current module. The function
|
||||
must have arity 0 (no arguments) and has to return `:ok`, otherwise the
|
||||
loading of the module will be aborted.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@on_load :load_check
|
||||
|
||||
def load_check do
|
||||
if some_condition() do
|
||||
:ok
|
||||
else
|
||||
nil
|
||||
def myfun(arg) do
|
||||
M.not_a_function(arg)
|
||||
end
|
||||
end
|
||||
|
||||
def some_condition do
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
* `@vsn`
|
||||
|
||||
Specify the module version. Accepts any valid Elixir value.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@vsn "1.0"
|
||||
end
|
||||
|
||||
The following attributes are part of typespecs and are also reserved by
|
||||
Elixir:
|
||||
Elixir (see `Kernel.Typespec` for more information about typespecs):
|
||||
|
||||
* `@type` - defines a type to be used in `@spec`
|
||||
* `@typep` - defines a private type to be used in `@spec`
|
||||
* `@opaque` - defines an opaque type to be used in `@spec`
|
||||
* `@spec` - provides a specification for a function
|
||||
* `@callback` - provides a specification for a behaviour callback
|
||||
* `@type` - defines a type to be used in `@spec`
|
||||
* `@typep` - defines a private type to be used in `@spec`
|
||||
* `@opaque` - defines an opaque type to be used in `@spec`
|
||||
* `@spec` - provides a specification for a function
|
||||
* `@callback` - provides a specification for a behaviour callback
|
||||
* `@macrocallback` - provides a specification for a macro behaviour callback
|
||||
* `@optional_callbacks` - specifies which behaviour callbacks and macro
|
||||
behaviour callbacks are optional
|
||||
|
||||
In addition to the built-in attributes outlined above, custom attributes may
|
||||
also be added. A custom attribute is any valid identifier prefixed with an
|
||||
`@` and followed by a valid Elixir value:
|
||||
|
||||
defmodule M do
|
||||
@custom_attr [some: "stuff"]
|
||||
end
|
||||
defmodule M do
|
||||
@custom_attr [some: "stuff"]
|
||||
end
|
||||
|
||||
For more advanced options available when defining custom attributes, see
|
||||
`register_attribute/3`.
|
||||
|
||||
## Compile callbacks
|
||||
## Runtime information about a module
|
||||
|
||||
There are three callbacks that are invoked when functions are defined,
|
||||
as well as before and immediately after the module bytecode is generated.
|
||||
|
||||
### `@after_compile`
|
||||
|
||||
A hook that will be invoked right after the current module is compiled.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`. The function
|
||||
must take two arguments: the module environment and its bytecode.
|
||||
When just a module is provided, the function is assumed to be
|
||||
`__after_compile__/2`.
|
||||
|
||||
#### Example
|
||||
|
||||
defmodule M do
|
||||
@after_compile __MODULE__
|
||||
|
||||
def __after_compile__(env, _bytecode) do
|
||||
IO.inspect env
|
||||
end
|
||||
end
|
||||
|
||||
### `@before_compile`
|
||||
|
||||
A hook that will be invoked before the module is compiled.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function/macro atom>}`. The
|
||||
function/macro must take one argument: the module environment. If it's a
|
||||
macro, its returned value will be injected at the end of the module definition
|
||||
before the compilation starts.
|
||||
|
||||
When just a module is provided, the function/macro is assumed to be
|
||||
`__before_compile__/1`.
|
||||
|
||||
Note: unlike `@after_compile`, the callback function/macro must
|
||||
be placed in a separate module (because when the callback is invoked,
|
||||
the current module does not yet exist).
|
||||
|
||||
#### Example
|
||||
|
||||
defmodule A do
|
||||
defmacro __before_compile__(_env) do
|
||||
quote do
|
||||
def hello, do: "world"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmodule B do
|
||||
@before_compile A
|
||||
end
|
||||
|
||||
### `@on_definition`
|
||||
|
||||
A hook that will be invoked when each function or macro in the current
|
||||
module is defined. Useful when annotating functions.
|
||||
|
||||
Accepts a module or a tuple `{<module>, <function atom>}`. The function
|
||||
must take 6 arguments:
|
||||
|
||||
- the module environment
|
||||
- kind: `:def`, `:defp`, `:defmacro`, or `:defmacrop`
|
||||
- function/macro name
|
||||
- list of quoted arguments
|
||||
- list of quoted guards
|
||||
- quoted function body
|
||||
|
||||
Note the hook receives the quoted arguments and it is invoked before
|
||||
the function is stored in the module. So `Module.defines?/2` will return
|
||||
`false` for the first clause of every function.
|
||||
|
||||
If the function/macro being defined has multiple clauses, the hook will
|
||||
be called for each clause.
|
||||
|
||||
Unlike other hooks, `@on_definition` will only invoke functions
|
||||
and never macros. This is because the hook is invoked inside the context
|
||||
of the function (and nested function definitions are not allowed in
|
||||
Elixir).
|
||||
|
||||
When just a module is provided, the function is assumed to be
|
||||
`__on_definition__/6`.
|
||||
|
||||
#### Example
|
||||
|
||||
defmodule H do
|
||||
def on_def(_env, kind, name, args, guards, body) do
|
||||
IO.puts "Defining #{kind} named #{name} with args:"
|
||||
IO.inspect args
|
||||
IO.puts "and guards"
|
||||
IO.inspect guards
|
||||
IO.puts "and body"
|
||||
IO.puts Macro.to_string(body)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule M do
|
||||
@on_definition {H, :on_def}
|
||||
|
||||
def hello(arg) when is_binary(arg) or is_list(arg) do
|
||||
"Hello" <> to_string(arg)
|
||||
end
|
||||
|
||||
def hello(_) do
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
## Compile options
|
||||
|
||||
The `@compile` attribute accepts diverse options that is used by both
|
||||
Elixir and Erlang compilers. Some of the common use cases are documented
|
||||
below:
|
||||
|
||||
* `@compile :debug_info` - includes `:debug_info` regardless of the
|
||||
setting in `Code.compiler_options`
|
||||
|
||||
* `@compile {:debug_info, false}` - disables `:debug_info` regardless
|
||||
of the setting in `Code.compiler_options`
|
||||
|
||||
* `@compile {:inline, some_fun: 2, other_fun: 3}` - inlines the given
|
||||
name/arity pairs
|
||||
|
||||
* `@compile {:autoload, false}` - disables automatic loading of
|
||||
modules after compilation. Instead, the module will be loaded after
|
||||
it is dispatched to
|
||||
|
||||
You can see a handful more options used by the Erlang compiler in
|
||||
the documentation for the `:compile` module.
|
||||
It is possible to query a module at runtime to find out which functions and
|
||||
macros it defines, extract its docstrings, etc. See `__info__/1`.
|
||||
'''
|
||||
|
||||
@doc """
|
||||
@@ -498,10 +426,6 @@ defmodule Module do
|
||||
unless Keyword.has_key?(opts, :file) do
|
||||
raise ArgumentError, "expected :file to be given as option"
|
||||
end
|
||||
|
||||
next = :erlang.unique_integer()
|
||||
line = Keyword.get(opts, :line, 0)
|
||||
quoted = :elixir_quote.linify_with_context_counter(line, {module, next}, quoted)
|
||||
:elixir_module.compile(module, quoted, [], :elixir.env_for_eval(opts))
|
||||
end
|
||||
|
||||
@@ -540,15 +464,14 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Concatenates a list of aliases and returns a new alias only if the alias
|
||||
was already referenced.
|
||||
|
||||
If the alias was not referenced yet, fails with `ArgumentError`.
|
||||
It handles charlists, binaries and atoms.
|
||||
Concatenates a list of aliases and returns a new alias only
|
||||
if the alias was already referenced. If the alias was not
|
||||
referenced yet, fails with `ArgumentError`.
|
||||
It handles char lists, binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.safe_concat([Module, Unknown])
|
||||
iex> Module.safe_concat([Unknown, Module])
|
||||
** (ArgumentError) argument error
|
||||
|
||||
iex> Module.safe_concat([List, Chars])
|
||||
@@ -561,15 +484,14 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Concatenates two aliases and returns a new alias only if the alias was
|
||||
already referenced.
|
||||
|
||||
If the alias was not referenced yet, fails with `ArgumentError`.
|
||||
It handles charlists, binaries and atoms.
|
||||
Concatenates two aliases and returns a new alias only
|
||||
if the alias was already referenced. If the alias was not
|
||||
referenced yet, fails with `ArgumentError`.
|
||||
It handles char lists, binaries and atoms.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.safe_concat(Module, Unknown)
|
||||
iex> Module.safe_concat(Unknown, Module)
|
||||
** (ArgumentError) argument error
|
||||
|
||||
iex> Module.safe_concat(List, Chars)
|
||||
@@ -582,10 +504,9 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Attaches documentation to a given function or type.
|
||||
|
||||
It expects the module the function/type belongs to, the line (a non
|
||||
negative integer), the kind (`def` or `defmacro`), a tuple representing
|
||||
Attaches documentation to a given function or type. It expects
|
||||
the module the function/type belongs to, the line (a non negative
|
||||
integer), the kind (`def` or `defmacro`), a tuple representing
|
||||
the function and its arity, the function signature (the signature
|
||||
should be omitted for types) and the documentation, which should
|
||||
be either a binary or a boolean.
|
||||
@@ -669,7 +590,7 @@ defmodule Module do
|
||||
defp simplify_signature(_, acc), do: autogenerated(acc, :arg)
|
||||
|
||||
defp autogenerated(acc, key) do
|
||||
{key, [key | acc]}
|
||||
{key, [key|acc]}
|
||||
end
|
||||
|
||||
defp expand_signature(key, {all_keys, acc}) when is_atom(key) do
|
||||
@@ -698,19 +619,19 @@ defmodule Module do
|
||||
length(:lists.filter(fn(el) -> el == key end, list))
|
||||
end
|
||||
|
||||
defp camelcase_to_underscore(<<c::utf8, rest::binary>>) when c >= ?A and c <= ?Z,
|
||||
do: do_camelcase_to_underscore(rest, <<c + 32::utf8>>)
|
||||
defp do_camelcase_to_underscore(<<c::utf8, rest::binary>>, acc) when c >= ?A and c <= ?Z,
|
||||
do: do_camelcase_to_underscore(rest, <<acc::binary, ?_, c + 32::utf8>>)
|
||||
defp do_camelcase_to_underscore(<<c::utf8, rest::binary>>, acc),
|
||||
do: do_camelcase_to_underscore(rest, <<acc::binary, c>>)
|
||||
defp camelcase_to_underscore(<<c :: utf8, rest :: binary>>) when c >= ?A and c <= ?Z,
|
||||
do: do_camelcase_to_underscore(rest, <<c + 32 :: utf8>>)
|
||||
defp do_camelcase_to_underscore(<<c :: utf8, rest :: binary>>, acc) when c >= ?A and c <= ?Z,
|
||||
do: do_camelcase_to_underscore(rest, <<acc :: binary, ?_, c + 32 :: utf8>>)
|
||||
defp do_camelcase_to_underscore(<<c :: utf8, rest :: binary>>, acc),
|
||||
do: do_camelcase_to_underscore(rest, <<acc :: binary, c>>)
|
||||
defp do_camelcase_to_underscore(<<>>, acc),
|
||||
do: acc
|
||||
|
||||
# Merge
|
||||
|
||||
defp merge_signatures([h1 | t1], [h2 | t2], i) do
|
||||
[merge_signature(h1, h2, i) | merge_signatures(t1, t2, i + 1)]
|
||||
defp merge_signatures([h1|t1], [h2|t2], i) do
|
||||
[merge_signature(h1, h2, i)|merge_signatures(t1, t2, i + 1)]
|
||||
end
|
||||
|
||||
defp merge_signatures([], [], _) do
|
||||
@@ -737,7 +658,6 @@ defmodule Module do
|
||||
|
||||
@doc """
|
||||
Checks if the module defines the given function or macro.
|
||||
|
||||
Use `defines?/3` to assert for a specific type.
|
||||
|
||||
## Examples
|
||||
@@ -752,14 +672,13 @@ defmodule Module do
|
||||
def defines?(module, tuple) when is_tuple(tuple) do
|
||||
assert_not_compiled!(:defines?, module)
|
||||
table = defs_table_for(module)
|
||||
:ets.lookup(table, {:def, tuple}) != []
|
||||
:ets.lookup(table, tuple) != []
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the module defines a function or macro of the
|
||||
given `kind`.
|
||||
|
||||
`kind` can be any of `:def`, `:defp`, `:defmacro` or `:defmacrop`.
|
||||
given `kind`. `kind` can be any of `:def`, `:defp`,
|
||||
`:defmacro` or `:defmacrop`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -773,7 +692,7 @@ defmodule Module do
|
||||
def defines?(module, tuple, kind) do
|
||||
assert_not_compiled!(:defines?, module)
|
||||
table = defs_table_for(module)
|
||||
case :ets.lookup(table, {:def, tuple}) do
|
||||
case :ets.lookup(table, tuple) do
|
||||
[{_, ^kind, _, _, _, _, _}] -> true
|
||||
_ -> false
|
||||
end
|
||||
@@ -793,7 +712,7 @@ defmodule Module do
|
||||
def definitions_in(module) do
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
table = defs_table_for(module)
|
||||
:lists.concat :ets.match(table, {{:def, :'$1'}, :_, :_, :_, :_, :_, :_})
|
||||
:lists.concat :ets.match(table, {:'$1', :_, :_, :_, :_, :_, :_})
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -812,12 +731,11 @@ defmodule Module do
|
||||
def definitions_in(module, kind) do
|
||||
assert_not_compiled!(:definitions_in, module)
|
||||
table = defs_table_for(module)
|
||||
:lists.concat :ets.match(table, {{:def, :'$1'}, kind, :_, :_, :_, :_, :_})
|
||||
:lists.concat :ets.match(table, {:'$1', kind, :_, :_, :_, :_, :_})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes the given functions in `module` overridable.
|
||||
|
||||
An overridable function is lazily defined, allowing a
|
||||
developer to customize it. See `Kernel.defoverridable/1` for
|
||||
more information and documentation.
|
||||
@@ -826,25 +744,24 @@ defmodule Module do
|
||||
assert_not_compiled!(:make_overridable, module)
|
||||
|
||||
:lists.foreach(fn tuple ->
|
||||
case :elixir_def.take_definition(module, tuple) do
|
||||
case :elixir_def.lookup_definition(module, tuple) do
|
||||
false ->
|
||||
{name, arity} = tuple
|
||||
raise "cannot make function #{name}/#{arity} overridable because it was not defined"
|
||||
clause ->
|
||||
neighbours =
|
||||
if :elixir_compiler.get_opt(:internal) do
|
||||
[]
|
||||
else
|
||||
Module.LocalsTracker.yank(module, tuple)
|
||||
end
|
||||
:elixir_def.delete_definition(module, tuple)
|
||||
|
||||
old = :elixir_def_overridable.overridable(module)
|
||||
count = case :maps.find(tuple, old) do
|
||||
{:ok, {count, _, _, _}} -> count + 1
|
||||
:error -> 1
|
||||
neighbours = if :elixir_compiler.get_opt(:internal) do
|
||||
[]
|
||||
else
|
||||
Module.LocalsTracker.yank(module, tuple)
|
||||
end
|
||||
new = :maps.put(tuple, {count, clause, neighbours, false}, old)
|
||||
:elixir_def_overridable.overridable(module, new)
|
||||
|
||||
old = :elixir_def_overridable.overridable(module)
|
||||
merged = :orddict.update(tuple, fn({count, _, _, _}) ->
|
||||
{count + 1, clause, neighbours, false}
|
||||
end, {1, clause, neighbours, false}, old)
|
||||
:elixir_def_overridable.overridable(module, merged)
|
||||
end
|
||||
end, tuples)
|
||||
end
|
||||
@@ -853,14 +770,12 @@ defmodule Module do
|
||||
Returns `true` if `tuple` in `module` is marked as overridable.
|
||||
"""
|
||||
def overridable?(module, tuple) do
|
||||
:maps.is_key(tuple, :elixir_def_overridable.overridable(module))
|
||||
!!List.keyfind(:elixir_def_overridable.overridable(module), tuple, 0)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts an Erlang attribute to the given module with the given
|
||||
key and value.
|
||||
|
||||
The semantics of putting the attribute depends
|
||||
key and value. The semantics of putting the attribute depends
|
||||
if the attribute was registered or not via `register_attribute/3`.
|
||||
|
||||
## Examples
|
||||
@@ -870,22 +785,16 @@ defmodule Module do
|
||||
end
|
||||
|
||||
"""
|
||||
def put_attribute(module, key, value) do
|
||||
put_attribute(module, key, value, nil)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def put_attribute(module, key, value, stack) when is_atom(key) do
|
||||
def put_attribute(module, key, value) when is_atom(key) do
|
||||
assert_not_compiled!(:put_attribute, module)
|
||||
table = data_table_for(module)
|
||||
value = preprocess_attribute(key, value)
|
||||
acc = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||
warn_if_redefining_doc_attribute(stack, table, key)
|
||||
|
||||
new =
|
||||
if :lists.member(key, acc) do
|
||||
case :ets.lookup(table, key) do
|
||||
[{^key, old}] -> [value | old]
|
||||
[{^key, old}] -> [value|old]
|
||||
[] -> [value]
|
||||
end
|
||||
else
|
||||
@@ -896,12 +805,9 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets the given attribute from a module.
|
||||
|
||||
If the attribute was marked with `accumulate` with
|
||||
`Module.register_attribute/3`, a list is always returned. `nil` is returned
|
||||
if the attribute has not been marked with `accumulate` and has not been set
|
||||
to any value.
|
||||
Gets the given attribute from a module. If the attribute
|
||||
was marked with `accumulate` with `Module.register_attribute/3`,
|
||||
a list is always returned.
|
||||
|
||||
The `@` macro compiles to a call to this function. For example,
|
||||
the following code:
|
||||
@@ -985,12 +891,12 @@ defmodule Module do
|
||||
|
||||
if Keyword.get(opts, :persist) do
|
||||
old = :ets.lookup_element(table, {:elixir, :persisted_attributes}, 2)
|
||||
:ets.insert(table, {{:elixir, :persisted_attributes}, [new | old]})
|
||||
:ets.insert(table, {{:elixir, :persisted_attributes}, [new|old]})
|
||||
end
|
||||
|
||||
if Keyword.get(opts, :accumulate) do
|
||||
old = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||
:ets.insert(table, {{:elixir, :acc_attributes}, [new | old]})
|
||||
:ets.insert(table, {{:elixir, :acc_attributes}, [new|old]})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -999,8 +905,8 @@ defmodule Module do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Module.split Very.Long.Module.Name.And.Even.Longer
|
||||
["Very", "Long", "Module", "Name", "And", "Even", "Longer"]
|
||||
Module.split Very.Long.Module.Name.And.Even.Longer
|
||||
#=> ["Very", "Long", "Module", "Name", "And", "Even", "Longer"]
|
||||
|
||||
"""
|
||||
def split(module) when is_atom(module) do
|
||||
@@ -1054,7 +960,7 @@ defmodule Module do
|
||||
|
||||
new =
|
||||
case :ets.lookup(table, key) do
|
||||
[{^key, old}] -> [value | old]
|
||||
[{^key, old}] -> [value|old]
|
||||
[] -> [value]
|
||||
end
|
||||
|
||||
@@ -1062,22 +968,22 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def get_attribute(module, key, stack) when is_atom(key) and (is_list(stack) or is_nil(stack)) do
|
||||
def get_attribute(module, key, warn) when is_atom(key) and (is_list(warn) or is_nil(warn)) do
|
||||
assert_not_compiled!(:get_attribute, module)
|
||||
table = data_table_for(module)
|
||||
|
||||
case :ets.lookup(table, key) do
|
||||
[{^key, val}] ->
|
||||
val
|
||||
postprocess_attribute(key, val)
|
||||
[] ->
|
||||
acc = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||
|
||||
cond do
|
||||
:lists.member(key, acc) ->
|
||||
[]
|
||||
is_list(stack) ->
|
||||
IO.warn "undefined module attribute @#{key}, " <>
|
||||
"please remove access to @#{key} or explicitly set it before access", stack
|
||||
is_list(warn) ->
|
||||
:elixir_errors.warn warn_info(warn), "undefined module attribute @#{key}, " <>
|
||||
"please remove access to @#{key} or explicitly set it before access"
|
||||
nil
|
||||
true ->
|
||||
nil
|
||||
@@ -1085,15 +991,17 @@ defmodule Module do
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp preprocess_attribute(key, value) when key in [:moduledoc, :typedoc, :doc] do
|
||||
unless match?({line, _} when is_integer(line), value) do
|
||||
raise ArgumentError, "expected #{key} attribute given in the {line, doc} format, got: #{inspect(value)}"
|
||||
end
|
||||
value
|
||||
defp warn_info([entry|_]) do
|
||||
opts = elem(entry, tuple_size(entry) - 1)
|
||||
Exception.format_file_line(Keyword.get(opts, :file), Keyword.get(opts, :line)) <> " "
|
||||
end
|
||||
|
||||
defp warn_info([]) do
|
||||
""
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp preprocess_attribute(:on_load, atom) when is_atom(atom) do
|
||||
{atom, 0}
|
||||
end
|
||||
@@ -1115,19 +1023,28 @@ defmodule Module do
|
||||
defp preprocess_attribute(:on_definition, atom) when is_atom(atom),
|
||||
do: {atom, :__on_definition__}
|
||||
|
||||
defp preprocess_attribute(key, _value) when key in [:type, :typep, :export_type, :opaque, :callback, :macrocallback, :optional_callbacks] do
|
||||
raise ArgumentError, "attributes type, typep, export_type, opaque, callback, macrocallback, and optional_callbacks " <>
|
||||
"must be set directly via the @ notation"
|
||||
defp preprocess_attribute(key, _value) when key in [:type, :typep, :export_type, :opaque, :callback, :macrocallback] do
|
||||
raise ArgumentError, "attributes type, typep, export_type, opaque, callback and macrocallback " <>
|
||||
"must be set via Kernel.Typespec"
|
||||
end
|
||||
|
||||
defp preprocess_attribute(_key, value) do
|
||||
value
|
||||
end
|
||||
|
||||
defp postprocess_attribute(:doc, {_line, doc}), do: doc
|
||||
defp postprocess_attribute(:typedoc, {_line, doc}), do: doc
|
||||
defp postprocess_attribute(:moduledoc, {_line, doc}), do: doc
|
||||
defp postprocess_attribute(_, value), do: value
|
||||
|
||||
defp get_doc_info(table, env) do
|
||||
case :ets.take(table, :doc) do
|
||||
[doc: {_, _} = pair] -> pair
|
||||
[] -> {env.line, nil}
|
||||
# TODO: Use :ets.take/2 with Erlang 18
|
||||
case :ets.lookup(table, :doc) do
|
||||
[doc: {_, _} = pair] ->
|
||||
:ets.delete(table, :doc)
|
||||
pair
|
||||
[] ->
|
||||
{env.line, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1144,16 +1061,4 @@ defmodule Module do
|
||||
raise ArgumentError,
|
||||
"could not call #{fun} on module #{inspect module} because it was already compiled"
|
||||
end
|
||||
|
||||
defp warn_if_redefining_doc_attribute(stack, table, key)
|
||||
when is_list(stack) and key in [:doc, :typedoc, :moduledoc] do
|
||||
case :ets.lookup(table, key) do
|
||||
[{_, {line, val}}] when val != false ->
|
||||
IO.warn "redefining @#{key} attribute previously set at line #{line}", stack
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp warn_if_redefining_doc_attribute(nil, _table, _key), do: false
|
||||
end
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
# This is an Elixir module responsible for tracking
|
||||
# This is a module Elixir responsible for tracking
|
||||
# calls in order to extract Elixir modules' behaviour
|
||||
# during compilation time.
|
||||
#
|
||||
# ## Implementation
|
||||
#
|
||||
# The implementation uses the digraph module to track
|
||||
# all dependencies. The graph starts with one main vertex:
|
||||
# all dependencies. The graph starts with one main vertice:
|
||||
#
|
||||
# * `:local` - points to local functions
|
||||
#
|
||||
# We can also have the following vertices:
|
||||
# We also have can the following vertices:
|
||||
#
|
||||
# * `Module` - a module that was invoked via an import
|
||||
# * `{name, arity}` - a local function/arity pair
|
||||
@@ -121,7 +121,7 @@ defmodule Module.LocalsTracker do
|
||||
:gen_server.cast(pid, {:add_local, from, to})
|
||||
end
|
||||
|
||||
# Adds an import dispatch to the given target.
|
||||
# Adds a import dispatch to the given target.
|
||||
@doc false
|
||||
def add_import(pid, function, module, target) when is_atom(module) and is_tuple(target) do
|
||||
:gen_server.cast(pid, {:add_import, function, module, target})
|
||||
@@ -174,9 +174,9 @@ defmodule Module.LocalsTracker do
|
||||
reduce_unreachable(private, [], :sets.from_list(unreachable))
|
||||
end
|
||||
|
||||
defp reduce_unreachable([{vertex, callers} | t], acc, unreachable) do
|
||||
defp reduce_unreachable([{vertex, callers}|t], acc, unreachable) do
|
||||
if :sets.is_subset(callers, unreachable) do
|
||||
reduce_unreachable(t, [{vertex, callers} | acc], unreachable)
|
||||
reduce_unreachable(t, [{vertex, callers}|acc], unreachable)
|
||||
else
|
||||
reduce_unreachable(acc ++ t, [], :sets.del_element(vertex, unreachable))
|
||||
end
|
||||
@@ -195,7 +195,7 @@ defmodule Module.LocalsTracker do
|
||||
if :lists.member(tuple, reachable) do
|
||||
acc
|
||||
else
|
||||
[{:unused_def, tuple, kind} | acc]
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -207,12 +207,12 @@ defmodule Module.LocalsTracker do
|
||||
invoked = for {n, a} <- reachable, n == name, a in min..max, do: a
|
||||
|
||||
if invoked == [] do
|
||||
[{:unused_def, tuple, kind} | acc]
|
||||
[{:unused_def, tuple, kind}|acc]
|
||||
else
|
||||
case :lists.min(invoked) - min do
|
||||
0 -> acc
|
||||
^default -> [{:unused_args, tuple} | acc]
|
||||
unused_args -> [{:unused_args, tuple, unused_args} | acc]
|
||||
^default -> [{:unused_args, tuple}|acc]
|
||||
unused_args -> [{:unused_args, tuple, unused_args}|acc]
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -244,11 +244,11 @@ defmodule Module.LocalsTracker do
|
||||
@doc false
|
||||
def handle_call({:cache_env, env}, _from, {d, cache}) do
|
||||
case cache do
|
||||
[{i, ^env} | _] ->
|
||||
[{i, ^env}|_] ->
|
||||
{:reply, i, {d, cache}}
|
||||
t ->
|
||||
i = length(t)
|
||||
{:reply, i, {d, [{i, env} | t]}}
|
||||
{:reply, i, {d, [{i, env}|t]}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -353,7 +353,7 @@ defmodule Module.LocalsTracker do
|
||||
|
||||
defp replace_edge!(d, from, to) do
|
||||
_ = unless :lists.member(to, :digraph.out_neighbours(d, from)) do
|
||||
[:"$e" | _] = :digraph.add_edge(d, from, to)
|
||||
[:"$e"|_] = :digraph.add_edge(d, from, to)
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
+142
-304
@@ -1,6 +1,6 @@
|
||||
defmodule OptionParser do
|
||||
@moduledoc """
|
||||
This module contains functions to parse command line options.
|
||||
This module contains functions to parse command line arguments.
|
||||
"""
|
||||
|
||||
@type argv :: [String.t]
|
||||
@@ -8,31 +8,13 @@ defmodule OptionParser do
|
||||
@type errors :: [{String.t, String.t | nil}]
|
||||
@type options :: [switches: Keyword.t, strict: Keyword.t, aliases: Keyword.t]
|
||||
|
||||
defmodule ParseError do
|
||||
defexception [:message]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses `argv` into a keywords list.
|
||||
|
||||
It returns a three-element tuple with the form `{parsed, args, invalid}`, where:
|
||||
It returns the parsed values, remaining arguments and the
|
||||
invalid options.
|
||||
|
||||
* `parsed` is a keyword list of parsed switches with `{switch_name, value}`
|
||||
tuples in it; `switch_name` is the atom representing the switch name while
|
||||
`value` is the value for that switch parsed according to `opts` (see the
|
||||
"Examples" section for more information)
|
||||
* `args` is a list of the remaining arguments in `argv` as strings
|
||||
* `invalid` is a list of invalid options as `{option_name, value}` where
|
||||
`option_name` is the raw option and `value` is `nil` if the option wasn't
|
||||
expected or the string value if the value didn't have the expected type for
|
||||
the corresponding option
|
||||
|
||||
Elixir converts switches to underscored atoms, so `--source-path` becomes
|
||||
`:source_path`. This is done to better suit Elixir conventions. However, this
|
||||
means that switches can't contain underscores and switches that do contain
|
||||
underscores are always returned in the list of invalid options.
|
||||
|
||||
Without any options, this function will try to parse all switches in the `argv`.
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse(["--debug"])
|
||||
{[debug: true], [], []}
|
||||
@@ -43,85 +25,49 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
|
||||
|
||||
Switches followed by a value will be assigned the value, as a string.
|
||||
Switches without an argument, like `--debug` in the examples above, will
|
||||
automatically be set to `true`.
|
||||
By default, Elixir will try to automatically parse switches.
|
||||
Switches without an argument, like `--debug` will automatically
|
||||
be set to `true`. Switches followed by a value will be assigned
|
||||
to the value, always as strings.
|
||||
|
||||
## Options
|
||||
Note Elixir also converts the switches to underscore atoms, as
|
||||
`--source-path` becomes `:source_path`, to better suit Elixir
|
||||
conventions. This means that option names on the command line cannot contain
|
||||
underscores; such options will be reported as `:undefined` (in strict mode)
|
||||
or `:invalid` (in basic mode).
|
||||
|
||||
The following options are supported:
|
||||
## Switches
|
||||
|
||||
* `:switches` or `:strict` - see the "Switch definitions" section below
|
||||
* `:aliases` - see the "Aliases" section below
|
||||
Many times though, it is better to explicitly list the available
|
||||
switches and their formats. The switches can be specified via two
|
||||
different options:
|
||||
|
||||
## Switch definitions
|
||||
* `:strict` - the switches are strict. Any switch that does not
|
||||
exist in the switch list is treated as an error.
|
||||
|
||||
Often it is better to explicitly list the known
|
||||
switches and their formats. The switches can be specified via one of two
|
||||
options:
|
||||
* `:switches` - defines some switches. Switches that does not
|
||||
exist in the switch list are still attempted to be parsed.
|
||||
|
||||
* `:switches` - defines some switches and their types. This function
|
||||
still attempts to parse switches that are not in this list.
|
||||
* `:strict` - defines strict switches. Any switch in `argv` that is not
|
||||
specified in the list is returned in the invalid options list.
|
||||
Note only `:strict` or `:switches` may be given at once.
|
||||
|
||||
Both these options accept a keyword list of `{name, type}` tuples where `name`
|
||||
is an atom defining the name of the switch and `type` is an atom that
|
||||
specifies the type for the value of this switch (see the "Types" section below
|
||||
for the possible types and more information about type casting).
|
||||
For each switch, the following types are supported:
|
||||
|
||||
Note that you should only supply the `:switches` or `:strict` option.
|
||||
If you supply both, an `ArgumentError` exception will be raised.
|
||||
* `:boolean` - marks the given switch as a boolean. Boolean switches
|
||||
never consume the following value unless it is `true` or
|
||||
`false`.
|
||||
* `:integer` - parses the switch as an integer.
|
||||
* `:float` - parses the switch as a float.
|
||||
* `:string` - returns the switch as a string.
|
||||
|
||||
### Types
|
||||
If a switch can't be parsed or is not specified in the strict case,
|
||||
the option is returned in the invalid options list (third element
|
||||
of the returned tuple).
|
||||
|
||||
Switches parsed by `OptionParser` may take zero or one arguments.
|
||||
The following extra "types" are supported:
|
||||
|
||||
The following switches types take no arguments:
|
||||
* `:keep` - keeps duplicated items in the list instead of overriding
|
||||
|
||||
* `:boolean` - sets the value to `true` when given (see also the
|
||||
"Negation switches" section below)
|
||||
* `:count` - counts the number of times the switch is given
|
||||
|
||||
The following switches take one argument:
|
||||
|
||||
* `:integer` - parses the value as an integer
|
||||
* `:float` - parses the value as a float
|
||||
* `:string` - parses the value as a string
|
||||
|
||||
If a switch can't be parsed according to the given type, it is returned
|
||||
in the invalid options list.
|
||||
|
||||
### Modifiers
|
||||
|
||||
Switches can be specified with modifiers, which change how
|
||||
they behave. The following modifiers are supported:
|
||||
|
||||
* `:keep` - keeps duplicated items instead of overriding them; works with
|
||||
all types except `:count`. Specifying `switch_name: :keep` assumes the
|
||||
type of `:switch_name` will be `:string`.
|
||||
|
||||
Note that if you want to use `:keep` with a type other than `:string`, use a list
|
||||
as the type for the switch. For example: `[foo: [:integer, :keep]]`.
|
||||
|
||||
### Negation switches
|
||||
|
||||
In case a switch `SWITCH` is specified to have type `:boolean`, it may be
|
||||
passed as `--no-SWITCH` as well which will set the option to `false`:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
|
||||
## Aliases
|
||||
|
||||
A set of aliases can be specified in the `:aliases` option:
|
||||
|
||||
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
{[debug: true], [], []}
|
||||
|
||||
## Examples
|
||||
|
||||
Here are some examples of working with different types and modifiers:
|
||||
Examples:
|
||||
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file"], strict: [unlock: :boolean])
|
||||
{[unlock: true], ["path/to/file"], []}
|
||||
@@ -136,12 +82,6 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--limit", "xyz"], strict: [limit: :integer])
|
||||
{[], [], [{"--limit", "xyz"}]}
|
||||
|
||||
iex> OptionParser.parse(["--verbose"], switches: [verbose: :count])
|
||||
{[verbose: 1], [], []}
|
||||
|
||||
iex> OptionParser.parse(["-v", "-v"], aliases: [v: :verbose], strict: [verbose: :count])
|
||||
{[verbose: 2], [], []}
|
||||
|
||||
iex> OptionParser.parse(["--unknown", "xyz"], strict: [])
|
||||
{[], ["xyz"], [{"--unknown", nil}]}
|
||||
|
||||
@@ -152,49 +92,27 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--unlock", "path/to/file", "--unlock", "path/to/another/file"], strict: [unlock: :keep])
|
||||
{[unlock: "path/to/file", unlock: "path/to/another/file"], [], []}
|
||||
|
||||
## Negation switches
|
||||
|
||||
In case a switch is declared as boolean, it may be passed as `--no-SWITCH`
|
||||
which will set the option to `false`:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
|
||||
## Aliases
|
||||
|
||||
A set of aliases can be given as options too:
|
||||
|
||||
iex> OptionParser.parse(["-d"], aliases: [d: :debug])
|
||||
{[debug: true], [], []}
|
||||
|
||||
"""
|
||||
@spec parse(argv, options) :: {parsed, argv, errors}
|
||||
def parse(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
do_parse(argv, compile_config(opts), [], [], [], true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `parse/2` but raises an `OptionParser.ParseError`
|
||||
exception if any invalid options are given.
|
||||
|
||||
If there are no errors, returns a `{parsed, rest}` tuple where:
|
||||
|
||||
* `parsed` is the list of parsed switches (same as in `parse/2`)
|
||||
* `rest` is the list of arguments (same as in `parse/2`)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse!(["--debug", "path/to/file"], strict: [debug: :boolean])
|
||||
{[debug: true], ["path/to/file"]}
|
||||
|
||||
iex> OptionParser.parse!(["--limit", "xyz"], strict: [limit: :integer])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--limit : Expected type integer, got "xyz"
|
||||
|
||||
iex> OptionParser.parse!(["--unknown", "xyz"], strict: [])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--unknown : Unknown option
|
||||
|
||||
iex> OptionParser.parse!(["-l", "xyz", "-f", "bar"],
|
||||
...> switches: [limit: :integer, foo: :integer], aliases: [l: :limit, f: :foo])
|
||||
** (OptionParser.ParseError) 2 errors found!
|
||||
-l : Expected type integer, got "xyz"
|
||||
-f : Expected type integer, got "bar"
|
||||
|
||||
"""
|
||||
@spec parse!(argv, options) :: {parsed, argv} | no_return
|
||||
def parse!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
case parse(argv, opts) do
|
||||
{parsed, args, []} -> {parsed, args}
|
||||
{_, _, errors} -> raise ParseError, format_errors(errors, opts)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Similar to `parse/2` but only parses the head of `argv`;
|
||||
as soon as it finds a non-switch, it stops parsing.
|
||||
@@ -215,38 +133,6 @@ defmodule OptionParser do
|
||||
do_parse(argv, compile_config(opts), [], [], [], false)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `parse_head/2` but raises an `OptionParser.ParseError`
|
||||
exception if any invalid options are given.
|
||||
|
||||
If there are no errors, returns a `{parsed, rest}` tuple where:
|
||||
|
||||
* `parsed` is the list of parsed switches (same as in `parse_head/2`)
|
||||
* `rest` is the list of arguments (same as in `parse_head/2`)
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.parse_head!(["--source", "lib", "path/to/file", "--verbose"])
|
||||
{[source: "lib"], ["path/to/file", "--verbose"]}
|
||||
|
||||
iex> OptionParser.parse_head!(["--number", "lib", "test/enum_test.exs", "--verbose"], strict: [number: :integer])
|
||||
** (OptionParser.ParseError) 1 error found!
|
||||
--number : Expected type integer, got "lib"
|
||||
|
||||
iex> OptionParser.parse_head!(["--verbose", "--source", "lib", "test/enum_test.exs", "--unlock"],
|
||||
...> strict: [verbose: :integer, source: :integer])
|
||||
** (OptionParser.ParseError) 2 errors found!
|
||||
--verbose : Missing argument of type integer
|
||||
--source : Expected type integer, got "lib"
|
||||
"""
|
||||
@spec parse_head!(argv, options) :: {parsed, argv} | no_return
|
||||
def parse_head!(argv, opts \\ []) when is_list(argv) and is_list(opts) do
|
||||
case parse_head(argv, opts) do
|
||||
{parsed, args, []} -> {parsed, args}
|
||||
{_, _, errors} -> raise ParseError, format_errors(errors, opts)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse([], _config, opts, args, invalid, _all?) do
|
||||
{Enum.reverse(opts), Enum.reverse(args), Enum.reverse(invalid)}
|
||||
end
|
||||
@@ -254,26 +140,26 @@ defmodule OptionParser do
|
||||
defp do_parse(argv, {aliases, switches, strict}=config, opts, args, invalid, all?) do
|
||||
case next(argv, aliases, switches, strict) do
|
||||
{:ok, option, value, rest} ->
|
||||
# the option exists and it was successfully parsed
|
||||
# the option exist and it was successfully parsed
|
||||
kinds = List.wrap Keyword.get(switches, option)
|
||||
new_opts = do_store_option(opts, option, value, kinds)
|
||||
do_parse(rest, config, new_opts, args, invalid, all?)
|
||||
|
||||
{:invalid, option, value, rest} ->
|
||||
# the option exist but it has wrong value
|
||||
do_parse(rest, config, opts, args, [{option, value} | invalid], all?)
|
||||
do_parse(rest, config, opts, args, [{option, value}|invalid], all?)
|
||||
|
||||
{:undefined, option, _value, rest} ->
|
||||
# the option does not exist (for strict cases)
|
||||
do_parse(rest, config, opts, args, [{option, nil} | invalid], all?)
|
||||
do_parse(rest, config, opts, args, [{option, nil}|invalid], all?)
|
||||
|
||||
{:error, ["--" | rest]} ->
|
||||
{:error, ["--"|rest]} ->
|
||||
{Enum.reverse(opts), Enum.reverse(args, rest), Enum.reverse(invalid)}
|
||||
|
||||
{:error, [arg | rest] = remaining_args} ->
|
||||
{:error, [arg|rest]=remaining_args} ->
|
||||
# there is no option
|
||||
if all? do
|
||||
do_parse(rest, config, opts, [arg | args], invalid, all?)
|
||||
do_parse(rest, config, opts, [arg|args], invalid, all?)
|
||||
else
|
||||
{Enum.reverse(opts), Enum.reverse(args, remaining_args), Enum.reverse(invalid)}
|
||||
end
|
||||
@@ -284,20 +170,20 @@ defmodule OptionParser do
|
||||
Low-level function that parses one option.
|
||||
|
||||
It accepts the same options as `parse/2` and `parse_head/2`
|
||||
as both functions are built on top of this function. This function
|
||||
as both functions are built on top of next. This function
|
||||
may return:
|
||||
|
||||
* `{:ok, key, value, rest}` - the option `key` with `value` was
|
||||
successfully parsed
|
||||
|
||||
* `{:invalid, key, value, rest}` - the option `key` is invalid with `value`
|
||||
(returned when the value cannot be parsed according to the switch type)
|
||||
(returned when the switch type does not match the one given via the
|
||||
command line)
|
||||
|
||||
* `{:undefined, key, value, rest}` - the option `key` is undefined
|
||||
(returned in strict mode when the switch is unknown)
|
||||
|
||||
* `{:error, rest}` - there are no switches at the head of the given `argv`
|
||||
(returned on strict cases and the switch is unknown)
|
||||
|
||||
* `{:error, rest}` - there are no switches at the top of the given argv
|
||||
"""
|
||||
|
||||
@spec next(argv, options) ::
|
||||
@@ -315,35 +201,32 @@ defmodule OptionParser do
|
||||
{:error, []}
|
||||
end
|
||||
|
||||
defp next(["--" | _] = argv, _aliases, _switches, _strict) do
|
||||
defp next(["--"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["-" | _] = argv, _aliases, _switches, _strict) do
|
||||
defp next(["-"|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["- " <> _ | _] = argv, _aliases, _switches, _strict) do
|
||||
defp next(["- " <> _|_]=argv, _aliases, _switches, _strict) do
|
||||
{:error, argv}
|
||||
end
|
||||
|
||||
defp next(["-" <> option | rest] = argv, aliases, switches, strict) do
|
||||
defp next(["-" <> option|rest], aliases, switches, strict) do
|
||||
{option, value} = split_option(option)
|
||||
original = "-" <> option
|
||||
opt_name_bin = "-" <> option
|
||||
tagged = tag_option(option, switches, aliases)
|
||||
|
||||
cond do
|
||||
negative_number?(original) ->
|
||||
{:error, argv}
|
||||
strict and not option_defined?(tagged, switches) ->
|
||||
{:undefined, original, value, rest}
|
||||
true ->
|
||||
{option, kinds, value} = normalize_option(tagged, value, switches)
|
||||
{value, kinds, rest} = normalize_value(value, kinds, rest, strict)
|
||||
case validate_option(value, kinds) do
|
||||
{:ok, new_value} -> {:ok, option, new_value, rest}
|
||||
:invalid -> {:invalid, original, value, rest}
|
||||
end
|
||||
if strict and not option_defined?(tagged, switches) do
|
||||
{:undefined, opt_name_bin, value, rest}
|
||||
else
|
||||
{opt_name, kinds, value} = normalize_option(tagged, value, switches)
|
||||
{value, kinds, rest} = normalize_value(value, kinds, rest, strict)
|
||||
case validate_option(value, kinds) do
|
||||
{:ok, new_value} -> {:ok, opt_name, new_value, rest}
|
||||
:invalid -> {:invalid, opt_name_bin, value, rest}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -352,11 +235,10 @@ defmodule OptionParser do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a key-value enumerable and converts it to argv.
|
||||
Receives a key-value enumerable and convert it to argv.
|
||||
|
||||
Keys must be atoms. Keys with `nil` value are discarded,
|
||||
Keys must be atoms. Keys with nil value are discarded,
|
||||
boolean values are converted to `--key` or `--no-key`
|
||||
(if the value is `true` or `false`, respectively),
|
||||
and all other values are converted using `to_string/1`.
|
||||
|
||||
## Examples
|
||||
@@ -385,9 +267,6 @@ defmodule OptionParser do
|
||||
@doc ~S"""
|
||||
Splits a string into argv chunks.
|
||||
|
||||
This function splits the given `string` into a list of strings in a similar
|
||||
way to many shells.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> OptionParser.split("foo bar")
|
||||
@@ -395,32 +274,31 @@ defmodule OptionParser do
|
||||
|
||||
iex> OptionParser.split("foo \"bar baz\"")
|
||||
["foo", "bar baz"]
|
||||
|
||||
"""
|
||||
@spec split(String.t) :: argv
|
||||
def split(string) do
|
||||
do_split(String.trim_leading(string, " "), "", [], nil)
|
||||
do_split(strip_leading_spaces(string), "", [], nil)
|
||||
end
|
||||
|
||||
# If we have an escaped quote, simply remove the escape
|
||||
defp do_split(<<?\\, quote, t::binary>>, buffer, acc, quote),
|
||||
defp do_split(<<?\\, quote, t :: binary>>, buffer, acc, quote),
|
||||
do: do_split(t, <<buffer::binary, quote>>, acc, quote)
|
||||
|
||||
# If we have a quote and we were not in a quote, start one
|
||||
defp do_split(<<quote, t::binary>>, buffer, acc, nil) when quote in [?", ?'],
|
||||
defp do_split(<<quote, t :: binary>>, buffer, acc, nil) when quote in [?", ?'],
|
||||
do: do_split(t, buffer, acc, quote)
|
||||
|
||||
# If we have a quote and we were inside it, close it
|
||||
defp do_split(<<quote, t::binary>>, buffer, acc, quote),
|
||||
defp do_split(<<quote, t :: binary>>, buffer, acc, quote),
|
||||
do: do_split(t, buffer, acc, nil)
|
||||
|
||||
# If we have an escaped quote/space, simply remove the escape as long as we are not inside a quote
|
||||
defp do_split(<<?\\, h, t::binary>>, buffer, acc, nil) when h in [?\s, ?', ?"],
|
||||
defp do_split(<<?\\, h, t :: binary>>, buffer, acc, nil) when h in [?\s, ?', ?"],
|
||||
do: do_split(t, <<buffer::binary, h>>, acc, nil)
|
||||
|
||||
# If we have space and we are outside of a quote, start new segment
|
||||
defp do_split(<<?\s, t::binary>>, buffer, acc, nil),
|
||||
do: do_split(String.trim_leading(t, " "), "", [buffer | acc], nil)
|
||||
defp do_split(<<?\s, t :: binary>>, buffer, acc, nil),
|
||||
do: do_split(strip_leading_spaces(t), "", [buffer|acc], nil)
|
||||
|
||||
# All other characters are moved to buffer
|
||||
defp do_split(<<h, t::binary>>, buffer, acc, quote) do
|
||||
@@ -432,21 +310,22 @@ defmodule OptionParser do
|
||||
do: Enum.reverse(acc)
|
||||
|
||||
defp do_split(<<>>, buffer, acc, nil),
|
||||
do: Enum.reverse([buffer | acc])
|
||||
do: Enum.reverse([buffer|acc])
|
||||
|
||||
# Otherwise raise
|
||||
defp do_split(<<>>, _, _acc, marker) do
|
||||
raise "argv string did not terminate properly, a #{<<marker>>} was opened but never closed"
|
||||
end
|
||||
|
||||
defp strip_leading_spaces(" " <> t), do: strip_leading_spaces(t)
|
||||
defp strip_leading_spaces(t), do: t
|
||||
|
||||
## Helpers
|
||||
|
||||
defp compile_config(opts) do
|
||||
aliases = opts[:aliases] || []
|
||||
|
||||
{switches, strict} = cond do
|
||||
opts[:switches] && opts[:strict] ->
|
||||
raise ArgumentError, ":switches and :strict cannot be given together"
|
||||
s = opts[:switches] ->
|
||||
{s, false}
|
||||
s = opts[:strict] ->
|
||||
@@ -459,36 +338,30 @@ defmodule OptionParser do
|
||||
end
|
||||
|
||||
defp validate_option(value, kinds) do
|
||||
{invalid?, value} =
|
||||
cond do
|
||||
:invalid in kinds ->
|
||||
{true, value}
|
||||
:boolean in kinds ->
|
||||
case value do
|
||||
t when t in [true, "true"] -> {false, true}
|
||||
f when f in [false, "false"] -> {false, false}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:count in kinds ->
|
||||
case value do
|
||||
1 -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:integer in kinds ->
|
||||
case Integer.parse(value) do
|
||||
{value, ""} -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:float in kinds ->
|
||||
case Float.parse(value) do
|
||||
{value, ""} -> {false, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
true ->
|
||||
{false, value}
|
||||
end
|
||||
{is_invalid, value} = cond do
|
||||
:invalid in kinds ->
|
||||
{true, value}
|
||||
:boolean in kinds ->
|
||||
case value do
|
||||
t when t in [true, "true"] -> {nil, true}
|
||||
f when f in [false, "false"] -> {nil, false}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:integer in kinds ->
|
||||
case Integer.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
:float in kinds ->
|
||||
case Float.parse(value) do
|
||||
{value, ""} -> {nil, value}
|
||||
_ -> {true, value}
|
||||
end
|
||||
true ->
|
||||
{nil, value}
|
||||
end
|
||||
|
||||
if invalid? do
|
||||
if is_invalid do
|
||||
:invalid
|
||||
else
|
||||
{:ok, value}
|
||||
@@ -497,32 +370,15 @@ defmodule OptionParser do
|
||||
|
||||
defp do_store_option(dict, option, value, kinds) do
|
||||
cond do
|
||||
:count in kinds ->
|
||||
Keyword.update(dict, option, value, & &1 + 1)
|
||||
:keep in kinds ->
|
||||
[{option, value} | dict]
|
||||
[{option, value}|dict]
|
||||
true ->
|
||||
[{option, value} | Keyword.delete(dict, option)]
|
||||
[{option, value}|Keyword.delete(dict, option)]
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_option("-no-" <> option, switches, _aliases) do
|
||||
cond do
|
||||
(negated = get_option(option)) && :boolean in List.wrap(switches[negated]) ->
|
||||
{:negated, negated}
|
||||
option = get_option("no-" <> option) ->
|
||||
{:default, option}
|
||||
true ->
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_option("-" <> option, _switches, _aliases) do
|
||||
if option = get_option(option) do
|
||||
{:default, option}
|
||||
else
|
||||
:unknown
|
||||
end
|
||||
defp tag_option(<<?-, option :: binary>>, switches, _aliases) do
|
||||
get_negated(option, switches)
|
||||
end
|
||||
|
||||
defp tag_option(option, _switches, aliases) when is_binary(option) do
|
||||
@@ -563,18 +419,15 @@ defmodule OptionParser do
|
||||
end
|
||||
|
||||
defp normalize_value(nil, kinds, t, strict) do
|
||||
nil_or_true = if strict, do: nil, else: true
|
||||
cond do
|
||||
:boolean in kinds ->
|
||||
{true, kinds, t}
|
||||
:count in kinds ->
|
||||
{1, kinds, t}
|
||||
value_in_tail?(t) ->
|
||||
[h | t] = t
|
||||
[h|t] = t
|
||||
{h, kinds, t}
|
||||
kinds == [] and strict ->
|
||||
{nil, kinds, t}
|
||||
kinds == [] ->
|
||||
{true, kinds, t}
|
||||
{nil_or_true, kinds, t}
|
||||
true ->
|
||||
{nil, [:invalid], t}
|
||||
end
|
||||
@@ -584,11 +437,11 @@ defmodule OptionParser do
|
||||
{value, kinds, t}
|
||||
end
|
||||
|
||||
defp value_in_tail?(["-" | _]), do: true
|
||||
defp value_in_tail?(["- " <> _ | _]), do: true
|
||||
defp value_in_tail?(["-" <> arg | _]), do: negative_number?("-" <> arg)
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
defp value_in_tail?(["-"|_]), do: true
|
||||
defp value_in_tail?(["- " <> _|_]), do: true
|
||||
defp value_in_tail?(["-" <> _|_]), do: false
|
||||
defp value_in_tail?([]), do: false
|
||||
defp value_in_tail?(_), do: true
|
||||
|
||||
defp split_option(option) do
|
||||
case :binary.split(option, "=") do
|
||||
@@ -597,16 +450,17 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp to_underscore(option),
|
||||
do: to_underscore(option, <<>>)
|
||||
defp to_underscore("_" <> _rest, _acc),
|
||||
do: nil
|
||||
defp to_underscore(option), do: to_underscore(option, <<>>)
|
||||
|
||||
defp to_underscore("_" <> _rest, _acc), do: nil
|
||||
|
||||
defp to_underscore("-" <> rest, acc),
|
||||
do: to_underscore(rest, acc <> "_")
|
||||
|
||||
defp to_underscore(<<c>> <> rest, acc),
|
||||
do: to_underscore(rest, <<acc::binary, c>>)
|
||||
defp to_underscore(<<>>, acc),
|
||||
do: acc
|
||||
|
||||
defp to_underscore(<<>>, acc), do: acc
|
||||
|
||||
defp get_option(option) do
|
||||
if str = to_underscore(option) do
|
||||
@@ -614,38 +468,22 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp negative_number?(arg) do
|
||||
match?({_, ""}, Float.parse(arg))
|
||||
end
|
||||
|
||||
defp format_errors(errors, opts) do
|
||||
types = opts[:switches] || opts[:strict]
|
||||
info = Enum.map(errors, &format_error(&1, opts, types))
|
||||
total = length(errors)
|
||||
error = if total == 1, do: "error", else: "errors"
|
||||
"#{total} #{error} found!#{info}"
|
||||
end
|
||||
|
||||
defp format_error({option, nil}, opts, types) do
|
||||
if type = get_type(option, opts, types) do
|
||||
"\n#{option} : Missing argument of type #{type}"
|
||||
else
|
||||
"\n#{option} : Unknown option"
|
||||
defp get_negated("no-" <> rest = original, switches) do
|
||||
cond do
|
||||
(negated = get_option(rest)) && :boolean in List.wrap(switches[negated]) ->
|
||||
{:negated, negated}
|
||||
option = get_option(original) ->
|
||||
{:default, option}
|
||||
true ->
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
|
||||
defp format_error({option, value}, opts, types) do
|
||||
type = get_type(option, opts, types)
|
||||
"\n#{option} : Expected type #{type}, got #{inspect value}"
|
||||
end
|
||||
|
||||
defp get_type(option, opts, types) do
|
||||
option_key = option |> String.trim_leading("-") |> get_option()
|
||||
|
||||
if option_alias = opts[:aliases][option_key] do
|
||||
types[option_alias]
|
||||
defp get_negated(rest, _switches) do
|
||||
if option = get_option(rest) do
|
||||
{:default, option}
|
||||
else
|
||||
types[option_key]
|
||||
:unknown
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+71
-73
@@ -3,13 +3,13 @@ defmodule Path do
|
||||
This module provides conveniences for manipulating or
|
||||
retrieving file system paths.
|
||||
|
||||
The functions in this module may receive a chardata as
|
||||
The functions in this module may receive a char data as
|
||||
argument (i.e. a string or a list of characters / string)
|
||||
and will always return a string (encoded in UTF-8).
|
||||
|
||||
The majority of the functions in this module do not
|
||||
interact with the file system, except for a few functions
|
||||
that require it (like `wildcard/2` and `expand/1`).
|
||||
that require it (like `wildcard/1` and `expand/1`).
|
||||
"""
|
||||
|
||||
alias :filename, as: FN
|
||||
@@ -69,15 +69,15 @@ defmodule Path do
|
||||
end
|
||||
|
||||
# Absolute path on current drive
|
||||
defp absname_vr(["/" | rest], [volume | _], _relative),
|
||||
do: absname_join([volume | rest])
|
||||
defp absname_vr(["/"|rest], [volume|_], _relative),
|
||||
do: absname_join([volume|rest])
|
||||
|
||||
# Relative to current directory on current drive.
|
||||
defp absname_vr([<<x, ?:>> | rest], [<<x, _::binary>> | _], relative),
|
||||
defp absname_vr([<<x, ?:>>|rest], [<<x, _ :: binary>>|_], relative),
|
||||
do: absname(absname_join(rest), relative)
|
||||
|
||||
# Relative to current directory on another drive.
|
||||
defp absname_vr([<<x, ?:>> | name], _, _relative) do
|
||||
defp absname_vr([<<x, ?:>>|name], _, _relative) do
|
||||
cwd =
|
||||
case :file.get_cwd([x, ?:]) do
|
||||
{:ok, dir} -> IO.chardata_to_string(dir)
|
||||
@@ -87,8 +87,8 @@ defmodule Path do
|
||||
end
|
||||
|
||||
# Joins a list
|
||||
defp absname_join([name1, name2 | rest]), do:
|
||||
absname_join([absname_join(name1, name2) | rest])
|
||||
defp absname_join([name1, name2|rest]), do:
|
||||
absname_join([absname_join(name1, name2)|rest])
|
||||
defp absname_join([name]), do:
|
||||
do_absname_join(IO.chardata_to_string(name), <<>>, [], major_os_type())
|
||||
|
||||
@@ -96,30 +96,30 @@ defmodule Path do
|
||||
defp absname_join(left, right),
|
||||
do: do_absname_join(IO.chardata_to_string(left), relative(right), [], major_os_type())
|
||||
|
||||
defp do_absname_join(<<uc_letter, ?:, rest::binary>>, relativename, [], :win32) when uc_letter in ?A..?Z, do:
|
||||
defp do_absname_join(<<uc_letter, ?:, rest :: binary>>, relativename, [], :win32) when uc_letter in ?A..?Z, do:
|
||||
do_absname_join(rest, relativename, [?:, uc_letter+?a-?A], :win32)
|
||||
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32), do:
|
||||
do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?., ?/ | result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/ | result], os_type)
|
||||
defp do_absname_join(<<?/, rest::binary>>, relativename, [?/ | result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/ | result], os_type)
|
||||
defp do_absname_join(<<?\\, rest :: binary>>, relativename, result, :win32), do:
|
||||
do_absname_join(<<?/, rest :: binary>>, relativename, result, :win32)
|
||||
defp do_absname_join(<<?/, rest :: binary>>, relativename, [?., ?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<?/, rest :: binary>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(rest, relativename, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, <<>>, result, os_type), do:
|
||||
IO.iodata_to_binary(reverse_maybe_remove_dirsep(result, os_type))
|
||||
defp do_absname_join(<<>>, relativename, [?: | rest], :win32), do:
|
||||
do_absname_join(relativename, <<>>, [?: | rest], :win32)
|
||||
defp do_absname_join(<<>>, relativename, [?/ | result], os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/ | result], os_type)
|
||||
defp do_absname_join(<<>>, relativename, [?:|rest], :win32), do:
|
||||
do_absname_join(relativename, <<>>, [?:|rest], :win32)
|
||||
defp do_absname_join(<<>>, relativename, [?/|result], os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<>>, relativename, result, os_type), do:
|
||||
do_absname_join(relativename, <<>>, [?/ | result], os_type)
|
||||
defp do_absname_join(<<char, rest::binary>>, relativename, result, os_type), do:
|
||||
do_absname_join(rest, relativename, [char | result], os_type)
|
||||
do_absname_join(relativename, <<>>, [?/|result], os_type)
|
||||
defp do_absname_join(<<char, rest :: binary>>, relativename, result, os_type), do:
|
||||
do_absname_join(rest, relativename, [char|result], os_type)
|
||||
|
||||
defp reverse_maybe_remove_dirsep([?/, ?:, letter], :win32), do:
|
||||
[letter, ?:, ?/]
|
||||
defp reverse_maybe_remove_dirsep([?/], _), do:
|
||||
[?/]
|
||||
defp reverse_maybe_remove_dirsep([?/ | name], _), do:
|
||||
defp reverse_maybe_remove_dirsep([?/|name], _), do:
|
||||
:lists.reverse(name)
|
||||
defp reverse_maybe_remove_dirsep(name, _), do:
|
||||
:lists.reverse(name)
|
||||
@@ -131,7 +131,7 @@ defmodule Path do
|
||||
## Examples
|
||||
|
||||
Path.expand("/foo/bar/../bar")
|
||||
#=> "/foo/bar"
|
||||
"/foo/bar"
|
||||
|
||||
"""
|
||||
@spec expand(t) :: binary
|
||||
@@ -225,36 +225,36 @@ defmodule Path do
|
||||
end
|
||||
end
|
||||
|
||||
defp unix_pathtype(<<?/, relative::binary>>), do:
|
||||
defp unix_pathtype(<<?/, relative :: binary>>), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([?/ | relative]), do:
|
||||
defp unix_pathtype([?/|relative]), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([list | rest]) when is_list(list), do:
|
||||
defp unix_pathtype([list|rest]) when is_list(list), do:
|
||||
unix_pathtype(list ++ rest)
|
||||
defp unix_pathtype(relative), do:
|
||||
{:relative, relative}
|
||||
|
||||
@slash [?/, ?\\]
|
||||
|
||||
defp win32_pathtype([list | rest]) when is_list(list), do:
|
||||
defp win32_pathtype([list|rest]) when is_list(list), do:
|
||||
win32_pathtype(list++rest)
|
||||
defp win32_pathtype([char, list | rest]) when is_list(list), do:
|
||||
win32_pathtype([char | list++rest])
|
||||
defp win32_pathtype(<<c1, c2, relative::binary>>) when c1 in @slash and c2 in @slash, do:
|
||||
defp win32_pathtype([char, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([char|list++rest])
|
||||
defp win32_pathtype(<<c1, c2, relative :: binary>>) when c1 in @slash and c2 in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype(<<c, relative::binary>>) when c in @slash, do:
|
||||
defp win32_pathtype(<<c, relative :: binary>>) when c in @slash, do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype(<<_letter, ?:, c, relative::binary>>) when c in @slash, do:
|
||||
defp win32_pathtype(<<_letter, ?:, c, relative :: binary>>) when c in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype(<<_letter, ?:, relative::binary>>), do:
|
||||
defp win32_pathtype(<<_letter, ?:, relative :: binary>>), do:
|
||||
{:volumerelative, relative}
|
||||
|
||||
defp win32_pathtype([c1, c2 | relative]) when c1 in @slash and c2 in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([c | relative]) when c in @slash, do:
|
||||
{:volumerelative, relative}
|
||||
defp win32_pathtype([c1, c2, list | rest]) when is_list(list), do:
|
||||
win32_pathtype([c1, c2 | list++rest])
|
||||
defp win32_pathtype([c1, c2, list|rest]) when is_list(list), do:
|
||||
win32_pathtype([c1, c2|list++rest])
|
||||
defp win32_pathtype([_letter, ?:, c | relative]) when c in @slash, do:
|
||||
{:absolute, relative}
|
||||
defp win32_pathtype([_letter, ?: | relative]), do:
|
||||
@@ -290,11 +290,11 @@ defmodule Path do
|
||||
relative_to(split(path), split(from), path)
|
||||
end
|
||||
|
||||
defp relative_to([h | t1], [h | t2], original) do
|
||||
defp relative_to([h|t1], [h|t2], original) do
|
||||
relative_to(t1, t2, original)
|
||||
end
|
||||
|
||||
defp relative_to([_ | _] = l1, [], _original) do
|
||||
defp relative_to([_|_] = l1, [], _original) do
|
||||
join(l1)
|
||||
end
|
||||
|
||||
@@ -363,11 +363,10 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.dirname("/foo/bar.ex")
|
||||
"/foo"
|
||||
|
||||
iex> Path.dirname("/foo/bar/baz.ex")
|
||||
"/foo/bar"
|
||||
Path.dirname("/foo/bar.ex")
|
||||
#=> "/foo"
|
||||
Path.dirname("/foo/bar/baz.ex")
|
||||
#=> "/foo/bar"
|
||||
|
||||
"""
|
||||
@spec dirname(t) :: binary
|
||||
@@ -446,8 +445,8 @@ defmodule Path do
|
||||
|
||||
"""
|
||||
@spec join([t]) :: binary
|
||||
def join([name1, name2 | rest]), do:
|
||||
join([join(name1, name2) | rest])
|
||||
def join([name1, name2|rest]), do:
|
||||
join([join(name1, name2)|rest])
|
||||
def join([name]), do:
|
||||
name
|
||||
|
||||
@@ -471,11 +470,10 @@ defmodule Path do
|
||||
end
|
||||
|
||||
defp do_join("", right, os_type), do: relative(right, os_type)
|
||||
defp do_join("/", right, os_type), do: "/" <> relative(right, os_type)
|
||||
defp do_join(left, "", _os_type), do: left
|
||||
defp do_join(left, right, os_type), do: remove_dirsep(left, os_type) <> "/" <> relative(right, os_type)
|
||||
|
||||
defp remove_dirsep("", _os_type), do: ""
|
||||
defp remove_dirsep("/", _os_type), do: "/"
|
||||
defp remove_dirsep(bin, os_type) do
|
||||
last = :binary.last(bin)
|
||||
if last == ?/ or (last == ?\\ and os_type == :win32) do
|
||||
@@ -562,14 +560,10 @@ defmodule Path do
|
||||
* `**` - two adjacent `*`'s used as a single pattern will match all
|
||||
files and zero or more directories and subdirectories
|
||||
|
||||
* `[char1,char2,...]` - matches any of the characters listed; two
|
||||
characters separated by a hyphen will match a range of characters.
|
||||
Do not add spaces before and after the comma as it would then match
|
||||
paths containing the space character itself.
|
||||
* `[char1, char2, ...]` - matches any of the characters listed; two
|
||||
characters separated by a hyphen will match a range of characters
|
||||
|
||||
* `{item1,item2,...}` - matches one of the alternatives
|
||||
Do not add spaces before and after the comma as it would then match
|
||||
paths containing the space character itself.
|
||||
* `{item1, item2, ...}` - matches one of the alternatives
|
||||
|
||||
Other characters represent themselves. Only paths that have
|
||||
exactly the same character in the same position will match. Note
|
||||
@@ -634,28 +628,32 @@ defmodule Path do
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_dot(<<"/", rest::binary>>),
|
||||
do: "/" <> do_expand_dot(rest)
|
||||
defp expand_dot(<<letter, ":/", rest::binary>>) when letter in ?a..?z,
|
||||
do: <<letter, ":/">> <> do_expand_dot(rest)
|
||||
defp expand_dot(<<"/../", rest::binary>>),
|
||||
do: expand_dot("/" <> rest)
|
||||
defp expand_dot(<<letter, ":/../", rest::binary>>) when letter in ?a..?z,
|
||||
do: expand_dot(<<letter, ":/", rest::binary>>)
|
||||
defp expand_dot("/.."),
|
||||
do: "/"
|
||||
defp expand_dot(<<letter, ":/..">>) when letter in ?a..?z,
|
||||
do: expand_dot(<<letter, ":/">>)
|
||||
defp expand_dot(path),
|
||||
do: do_expand_dot(path)
|
||||
do: expand_dot(:binary.split(path, "/", [:global]), [])
|
||||
|
||||
defp do_expand_dot(path),
|
||||
do: do_expand_dot(:binary.split(path, "/", [:global]), [])
|
||||
defp expand_dot([".."|t], [_, _|acc]) do
|
||||
expand_dot t, acc
|
||||
end
|
||||
|
||||
defp do_expand_dot([".." | t], [_, _ | acc]),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([".." | t], []),
|
||||
do: do_expand_dot(t, [])
|
||||
defp do_expand_dot(["." | t], acc),
|
||||
do: do_expand_dot(t, acc)
|
||||
defp do_expand_dot([h | t], acc),
|
||||
do: do_expand_dot(t, ["/", h | acc])
|
||||
defp do_expand_dot([], []),
|
||||
do: ""
|
||||
defp do_expand_dot([], ["/" | acc]),
|
||||
do: IO.iodata_to_binary(:lists.reverse(acc))
|
||||
defp expand_dot(["."|t], acc) do
|
||||
expand_dot t, acc
|
||||
end
|
||||
|
||||
defp expand_dot([h|t], acc) do
|
||||
expand_dot t, ["/", h|acc]
|
||||
end
|
||||
|
||||
defp expand_dot([], ["/"|acc]) do
|
||||
IO.iodata_to_binary(:lists.reverse(acc))
|
||||
end
|
||||
|
||||
defp major_os_type do
|
||||
:os.type |> elem(0)
|
||||
|
||||
+9
-54
@@ -3,98 +3,56 @@ defmodule Port do
|
||||
Functions related to Erlang ports.
|
||||
"""
|
||||
|
||||
@type name :: {:spawn, charlist | binary} |
|
||||
{:spawn_driver, charlist | binary} |
|
||||
{:spawn_executable, charlist | atom} |
|
||||
{:fd, non_neg_integer, non_neg_integer}
|
||||
|
||||
@doc """
|
||||
Opens an Erlang port given a tuple `name` and a list of `settings`.
|
||||
|
||||
## Name
|
||||
|
||||
The supported values for `name` are:
|
||||
|
||||
* `{:spawn, command}` - to run an external program. The first space separated
|
||||
word of `command` will be considered as the name of the program to run, so
|
||||
use `{:spawn_executable, command}` to run a program having spaces in its name.
|
||||
* `{:spawn_driver, command}` - similar to `{:spawn, command}`, but to run a
|
||||
loaded driver.
|
||||
* `{:spawn_executable, filename}` - similar to `{:spawn, filename}`, but to run
|
||||
an external executable. With this option, `filename` in its whole is considered
|
||||
the name of the program to execute.
|
||||
* `{:fd, fd_in, fd_out}` - to access file descriptors used by Erlang, `fd_in`
|
||||
being used for standard input, `fd_out` for standard output.
|
||||
|
||||
For more information, see [`:erlang.open_port/2`](http://www.erlang.org/doc/man/erlang.html#open_port-2).
|
||||
See [`:erlang.open_port/2`](http://www.erlang.org/doc/man/erlang.html#open_port-2)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec open(name, list) :: port
|
||||
def open(name, settings) do
|
||||
:erlang.open_port(name, settings)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Closes the `port`.
|
||||
|
||||
For more information, see [`:erlang.port_close/1`](http://www.erlang.org/doc/man/erlang.html#port_close-1).
|
||||
See [`:erlang.port_close/1`](http://www.erlang.org/doc/man/erlang.html#port_close-1)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec close(port) :: true
|
||||
def close(port) do
|
||||
:erlang.port_close(port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends `data` to the port driver `port`.
|
||||
|
||||
For more information, see [`:erlang.port_command/2`](http://www.erlang.org/doc/man/erlang.html#port_command-2).
|
||||
See [`:erlang.port_command/2`](http://www.erlang.org/doc/man/erlang.html#port_command-2)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec command(port, iodata, [:force | :nosuspend]) :: boolean
|
||||
def command(port, data, options \\ []) do
|
||||
:erlang.port_command(port, data, options)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Associates the `port` identifier with a `pid`.
|
||||
|
||||
For more information, see [`:erlang.port_connect/2`](http://www.erlang.org/doc/man/erlang.html#port_connect-2).
|
||||
See [`:erlang.port_connect/2`](http://www.erlang.org/doc/man/erlang.html#port_connect-2)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec connect(port, pid) :: true
|
||||
def connect(port, pid) do
|
||||
:erlang.port_connect(port, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends a synchronous control command to the `port` and returns its reply as a binary.
|
||||
|
||||
Not all port drivers support this feature.
|
||||
|
||||
For more information, see [`:erlang.port_control/3`](http://www.erlang.org/doc/man/erlang.html#port_control-3).
|
||||
See [`:erlang.port_control/3`](http://www.erlang.org/doc/man/erlang.html#port_control-3)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec control(port, integer, iodata) :: iodata | binary
|
||||
def control(port, operation, data) do
|
||||
:erlang.port_control(port, operation, data)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Makes a synchronous call to the `port` and returns its reply as a term.
|
||||
|
||||
Not all port drivers support this control feature.
|
||||
|
||||
For more information, see [`:erlang.port_call/3`](http://www.erlang.org/doc/man/erlang.html#port_call-3).
|
||||
See [`:erlang.port_call/3`](http://www.erlang.org/doc/man/erlang.html#port_call-3)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec call(port, integer, term) :: term
|
||||
def call(port, operation, data) do
|
||||
:erlang.port_call(port, operation, data)
|
||||
end
|
||||
@@ -103,7 +61,7 @@ defmodule Port do
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/1`](http://www.erlang.org/doc/man/erlang.html#port_info-1).
|
||||
See [`:erlang.port_info/1`](http://www.erlang.org/doc/man/erlang.html#port_info-1)
|
||||
"""
|
||||
def info(port) do
|
||||
nillify :erlang.port_info(port)
|
||||
@@ -113,7 +71,7 @@ defmodule Port do
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
|
||||
For more information, see [`:erlang.port_info/2`](http://www.erlang.org/doc/man/erlang.html#port_info-2).
|
||||
See [`:erlang.port_info/2`](http://www.erlang.org/doc/man/erlang.html#port_info-2)
|
||||
"""
|
||||
@spec info(port, atom) :: {atom, term} | nil
|
||||
def info(port, spec)
|
||||
@@ -130,13 +88,10 @@ defmodule Port do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of the ports for the current node.
|
||||
|
||||
For more information, see [`:erlang.ports/0`](http://www.erlang.org/doc/man/erlang.html#ports-0).
|
||||
See [`:erlang.ports/0`](http://www.erlang.org/doc/man/erlang.html#ports-0)
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec list :: [port]
|
||||
def list do
|
||||
:erlang.ports
|
||||
end
|
||||
|
||||
+40
-177
@@ -19,8 +19,6 @@ defmodule Process do
|
||||
and has not exited yet). Otherwise, returns `false`.
|
||||
|
||||
`pid` must refer to a process at the local node.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec alive?(pid) :: boolean
|
||||
def alive?(pid) do
|
||||
@@ -28,9 +26,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all key-value pairs in the process dictionary.
|
||||
|
||||
Inlined by the compiler.
|
||||
Returns all key-values in the dictionary.
|
||||
"""
|
||||
@spec get :: [{term, term}]
|
||||
def get do
|
||||
@@ -38,7 +34,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for the given `key` or `default` if `key` is not set.
|
||||
Returns the value for the given `key`.
|
||||
"""
|
||||
@spec get(term) :: term
|
||||
@spec get(term, default :: term) :: term
|
||||
@@ -51,20 +47,8 @@ defmodule Process do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys in the process dictionary.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec get_keys() :: [term]
|
||||
def get_keys() do
|
||||
:erlang.get_keys()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys that have the given `value`.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec get_keys(term) :: [term]
|
||||
def get_keys(value) do
|
||||
@@ -72,10 +56,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stores the given `key`-`value` pair in the process dictionary.
|
||||
|
||||
The return value is the value that was previously stored under the key `key`
|
||||
(or `nil` in case no value was stored under `key`).
|
||||
Stores the given key-value in the process dictionary.
|
||||
"""
|
||||
@spec put(term, term) :: term | nil
|
||||
def put(key, value) do
|
||||
@@ -83,7 +64,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes the given `key` from the process dictionary.
|
||||
Deletes the given `key` from the dictionary.
|
||||
"""
|
||||
@spec delete(term) :: term | nil
|
||||
def delete(key) do
|
||||
@@ -91,27 +72,23 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends an exit signal with the given `reason` to the `pid`.
|
||||
Sends an exit signal with the given reason to the pid.
|
||||
|
||||
The following behaviour applies if `reason` is any term except `:normal`
|
||||
or `:kill`:
|
||||
The following behaviour applies if reason is any term except `:normal` or `:kill`:
|
||||
|
||||
1. If `pid` is not trapping exits, `pid` will exit with the given
|
||||
`reason`.
|
||||
1. If pid is not trapping exits, pid will exit with the given reason.
|
||||
|
||||
2. If `pid` is trapping exits, the exit signal is transformed into a
|
||||
message `{:EXIT, from, reason}` and delivered to the message queue
|
||||
of `pid`.
|
||||
2. If pid is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, reason}` and delivered to the message queue of pid.
|
||||
|
||||
3. If `reason` is the atom `:normal`, `pid` will not exit (unless it
|
||||
is the calling process's pid, in which case it will exit with the
|
||||
reason `:normal`). If it is trapping exits, the exit signal is
|
||||
transformed into a message `{:EXIT, from, :normal}` and delivered
|
||||
to its message queue.
|
||||
3. If reason is the atom `:normal`, pid will not exit (unless it is the calling
|
||||
process's pid, in which case it will exit with the reason `:normal`).
|
||||
If it is trapping exits, the exit signal is transformed into a message
|
||||
`{:EXIT, from, :normal}` and delivered to its message queue.
|
||||
|
||||
4. If `reason` is the atom `:kill`, that is if `exit(pid, :kill)` is
|
||||
called, an untrappable exit signal is sent to `pid` which will
|
||||
unconditionally exit with exit reason `:killed`.
|
||||
4. If reason is the atom `:kill`, that is if `exit(pid, :kill)` is called,
|
||||
an untrappable exit signal is sent to pid which will unconditionally
|
||||
exit with exit reason `:killed`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -125,80 +102,6 @@ defmodule Process do
|
||||
:erlang.exit(pid, reason)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sleeps the current process by `timeout`.
|
||||
|
||||
`timeout` is either the number of milliseconds to sleep as an
|
||||
integer or the atom `:infinity`. When `:infinity` is given,
|
||||
the current process will suspend forever.
|
||||
|
||||
**Use this function with extreme care**. For almost all situations
|
||||
where you would use `sleep/1` in Elixir, there is likely a
|
||||
more correct, faster and precise way of achieving it with
|
||||
message passing.
|
||||
|
||||
For example, if you are waiting a process to perform some
|
||||
action, it is better to communicate.
|
||||
|
||||
In other words, **do not**:
|
||||
|
||||
Task.start_link fn ->
|
||||
do_something()
|
||||
...
|
||||
end
|
||||
|
||||
# Wait until work is done
|
||||
Process.sleep(2000)
|
||||
|
||||
But **do**:
|
||||
|
||||
parent = self()
|
||||
Task.start_link fn ->
|
||||
do_something()
|
||||
send parent, :work_is_done
|
||||
...
|
||||
end
|
||||
|
||||
receive do
|
||||
:work_is_done -> :ok
|
||||
after
|
||||
30_000 -> :timeout # Optional timeout
|
||||
end
|
||||
|
||||
Or even use `Task.async/1` and `Task.await/2` in the example
|
||||
above.
|
||||
|
||||
Similarly, if you are waiting for a process to terminate,
|
||||
use monitor instead of sleep. **Do not**:
|
||||
|
||||
Task.start_link fn ->
|
||||
...
|
||||
end
|
||||
|
||||
# Wait until task terminates
|
||||
Process.sleep(2000)
|
||||
|
||||
Instead **do**:
|
||||
|
||||
{:ok, pid} =
|
||||
Task.start_link fn ->
|
||||
...
|
||||
end
|
||||
|
||||
ref = Process.monitor(pid)
|
||||
receive do
|
||||
{:DOWN, ^ref, _, _, _} -> :task_is_down
|
||||
after
|
||||
30_000 -> :timeout # Optional timeout
|
||||
end
|
||||
|
||||
"""
|
||||
def sleep(timeout)
|
||||
when is_integer(timeout) and timeout >= 0
|
||||
when timeout == :infinity do
|
||||
receive after: (timeout -> :ok)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sends a message to the given process.
|
||||
|
||||
@@ -217,10 +120,11 @@ defmodule Process do
|
||||
:noconnect
|
||||
|
||||
"""
|
||||
@spec send(dest, msg, [option]) :: :ok | :noconnect | :nosuspend when
|
||||
@spec send(dest, msg, [option]) :: result when
|
||||
dest: pid | port | atom | {atom, node},
|
||||
msg: any,
|
||||
option: :noconnect | :nosuspend
|
||||
option: :noconnect | :nosuspend,
|
||||
result: :ok | :noconnect | :nosuspend
|
||||
def send(dest, msg, options) do
|
||||
:erlang.send(dest, msg, options)
|
||||
end
|
||||
@@ -234,7 +138,8 @@ defmodule Process do
|
||||
not refer to a process.
|
||||
|
||||
This function returns a timer reference, which can be read or canceled with
|
||||
`read_timer/1` and `cancel_timer/1`.
|
||||
`:erlang.read_timer/1`, `:erlang.start_timer/3` and `:erlang.cancel_timer/1`.
|
||||
Note `time` cannot be greater than `4294967295`.
|
||||
|
||||
Finally, the timer will be automatically canceled if the given `dest` is a pid
|
||||
which is not alive or when the given pid exits. Note that timers will not be
|
||||
@@ -246,46 +151,6 @@ defmodule Process do
|
||||
:erlang.send_after(time, dest, msg)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Cancels a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milliseconds
|
||||
left until the timer would have expired.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
not be found. This can be either because the timer expired, already has
|
||||
been canceled, or because `timer_ref` never corresponded to a timer.
|
||||
|
||||
If the timer has expired, the timeout message has been sent, but it does
|
||||
not tell you whether or not it has arrived at its destination yet.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec cancel_timer(reference) :: non_neg_integer | false
|
||||
def cancel_timer(timer_ref) do
|
||||
:erlang.cancel_timer(timer_ref)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Reads a timer created by `send_after/3`.
|
||||
|
||||
When the result is an integer, it represents the time in milliseconds
|
||||
left until the timer will expire.
|
||||
|
||||
When the result is `false`, a timer corresponding to `timer_ref` could
|
||||
not be found. This can be either because the timer expired, already has
|
||||
been canceled, or because `timer_ref` never corresponded to a timer.
|
||||
|
||||
If the timer has expired, the timeout message has been sent, but it does
|
||||
not tell you whether or not it has arrived at its destination yet.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec read_timer(reference) :: non_neg_integer | false
|
||||
def read_timer(timer_ref) do
|
||||
:erlang.read_timer(timer_ref)
|
||||
end
|
||||
|
||||
@type spawn_opt :: :link | :monitor | {:priority, :low | :normal | :high} |
|
||||
{:fullsweep_after, non_neg_integer} |
|
||||
{:min_heap_size, non_neg_integer} |
|
||||
@@ -293,7 +158,8 @@ defmodule Process do
|
||||
@type spawn_opts :: [spawn_opt]
|
||||
|
||||
@doc """
|
||||
Spawns the given function according to the given options.
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
if `:monitor` is given as an option, it will return a tuple
|
||||
@@ -311,7 +177,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Spawns the given function from module `mod`, passing the given `args`
|
||||
Spawns the given module and function passing the given args
|
||||
according to the given options.
|
||||
|
||||
The result depends on the given options. In particular,
|
||||
@@ -330,11 +196,9 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
The calling process starts monitoring the given `item`.
|
||||
The calling process starts monitoring the item given.
|
||||
It returns the monitor reference.
|
||||
|
||||
See [the need for monitoring](http://elixir-lang.org/getting-started/mix-otp/genserver.html#the-need-for-monitoring)
|
||||
for an example.
|
||||
See [`:erlang.monitor/2`](http://www.erlang.org/doc/man/erlang.html#monitor-2) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -402,22 +266,21 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Associates the atom `name` with a `pid` or a port identifier.
|
||||
Associates the name with a pid or a port identifier. `name`, which must
|
||||
be an atom, can be used instead of the pid / port identifier with the
|
||||
`Kernel.send/2` function.
|
||||
|
||||
`name`, can then be used instead of the `pid` / port identifier with the `Kernel.send/2`
|
||||
function. `Process.register/2` will fail with `ArgumentError` if the pid supplied
|
||||
is no longer alive, (check with `alive?/1`) or if the name is already registered
|
||||
(check with `whereis/1`) or if the `pid` is already registered to a different `name`.
|
||||
`Process.register/2` will fail with `ArgumentError` if the pid supplied
|
||||
is no longer alive, (check with `alive?/1`) or if the name is
|
||||
already registered (check with `whereis/1`).
|
||||
"""
|
||||
@spec register(pid | port, atom) :: true
|
||||
def register(pid, name) when not name in [nil, false, true] and is_atom(name) do
|
||||
def register(pid, name) when not name in [nil, false, true] do
|
||||
:erlang.register(name, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes the registered `name`, associated with a pid or a port identifier.
|
||||
|
||||
Fails with `ArgumentError` if the name is not registered to any pid or port.
|
||||
Removes the registered name, associated with a pid or a port identifier.
|
||||
|
||||
See [`:erlang.unregister/1`](http://www.erlang.org/doc/man/erlang.html#unregister-1) for more info.
|
||||
"""
|
||||
@@ -427,7 +290,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the pid or port identifier with the registered `name`.
|
||||
Returns the pid or port identifier with the registered name.
|
||||
Returns `nil` if the name is not registered.
|
||||
|
||||
See [`:erlang.whereis/1`](http://www.erlang.org/doc/man/erlang.html#whereis-1) for more info.
|
||||
@@ -467,7 +330,7 @@ defmodule Process do
|
||||
:sensitive
|
||||
@doc """
|
||||
Sets certain flags for the process which calls this function.
|
||||
Returns the old value of the `flag`.
|
||||
Returns the old value of the flag.
|
||||
|
||||
See [`:erlang.process_flag/2`](http://www.erlang.org/doc/man/erlang.html#process_flag-2) for more info.
|
||||
"""
|
||||
@@ -478,8 +341,8 @@ defmodule Process do
|
||||
|
||||
@doc """
|
||||
Sets certain flags for the process `pid`, in the same manner as `flag/2`.
|
||||
Returns the old value of the `flag`. The allowed values for `flag` are
|
||||
only a subset of those allowed in `flag/2`, namely `:save_calls`.
|
||||
Returns the old value of the flag. The allowed values for `flag` are
|
||||
only a subset of those allowed in `flag/2`, namely: `save_calls`.
|
||||
|
||||
See [`:erlang.process_flag/3`](http://www.erlang.org/doc/man/erlang.html#process_flag-3) for more info.
|
||||
"""
|
||||
@@ -489,7 +352,7 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid`, or returns `nil` if the process
|
||||
Returns information about the process identified by `pid` or `nil` if the process
|
||||
is not alive.
|
||||
Use this only for debugging information.
|
||||
|
||||
@@ -501,8 +364,8 @@ defmodule Process do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the process identified by `pid`,
|
||||
or returns `nil` if the process is not alive.
|
||||
Returns information about the process identified by `pid`
|
||||
or `nil` if the process is not alive.
|
||||
|
||||
See [`:erlang.process_info/2`](http://www.erlang.org/doc/man/erlang.html#process_info-2) for more info.
|
||||
"""
|
||||
|
||||
+56
-67
@@ -31,7 +31,7 @@ defmodule Protocol do
|
||||
name = unquote(name)
|
||||
arity = unquote(arity)
|
||||
|
||||
@functions [{name, arity} | @functions]
|
||||
@functions [{name, arity}|@functions]
|
||||
|
||||
# Generate a fake definition with the user
|
||||
# signature that will be used by docs
|
||||
@@ -56,7 +56,7 @@ defmodule Protocol do
|
||||
@doc """
|
||||
Checks if the given module is loaded and is protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises `ArgumentError`.
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_protocol!(module) :: :ok | no_return
|
||||
def assert_protocol!(module) do
|
||||
@@ -83,7 +83,7 @@ defmodule Protocol do
|
||||
Checks if the given module is loaded and is an implementation
|
||||
of the given protocol.
|
||||
|
||||
Returns `:ok` if so, otherwise raises `ArgumentError`.
|
||||
Returns `:ok` if so, otherwise raises ArgumentError.
|
||||
"""
|
||||
@spec assert_impl!(module, module) :: :ok | no_return
|
||||
def assert_impl!(protocol, base) do
|
||||
@@ -129,8 +129,8 @@ defmodule Protocol do
|
||||
@doc """
|
||||
Extracts all protocols from the given paths.
|
||||
|
||||
The paths can be either a charlist or a string. Internally
|
||||
they are worked on as charlists, so passing them as lists
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the protocols.
|
||||
@@ -144,7 +144,7 @@ defmodule Protocol do
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_protocols([charlist | String.t]) :: [atom]
|
||||
@spec extract_protocols([char_list | String.t]) :: [atom]
|
||||
def extract_protocols(paths) do
|
||||
extract_matching_by_attribute paths, 'Elixir.',
|
||||
fn module, attributes ->
|
||||
@@ -159,8 +159,8 @@ defmodule Protocol do
|
||||
Extracts all types implemented for the given protocol from
|
||||
the given paths.
|
||||
|
||||
The paths can be either a charlist or a string. Internally
|
||||
they are worked on as charlists, so passing them as lists
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
avoid extra conversion.
|
||||
|
||||
Does not load any of the implementations.
|
||||
@@ -174,9 +174,9 @@ defmodule Protocol do
|
||||
true
|
||||
|
||||
"""
|
||||
@spec extract_impls(module, [charlist | String.t]) :: [atom]
|
||||
@spec extract_impls(module, [char_list | String.t]) :: [atom]
|
||||
def extract_impls(protocol, paths) when is_atom(protocol) do
|
||||
prefix = Atom.to_charlist(protocol) ++ '.'
|
||||
prefix = Atom.to_char_list(protocol) ++ '.'
|
||||
extract_matching_by_attribute paths, prefix, fn
|
||||
_mod, attributes ->
|
||||
case attributes[:impl] do
|
||||
@@ -200,7 +200,7 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defp list_dir(path), do: list_dir(to_charlist(path))
|
||||
defp list_dir(path), do: list_dir(to_char_list(path))
|
||||
|
||||
defp extract_from_file(path, file, prefix, callback) do
|
||||
if :lists.prefix(prefix, file) and :filename.extension(file) == '.beam' do
|
||||
@@ -217,6 +217,15 @@ defmodule Protocol do
|
||||
end
|
||||
end
|
||||
|
||||
defmacrop if_ok(expr, call) do
|
||||
quote do
|
||||
case unquote(expr) do
|
||||
{:ok, var} -> unquote(Macro.pipe(quote(do: var), call, 0))
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the protocol was consolidated.
|
||||
"""
|
||||
@@ -254,9 +263,9 @@ defmodule Protocol do
|
||||
{:error, :not_a_protocol} |
|
||||
{:error, :no_beam_info}
|
||||
def consolidate(protocol, types) when is_atom(protocol) do
|
||||
with {:ok, info} <- beam_protocol(protocol),
|
||||
{:ok, code, docs} <- change_debug_info(info, types),
|
||||
do: compile(code, docs)
|
||||
beam_protocol(protocol)
|
||||
|> if_ok(change_debug_info types)
|
||||
|> if_ok(compile)
|
||||
end
|
||||
|
||||
@docs_chunk 'ExDc'
|
||||
@@ -290,15 +299,15 @@ defmodule Protocol do
|
||||
# impl_for/1 dispatch version.
|
||||
defp change_debug_info({protocol, any, code, docs}, types) do
|
||||
types = if any, do: types, else: List.delete(types, Any)
|
||||
all = [Any] ++ for {_guard, mod} <- __builtin__(), do: mod
|
||||
all = [Any] ++ for {_guard, mod} <- builtin, do: mod
|
||||
structs = types -- all
|
||||
case change_impl_for(code, protocol, types, structs, false, []) do
|
||||
{:ok, ret} -> {:ok, ret, docs}
|
||||
{:ok, ret} -> {:ok, {ret, docs}}
|
||||
other -> other
|
||||
end
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :__protocol__, 1, clauses} | t], protocol, types, structs, _, acc) do
|
||||
defp change_impl_for([{:function, line, :__protocol__, 1, clauses}|t], protocol, types, structs, _, acc) do
|
||||
clauses = :lists.map(fn
|
||||
{:clause, l, [{:atom, _, :consolidated?}], [], [{:atom, _, _}]} ->
|
||||
{:clause, l, [{:atom, 0, :consolidated?}], [], [{:atom, 0, true}]}
|
||||
@@ -307,34 +316,34 @@ defmodule Protocol do
|
||||
end, clauses)
|
||||
|
||||
change_impl_for(t, protocol, types, structs, true,
|
||||
[{:function, line, :__protocol__, 1, clauses} | acc])
|
||||
[{:function, line, :__protocol__, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :impl_for, 1, _} | t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
defp change_impl_for([{:function, line, :impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any), else: nil
|
||||
|
||||
clauses = for {guard, mod} <- __builtin__(),
|
||||
clauses = for {guard, mod} <- builtin,
|
||||
mod in types,
|
||||
do: builtin_clause_for(mod, guard, protocol, line)
|
||||
|
||||
clauses = [struct_clause_for(line) | clauses] ++
|
||||
clauses = [struct_clause_for(line)|clauses] ++
|
||||
[fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :impl_for, 1, clauses} | acc])
|
||||
[{:function, line, :impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([{:function, line, :struct_impl_for, 1, _} | t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any)
|
||||
defp change_impl_for([{:function, line, :struct_impl_for, 1, _}|t], protocol, types, structs, is_protocol, acc) do
|
||||
fallback = if Any in types, do: load_impl(protocol, Any), else: nil
|
||||
clauses = for struct <- structs, do: each_struct_clause_for(struct, protocol, line)
|
||||
clauses = clauses ++ [fallback_clause_for(fallback, protocol, line)]
|
||||
|
||||
change_impl_for(t, protocol, types, structs, is_protocol,
|
||||
[{:function, line, :struct_impl_for, 1, clauses} | acc])
|
||||
[{:function, line, :struct_impl_for, 1, clauses}|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([h | t], protocol, info, types, is_protocol, acc) do
|
||||
change_impl_for(t, protocol, info, types, is_protocol, [h | acc])
|
||||
defp change_impl_for([h|t], protocol, info, types, is_protocol, acc) do
|
||||
change_impl_for(t, protocol, info, types, is_protocol, [h|acc])
|
||||
end
|
||||
|
||||
defp change_impl_for([], protocol, _info, _types, is_protocol, acc) do
|
||||
@@ -384,14 +393,13 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
# Finally compile the module and emit its bytecode.
|
||||
defp compile({protocol, code}, docs) do
|
||||
defp compile({{protocol, code}, docs}) do
|
||||
opts = if Code.compiler_options[:debug_info], do: [:debug_info], else: []
|
||||
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return | opts])
|
||||
{:ok,
|
||||
case docs do
|
||||
:missing_chunk -> binary
|
||||
_ -> :elixir_module.add_beam_chunk(binary, @docs_chunk, docs)
|
||||
end}
|
||||
{:ok, ^protocol, binary, _warnings} = :compile.forms(code, [:return|opts])
|
||||
unless docs == :missing_chunk do
|
||||
binary = :elixir_module.add_beam_chunk(binary, @docs_chunk, docs)
|
||||
end
|
||||
{:ok, binary}
|
||||
end
|
||||
|
||||
## Definition callbacks
|
||||
@@ -426,9 +434,8 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
defp after_defprotocol do
|
||||
quote bind_quoted: [builtin: __builtin__()] do
|
||||
@doc false
|
||||
@spec impl_for(term) :: atom | nil
|
||||
quote bind_quoted: [builtin: builtin] do
|
||||
@spec impl_for(term) :: atom() | nil
|
||||
Kernel.def impl_for(data)
|
||||
|
||||
# Define the implementation for structs.
|
||||
@@ -451,18 +458,7 @@ defmodule Protocol do
|
||||
end
|
||||
end, builtin)
|
||||
|
||||
# Define a catch-all impl_for/1 clause to pacify Dialyzer (since
|
||||
# destructuring opaque types is illegal, Dialyzer will think none of the
|
||||
# previous clauses matches opaque types, and without this clause, will
|
||||
# conclude that impl_for can't handle an opaque argument). This is a hack
|
||||
# since it relies on Dialyzer not being smart enough to conclude that all
|
||||
# opaque types will get the any_impl_for/0 implementation.
|
||||
Kernel.def impl_for(_) do
|
||||
any_impl_for()
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec impl_for!(term) :: atom | no_return
|
||||
@spec impl_for!(term) :: atom() | no_return()
|
||||
Kernel.def impl_for!(data) do
|
||||
impl_for(data) || raise(Protocol.UndefinedError, protocol: __MODULE__, value: data)
|
||||
end
|
||||
@@ -514,7 +510,7 @@ defmodule Protocol do
|
||||
@doc false
|
||||
def __functions_spec__([]),
|
||||
do: []
|
||||
def __functions_spec__([h | t]),
|
||||
def __functions_spec__([h|t]),
|
||||
do: [:lists.foldl(&{:|, [], [&1, &2]}, h, t), quote(do: ...)]
|
||||
|
||||
@doc false
|
||||
@@ -546,8 +542,14 @@ defmodule Protocol do
|
||||
for = unquote(for)
|
||||
name = Module.concat(protocol, for)
|
||||
|
||||
Protocol.assert_protocol!(protocol)
|
||||
Protocol.__ensure_defimpl__(protocol, for, __ENV__)
|
||||
# TODO: Remove this by 1.3
|
||||
if Atom.to_string(protocol) =~ "Elixir.Access" do
|
||||
:elixir_errors.warn __ENV__.line, __ENV__.file,
|
||||
"implementation of the Access protocol is deprecated. For customization of " <>
|
||||
"the dict[key] syntax, please implement the Dict behaviour instead"
|
||||
else
|
||||
Protocol.assert_protocol!(protocol)
|
||||
end
|
||||
|
||||
defmodule name do
|
||||
@behaviour protocol
|
||||
@@ -587,7 +589,6 @@ defmodule Protocol do
|
||||
defp derive(protocol, for, struct, opts, env) do
|
||||
extra = ", cannot derive #{inspect protocol} for #{inspect for}"
|
||||
assert_protocol!(protocol, extra)
|
||||
__ensure_defimpl__(protocol, for, env)
|
||||
assert_impl!(protocol, Any, extra)
|
||||
|
||||
# Clean up variables from eval context
|
||||
@@ -616,17 +617,6 @@ defmodule Protocol do
|
||||
end)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __ensure_defimpl__(protocol, for, env) do
|
||||
if Protocol.consolidated?(protocol) do
|
||||
message =
|
||||
"the #{inspect protocol} protocol has already been consolidated" <>
|
||||
", an implementation for #{inspect for} has no effect"
|
||||
:elixir_errors.warn(env.line, env.file, message)
|
||||
end
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc false
|
||||
def __spec__?(module, name, arity) do
|
||||
signature = {name, arity}
|
||||
@@ -645,8 +635,7 @@ defmodule Protocol do
|
||||
|
||||
## Helpers
|
||||
|
||||
@doc false
|
||||
def __builtin__ do
|
||||
defp builtin do
|
||||
[is_tuple: Tuple,
|
||||
is_atom: Atom,
|
||||
is_list: List,
|
||||
|
||||
+22
-37
@@ -1,12 +1,12 @@
|
||||
defmodule Range do
|
||||
@moduledoc """
|
||||
Defines a range.
|
||||
Defines a Range.
|
||||
|
||||
A range represents a discrete number of values where
|
||||
A Range represents a discrete number of values where
|
||||
the first and last values are integers.
|
||||
|
||||
Ranges can be either increasing (first <= last) or
|
||||
decreasing (first > last). Ranges are also always
|
||||
decresing (first > last). Ranges are also always
|
||||
inclusive.
|
||||
|
||||
A Range is represented internally as a struct. However,
|
||||
@@ -15,50 +15,28 @@ defmodule Range do
|
||||
|
||||
iex> range = 1..3
|
||||
1..3
|
||||
iex> first..last = range
|
||||
iex> first .. last = range
|
||||
iex> first
|
||||
1
|
||||
iex> last
|
||||
3
|
||||
|
||||
A Range implements the Enumerable protocol, which means
|
||||
all of the functions in the Enum module is available:
|
||||
|
||||
iex> range = 1..10
|
||||
1..10
|
||||
iex> Enum.reduce(range, 0, fn i, acc -> i * i + acc end)
|
||||
385
|
||||
iex> Enum.count(range)
|
||||
10
|
||||
iex> Enum.member?(range, 11)
|
||||
false
|
||||
iex> Enum.member?(range, 8)
|
||||
true
|
||||
|
||||
"""
|
||||
|
||||
defstruct first: nil, last: nil
|
||||
|
||||
@type t :: %Range{first: integer, last: integer}
|
||||
@type t :: %Range{}
|
||||
@type t(first, last) :: %Range{first: first, last: last}
|
||||
|
||||
|
||||
@doc """
|
||||
Creates a new range.
|
||||
"""
|
||||
@spec new(integer, integer) :: t
|
||||
def new(first, last) when is_integer(first) and is_integer(last) do
|
||||
def new(first, last) do
|
||||
%Range{first: first, last: last}
|
||||
end
|
||||
|
||||
def new(first, last) do
|
||||
raise ArgumentError,
|
||||
"ranges (first..last) expect both sides to be integers, " <>
|
||||
"got: #{inspect first}..#{inspect last}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is a valid range.
|
||||
Returns `true` if the given argument is a range.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -69,14 +47,13 @@ defmodule Range do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec range?(term) :: boolean
|
||||
def range?(term)
|
||||
def range?(first..last) when is_integer(first) and is_integer(last), do: true
|
||||
def range?(%Range{}), do: true
|
||||
def range?(_), do: false
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Range do
|
||||
def reduce(first..last, acc, fun) do
|
||||
def reduce(first .. last, acc, fun) do
|
||||
validate_range!(first, last)
|
||||
reduce(first, last, acc, fun, last >= first)
|
||||
end
|
||||
|
||||
@@ -100,7 +77,8 @@ defimpl Enumerable, for: Range do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
def member?(first..last, value) when is_integer(value) do
|
||||
def member?(first .. last, value) when is_integer(value) do
|
||||
validate_range!(first, last)
|
||||
if first <= last do
|
||||
{:ok, first <= value and value <= last}
|
||||
else
|
||||
@@ -108,23 +86,30 @@ defimpl Enumerable, for: Range do
|
||||
end
|
||||
end
|
||||
|
||||
def member?(_.._, _value) do
|
||||
def member?(_ .. _, _value) do
|
||||
{:ok, false}
|
||||
end
|
||||
|
||||
def count(first..last) do
|
||||
def count(first .. last) do
|
||||
validate_range!(first, last)
|
||||
if first <= last do
|
||||
{:ok, last - first + 1}
|
||||
else
|
||||
{:ok, first - last + 1}
|
||||
end
|
||||
end
|
||||
|
||||
defp validate_range!(first, last) when is_integer(first) and is_integer(last), do: :ok
|
||||
defp validate_range!(first, last) do
|
||||
raise ArgumentError,
|
||||
"ranges (left .. right) expect both sides to be integers, got: #{inspect first..last}"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Range do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(first..last, opts) do
|
||||
def inspect(first .. last, opts) do
|
||||
concat [to_doc(first, opts), "..", to_doc(last, opts)]
|
||||
end
|
||||
end
|
||||
|
||||
+26
-37
@@ -1,6 +1,6 @@
|
||||
defmodule Record do
|
||||
@moduledoc """
|
||||
Module to work with, define and import records.
|
||||
Module to work, define and import records.
|
||||
|
||||
Records are simply tuples where the first element is an atom:
|
||||
|
||||
@@ -10,7 +10,7 @@ defmodule Record do
|
||||
This module provides conveniences for working with records at
|
||||
compilation time, where compile-time field names are used to
|
||||
manipulate the tuples, providing fast operations on top of
|
||||
the tuples' compact structure.
|
||||
the tuples compact structure.
|
||||
|
||||
In Elixir, records are used mostly in two situations:
|
||||
|
||||
@@ -85,14 +85,14 @@ defmodule Record do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_atom(unquote(kind)) and is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0 and
|
||||
elem(unquote(data), 0) == unquote(kind)
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and :erlang.element(1, unquote(data)) == unquote(kind)
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
kind = unquote(kind)
|
||||
is_atom(kind) and is_tuple(result) and tuple_size(result) > 0 and elem(result, 0) == kind
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and :erlang.element(1, result) == unquote(kind)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -116,13 +116,14 @@ defmodule Record do
|
||||
case Macro.Env.in_guard?(__CALLER__) do
|
||||
true ->
|
||||
quote do
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0 and
|
||||
is_atom(elem(unquote(data), 0))
|
||||
is_tuple(unquote(data)) and tuple_size(unquote(data)) > 0
|
||||
and is_atom(:erlang.element(1, unquote(data)))
|
||||
end
|
||||
false ->
|
||||
quote do
|
||||
result = unquote(data)
|
||||
is_tuple(result) and tuple_size(result) > 0 and is_atom(elem(result, 0))
|
||||
is_tuple(result) and tuple_size(result) > 0
|
||||
and is_atom(:erlang.element(1, result))
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -178,7 +179,7 @@ defmodule Record do
|
||||
|
||||
## Defining extracted records with anonymous functions
|
||||
|
||||
If a record defines an anonymous function, an `ArgumentError`
|
||||
If a record defines an anonymous function, an ArgumentError
|
||||
will occur if you attempt to create a record with it.
|
||||
This can occur unintentionally when defining a record after extracting
|
||||
it from an Erlang library that uses anonymous functions for defaults.
|
||||
@@ -259,8 +260,8 @@ defmodule Record do
|
||||
create(atom, fields, args, caller)
|
||||
true ->
|
||||
case Macro.expand(args, caller) do
|
||||
{:{}, _, [^atom | list]} when length(list) == length(fields) ->
|
||||
record = List.to_tuple([atom | list])
|
||||
{:{}, _, [^atom|list]} when length(list) == length(fields) ->
|
||||
record = List.to_tuple([atom|list])
|
||||
Macro.escape(Record.__keyword__(atom, fields, record))
|
||||
{^atom, arg} when length(fields) == 1 ->
|
||||
Macro.escape(Record.__keyword__(atom, fields, {atom, arg}))
|
||||
@@ -296,15 +297,17 @@ defmodule Record do
|
||||
# Creates a new record with the given default fields and keyword values.
|
||||
defp create(atom, fields, keyword, caller) do
|
||||
in_match = Macro.Env.in_match?(caller)
|
||||
keyword = apply_underscore(fields, keyword)
|
||||
|
||||
{match, remaining} =
|
||||
Enum.map_reduce(fields, keyword, fn({field, default}, each_keyword) ->
|
||||
new_fields =
|
||||
case Keyword.fetch(each_keyword, field) do
|
||||
{:ok, value} -> value
|
||||
:error when in_match -> {:_, [], nil}
|
||||
:error -> Macro.escape(default)
|
||||
case Keyword.has_key?(each_keyword, field) do
|
||||
true -> Keyword.get(each_keyword, field)
|
||||
false ->
|
||||
case in_match do
|
||||
true -> {:_, [], nil}
|
||||
false -> Macro.escape(default)
|
||||
end
|
||||
end
|
||||
|
||||
{new_fields, Keyword.delete(each_keyword, field)}
|
||||
@@ -312,7 +315,7 @@ defmodule Record do
|
||||
|
||||
case remaining do
|
||||
[] ->
|
||||
{:{}, [], [atom | match]}
|
||||
{:{}, [], [atom|match]}
|
||||
_ ->
|
||||
keys = for {key, _} <- remaining, do: key
|
||||
raise ArgumentError, "record #{inspect atom} does not have the key: #{inspect hd(keys)}"
|
||||
@@ -325,8 +328,6 @@ defmodule Record do
|
||||
raise ArgumentError, "cannot invoke update style macro inside match"
|
||||
end
|
||||
|
||||
keyword = apply_underscore(fields, keyword)
|
||||
|
||||
Enum.reduce keyword, var, fn({key, value}, acc) ->
|
||||
index = find_index(fields, key, 0)
|
||||
if index do
|
||||
@@ -351,15 +352,15 @@ defmodule Record do
|
||||
end
|
||||
end
|
||||
|
||||
defp find_index([{k, _} | _], k, i), do: i + 2
|
||||
defp find_index([{_, _} | t], k, i), do: find_index(t, k, i + 1)
|
||||
defp find_index([{k, _}|_], k, i), do: i + 2
|
||||
defp find_index([{_, _}|t], k, i), do: find_index(t, k, i + 1)
|
||||
defp find_index([], _k, _i), do: nil
|
||||
|
||||
# Returns a keyword list of the record
|
||||
@doc false
|
||||
def __keyword__(atom, fields, record) do
|
||||
if is_record(record, atom) do
|
||||
[_tag | values] = Tuple.to_list(record)
|
||||
[_tag|values] = Tuple.to_list(record)
|
||||
join_keyword(fields, values, [])
|
||||
else
|
||||
msg = "expected argument to be a literal atom, literal keyword or a #{inspect atom} record, got runtime: #{inspect record}"
|
||||
@@ -367,20 +368,8 @@ defmodule Record do
|
||||
end
|
||||
end
|
||||
|
||||
defp join_keyword([{field, _default} | fields], [value | values], acc),
|
||||
do: join_keyword(fields, values, [{field, value} | acc])
|
||||
defp join_keyword([{field, _default}|fields], [value|values], acc),
|
||||
do: join_keyword(fields, values, [{field, value}| acc])
|
||||
defp join_keyword([], [], acc),
|
||||
do: :lists.reverse(acc)
|
||||
|
||||
defp apply_underscore(fields, keyword) do
|
||||
case Keyword.fetch(keyword, :_) do
|
||||
{:ok, default} ->
|
||||
fields
|
||||
|> Enum.map(fn {k, _} -> {k, default} end)
|
||||
|> Keyword.merge(keyword)
|
||||
|> Keyword.delete(:_)
|
||||
:error ->
|
||||
keyword
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -27,7 +27,7 @@ defmodule Record.Extractor do
|
||||
|
||||
# Find file using the same lookup as the *include* attribute from Erlang modules.
|
||||
defp from_file(file) do
|
||||
file = String.to_charlist(file)
|
||||
file = String.to_char_list(file)
|
||||
case :code.where_is_file(file) do
|
||||
:non_existing -> file
|
||||
realfile -> realfile
|
||||
@@ -36,12 +36,12 @@ defmodule Record.Extractor do
|
||||
|
||||
# Find file using the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
defp from_lib_file(file) do
|
||||
[app | path] = :filename.split(String.to_charlist(file))
|
||||
[app|path] = :filename.split(String.to_char_list(file))
|
||||
case :code.lib_dir(List.to_atom(app)) do
|
||||
{:error, _} ->
|
||||
raise ArgumentError, "lib file #{file} could not be found"
|
||||
libpath ->
|
||||
:filename.join([libpath | path])
|
||||
:filename.join([libpath|path])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
+52
-83
@@ -1,7 +1,6 @@
|
||||
defmodule Regex do
|
||||
@moduledoc ~S"""
|
||||
Provides regular expressions for Elixir. Built on top of Erlang's `:re`
|
||||
module.
|
||||
Regular expressions for Elixir built on top of Erlang's `:re` module.
|
||||
|
||||
As the `:re` module, Regex is based on PCRE
|
||||
(Perl Compatible Regular Expressions). More information can be
|
||||
@@ -13,7 +12,7 @@ defmodule Regex do
|
||||
# A simple regular expressions that matches foo anywhere in the string
|
||||
~r/foo/
|
||||
|
||||
# A regular expression with case insensitive and Unicode options
|
||||
# A regular expression with case insensitive and unicode options
|
||||
~r/foo/iu
|
||||
|
||||
A Regex is represented internally as the `Regex` struct. Therefore,
|
||||
@@ -23,9 +22,9 @@ defmodule Regex do
|
||||
|
||||
The modifiers available when creating a Regex are:
|
||||
|
||||
* `unicode` (u) - enables Unicode specific patterns like `\p` and change
|
||||
modifiers like `\w`, `\W`, `\s` and friends to also match on Unicode.
|
||||
It expects valid Unicode strings to be given on match
|
||||
* `unicode` (u) - enables unicode specific patterns like `\p` and changes
|
||||
modifiers like `\w`, `\W`, `\s` and friends to also match on unicode.
|
||||
It expects valid unicode strings to be given on match
|
||||
|
||||
* `caseless` (i) - add case insensitivity
|
||||
|
||||
@@ -56,7 +55,7 @@ defmodule Regex do
|
||||
|
||||
## Captures
|
||||
|
||||
Many functions in this module handle what to capture in a regex
|
||||
Many functions in this module allows what to capture in a regex
|
||||
match via the `:capture` option. The supported values are:
|
||||
|
||||
* `:all` - all captured subpatterns including the complete matching string
|
||||
@@ -135,7 +134,7 @@ defmodule Regex do
|
||||
Compiles the regular expression according to the given options.
|
||||
Fails with `Regex.CompileError` if the regex cannot be compiled.
|
||||
"""
|
||||
@spec compile!(binary, binary | [term]) :: t
|
||||
@spec compile(binary, binary | [term]) :: t
|
||||
def compile!(source, options \\ "") do
|
||||
case compile(source, options) do
|
||||
{:ok, regex} -> regex
|
||||
@@ -173,7 +172,8 @@ defmodule Regex do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec regex?(any) :: boolean
|
||||
@spec regex?(t) :: true
|
||||
@spec regex?(any) :: false
|
||||
def regex?(term)
|
||||
def regex?(%Regex{}), do: true
|
||||
def regex?(_), do: false
|
||||
@@ -347,9 +347,6 @@ defmodule Regex do
|
||||
order. Defaults to `:first` which means captures inside the regex do not
|
||||
affect the splitting process.
|
||||
|
||||
* `:include_captures` - when `true`, includes in the result the matches of
|
||||
the regular expression. Defaults to `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Regex.split(~r/-/, "a-b-c")
|
||||
@@ -370,12 +367,6 @@ defmodule Regex do
|
||||
iex> Regex.split(~r/a(?<second>b)c/, "abc", on: [:second])
|
||||
["a", "c"]
|
||||
|
||||
iex> Regex.split(~r/(x)/, "Elixir", include_captures: true)
|
||||
["Eli", "x", "ir"]
|
||||
|
||||
iex> Regex.split(~r/a(?<second>b)c/, "abc", on: [:second], include_captures: true)
|
||||
["a", "b", "c"]
|
||||
|
||||
"""
|
||||
@spec split(t, String.t, [term]) :: [String.t]
|
||||
def split(regex, string, options \\ [])
|
||||
@@ -388,14 +379,13 @@ defmodule Regex do
|
||||
end
|
||||
end
|
||||
|
||||
def split(%Regex{re_pattern: compiled}, string, opts) when is_binary(string) and is_list(opts) do
|
||||
def split(%Regex{re_pattern: compiled}, string, opts) when is_binary(string) do
|
||||
on = Keyword.get(opts, :on, :first)
|
||||
case :re.run(string, compiled, [:global, capture: on]) do
|
||||
{:match, matches} ->
|
||||
do_split(matches, string, 0,
|
||||
parts_to_index(Keyword.get(opts, :parts, :infinity)),
|
||||
Keyword.get(opts, :trim, false),
|
||||
Keyword.get(opts, :include_captures, false))
|
||||
Keyword.get(opts, :trim, false))
|
||||
:match ->
|
||||
[string]
|
||||
:nomatch ->
|
||||
@@ -406,47 +396,30 @@ defmodule Regex do
|
||||
defp parts_to_index(:infinity), do: 0
|
||||
defp parts_to_index(n) when is_integer(n) and n > 0, do: n
|
||||
|
||||
defp do_split(_, string, offset, _counter, true, _with_captures) when byte_size(string) <= offset,
|
||||
defp do_split(_, string, offset, _counter, true) when byte_size(string) <= offset,
|
||||
do: []
|
||||
|
||||
defp do_split(_, string, offset, 1, _trim, _with_captures),
|
||||
defp do_split(_, string, offset, 1, _trim),
|
||||
do: [binary_part(string, offset, byte_size(string) - offset)]
|
||||
|
||||
defp do_split([], string, offset, _counter, _trim, _with_captures),
|
||||
defp do_split([], string, offset, _counter, _trim),
|
||||
do: [binary_part(string, offset, byte_size(string) - offset)]
|
||||
|
||||
defp do_split([[{pos, _} | h] | t], string, offset, counter, trim, with_captures) when pos - offset < 0,
|
||||
do: do_split([h | t], string, offset, counter, trim, with_captures)
|
||||
defp do_split([[{pos, _}|h]|t], string, offset, counter, trim) when pos - offset < 0,
|
||||
do: do_split([h|t], string, offset, counter, trim)
|
||||
|
||||
defp do_split([[] | t], string, offset, counter, trim, with_captures),
|
||||
do: do_split(t, string, offset, counter, trim, with_captures)
|
||||
defp do_split([[]|t], string, offset, counter, trim),
|
||||
do: do_split(t, string, offset, counter, trim)
|
||||
|
||||
defp do_split([[{pos, length} | h] | t], string, offset, counter, trim, true) do
|
||||
new_offset = pos + length
|
||||
keep = pos - offset
|
||||
|
||||
if keep == 0 and length == 0 do
|
||||
do_split([h | t], string, new_offset, counter, trim, true)
|
||||
else
|
||||
<<_::binary-size(offset), part::binary-size(keep), match::binary-size(length), _::binary>> = string
|
||||
|
||||
if keep == 0 and (length == 0 or trim) do
|
||||
[match | do_split([h | t], string, new_offset, counter - 1, trim, true)]
|
||||
else
|
||||
[part, match | do_split([h | t], string, new_offset, counter - 1, trim, true)]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_split([[{pos, length} | h] | t], string, offset, counter, trim, false) do
|
||||
defp do_split([[{pos, length}|h]|t], string, offset, counter, trim) do
|
||||
new_offset = pos + length
|
||||
keep = pos - offset
|
||||
|
||||
if keep == 0 and (length == 0 or trim) do
|
||||
do_split([h | t], string, new_offset, counter, trim, false)
|
||||
do_split([h|t], string, new_offset, counter, trim)
|
||||
else
|
||||
<<_::binary-size(offset), part::binary-size(keep), _::binary>> = string
|
||||
[part | do_split([h | t], string, new_offset, counter - 1, trim, false)]
|
||||
[part|do_split([h|t], string, new_offset, counter - 1, trim)]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -496,26 +469,24 @@ defmodule Regex do
|
||||
@spec replace(t, String.t, String.t | (... -> String.t), [term]) :: String.t
|
||||
def replace(regex, string, replacement, options \\ [])
|
||||
|
||||
def replace(regex, string, replacement, options)
|
||||
when is_binary(string) and is_binary(replacement) and is_list(options) do
|
||||
def replace(regex, string, replacement, options) when is_binary(replacement) do
|
||||
do_replace(regex, string, precompile_replacement(replacement), options)
|
||||
end
|
||||
|
||||
def replace(regex, string, replacement, options)
|
||||
when is_binary(string) and is_function(replacement) and is_list(options) do
|
||||
def replace(regex, string, replacement, options) when is_function(replacement) do
|
||||
{:arity, arity} = :erlang.fun_info(replacement, :arity)
|
||||
do_replace(regex, string, {replacement, arity}, options)
|
||||
end
|
||||
|
||||
defp do_replace(%Regex{re_pattern: compiled}, string, replacement, options) do
|
||||
opts = if Keyword.get(options, :global) != false, do: [:global], else: []
|
||||
opts = [{:capture, :all, :index} | opts]
|
||||
opts = [{:capture, :all, :index}|opts]
|
||||
|
||||
case :re.run(string, compiled, opts) do
|
||||
:nomatch ->
|
||||
string
|
||||
{:match, [mlist | t]} when is_list(mlist) ->
|
||||
apply_list(string, replacement, [mlist | t]) |> IO.iodata_to_binary
|
||||
{:match, [mlist|t]} when is_list(mlist) ->
|
||||
apply_list(string, replacement, [mlist|t]) |> IO.iodata_to_binary
|
||||
{:match, slist} ->
|
||||
apply_list(string, replacement, [slist]) |> IO.iodata_to_binary
|
||||
end
|
||||
@@ -524,32 +495,32 @@ defmodule Regex do
|
||||
defp precompile_replacement(""),
|
||||
do: []
|
||||
|
||||
defp precompile_replacement(<<?\\, ?g, ?{, rest::binary>>) when byte_size(rest) > 0 do
|
||||
{ns, <<?}, rest::binary>>} = pick_int(rest)
|
||||
defp precompile_replacement(<<?\\, ?g, ?{, rest :: binary>>) when byte_size(rest) > 0 do
|
||||
{ns, <<?}, rest :: binary>>} = pick_int(rest)
|
||||
[List.to_integer(ns) | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<?\\, ?\\, rest::binary>>) do
|
||||
defp precompile_replacement(<<?\\, ?\\, rest :: binary>>) do
|
||||
[<<?\\>> | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<?\\, x, rest::binary>>) when x in ?0..?9 do
|
||||
defp precompile_replacement(<<?\\, x, rest :: binary>>) when x in ?0..?9 do
|
||||
{ns, rest} = pick_int(rest)
|
||||
[List.to_integer([x | ns]) | precompile_replacement(rest)]
|
||||
[List.to_integer([x|ns]) | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<x, rest::binary>>) do
|
||||
defp precompile_replacement(<<x, rest :: binary>>) do
|
||||
case precompile_replacement(rest) do
|
||||
[head | t] when is_binary(head) ->
|
||||
[<<x, head::binary>> | t]
|
||||
[<<x, head :: binary>> | t]
|
||||
other ->
|
||||
[<<x>> | other]
|
||||
end
|
||||
end
|
||||
|
||||
defp pick_int(<<x, rest::binary>>) when x in ?0..?9 do
|
||||
defp pick_int(<<x, rest :: binary>>) when x in ?0..?9 do
|
||||
{found, rest} = pick_int(rest)
|
||||
{[x | found], rest}
|
||||
{[x|found], rest}
|
||||
end
|
||||
|
||||
defp pick_int(bin) do
|
||||
@@ -570,12 +541,12 @@ defmodule Regex do
|
||||
|
||||
defp apply_list(whole, string, pos, replacement, [[{mpos, _} | _] | _] = list) when mpos > pos do
|
||||
length = mpos - pos
|
||||
<<untouched::binary-size(length), rest::binary>> = string
|
||||
<<untouched :: binary-size(length), rest :: binary>> = string
|
||||
[untouched | apply_list(whole, rest, mpos, replacement, list)]
|
||||
end
|
||||
|
||||
defp apply_list(whole, string, pos, replacement, [[{pos, length} | _] = head | tail]) do
|
||||
<<_::size(length)-binary, rest::binary>> = string
|
||||
<<_ :: size(length)-binary, rest :: binary>> = string
|
||||
new_data = apply_replace(whole, replacement, head)
|
||||
[new_data | apply_list(whole, rest, pos + length, replacement, tail)]
|
||||
end
|
||||
@@ -608,7 +579,7 @@ defmodule Regex do
|
||||
end
|
||||
|
||||
defp get_index(string, {pos, len}) do
|
||||
<<_::size(pos)-binary, res::size(len)-binary, _::binary>> = string
|
||||
<<_ :: size(pos)-binary, res :: size(len)-binary, _ :: binary>> = string
|
||||
res
|
||||
end
|
||||
|
||||
@@ -617,14 +588,14 @@ defmodule Regex do
|
||||
end
|
||||
|
||||
defp get_indexes(string, [], arity) do
|
||||
["" | get_indexes(string, [], arity - 1)]
|
||||
[""|get_indexes(string, [], arity - 1)]
|
||||
end
|
||||
|
||||
defp get_indexes(string, [h | t], arity) do
|
||||
[get_index(string, h) | get_indexes(string, t, arity - 1)]
|
||||
defp get_indexes(string, [h|t], arity) do
|
||||
[get_index(string, h)|get_indexes(string, t, arity - 1)]
|
||||
end
|
||||
|
||||
{:ok, pattern} = :re.compile(~S"[.^$*+?()\[\]{}\\\|\s#]", [:unicode])
|
||||
{:ok, pattern} = :re.compile(~S"[.^$*+?()[{\\\|\s#]", [:unicode])
|
||||
@escape_pattern pattern
|
||||
|
||||
@doc ~S"""
|
||||
@@ -658,19 +629,17 @@ defmodule Regex do
|
||||
|
||||
# Private Helpers
|
||||
|
||||
defp translate_options(<<?u, t::binary>>, acc), do: translate_options(t, [:unicode, :ucp | acc])
|
||||
defp translate_options(<<?i, t::binary>>, acc), do: translate_options(t, [:caseless | acc])
|
||||
defp translate_options(<<?x, t::binary>>, acc), do: translate_options(t, [:extended | acc])
|
||||
defp translate_options(<<?f, t::binary>>, acc), do: translate_options(t, [:firstline | acc])
|
||||
defp translate_options(<<?U, t::binary>>, acc), do: translate_options(t, [:ungreedy | acc])
|
||||
defp translate_options(<<?s, t::binary>>, acc), do: translate_options(t, [:dotall, {:newline, :anycrlf} | acc])
|
||||
defp translate_options(<<?m, t::binary>>, acc), do: translate_options(t, [:multiline | acc])
|
||||
defp translate_options(<<?u, t :: binary>>, acc), do: translate_options(t, [:unicode, :ucp|acc])
|
||||
defp translate_options(<<?i, t :: binary>>, acc), do: translate_options(t, [:caseless|acc])
|
||||
defp translate_options(<<?x, t :: binary>>, acc), do: translate_options(t, [:extended|acc])
|
||||
defp translate_options(<<?f, t :: binary>>, acc), do: translate_options(t, [:firstline|acc])
|
||||
defp translate_options(<<?U, t :: binary>>, acc), do: translate_options(t, [:ungreedy|acc])
|
||||
defp translate_options(<<?s, t :: binary>>, acc), do: translate_options(t, [:dotall, {:newline, :anycrlf}|acc])
|
||||
defp translate_options(<<?m, t :: binary>>, acc), do: translate_options(t, [:multiline|acc])
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
defp translate_options(<<?r, t::binary>>, acc) do
|
||||
IO.warn "the /r modifier in regular expressions is deprecated, please use /U instead"
|
||||
translate_options(t, [:ungreedy | acc])
|
||||
end
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
defp translate_options(<<?r, t :: binary>>, acc), do: translate_options(t, [:ungreedy|acc])
|
||||
|
||||
defp translate_options(<<>>, acc), do: acc
|
||||
defp translate_options(rest, _acc), do: {:error, rest}
|
||||
|
||||
+202
-9
@@ -1,15 +1,48 @@
|
||||
defmodule Set do
|
||||
@moduledoc ~S"""
|
||||
WARNING: this module is deprecated.
|
||||
This module specifies the `Set` behaviour expected to be
|
||||
implemented by different representations of sets.
|
||||
|
||||
Use the `MapSet` module instead.
|
||||
It also provides functions that redirect to the
|
||||
underlying implementation, allowing a developer to work with
|
||||
different `Set` implementations using a common API.
|
||||
|
||||
To create a new set, use the `new` function which each set implementation
|
||||
defines:
|
||||
|
||||
HashSet.new #=> creates an empty HashSet
|
||||
|
||||
In the examples below, `set_impl` means a specific
|
||||
`Set` implementation, for example `HashSet`.
|
||||
|
||||
## Protocols
|
||||
|
||||
Sets are required to implement both the `Enumerable` and `Collectable`
|
||||
protocols.
|
||||
|
||||
## Matching
|
||||
|
||||
Sets are required to implement all equality checks using the match (`===`)
|
||||
operator.
|
||||
"""
|
||||
|
||||
@type value :: any
|
||||
@type values :: [ value ]
|
||||
@type t :: map
|
||||
|
||||
# TODO: Deprecate every function by 1.4
|
||||
@callback new :: t
|
||||
@callback delete(t, value) :: t
|
||||
@callback difference(t, t) :: t
|
||||
@callback disjoint?(t, t) :: boolean
|
||||
@callback equal?(t, t) :: boolean
|
||||
@callback intersection(t, t) :: t
|
||||
@callback member?(t, value) :: boolean
|
||||
@callback put(t, value) :: t
|
||||
@callback size(t) :: non_neg_integer
|
||||
@callback subset?(t, t) :: boolean
|
||||
@callback to_list(t) :: list()
|
||||
@callback union(t, t) :: t
|
||||
|
||||
defmacrop target(set) do
|
||||
quote do
|
||||
case unquote(set) do
|
||||
@@ -21,10 +54,42 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes `value` from `set`.
|
||||
|
||||
Returns a new set which is a copy of `set` but without `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> s = Enum.into([1, 2, 3], set_impl.new)
|
||||
iex> Set.delete(s, 4) |> Enum.sort
|
||||
[1, 2, 3]
|
||||
|
||||
iex> s = Enum.into([1, 2, 3], set_impl.new)
|
||||
iex> Set.delete(s, 2) |> Enum.sort
|
||||
[1, 3]
|
||||
|
||||
"""
|
||||
@spec delete(t, value) :: t
|
||||
def delete(set, value) do
|
||||
target(set).delete(set, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set that is `set1` without the members of `set2`.
|
||||
|
||||
Note that this function is polymorphic as it calculates the difference for
|
||||
sets of the same type as well as of sets of different types. Each set
|
||||
implementation also provides a `difference` function which only works with
|
||||
sets of that type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.difference(Enum.into([1, 2], set_impl.new), Enum.into([2, 3, 4], set_impl.new)) |> Enum.sort
|
||||
[1]
|
||||
|
||||
"""
|
||||
@spec difference(t, t) :: t
|
||||
def difference(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -32,12 +97,29 @@ defmodule Set do
|
||||
if target1 == target2 do
|
||||
target1.difference(set1, set2)
|
||||
else
|
||||
Enumerable.reduce(set2, {:cont, set1}, fn v, acc ->
|
||||
target2.reduce(set2, {:cont, set1}, fn v, acc ->
|
||||
{:cont, target1.delete(acc, v)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1` and `set2` have no members in common.
|
||||
|
||||
Note that this function is polymorphic as it checks for disjoint sets of
|
||||
any type. Each set implementation also provides a `disjoint?` function,
|
||||
but that function can only work with sets of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.disjoint?(Enum.into([1, 2], set_impl.new), Enum.into([3, 4], set_impl.new))
|
||||
true
|
||||
|
||||
iex> Set.disjoint?(Enum.into([1, 2], set_impl.new), Enum.into([2, 3], set_impl.new))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec disjoint?(t, t) :: boolean
|
||||
def disjoint?(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -45,7 +127,7 @@ defmodule Set do
|
||||
if target1 == target2 do
|
||||
target1.disjoint?(set1, set2)
|
||||
else
|
||||
Enumerable.reduce(set2, {:cont, true}, fn member, acc ->
|
||||
target2.reduce(set2, {:cont, true}, fn member, acc ->
|
||||
case target1.member?(set1, member) do
|
||||
false -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
@@ -55,10 +137,28 @@ defmodule Set do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec empty(t) :: t
|
||||
def empty(set) do
|
||||
target(set).empty(set)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two sets are equal using `===`.
|
||||
|
||||
Note that this function is polymorphic as it compares sets of
|
||||
any type. Each set implementation also provides an `equal?`
|
||||
function, but that function can only work with sets of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.equal?(Enum.into([1, 2], set_impl.new), Enum.into([2, 1, 1], set_impl.new))
|
||||
true
|
||||
|
||||
iex> Set.equal?(Enum.into([1, 2], set_impl.new), Enum.into([3, 4], set_impl.new))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec equal?(t, t) :: boolean
|
||||
def equal?(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -75,7 +175,23 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing only members that `set1` and `set2` have in common.
|
||||
|
||||
Note that this function is polymorphic as it calculates the intersection of
|
||||
any type. Each set implementation also provides an `intersection` function,
|
||||
but that function can only work with sets of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.intersection(Enum.into([1, 2], set_impl.new), Enum.into([2, 3, 4], set_impl.new)) |> Enum.sort
|
||||
[2]
|
||||
|
||||
iex> Set.intersection(Enum.into([1, 2], set_impl.new), Enum.into([3, 4], set_impl.new)) |> Enum.sort
|
||||
[]
|
||||
|
||||
"""
|
||||
@spec intersection(t, t) :: t
|
||||
def intersection(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -83,26 +199,79 @@ defmodule Set do
|
||||
if target1 == target2 do
|
||||
target1.intersection(set1, set2)
|
||||
else
|
||||
Enumerable.reduce(set1, {:cont, target1.new}, fn v, acc ->
|
||||
target1.reduce(set1, {:cont, target1.new}, fn v, acc ->
|
||||
{:cont, if(target2.member?(set2, v), do: target1.put(acc, v), else: acc)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set` contains `value`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.member?(Enum.into([1, 2, 3], set_impl.new), 2)
|
||||
true
|
||||
|
||||
iex> Set.member?(Enum.into([1, 2, 3], set_impl.new), 4)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec member?(t, value) :: boolean
|
||||
def member?(set, value) do
|
||||
target(set).member?(set, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts `value` into `set` if `set` doesn't already contain it.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.put(Enum.into([1, 2, 3], set_impl.new), 3) |> Enum.sort
|
||||
[1, 2, 3]
|
||||
|
||||
iex> Set.put(Enum.into([1, 2, 3], set_impl.new), 4) |> Enum.sort
|
||||
[1, 2, 3, 4]
|
||||
|
||||
"""
|
||||
@spec put(t, value) :: t
|
||||
def put(set, value) do
|
||||
target(set).put(set, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of elements in `set`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.size(Enum.into([1, 2, 3], set_impl.new))
|
||||
3
|
||||
|
||||
"""
|
||||
@spec size(t) :: non_neg_integer
|
||||
def size(set) do
|
||||
target(set).size(set)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if `set1`'s members are all contained in `set2`.
|
||||
|
||||
This function checks if `set1` is a subset of `set2`.
|
||||
|
||||
Note that this function is polymorphic as it checks the subset for
|
||||
any type. Each set implementation also provides a `subset?` function,
|
||||
but that function can only work with sets of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.subset?(Enum.into([1, 2], set_impl.new), Enum.into([1, 2, 3], set_impl.new))
|
||||
true
|
||||
|
||||
iex> Set.subset?(Enum.into([1, 2, 3], set_impl.new), Enum.into([1, 2], set_impl.new))
|
||||
false
|
||||
|
||||
"""
|
||||
@spec subset?(t, t) :: boolean
|
||||
def subset?(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -114,10 +283,34 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `set` to a list.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> set_impl.to_list(Enum.into([1, 2, 3], set_impl.new)) |> Enum.sort
|
||||
[1, 2, 3]
|
||||
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(set) do
|
||||
target(set).to_list(set)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing all members of `set1` and `set2`.
|
||||
|
||||
Note that this function is polymorphic as it calculates the union of sets of
|
||||
any type. Each set implementation also provides a `union` function,
|
||||
but that function can only work with sets of the same type.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Set.union(Enum.into([1, 2], set_impl.new), Enum.into([2, 3, 4], set_impl.new)) |> Enum.sort
|
||||
[1, 2, 3, 4]
|
||||
|
||||
"""
|
||||
@spec union(t, t) :: t
|
||||
def union(set1, set2) do
|
||||
target1 = target(set1)
|
||||
target2 = target(set2)
|
||||
@@ -125,14 +318,14 @@ defmodule Set do
|
||||
if target1 == target2 do
|
||||
target1.union(set1, set2)
|
||||
else
|
||||
Enumerable.reduce(set2, {:cont, set1}, fn v, acc ->
|
||||
target2.reduce(set2, {:cont, set1}, fn v, acc ->
|
||||
{:cont, target1.put(acc, v)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_subset?(_target1, target2, set1, set2) do
|
||||
Enumerable.reduce(set1, {:cont, true}, fn member, acc ->
|
||||
defp do_subset?(target1, target2, set1, set2) do
|
||||
target1.reduce(set1, {:cont, true}, fn member, acc ->
|
||||
case target2.member?(set2, member) do
|
||||
true -> {:cont, acc}
|
||||
_ -> {:halt, false}
|
||||
|
||||
+81
-125
@@ -22,7 +22,7 @@ defmodule Stream do
|
||||
|
||||
Notice we started with a range and then we created a stream that is
|
||||
meant to multiply each item in the range by 2. At this point, no
|
||||
computation was done. Only when `Enum.map/2` is called we actually
|
||||
computation was done yet. Just when `Enum.map/2` is called we
|
||||
enumerate over each item in the range, multiplying it by 2 and adding 1.
|
||||
We say the functions in `Stream` are *lazy* and the functions in `Enum`
|
||||
are *eager*.
|
||||
@@ -33,10 +33,10 @@ defmodule Stream do
|
||||
computations that are executed at a later moment. Let's see another
|
||||
example:
|
||||
|
||||
1..3
|
||||
|> Enum.map(&IO.inspect(&1))
|
||||
|> Enum.map(&(&1 * 2))
|
||||
|> Enum.map(&IO.inspect(&1))
|
||||
1..3 |>
|
||||
Enum.map(&IO.inspect(&1)) |>
|
||||
Enum.map(&(&1 * 2)) |>
|
||||
Enum.map(&IO.inspect(&1))
|
||||
1
|
||||
2
|
||||
3
|
||||
@@ -49,10 +49,10 @@ defmodule Stream do
|
||||
element by 2 and finally printed each new value. In this example, the list
|
||||
was enumerated three times. Let's see an example with streams:
|
||||
|
||||
stream = 1..3
|
||||
|> Stream.map(&IO.inspect(&1))
|
||||
|> Stream.map(&(&1 * 2))
|
||||
|> Stream.map(&IO.inspect(&1))
|
||||
stream = 1..3 |>
|
||||
Stream.map(&IO.inspect(&1)) |>
|
||||
Stream.map(&(&1 * 2)) |>
|
||||
Stream.map(&IO.inspect(&1))
|
||||
Enum.to_list(stream)
|
||||
1
|
||||
2
|
||||
@@ -66,10 +66,10 @@ defmodule Stream do
|
||||
printed changed! With streams, we print the first item and then print
|
||||
its double. In this example, the list was enumerated just once!
|
||||
|
||||
That's what we meant when we said earlier that streams are composable,
|
||||
That's what we meant when we first said that streams are composable,
|
||||
lazy enumerables. Notice we could call `Stream.map/2` multiple times,
|
||||
effectively composing the streams and keeping them lazy. The computations
|
||||
are only performed when you call a function from the `Enum` module.
|
||||
effectively composing the streams and they are lazy. The computations
|
||||
are performed only when you call a function from the `Enum` module.
|
||||
|
||||
## Creating Streams
|
||||
|
||||
@@ -85,7 +85,7 @@ defmodule Stream do
|
||||
Note the functions in this module are guaranteed to return enumerables.
|
||||
Since enumerables can have different shapes (structs, anonymous functions,
|
||||
and so on), the functions in this module may return any of those shapes
|
||||
and that this may change at any time. For example, a function that today
|
||||
and that it may change at any time. For example, a function that today
|
||||
returns an anonymous function may return a struct in future releases.
|
||||
"""
|
||||
|
||||
@@ -96,6 +96,7 @@ defmodule Stream do
|
||||
@type element :: any
|
||||
@type index :: non_neg_integer
|
||||
@type default :: any
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
# Require Stream.Reducers and its callbacks
|
||||
require Stream.Reducers, as: R
|
||||
@@ -109,13 +110,13 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
defmacrop acc(h, n, t) do
|
||||
quote do: [unquote(h), unquote(n) | unquote(t)]
|
||||
quote do: [unquote(h), unquote(n)|unquote(t)]
|
||||
end
|
||||
|
||||
defmacrop next_with_acc(f, entry, h, n, t) do
|
||||
quote do
|
||||
{reason, [h | t]} = unquote(f).(unquote(entry), [unquote(h) | unquote(t)])
|
||||
{reason, [h, unquote(n) | t]}
|
||||
{reason, [h|t]} = unquote(f).(unquote(entry), [unquote(h)|unquote(t)])
|
||||
{reason, [h, unquote(n)|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -134,9 +135,9 @@ defmodule Stream do
|
||||
`step` is optional and, if not passed, defaults to `n`, i.e.
|
||||
chunks do not overlap. If the final chunk does not have `n`
|
||||
elements to fill the chunk, elements are taken as necessary
|
||||
from `leftover` if it was passed. If `leftover` is passed and
|
||||
does not have enough elements to fill the chunk, then the chunk is
|
||||
returned anyway with less than `n` elements. If `leftover` is not
|
||||
from `pad` if it was passed. If `pad` is passed and does not
|
||||
have enough elements to fill the chunk, then the chunk is
|
||||
returned anyway with less than `n` elements. If `pad` is not
|
||||
passed at all or is `nil`, then the partial chunk is discarded
|
||||
from the result.
|
||||
|
||||
@@ -157,15 +158,14 @@ defmodule Stream do
|
||||
"""
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer, Enumerable.t | nil) :: Enumerable.t
|
||||
def chunk(enum, n, step, leftover \\ nil)
|
||||
when is_integer(n) and n > 0 and is_integer(step) and step > 0 do
|
||||
def chunk(enum, n, step, pad \\ nil) when n > 0 and step > 0 do
|
||||
limit = :erlang.max(n, step)
|
||||
if is_nil(leftover) do
|
||||
if is_nil(pad) do
|
||||
lazy enum, {[], 0}, fn(f1) -> R.chunk(n, step, limit, f1) end
|
||||
else
|
||||
lazy enum, {[], 0},
|
||||
fn(f1) -> R.chunk(n, step, limit, f1) end,
|
||||
&do_chunk(&1, n, leftover, &2)
|
||||
&do_chunk(&1, n, pad, &2)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -173,8 +173,8 @@ defmodule Stream do
|
||||
{:cont, acc}
|
||||
end
|
||||
|
||||
defp do_chunk(acc(h, {buffer, count} = old, t), n, leftover, f1) do
|
||||
buffer = :lists.reverse(buffer, Enum.take(leftover, n - count))
|
||||
defp do_chunk(acc(h, {buffer, count} = old, t), n, pad, f1) do
|
||||
buffer = :lists.reverse(buffer, Enum.take(pad, n - count))
|
||||
next_with_acc(f1, buffer, h, old, t)
|
||||
end
|
||||
|
||||
@@ -270,53 +270,23 @@ defmodule Stream do
|
||||
fn
|
||||
entry, [h, {count, buf1, []} | t] ->
|
||||
do_drop(:cont, n, entry, h, count, buf1, [], t)
|
||||
entry, [h, {count, buf1, [next | buf2]} | t] ->
|
||||
{reason, [h | t]} = f1.(next, [h | t])
|
||||
entry, [h, {count, buf1, [next|buf2]} | t] ->
|
||||
{reason, [h|t]} = f1.(next, [h|t])
|
||||
do_drop(reason, n, entry, h, count, buf1, buf2, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp do_drop(reason, n, entry, h, count, buf1, buf2, t) do
|
||||
buf1 = [entry | buf1]
|
||||
buf1 = [entry|buf1]
|
||||
count = count + 1
|
||||
if count == n do
|
||||
{reason, [h, {0, [], :lists.reverse(buf1)} | t]}
|
||||
{reason, [h, {0, [], :lists.reverse(buf1)}|t]}
|
||||
else
|
||||
{reason, [h, {count, buf1, buf2} | t]}
|
||||
{reason, [h, {count, buf1, buf2}|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that drops every `nth` item from the enumerable.
|
||||
|
||||
The first item is always dropped, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer, or `FunctionClauseError` will be thrown.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.drop_every(1..10, 2)
|
||||
iex> Enum.to_list(stream)
|
||||
[2, 4, 6, 8, 10]
|
||||
|
||||
iex> stream = Stream.drop_every(1..1000, 1)
|
||||
iex> Enum.to_list(stream)
|
||||
[]
|
||||
|
||||
iex> stream = Stream.drop_every([1, 2, 3, 4, 5], 0)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
"""
|
||||
@spec drop_every(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def drop_every(enum, 0), do: %Stream{enum: enum}
|
||||
def drop_every([], _nth), do: %Stream{enum: []}
|
||||
|
||||
def drop_every(enum, nth) when is_integer(nth) and nth > 0 do
|
||||
lazy enum, nth, fn(f1) -> R.drop_every(nth, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily drops elements of the enumerable while the given
|
||||
function returns `true`.
|
||||
@@ -362,7 +332,7 @@ defmodule Stream do
|
||||
|
||||
@doc """
|
||||
Creates a stream that will apply the given function on enumeration and
|
||||
flatten the result, but only one level deep.
|
||||
flatten the result.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -370,10 +340,6 @@ defmodule Stream do
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 2, 4, 3, 6]
|
||||
|
||||
iex> stream = Stream.flat_map([1, 2, 3], fn(x) -> [[x]] end)
|
||||
iex> Enum.to_list(stream)
|
||||
[[1], [2], [3]]
|
||||
|
||||
"""
|
||||
@spec flat_map(Enumerable.t, (element -> Enumerable.t)) :: Enumerable.t
|
||||
def flat_map(enum, mapper) do
|
||||
@@ -415,12 +381,9 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that emits a value after the given period `n`
|
||||
in milliseconds.
|
||||
Creates a stream that emits a value after the given period `n` in milliseconds.
|
||||
|
||||
The values emitted are an increasing counter starting at `0`.
|
||||
This operation will block the caller by the given interval
|
||||
every time a new item is streamed.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -428,9 +391,10 @@ defmodule Stream do
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
|
||||
"""
|
||||
# TODO: Allow it to handle system messages.
|
||||
@spec interval(non_neg_integer) :: Enumerable.t
|
||||
def interval(n) do
|
||||
unfold 0, fn(count) ->
|
||||
unfold 0, fn (count) ->
|
||||
:timer.sleep(n)
|
||||
{count, count + 1}
|
||||
end
|
||||
@@ -449,26 +413,26 @@ defmodule Stream do
|
||||
|
||||
defp do_into(enum, collectable, transform, acc, fun) do
|
||||
{initial, into} = Collectable.into(collectable)
|
||||
composed = fn x, [acc | collectable] ->
|
||||
composed = fn x, [acc|collectable] ->
|
||||
collectable = into.(collectable, {:cont, transform.(x)})
|
||||
{reason, acc} = fun.(x, acc)
|
||||
{reason, [acc | collectable]}
|
||||
{reason, [acc|collectable]}
|
||||
end
|
||||
do_into(&Enumerable.reduce(enum, &1, composed), initial, into, acc)
|
||||
end
|
||||
|
||||
defp do_into(reduce, collectable, into, {command, acc}) do
|
||||
try do
|
||||
reduce.({command, [acc | collectable]})
|
||||
reduce.({command, [acc|collectable]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
into.(collectable, :halt)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:suspended, [acc | collectable], continuation} ->
|
||||
{:suspended, [acc|collectable], continuation} ->
|
||||
{:suspended, acc, &do_into(continuation, collectable, into, &1)}
|
||||
{reason, [acc | collectable]} ->
|
||||
{reason, [acc|collectable]} ->
|
||||
into.(collectable, :done)
|
||||
{reason, acc}
|
||||
end
|
||||
@@ -651,8 +615,7 @@ defmodule Stream do
|
||||
@doc """
|
||||
Creates a stream that emits a single value after `n` milliseconds.
|
||||
|
||||
The value emitted is `0`. This operation will block the caller by
|
||||
the given time until the item is streamed.
|
||||
The value emitted is `0`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -735,7 +698,7 @@ defmodule Stream do
|
||||
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, after_fun) do
|
||||
case next.({:cont, next_acc}) do
|
||||
{:suspended, [val | next_acc], next} ->
|
||||
{:suspended, [val|next_acc], next} ->
|
||||
try do
|
||||
user.(val, user_acc)
|
||||
catch
|
||||
@@ -787,7 +750,7 @@ defmodule Stream do
|
||||
|
||||
defp do_enum_transform(user_acc, user, fun, next_acc, next, {op, inner_acc}, inner, reduce, after_fun) do
|
||||
try do
|
||||
reduce.({op, [:outer | inner_acc]})
|
||||
reduce.({op, [:outer|inner_acc]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
@@ -797,15 +760,15 @@ defmodule Stream do
|
||||
else
|
||||
# Only take into account outer halts when the op is not halt itself.
|
||||
# Otherwise, we were the ones wishing to halt, so we should just stop.
|
||||
{:halted, [:outer | acc]} when op != :halt ->
|
||||
{:halted, [:outer|acc]} when op != :halt ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:halted, [_ | acc]} ->
|
||||
{:halted, [_|acc]} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, acc}
|
||||
{:done, [_ | acc]} ->
|
||||
{:done, [_|acc]} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:suspended, [_ | acc], c} ->
|
||||
{:suspended, [_|acc], c} ->
|
||||
{:suspended, acc, &do_enum_transform(user_acc, user, fun, next_acc, next, &1, inner, c, after_fun)}
|
||||
end
|
||||
end
|
||||
@@ -813,15 +776,15 @@ defmodule Stream do
|
||||
defp do_after(nil, _user_acc), do: :ok
|
||||
defp do_after(fun, user_acc), do: fun.(user_acc)
|
||||
|
||||
defp do_transform_each(x, [:outer | acc], f) do
|
||||
defp do_transform_each(x, [:outer|acc], f) do
|
||||
case f.(x, acc) do
|
||||
{:halt, res} -> {:halt, [:inner | res]}
|
||||
{op, res} -> {op, [:outer | res]}
|
||||
{:halt, res} -> {:halt, [:inner|res]}
|
||||
{op, res} -> {op, [:outer|res]}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_transform_step(x, acc) do
|
||||
{:suspend, [x | acc]}
|
||||
{:suspend, [x|acc]}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -844,7 +807,7 @@ defmodule Stream do
|
||||
@spec uniq(Enumerable.t) :: Enumerable.t
|
||||
@spec uniq(Enumerable.t, (element -> term)) :: Enumerable.t
|
||||
def uniq(enum, fun \\ fn x -> x end) do
|
||||
lazy enum, %{}, fn f1 -> R.uniq(fun, f1) end
|
||||
lazy enum, HashSet.new, fn f1 -> R.uniq(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -857,15 +820,10 @@ defmodule Stream do
|
||||
iex> Enum.to_list(stream)
|
||||
[{1, 0}, {2, 1}, {3, 2}]
|
||||
|
||||
iex> stream = Stream.with_index([1, 2, 3], 3)
|
||||
iex> Enum.to_list(stream)
|
||||
[{1, 3}, {2, 4}, {3, 5}]
|
||||
|
||||
"""
|
||||
@spec with_index(Enumerable.t) :: Enumerable.t
|
||||
@spec with_index(Enumerable.t, integer) :: Enumerable.t
|
||||
def with_index(enum, offset \\ 0) do
|
||||
lazy enum, offset, fn(f1) -> R.with_index(f1) end
|
||||
def with_index(enum) do
|
||||
lazy enum, 0, fn(f1) -> R.with_index(f1) end
|
||||
end
|
||||
|
||||
## Combiners
|
||||
@@ -954,10 +912,10 @@ defmodule Stream do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_zip([{fun, fun_acc} | t], acc, callback, list, buffer) do
|
||||
defp do_zip([{fun, fun_acc}|t], acc, callback, list, buffer) do
|
||||
case fun.({:cont, fun_acc}) do
|
||||
{:suspended, [i | fun_acc], fun} ->
|
||||
do_zip(t, acc, callback, [i | list], [{fun, fun_acc} | buffer])
|
||||
{:suspended, [i|fun_acc], fun} ->
|
||||
do_zip(t, acc, callback, [i|list], [{fun, fun_acc}|buffer])
|
||||
{_, _} ->
|
||||
do_zip_close(:lists.reverse(buffer, t))
|
||||
{:done, acc}
|
||||
@@ -970,13 +928,13 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
defp do_zip_close([]), do: :ok
|
||||
defp do_zip_close([{fun, acc} | t]) do
|
||||
defp do_zip_close([{fun, acc}|t]) do
|
||||
fun.({:halt, acc})
|
||||
do_zip_close(t)
|
||||
end
|
||||
|
||||
defp do_zip_step(x, acc) do
|
||||
{:suspend, [x | acc]}
|
||||
{:suspend, [x|acc]}
|
||||
end
|
||||
|
||||
## Sources
|
||||
@@ -1065,10 +1023,8 @@ defmodule Stream do
|
||||
|
||||
## Examples
|
||||
|
||||
# Although not necessary, let's seed the random algorithm
|
||||
iex> :rand.seed(:exsplus, {1, 2, 3})
|
||||
iex> Stream.repeatedly(&:rand.uniform/0) |> Enum.take(3)
|
||||
[0.40502929729990744, 0.45336720247823126, 0.04094511692041057]
|
||||
iex> Stream.repeatedly(&:random.uniform/0) |> Enum.take(3)
|
||||
[0.4435846174457203, 0.7230402056221108, 0.94581636451987]
|
||||
|
||||
"""
|
||||
@spec repeatedly((() -> element)) :: Enumerable.t
|
||||
@@ -1091,7 +1047,7 @@ defmodule Stream do
|
||||
@doc """
|
||||
Emits a sequence of values for the given resource.
|
||||
|
||||
Similar to `transform/3` but the initial accumulated value is
|
||||
Similar to `transform/2` but the initial accumulated value is
|
||||
computed lazily via `start_fun` and executes an `after_fun` at
|
||||
the end of enumeration (both in cases of success and failure).
|
||||
|
||||
@@ -1116,7 +1072,7 @@ defmodule Stream do
|
||||
fn file -> File.close(file) end)
|
||||
|
||||
"""
|
||||
@spec resource((() -> acc), (acc -> {[element], acc} | {:halt, acc}), (acc -> term)) :: Enumerable.t
|
||||
@spec resource((() -> acc), (acc -> {element, acc} | nil), (acc -> term)) :: Enumerable.t
|
||||
def resource(start_fun, next_fun, after_fun) do
|
||||
&do_resource(start_fun.(), next_fun, &1, &2, after_fun)
|
||||
end
|
||||
@@ -1178,28 +1134,28 @@ defmodule Stream do
|
||||
|
||||
defp do_enum_resource(next_acc, next_fun, {op, acc}, fun, after_fun, reduce) do
|
||||
try do
|
||||
reduce.({op, [:outer | acc]})
|
||||
reduce.({op, [:outer|acc]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
after_fun.(next_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:halted, [:outer | acc]} ->
|
||||
{:halted, [:outer|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
|
||||
{:halted, [:inner | acc]} ->
|
||||
{:halted, [:inner|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:halt, acc}, fun, after_fun)
|
||||
{:done, [_ | acc]} ->
|
||||
{:done, [_|acc]} ->
|
||||
do_resource(next_acc, next_fun, {:cont, acc}, fun, after_fun)
|
||||
{:suspended, [_ | acc], c} ->
|
||||
{:suspended, [_|acc], c} ->
|
||||
{:suspended, acc, &do_enum_resource(next_acc, next_fun, &1, fun, after_fun, c)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_resource_each(x, [:outer | acc], f) do
|
||||
defp do_resource_each(x, [:outer|acc], f) do
|
||||
case f.(x, acc) do
|
||||
{:halt, res} -> {:halt, [:inner | res]}
|
||||
{op, res} -> {op, [:outer | res]}
|
||||
{:halt, res} -> {:halt, [:inner|res]}
|
||||
{op, res} -> {op, [:outer|res]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1241,17 +1197,17 @@ defmodule Stream do
|
||||
@compile {:inline, lazy: 2, lazy: 3, lazy: 4}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs} = lazy, fun),
|
||||
do: %{lazy | funs: [fun | funs] }
|
||||
do: %{lazy | funs: [fun|funs] }
|
||||
defp lazy(enum, fun),
|
||||
do: %Stream{enum: enum, funs: [fun]}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs, accs: accs} = lazy, acc, fun),
|
||||
do: %{lazy | funs: [fun | funs], accs: [acc | accs] }
|
||||
do: %{lazy | funs: [fun|funs], accs: [acc|accs] }
|
||||
defp lazy(enum, acc, fun),
|
||||
do: %Stream{enum: enum, funs: [fun], accs: [acc]}
|
||||
|
||||
defp lazy(%Stream{done: nil, funs: funs, accs: accs} = lazy, acc, fun, done),
|
||||
do: %{lazy | funs: [fun | funs], accs: [acc | accs], done: done}
|
||||
do: %{lazy | funs: [fun|funs], accs: [acc|accs], done: done}
|
||||
defp lazy(enum, acc, fun, done),
|
||||
do: %Stream{enum: enum, funs: [fun], accs: [acc], done: done}
|
||||
end
|
||||
@@ -1281,8 +1237,8 @@ defimpl Enumerable, for: Stream do
|
||||
end
|
||||
|
||||
defp do_each(reduce, done, accs, {command, acc}) do
|
||||
case reduce.({command, [acc | accs]}) do
|
||||
{:suspended, [acc | accs], continuation} ->
|
||||
case reduce.({command, [acc|accs]}) do
|
||||
{:suspended, [acc|accs], continuation} ->
|
||||
{:suspended, acc, &do_each(continuation, done, accs, &1)}
|
||||
{:halted, accs} ->
|
||||
do_done {:halted, accs}, done
|
||||
@@ -1291,13 +1247,13 @@ defimpl Enumerable, for: Stream do
|
||||
end
|
||||
end
|
||||
|
||||
defp do_done({reason, [acc | _]}, nil), do: {reason, acc}
|
||||
defp do_done({reason, [acc | t]}, {done, fun}) do
|
||||
[h | _] = Enum.reverse(t)
|
||||
defp do_done({reason, [acc|_]}, nil), do: {reason, acc}
|
||||
defp do_done({reason, [acc|t]}, {done, fun}) do
|
||||
[h|_] = Enum.reverse(t)
|
||||
case done.([acc, h], fun) do
|
||||
{:cont, [acc | _]} -> {reason, acc}
|
||||
{:halt, [acc | _]} -> {:halted, acc}
|
||||
{:suspend, [acc | _]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
||||
{:cont, [acc|_]} -> {reason, acc}
|
||||
{:halt, [acc|_]} -> {:halted, acc}
|
||||
{:suspend, [acc|_]} -> {:suspended, acc, &({:done, elem(&1, 1)})}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -5,7 +5,7 @@ defmodule Stream.Reducers do
|
||||
defmacro chunk(n, step, limit, f \\ nil) do
|
||||
quote do
|
||||
fn entry, acc(h, {buffer, count}, t) ->
|
||||
buffer = [entry | buffer]
|
||||
buffer = [entry|buffer]
|
||||
count = count + 1
|
||||
|
||||
new =
|
||||
@@ -31,7 +31,7 @@ defmodule Stream.Reducers do
|
||||
entry, acc(h, {buffer, value}, t) ->
|
||||
new_value = unquote(callback).(entry)
|
||||
if new_value == value do
|
||||
skip(acc(h, {[entry | buffer], value}, t))
|
||||
skip(acc(h, {[entry|buffer], value}, t))
|
||||
else
|
||||
next_with_acc(unquote(f), :lists.reverse(buffer), h, {[entry], new_value}, t)
|
||||
end
|
||||
@@ -64,18 +64,6 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
end
|
||||
|
||||
defmacro drop_every(nth, f \\ nil) do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(h, n, t) when n === :first
|
||||
when n === unquote(nth) ->
|
||||
skip(acc(h, 1, t))
|
||||
entry, acc(h, n, t) ->
|
||||
next_with_acc(unquote(f), entry, h, n+1, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro drop_while(callback, f \\ nil) do
|
||||
quote do
|
||||
fn entry, acc(h, bool, t) = orig ->
|
||||
@@ -199,10 +187,10 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc(h, prev, t) = acc) ->
|
||||
value = unquote(callback).(entry)
|
||||
if Map.has_key?(prev, value) do
|
||||
if HashSet.member?(prev, value) do
|
||||
skip(acc)
|
||||
else
|
||||
next_with_acc(unquote(f), entry, h, Map.put(prev, value, true), t)
|
||||
next_with_acc(unquote(f), entry, h, HashSet.put(prev, value), t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+280
-715
File diff suppressed because it is too large
Load Diff
@@ -2,19 +2,19 @@ import Kernel, except: [to_string: 1]
|
||||
|
||||
defprotocol String.Chars do
|
||||
@moduledoc ~S"""
|
||||
The `String.Chars` protocol is responsible for
|
||||
The String.Chars protocol is responsible for
|
||||
converting a structure to a Binary (only if applicable).
|
||||
The only function required to be implemented is
|
||||
`to_string` which does the conversion.
|
||||
|
||||
The `to_string` function automatically imported
|
||||
by Kernel invokes this protocol. String
|
||||
interpolation also invokes `to_string` in its
|
||||
interpolation also invokes to_string in its
|
||||
arguments. For example, `"foo#{bar}"` is the same
|
||||
as `"foo" <> to_string(bar)`.
|
||||
"""
|
||||
|
||||
def to_string(term)
|
||||
def to_string(thing)
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Atom do
|
||||
@@ -28,30 +28,30 @@ defimpl String.Chars, for: Atom do
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: BitString do
|
||||
def to_string(term) when is_binary(term) do
|
||||
term
|
||||
def to_string(thing) when is_binary(thing) do
|
||||
thing
|
||||
end
|
||||
|
||||
def to_string(term) do
|
||||
def to_string(thing) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: term,
|
||||
value: thing,
|
||||
description: "cannot convert a bitstring to a string"
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: List do
|
||||
def to_string(charlist), do: List.to_string(charlist)
|
||||
def to_string(char_list), do: List.to_string(char_list)
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Integer do
|
||||
def to_string(term) do
|
||||
Integer.to_string(term)
|
||||
def to_string(thing) do
|
||||
Integer.to_string(thing)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: Float do
|
||||
def to_string(term) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(term))
|
||||
def to_string(thing) do
|
||||
IO.iodata_to_binary(:io_lib_format.fwrite_g(thing))
|
||||
end
|
||||
end
|
||||
|
||||
+37
-63
@@ -1,9 +1,6 @@
|
||||
defmodule StringIO do
|
||||
@moduledoc """
|
||||
Controls an IO device process that wraps a string.
|
||||
|
||||
A `StringIO` IO device can be passed as a "device" to
|
||||
most of the functions in the `IO` module.
|
||||
This module provides an IO device that wraps a string.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -18,9 +15,6 @@ defmodule StringIO do
|
||||
@doc """
|
||||
Creates an IO device.
|
||||
|
||||
`string` will be the initial input of the newly created
|
||||
device.
|
||||
|
||||
If the `:capture_prompt` option is set to `true`,
|
||||
prompts (specified as arguments to `IO.get*` functions)
|
||||
are captured.
|
||||
@@ -46,8 +40,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current input/output buffers for the given IO
|
||||
device.
|
||||
Returns current buffers.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -63,7 +56,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Flushes the output buffer and returns its current contents.
|
||||
Flushes output buffer.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -81,8 +74,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the IO device and returns the remaining input/output
|
||||
buffers.
|
||||
Stops the IO device and returns remaining buffers.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -135,20 +127,21 @@ defmodule StringIO do
|
||||
s
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, chars} = req, s) do
|
||||
put_chars(:latin1, chars, req, s)
|
||||
defp io_request({:put_chars, chars}, %{output: output} = s) do
|
||||
{:ok, %{s | output: << output :: binary, IO.chardata_to_string(chars) :: binary >>}}
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, m, f, as} = req, s) do
|
||||
put_chars(:latin1, apply(m, f, as), req, s)
|
||||
defp io_request({:put_chars, m, f, as}, %{output: output} = s) do
|
||||
chars = apply(m, f, as)
|
||||
{:ok, %{s | output: << output :: binary, IO.chardata_to_string(chars) :: binary >>}}
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, encoding, chars} = req, s) do
|
||||
put_chars(encoding, chars, req, s)
|
||||
defp io_request({:put_chars, _encoding, chars}, s) do
|
||||
io_request({:put_chars, chars}, s)
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, encoding, mod, func, args} = req, s) do
|
||||
put_chars(encoding, apply(mod, func, args), req, s)
|
||||
defp io_request({:put_chars, _encoding, mod, func, args}, s) do
|
||||
io_request({:put_chars, mod, func, args}, s)
|
||||
end
|
||||
|
||||
defp io_request({:get_chars, prompt, n}, s) when n >= 0 do
|
||||
@@ -203,17 +196,6 @@ defmodule StringIO do
|
||||
{{:error, :request}, s}
|
||||
end
|
||||
|
||||
## put_chars
|
||||
|
||||
defp put_chars(encoding, chars, req, %{output: output} = s) do
|
||||
case :unicode.characters_to_binary(chars, encoding, :unicode) do
|
||||
string when is_binary(string) ->
|
||||
{:ok, %{s | output: output <> string}}
|
||||
{_, _, _} ->
|
||||
{{:error, req}, s}
|
||||
end
|
||||
end
|
||||
|
||||
## get_chars
|
||||
|
||||
defp get_chars(encoding, prompt, n,
|
||||
@@ -222,14 +204,11 @@ defmodule StringIO do
|
||||
{:error, _} = error ->
|
||||
{error, s}
|
||||
{result, input} ->
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, IO.chardata_to_string(prompt)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
if capture_prompt do
|
||||
output = << output :: binary, IO.chardata_to_string(prompt) :: binary >>
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -242,7 +221,7 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
defp do_get_chars(input, :latin1, n) do
|
||||
<<chars::binary-size(n), rest::binary>> = input
|
||||
<<chars :: binary-size(n), rest :: binary>> = input
|
||||
{chars, rest}
|
||||
end
|
||||
|
||||
@@ -252,7 +231,7 @@ defmodule StringIO do
|
||||
{buf_count, split_pos} when buf_count < n or split_pos == :none ->
|
||||
{input, ""}
|
||||
{_buf_count, split_pos} ->
|
||||
<<chars::binary-size(split_pos), rest::binary>> = input
|
||||
<<chars :: binary-size(split_pos), rest :: binary>> = input
|
||||
{chars, rest}
|
||||
end
|
||||
catch
|
||||
@@ -273,14 +252,11 @@ defmodule StringIO do
|
||||
chars ->
|
||||
{result, input} = do_get_line(chars, encoding)
|
||||
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, IO.chardata_to_string(prompt)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
if capture_prompt do
|
||||
output = << output :: binary, IO.chardata_to_string(prompt) :: binary >>
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -306,20 +282,17 @@ defmodule StringIO do
|
||||
chars ->
|
||||
{result, input, count} = do_get_until(chars, encoding, mod, fun, args)
|
||||
|
||||
if capture_prompt do
|
||||
output = << output :: binary, :binary.copy(IO.chardata_to_string(prompt), count) :: binary >>
|
||||
end
|
||||
|
||||
input =
|
||||
case input do
|
||||
:eof -> ""
|
||||
_ -> :unicode.characters_to_binary(input, encoding)
|
||||
end
|
||||
|
||||
s =
|
||||
if capture_prompt do
|
||||
%{s | output: <<output::binary, :binary.copy(IO.chardata_to_string(prompt), count)::binary>>}
|
||||
else
|
||||
s
|
||||
end
|
||||
|
||||
{result, %{s | input: input}}
|
||||
{result, %{s | input: input, output: output}}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -338,10 +311,11 @@ defmodule StringIO do
|
||||
{line, rest} = collect_line(chars)
|
||||
|
||||
case apply(mod, fun, [continuation, line | args]) do
|
||||
{:done, result, :eof} ->
|
||||
{:done, result, rest1} ->
|
||||
unless rest1 == :eof do
|
||||
rest = rest1 ++ rest
|
||||
end
|
||||
{result, rest, count + 1}
|
||||
{:done, result, extra} ->
|
||||
{result, extra ++ rest, count + 1}
|
||||
{:more, next_continuation} ->
|
||||
do_get_until(rest, encoding, mod, fun, args, next_continuation, count + 1)
|
||||
end
|
||||
@@ -349,7 +323,7 @@ defmodule StringIO do
|
||||
|
||||
## io_requests
|
||||
|
||||
defp io_requests([r | rs], {:ok, s}) do
|
||||
defp io_requests([r|rs], {:ok, s}) do
|
||||
io_requests(rs, io_request(r, s))
|
||||
end
|
||||
|
||||
@@ -368,15 +342,15 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
defp collect_line([?\r, ?\n | rest], stack) do
|
||||
{:lists.reverse([?\n | stack]), rest}
|
||||
{:lists.reverse([?\n|stack]), rest}
|
||||
end
|
||||
|
||||
defp collect_line([?\n | rest], stack) do
|
||||
{:lists.reverse([?\n | stack]), rest}
|
||||
{:lists.reverse([?\n|stack]), rest}
|
||||
end
|
||||
|
||||
defp collect_line([h | t], stack) do
|
||||
collect_line(t, [h | stack])
|
||||
defp collect_line([h|t], stack) do
|
||||
collect_line(t, [h|stack])
|
||||
end
|
||||
|
||||
defp io_reply(from, reply_as, reply) do
|
||||
|
||||
+131
-161
@@ -1,17 +1,17 @@
|
||||
defmodule Supervisor do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
A behaviour module for implementing supervision functionality.
|
||||
|
||||
A supervisor is a process which supervises other processes, which we refer
|
||||
to as *child processes*. Supervisors are used to build a hierarchical process
|
||||
structure called a *supervision tree*. Supervision trees are a nice way to
|
||||
structure fault-tolerant applications.
|
||||
A supervisor is a process which supervises other processes called
|
||||
child processes. Supervisors are used to build a hierarchical process
|
||||
structure called a supervision tree, a nice way to structure fault-tolerant
|
||||
applications.
|
||||
|
||||
A supervisor implemented using this module has a standard set
|
||||
of interface functions and includes functionality for tracing and error
|
||||
reporting. It also fits into a supervision tree.
|
||||
A supervisor implemented using this module will have a standard set
|
||||
of interface functions and include functionality for tracing and error
|
||||
reporting. It will also fit into a supervision tree.
|
||||
|
||||
## Examples
|
||||
## Example
|
||||
|
||||
In order to define a supervisor, we need to first define a child process
|
||||
that is going to be supervised. In order to do so, we will define a GenServer
|
||||
@@ -24,12 +24,12 @@ defmodule Supervisor do
|
||||
GenServer.start_link(__MODULE__, state, opts)
|
||||
end
|
||||
|
||||
def handle_call(:pop, _from, [h | t]) do
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
{:reply, h, t}
|
||||
end
|
||||
|
||||
def handle_cast({:push, h}, t) do
|
||||
{:noreply, [h | t]}
|
||||
{:noreply, [h|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -38,56 +38,56 @@ defmodule Supervisor do
|
||||
# Import helpers for defining supervisors
|
||||
import Supervisor.Spec
|
||||
|
||||
# Supervise the Stack server which will be started with
|
||||
# a single argument [:hello] and the default registered
|
||||
# name of MyStack.
|
||||
# We are going to supervise the Stack server which
|
||||
# will be started with a single argument [:hello]
|
||||
# and the default name of :sup_stack.
|
||||
children = [
|
||||
worker(Stack, [[:hello], [name: MyStack]])
|
||||
worker(Stack, [[:hello], [name: :sup_stack]])
|
||||
]
|
||||
|
||||
# Start the supervisor with our child
|
||||
# Start the supervisor with our one child
|
||||
{:ok, pid} = Supervisor.start_link(children, strategy: :one_for_one)
|
||||
|
||||
Notice that when starting the GenServer, we are registering it
|
||||
with name `MyStack`, which allows us to call it directly and
|
||||
with name `:sup_stack`, which allows us to call it directly and
|
||||
get what is on the stack:
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
#=> :hello
|
||||
|
||||
GenServer.cast(MyStack, {:push, :world})
|
||||
GenServer.cast(:sup_stack, {:push, :world})
|
||||
#=> :ok
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
#=> :world
|
||||
|
||||
However, there is a bug in our stack server. If we call `:pop` and
|
||||
the stack is empty, it is going to crash because no clause matches:
|
||||
the stack is empty, it is going to crash because no clause matches.
|
||||
Let's try it:
|
||||
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
** (exit) exited in: GenServer.call(MyStack, :pop, 5000)
|
||||
** (exit) exited in: GenServer.call(:sup_stack, :pop, 5000)
|
||||
|
||||
Luckily, since the server is being supervised by a supervisor, the
|
||||
supervisor will automatically start a new one, with the initial stack
|
||||
of `[:hello]`:
|
||||
supervisor will automatically start a new one, with the default stack
|
||||
of `[:hello]` like before:
|
||||
|
||||
GenServer.call(MyStack, :pop)
|
||||
#=> :hello
|
||||
GenServer.call(:sup_stack, :pop) == :hello
|
||||
|
||||
Supervisors support different strategies; in the example above, we
|
||||
have chosen `:one_for_one`. Furthermore, each supervisor can have many
|
||||
workers and supervisors as children, each of them with their specific
|
||||
configuration, shutdown values, and restart strategies.
|
||||
|
||||
The rest of this documentation will cover supervision strategies; also read
|
||||
the documentation for the `Supervisor.Spec` module to learn about the
|
||||
specification for workers and supervisors.
|
||||
Continue reading this moduledoc to learn more about supervision strategies
|
||||
and then follow to the `Supervisor.Spec` module documentation to learn
|
||||
about the specification for workers and supervisors.
|
||||
|
||||
## Module-based supervisors
|
||||
|
||||
In the example above, a supervisor was started by passing the supervision
|
||||
structure to `start_link/2`. However, supervisors can also be created by
|
||||
explicitly defining a supervision module:
|
||||
In the example above, a supervisor was dynamically created by passing
|
||||
the supervision structure to `start_link/2`. However, supervisors
|
||||
can also be created by explicitly defining a supervision module:
|
||||
|
||||
defmodule MyApp.Supervisor do
|
||||
use Supervisor
|
||||
@@ -101,28 +101,24 @@ defmodule Supervisor do
|
||||
worker(Stack, [[:hello]])
|
||||
]
|
||||
|
||||
# supervise/2 is imported from Supervisor.Spec
|
||||
supervise(children, strategy: :one_for_one)
|
||||
end
|
||||
end
|
||||
|
||||
You may want to use a module-based supervisor if:
|
||||
|
||||
* You need to perform some particular action on supervisor
|
||||
* You need to do some particular action on supervisor
|
||||
initialization, like setting up an ETS table.
|
||||
|
||||
* You want to perform partial hot-code swapping of the
|
||||
tree. For example, if you add or remove children,
|
||||
the module-based supervision will add and remove the
|
||||
new children directly, while dynamic supervision
|
||||
new children directly, while the dynamic supervision
|
||||
requires the whole tree to be restarted in order to
|
||||
perform such swaps.
|
||||
|
||||
## Strategies
|
||||
|
||||
Supervisors support different supervision strategies (through the `:strategy`
|
||||
option, as seen above):
|
||||
|
||||
* `:one_for_one` - if a child process terminates, only that
|
||||
process is restarted.
|
||||
|
||||
@@ -131,7 +127,7 @@ defmodule Supervisor do
|
||||
the terminated one) are restarted.
|
||||
|
||||
* `:rest_for_one` - if a child process terminates, the "rest" of
|
||||
the child processes, i.e., the child processes after the terminated
|
||||
the child processes, i.e. the child processes after the terminated
|
||||
one in start order, are terminated. Then the terminated child
|
||||
process and the rest of the child processes are restarted.
|
||||
|
||||
@@ -143,10 +139,10 @@ defmodule Supervisor do
|
||||
|
||||
## Simple one for one
|
||||
|
||||
The `:simple_one_for_one` supervisor is useful when you want to dynamically
|
||||
start and stop supervised children. For example, imagine you want to
|
||||
dynamically create multiple stacks. We can do so by defining a `:simple_one_for_one`
|
||||
supervisor:
|
||||
The simple one for one supervisor is useful when you want to dynamically
|
||||
start and stop supervisor children. For example, imagine you want to
|
||||
dynamically create multiple stacks. We can do so by defining a simple one
|
||||
for one supervisor:
|
||||
|
||||
# Import helpers for defining supervisors
|
||||
import Supervisor.Spec
|
||||
@@ -162,16 +158,15 @@ defmodule Supervisor do
|
||||
|
||||
There are a couple differences here:
|
||||
|
||||
* the simple one for one specification can define only one child which
|
||||
* The simple one for one specification can define only one child which
|
||||
works as a template for when we call `start_child/2`
|
||||
|
||||
* we have defined the child to have a restart strategy of `:transient`. This
|
||||
means that, if the child process exits due to a `:normal`, `:shutdown`,
|
||||
* We have define the child to have restart strategy of transient. This
|
||||
means that, if the child process exits due to a `:normal`, `:shutdown`
|
||||
or `{:shutdown, term}` reason, it won't be restarted. This is useful
|
||||
as it allows our workers to politely shutdown and be removed from the
|
||||
`:simple_one_for_one` supervisor, without being restarted. You can find
|
||||
more information about restart strategies in the documentation for the
|
||||
`Supervisor.Spec` module
|
||||
simple one for one supervisor, without being restarted. You can find
|
||||
more information about restart strategies on `Supervisor.Spec`
|
||||
|
||||
With the supervisor defined, let's dynamically start stacks:
|
||||
|
||||
@@ -188,48 +183,38 @@ defmodule Supervisor do
|
||||
|
||||
## Exit reasons
|
||||
|
||||
From the example above, you may have noticed that the `:transient` restart
|
||||
strategy for the worker does not restart the child in case it exits with
|
||||
From the example above, you may have noticed that the transient restart
|
||||
strategy for the worker does not restart the child in case it crashes with
|
||||
reason `:normal`, `:shutdown` or `{:shutdown, term}`.
|
||||
|
||||
So one may ask: which exit reason should I choose when exiting my worker?
|
||||
So one may ask: which exit reason should I choose when existing my worker?
|
||||
There are three options:
|
||||
|
||||
* `:normal` - in such cases, the exit won't be logged, there is no restart
|
||||
in transient mode, and linked processes do not exit
|
||||
* `:normal` - on such cases, the exit won't be logged, there is no restart
|
||||
on transient mode and linked processes do not exit
|
||||
|
||||
* `:shutdown` or `{:shutdown, term}` - in such cases, the exit won't be
|
||||
logged, there is no restart in transient mode, and linked processes exit
|
||||
with the same reason unless they're trapping exits
|
||||
* `:shutdown` or `{:shutdown, term}` - on such cases, the exit won't be
|
||||
logged, there is no restart on transient mode and linked processes exit
|
||||
with the same reason unless trapping exits
|
||||
|
||||
* any other term - in such cases, the exit will be logged, there are
|
||||
restarts in transient mode, and linked processes exit with the same reason
|
||||
unless they're trapping exits
|
||||
* any other term - on such cases, the exit will be logged, there are
|
||||
restarts on transient mode and linked processes exit with the same reason
|
||||
unless trapping exits
|
||||
|
||||
## Name registration
|
||||
## Name Registration
|
||||
|
||||
A supervisor is bound to the same name registration rules as a `GenServer`.
|
||||
Read more about these rules in the documentation for `GenServer`.
|
||||
|
||||
Read more about it in the `GenServer` docs.
|
||||
"""
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@behaviour Supervisor
|
||||
@behaviour :supervisor
|
||||
import Supervisor.Spec
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Callback invoked to start the supervisor and during hot code upgrades.
|
||||
"""
|
||||
# TODO: Support {:ok, [child_spec], Keyword.t}
|
||||
# TODO: Document options here and update Supervisor.Spec
|
||||
@callback init(args :: term) ::
|
||||
{:ok, {:supervisor.sup_flags, [Supervisor.Spec.spec]}} |
|
||||
:ignore
|
||||
|
||||
@typedoc "Return values of `start_link` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore |
|
||||
{:error, {:already_started, pid} | {:shutdown, term} | term}
|
||||
@@ -255,29 +240,30 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Starts a supervisor with the given children.
|
||||
|
||||
A strategy is required to be provided through the `:strategy` option.
|
||||
Furthermore, the `:max_restarts` and `:max_seconds` options can be
|
||||
configured as described in the documentation for `Supervisor.Spec.supervise/2`.
|
||||
A strategy is required to be given as an option. Furthermore,
|
||||
the `:max_restarts` and `:max_seconds` value can be configured
|
||||
as described in `Supervisor.Spec.supervise/2` docs.
|
||||
|
||||
The options can also be used to register a supervisor name.
|
||||
The supported values are described under the "Name registration"
|
||||
The supported values are described under the `Name Registration`
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
If the supervisor and its child processes are successfully created
|
||||
(i.e., if the start function of each child process returns `{:ok, child}`,
|
||||
`{:ok, child, info}`, or `:ignore`) this function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the supervisor. If a process with the
|
||||
specified name already exists, the function returns `{:error,
|
||||
{:already_started, pid}}`, where `pid` is the pid of that process.
|
||||
(i.e. if the start function of all child processes returns `{:ok, child}`,
|
||||
`{:ok, child, info}`, or `:ignore`) the function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the supervisor. If there
|
||||
already exists a process with the specified name, the function returns
|
||||
`{:error, {:already_started, pid}}`, where pid is the pid of that
|
||||
process.
|
||||
|
||||
If the start function of any of the child processes fails or returns an error
|
||||
tuple or an erroneous value, the supervisor first terminates with reason
|
||||
`:shutdown` all the child processes that have already been started, and then
|
||||
terminates itself and returns `{:error, {:shutdown, reason}}`.
|
||||
If any of the child process start functions fail or return an error tuple or
|
||||
an erroneous value, the supervisor will first terminate all already
|
||||
started child processes with reason `:shutdown` and then terminate
|
||||
itself and return `{:error, {:shutdown, reason}}`.
|
||||
|
||||
Note that a supervisor started with this function is linked to the parent
|
||||
process and exits not only on crashes but also if the parent process exits
|
||||
with `:normal` reason.
|
||||
Note that the `Supervisor` is linked to the parent process
|
||||
and will exit not only on crashes but also if the parent process
|
||||
exits with `:normal` reason.
|
||||
"""
|
||||
@spec start_link([Supervisor.Spec.spec], options) :: on_start
|
||||
def start_link(children, options) when is_list(children) do
|
||||
@@ -288,10 +274,10 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Starts a supervisor module with the given `arg`.
|
||||
|
||||
To start the supervisor, the `init/1` callback will be invoked in the given
|
||||
`module`, with `arg` as its argument. The `init/1` callback must return a
|
||||
supervisor specification which can be created with the help of the functions
|
||||
in the `Supervisor.Spec` module (especially `Supervisor.Spec.supervise/2`).
|
||||
To start the supervisor, the `init/1` callback will be invoked
|
||||
in the given module. The `init/1` callback must return a
|
||||
supervision specification which can be created with the help
|
||||
of `Supervisor.Spec` module.
|
||||
|
||||
If the `init/1` callback returns `:ignore`, this function returns
|
||||
`:ignore` as well and the supervisor terminates with reason `:normal`.
|
||||
@@ -300,8 +286,10 @@ defmodule Supervisor do
|
||||
error, and the supervisor terminates with reason `term`.
|
||||
|
||||
The `:name` option can also be given in order to register a supervisor
|
||||
name, the supported values are described in the "Name registration"
|
||||
name, the supported values are described under the `Name Registration`
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
Other failure conditions are specified in `start_link/2` docs.
|
||||
"""
|
||||
@spec start_link(module, term) :: on_start
|
||||
@spec start_link(module, term, options) :: on_start
|
||||
@@ -317,34 +305,33 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Dynamically adds a child specification to `supervisor` and starts that child.
|
||||
Dynamically adds and starts a child specification to the supervisor.
|
||||
|
||||
`child_spec` should be a valid child specification (unless the supervisor
|
||||
is a `:simple_one_for_one` supervisor, see below). The child process will
|
||||
be started as defined in the child specification.
|
||||
|
||||
In the case of `:simple_one_for_one`, the child specification defined in
|
||||
the supervisor is used and instead of a `child_spec`, an arbitrary list
|
||||
the supervisor will be used and instead of a `child_spec`, an arbitrary list
|
||||
of terms is expected. The child process will then be started by appending
|
||||
the given list to the existing function arguments in the child specification.
|
||||
|
||||
If a child specification with the specified id already exists, `child_spec` is
|
||||
discarded and this function returns an error with `:already_started` or
|
||||
`:already_present` if the corresponding child process is running or not,
|
||||
respectively.
|
||||
If a child specification with the specified id already exists,
|
||||
`child_spec` is discarded and the function returns an error with `:already_started`
|
||||
or `:already_present` if the corresponding child process is running or not.
|
||||
|
||||
If the child process start function returns `{:ok, child}` or `{:ok, child,
|
||||
info}`, then child specification and pid are added to the supervisor and
|
||||
this function returns the same value.
|
||||
If the child process starts, function returns `{:ok, child}` or `{:ok, child, info}`,
|
||||
the child specification and pid is added to the supervisor and the function returns
|
||||
the same value.
|
||||
|
||||
If the child process start function returns `:ignore`, the child specification
|
||||
is added to the supervisor, the pid is set to `:undefined` and this function
|
||||
returns `{:ok, :undefined}`.
|
||||
If the child process starts, function returns `:ignore`, the child specification is
|
||||
added to the supervisor, the pid is set to undefined and the function returns
|
||||
`{:ok, :undefined}`.
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous
|
||||
value, or if it fails, the child specification is discarded and this function
|
||||
returns `{:error, error}` where `error` is a term containing information about
|
||||
the error and child specification.
|
||||
If the child process starts, function returns an error tuple or an erroneous value,
|
||||
or if it fails, the child specification is discarded and the function returns
|
||||
`{:error, error}` where `error` is a term containing information about the error
|
||||
and child specification.
|
||||
"""
|
||||
@spec start_child(supervisor, Supervisor.Spec.spec | [term]) :: on_start_child
|
||||
def start_child(supervisor, child_spec_or_args) do
|
||||
@@ -352,23 +339,22 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Terminates the given children, identified by pid or child id.
|
||||
Terminates the given pid or child id.
|
||||
|
||||
If the supervisor is not a `:simple_one_for_one`, the child id is expected
|
||||
and the process, if there's one, is terminated; the child specification is
|
||||
If the supervisor is not a `simple_one_for_one`, the child id is expected
|
||||
and the process, if there is one, is terminated; the child specification is
|
||||
kept unless the child is temporary.
|
||||
|
||||
In case of a `:simple_one_for_one` supervisor, a pid is expected. If the child
|
||||
specification identifier is given instead of a `pid`, this function returns
|
||||
`{:error, :simple_one_for_one}`.
|
||||
In case of a `simple_one_for_one` supervisor, a pid is expected. If the child
|
||||
specification identifier is given instead of a `pid`, the function will
|
||||
return `{:error, :simple_one_for_one}`.
|
||||
|
||||
A non-temporary child process may later be restarted by the supervisor. The child
|
||||
process can also be restarted explicitly by calling `restart_child/2`. Use
|
||||
`delete_child/2` to remove the child specification.
|
||||
|
||||
If successful, this function returns `:ok`. If there is no child specification
|
||||
for the given child id or there is no process with the given pid, this
|
||||
function returns `{:error, :not_found}`.
|
||||
If successful, the function returns `:ok`. If there is no child specification or
|
||||
pid, the function returns `{:error, :not_found}`.
|
||||
"""
|
||||
@spec terminate_child(supervisor, pid | Supervisor.Spec.child_id) :: :ok | {:error, error}
|
||||
when error: :not_found | :simple_one_for_one
|
||||
@@ -379,14 +365,14 @@ defmodule Supervisor do
|
||||
@doc """
|
||||
Deletes the child specification identified by `child_id`.
|
||||
|
||||
The corresponding child process must not be running; use `terminate_child/2`
|
||||
to terminate it if it's running.
|
||||
The corresponding child process must not be running, use `terminate_child/2`
|
||||
to terminate it.
|
||||
|
||||
If successful, this function returns `:ok`. This function may return an error
|
||||
with an appropriate error tuple if the `child_id` is not found, or if the
|
||||
current process is running or being restarted.
|
||||
If successful, the function returns `:ok`. This function may error with an
|
||||
appropriate error tuple if the `child_id` is not found, or if the current
|
||||
process is running or being restarted.
|
||||
|
||||
This operation is not supported by `:simple_one_for_one` supervisors.
|
||||
This operation is not supported by `simple_one_for_one` supervisors.
|
||||
"""
|
||||
@spec delete_child(supervisor, Supervisor.Spec.child_id) :: :ok | {:error, error}
|
||||
when error: :not_found | :simple_one_for_one | :running | :restarting
|
||||
@@ -403,20 +389,20 @@ defmodule Supervisor do
|
||||
Note that for temporary children, the child specification is automatically deleted
|
||||
when the child terminates, and thus it is not possible to restart such children.
|
||||
|
||||
If the child process start function returns `{:ok, child}` or `{:ok, child, info}`,
|
||||
the pid is added to the supervisor and this function returns the same value.
|
||||
If the child process start function returns `{:ok, child}` or
|
||||
`{:ok, child, info}`, the pid is added to the supervisor and the function returns
|
||||
the same value.
|
||||
|
||||
If the child process start function returns `:ignore`, the pid remains set to
|
||||
`:undefined` and this function returns `{:ok, :undefined}`.
|
||||
`:undefined` and the function returns `{:ok, :undefined}`.
|
||||
|
||||
This function may return an error with an appropriate error tuple if the
|
||||
`child_id` is not found, or if the current process is running or being
|
||||
restarted.
|
||||
This function may error with an appropriate error tuple if the `child_id` is not
|
||||
found, or if the current process is running or being restarted.
|
||||
|
||||
If the child process start function returns an error tuple or an erroneous value,
|
||||
or if it fails, this function returns `{:error, error}`.
|
||||
or if it fails, the function returns `{:error, error}`.
|
||||
|
||||
This operation is not supported by `:simple_one_for_one` supervisors.
|
||||
This operation is not supported by `simple_one_for_one` supervisors.
|
||||
"""
|
||||
@spec restart_child(supervisor, Supervisor.Spec.child_id) ::
|
||||
{:ok, child} | {:ok, child, term} | {:error, error}
|
||||
@@ -426,24 +412,23 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about all children of the given supervisor.
|
||||
Returns a list with information about all children.
|
||||
|
||||
Note that calling this function when supervising a large number of children
|
||||
under low memory conditions can cause an out of memory exception.
|
||||
|
||||
This function returns a list of `{id, child, type, modules}` tuples, where:
|
||||
This function returns a list of tuples containing:
|
||||
|
||||
* `id` - as defined in the child specification or `:undefined` in the case
|
||||
of a `simple_one_for_one` supervisor
|
||||
|
||||
* `child` - the pid of the corresponding child process, `:restarting` if the
|
||||
process is about to be restarted, or `:undefined` if there is no such
|
||||
process
|
||||
* `child` - the pid of the corresponding child process, the atom
|
||||
`:restarting` if the process is about to be restarted, or `:undefined` if
|
||||
there is no such process
|
||||
|
||||
* `type` - `:worker` or `:supervisor`, as specified by the child specification
|
||||
|
||||
* `modules` - as specified by the child specification
|
||||
* `type` - `:worker` or `:supervisor` as defined in the child specification
|
||||
|
||||
* `modules` - as defined in the child specification
|
||||
"""
|
||||
@spec which_children(supervisor) ::
|
||||
[{Supervisor.Spec.child_id | :undefined,
|
||||
@@ -455,7 +440,7 @@ defmodule Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a map containing count values for the given supervisor.
|
||||
Returns a map containing count values for the supervisor.
|
||||
|
||||
The map contains the following keys:
|
||||
|
||||
@@ -464,11 +449,11 @@ defmodule Supervisor do
|
||||
* `:active` - the count of all actively running child processes managed by
|
||||
this supervisor
|
||||
|
||||
* `:supervisors` - the count of all supervisors whether or not these
|
||||
child supervisors are still alive
|
||||
* `:supervisors` - the count of all supervisors whether or not the child
|
||||
process is still alive
|
||||
|
||||
* `:workers` - the count of all workers, whether or not these child workers
|
||||
are still alive
|
||||
* `:workers` - the count of all workers, whether or not the child process
|
||||
is still alive
|
||||
|
||||
"""
|
||||
@spec count_children(supervisor) ::
|
||||
@@ -478,21 +463,6 @@ defmodule Supervisor do
|
||||
call(supervisor, :count_children) |> :maps.from_list
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the given supervisor with the given `reason`.
|
||||
|
||||
It returns `:ok` if the supervisor terminates with the given
|
||||
reason. If it terminates with another reason, the call exits.
|
||||
|
||||
This function keeps OTP semantics regarding error reporting.
|
||||
If the reason is any other than `:normal`, `:shutdown` or
|
||||
`{:shutdown, _}`, an error report is logged.
|
||||
"""
|
||||
@spec stop(supervisor, reason :: term, timeout) :: :ok
|
||||
def stop(supervisor, reason \\ :normal, timeout \\ :infinity) do
|
||||
:gen.stop(supervisor, reason, timeout)
|
||||
end
|
||||
|
||||
@compile {:inline, call: 2}
|
||||
|
||||
defp call(supervisor, req) do
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
defmodule Supervisor.Default do
|
||||
@moduledoc false
|
||||
@behaviour :supervisor
|
||||
|
||||
@doc """
|
||||
Supervisor callback that simply returns the given args.
|
||||
|
||||
This is the supervisor used by `Supervisor.start_link/2`
|
||||
and others.
|
||||
This is the supervisor used by `Supervisor.start_link/2`.
|
||||
"""
|
||||
def init(args) do
|
||||
args
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,11 +1,11 @@
|
||||
defmodule Supervisor.Spec do
|
||||
@moduledoc """
|
||||
Convenience functions for defining supervisor specifications.
|
||||
Convenience functions for defining a supervision specification.
|
||||
|
||||
## Example
|
||||
|
||||
By using the functions in this module one can specify the children
|
||||
to be used under a supervisor, started with `Supervisor.start_link/2`:
|
||||
By using the functions in this module one can define a supervisor
|
||||
and start it with `Supervisor.start_link/2`:
|
||||
|
||||
import Supervisor.Spec
|
||||
|
||||
@@ -16,7 +16,7 @@ defmodule Supervisor.Spec do
|
||||
|
||||
Supervisor.start_link(children, strategy: :one_for_one)
|
||||
|
||||
Sometimes, it may be handy to define supervisors backed
|
||||
In many situations, it may be handy to define supervisors backed
|
||||
by a module:
|
||||
|
||||
defmodule MySupervisor do
|
||||
@@ -37,35 +37,42 @@ defmodule Supervisor.Spec do
|
||||
|
||||
Notice in this case we don't have to explicitly import
|
||||
`Supervisor.Spec` as `use Supervisor` automatically does so.
|
||||
Defining a module-based supervisor can be useful, for example,
|
||||
to perform initialization tasks in the `init/1` callback.
|
||||
|
||||
Explicit supervisors as above are required when there is a need to:
|
||||
|
||||
1. Partially change the supervision tree during hot-code swaps.
|
||||
|
||||
2. Define supervisors inside other supervisors.
|
||||
|
||||
3. Perform actions inside the supervision `init/1` callback.
|
||||
|
||||
For example, you may want to start an ETS table that is linked to
|
||||
the supervisor (i.e. if the supervision tree needs to be restarted,
|
||||
the ETS table must be restarted too).
|
||||
|
||||
## Supervisor and worker options
|
||||
|
||||
In the example above, we defined specs for workers and supervisors.
|
||||
These specs (both for workers as well as supervisors) accept the
|
||||
following options:
|
||||
In the example above, we have defined workers and supervisors
|
||||
and each accepts the following options:
|
||||
|
||||
* `:id` - a name used to identify the child specification
|
||||
internally by the supervisor; defaults to the given module
|
||||
name for the child worker/supervisor
|
||||
name
|
||||
|
||||
* `:function` - the function to invoke on the child to start it
|
||||
|
||||
* `:restart` - an atom that defines when a terminated child process should
|
||||
be restarted (see the "Restart values" section below)
|
||||
* `:restart` - defines when the child process should restart
|
||||
|
||||
* `:shutdown` - an atom that defines how a child process should be
|
||||
terminated (see the "Shutdown values" section below)
|
||||
* `:shutdown` - defines how a child process should be terminated
|
||||
|
||||
* `:modules` - it should be a list with one element `[module]`,
|
||||
where module is the name of the callback module only if the
|
||||
child process is a `Supervisor` or `GenServer`; if the child
|
||||
process is a `GenEvent`, `:modules` should be `:dynamic`
|
||||
process is a `GenEvent`, modules should be `:dynamic`
|
||||
|
||||
### Restart values (:restart)
|
||||
|
||||
The following restart values are supported in the `:restart` option:
|
||||
The following restart values are supported:
|
||||
|
||||
* `:permanent` - the child process is always restarted
|
||||
|
||||
@@ -73,31 +80,27 @@ defmodule Supervisor.Spec do
|
||||
when the supervisor's strategy is `:rest_for_one` or `:one_for_all`)
|
||||
|
||||
* `:transient` - the child process is restarted only if it
|
||||
terminates abnormally, i.e., with an exit reason other than
|
||||
terminates abnormally, i.e. with another exit reason than
|
||||
`:normal`, `:shutdown` or `{:shutdown, term}`
|
||||
|
||||
### Shutdown values (:shutdown)
|
||||
|
||||
The following shutdown values are supported in the `:shutdown` option:
|
||||
The following shutdown values are supported:
|
||||
|
||||
* `:brutal_kill` - the child process is unconditionally terminated
|
||||
using `exit(child, :kill)`
|
||||
using `exit(child, :kill)`.
|
||||
|
||||
* `:infinity` - if the child process is a supervisor, this is a mechanism
|
||||
to give the subtree enough time to shutdown; it can also be used with
|
||||
workers with care
|
||||
|
||||
* any integer - the value of `:shutdown` can also be any integer meaning
|
||||
that the supervisor tells the child process to terminate by calling
|
||||
`Process.exit(child, :shutdown)` and then waits for an exit signal back.
|
||||
If no exit signal is received within the specified time (the value of this
|
||||
option, in milliseconds), the child process is unconditionally terminated
|
||||
using `Process.exit(child, :kill)`
|
||||
* `:infinity` - if the child process is a supervisor, it is a mechanism
|
||||
to give the subtree enough time to shutdown. It can also be used with
|
||||
workers with care.
|
||||
|
||||
* Finally, it can also be any integer meaning that the supervisor tells
|
||||
the child process to terminate by calling `Process.exit(child, :shutdown)`
|
||||
and then waits for an exit signal back. If no exit signal is received
|
||||
within the specified time (in milliseconds), the child process is
|
||||
unconditionally terminated using `Process.exit(child, :kill)`.
|
||||
"""
|
||||
|
||||
# TODO: Update and provide a digest of strategies once we include DynamicSupervisor.
|
||||
|
||||
@typedoc "Supported strategies"
|
||||
@type strategy :: :simple_one_for_one | :one_for_one | :one_for_all | :rest_for_one
|
||||
|
||||
@@ -128,13 +131,11 @@ defmodule Supervisor.Spec do
|
||||
Receives a list of children (workers or supervisors) to
|
||||
supervise and a set of options.
|
||||
|
||||
Returns a tuple containing the supervisor specification. This tuple can be
|
||||
used as the return value of the `init/1` callback when implementing a
|
||||
module-based supervisor.
|
||||
Returns a tuple containing the supervisor specification.
|
||||
|
||||
## Examples
|
||||
|
||||
supervise(children, strategy: :one_for_one)
|
||||
supervise children, strategy: :one_for_one
|
||||
|
||||
## Options
|
||||
|
||||
@@ -144,20 +145,18 @@ defmodule Supervisor.Spec do
|
||||
in the `Supervisor` module docs.
|
||||
|
||||
* `:max_restarts` - the maximum amount of restarts allowed in
|
||||
a time frame. Defaults to `3`.
|
||||
a time frame. Defaults to 3.
|
||||
|
||||
* `:max_seconds` - the time frame in which `:max_restarts` applies.
|
||||
Defaults to `5`.
|
||||
Defaults to 5.
|
||||
|
||||
The `:strategy` option is required and by default a maximum of 3 restarts is
|
||||
allowed within 5 seconds. Check the `Supervisor` module for a detailed
|
||||
description of the available strategies.
|
||||
The `:strategy` option is required and by default maximum 3 restarts
|
||||
are allowed within 5 seconds. Please check the `Supervisor` module for
|
||||
a complete description of the available strategies.
|
||||
"""
|
||||
@spec supervise([spec], strategy: strategy,
|
||||
max_restarts: non_neg_integer,
|
||||
max_seconds: non_neg_integer) :: {:ok, tuple}
|
||||
# TODO: Make it return a tuple of format {:ok, children, opts}
|
||||
# TODO: Deprecate once the new tuple format has been established
|
||||
def supervise(children, options) do
|
||||
unless strategy = options[:strategy] do
|
||||
raise ArgumentError, "expected :strategy option to be given"
|
||||
@@ -170,7 +169,7 @@ defmodule Supervisor.Spec do
|
||||
{:ok, {{strategy, maxR, maxS}, children}}
|
||||
end
|
||||
|
||||
defp assert_unique_ids([id | rest]) do
|
||||
defp assert_unique_ids([id|rest]) do
|
||||
if id in rest do
|
||||
raise ArgumentError,
|
||||
"duplicated id #{inspect id} found in the supervisor specification, " <>
|
||||
@@ -188,7 +187,7 @@ defmodule Supervisor.Spec do
|
||||
Defines the given `module` as a worker which will be started
|
||||
with the given arguments.
|
||||
|
||||
worker(ExUnit.Runner, [], restart: :permanent)
|
||||
worker ExUnit.Runner, [], restart: :permanent
|
||||
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
@@ -199,8 +198,8 @@ defmodule Supervisor.Spec do
|
||||
shutdown: 5000,
|
||||
modules: [module]]
|
||||
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
Check `Supervisor.Spec` module docs for more information on
|
||||
the options.
|
||||
"""
|
||||
@spec worker(module, [term], [restart: restart, shutdown: shutdown,
|
||||
id: term, function: atom, modules: modules]) :: spec
|
||||
@@ -212,7 +211,7 @@ defmodule Supervisor.Spec do
|
||||
Defines the given `module` as a supervisor which will be started
|
||||
with the given arguments.
|
||||
|
||||
supervisor(ExUnit.Runner, [], restart: :permanent)
|
||||
supervisor ExUnit.Runner, [], restart: :permanent
|
||||
|
||||
By default, the function `start_link` is invoked on the given
|
||||
module. Overall, the default values for the options are:
|
||||
@@ -223,8 +222,8 @@ defmodule Supervisor.Spec do
|
||||
shutdown: :infinity,
|
||||
modules: [module]]
|
||||
|
||||
Check the documentation for the `Supervisor.Spec` module for more
|
||||
information on the options.
|
||||
Check `Supervisor.Spec` module docs for more information on
|
||||
the options.
|
||||
"""
|
||||
@spec supervisor(module, [term], [restart: restart, shutdown: shutdown,
|
||||
id: term, function: atom, modules: modules]) :: spec
|
||||
@@ -233,7 +232,6 @@ defmodule Supervisor.Spec do
|
||||
child(:supervisor, module, args, options)
|
||||
end
|
||||
|
||||
# TODO: Do and expose proper child validation
|
||||
defp child(type, module, args, options) do
|
||||
id = Keyword.get(options, :id, module)
|
||||
modules = Keyword.get(options, :modules, modules(module))
|
||||
@@ -245,7 +243,6 @@ defmodule Supervisor.Spec do
|
||||
restart, shutdown, type, modules}
|
||||
end
|
||||
|
||||
# TODO: Remove GenEvent when there is no more GenEvent v2.0
|
||||
defp modules(GenEvent), do: :dynamic
|
||||
defp modules(module), do: [module]
|
||||
end
|
||||
|
||||
+57
-386
@@ -1,151 +1,52 @@
|
||||
defmodule System do
|
||||
@moduledoc """
|
||||
The `System` module provides functions that interact directly
|
||||
The System module provides access to variables used or
|
||||
maintained by the VM and to functions that interact directly
|
||||
with the VM or the host system.
|
||||
|
||||
## Time
|
||||
|
||||
The `System` module also provides functions that work with time,
|
||||
returning different times kept by the system with support for
|
||||
different time units.
|
||||
|
||||
One of the complexities in relying on system times is that they
|
||||
may be adjusted. For example, when you enter and leave daylight
|
||||
saving time, the system clock will be adjusted, often adding
|
||||
or removing one hour. We call such changes "time warps". In
|
||||
order to understand how such changes may be harmful, imagine
|
||||
the following code:
|
||||
|
||||
## DO NOT DO THIS
|
||||
prev = System.os_time()
|
||||
# ... execute some code ...
|
||||
next = System.os_time()
|
||||
diff = next - prev
|
||||
|
||||
If, while the code is executing, the system clock changes,
|
||||
some code that executed in 1 second may be reported as taking
|
||||
over 1 hour! To address such concerns, the VM provides a
|
||||
monotonic time via `System.monotonic_time/0` which never
|
||||
decreases and does not leap:
|
||||
|
||||
## DO THIS
|
||||
prev = System.monotonic_time()
|
||||
# ... execute some code ...
|
||||
next = System.monotonic_time()
|
||||
diff = next - prev
|
||||
|
||||
Generally speaking, the VM provides three time measurements:
|
||||
|
||||
* `os_time/0` - the time reported by the OS. This time may be
|
||||
adjusted forwards or backwards in time with no limitation;
|
||||
|
||||
* `system_time/0` - the VM view of the `os_time/0`. The system time and OS
|
||||
time may not match in case of time warps although the VM works towards
|
||||
aligning them. This time is not monotonic (i.e., it may decrease)
|
||||
as its behaviour is configured [by the VM time warp
|
||||
mode](http://www.erlang.org/doc/apps/erts/time_correction.html#Time_Warp_Modes);
|
||||
|
||||
* `monotonic_time/0` - a monotonically increasing time provided
|
||||
by the Erlang VM.
|
||||
|
||||
The time functions in this module work in the `:native` unit
|
||||
(unless specified otherwise), which is OS dependent. Most of
|
||||
the time, all calculations are done in the `:native` unit, to
|
||||
avoid loss of precision, with `convert_time_unit/3` being
|
||||
invoked at the end to convert to a specific time unit like
|
||||
milliseconds or microseconds. See the `t:time_unit/0` type for
|
||||
more information.
|
||||
|
||||
For a more complete rundown on the VM support for different
|
||||
times, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html)
|
||||
in the Erlang docs.
|
||||
"""
|
||||
|
||||
@typedoc """
|
||||
The time unit to be passed to functions like `monotonic_time/1` and others.
|
||||
|
||||
The `:seconds`, `:milliseconds`, `:microseconds` and `:nanoseconds` time
|
||||
units controls the return value of the functions that accept a time unit.
|
||||
|
||||
A time unit can also be a strictly positive integer. In this case, it
|
||||
represents the "parts per second": the time will be returned in `1 /
|
||||
parts_per_second` seconds. For example, using the `:milliseconds` time unit
|
||||
is equivalent to using `1000` as the time unit (as the time will be returned
|
||||
in 1/1000 seconds - milliseconds).
|
||||
|
||||
Keep in mind the Erlang API will use `:milli_seconds`, `:micro_seconds`
|
||||
and `:nano_seconds` as time units although Elixir normalizes their spelling
|
||||
to match the SI convention.
|
||||
"""
|
||||
@type time_unit ::
|
||||
:seconds
|
||||
| :milliseconds
|
||||
| :microseconds
|
||||
| :nanoseconds
|
||||
| pos_integer
|
||||
|
||||
@base_dir :filename.join(__DIR__, "../../..")
|
||||
@version_file :filename.join(@base_dir, "VERSION")
|
||||
|
||||
defp strip(iodata) do
|
||||
:re.replace(iodata, "^[\s\r\n\t]+|[\s\r\n\t]+$", "", [:global, return: :binary])
|
||||
defp strip_re(iodata, pattern) do
|
||||
:re.replace(iodata, pattern, "", [return: :binary])
|
||||
end
|
||||
|
||||
defp read_stripped(path) do
|
||||
case :file.read_file(path) do
|
||||
{:ok, binary} ->
|
||||
strip(binary)
|
||||
_ ->
|
||||
""
|
||||
strip_re(binary, "^\s+|\s+$")
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
# Read and strip the version from the VERSION file.
|
||||
defmacrop get_version do
|
||||
case read_stripped(@version_file) do
|
||||
case read_stripped(:filename.join(__DIR__, "../../../VERSION")) do
|
||||
"" -> raise RuntimeError, message: "could not read the version number from VERSION"
|
||||
data -> data
|
||||
end
|
||||
end
|
||||
|
||||
# Tries to run "git rev-parse --short HEAD". In the case of success returns
|
||||
# the short revision hash. If that fails, returns an empty string.
|
||||
defmacrop get_revision do
|
||||
null =
|
||||
case :os.type do
|
||||
{:win32, _} -> 'NUL'
|
||||
_ -> '/dev/null'
|
||||
end
|
||||
|
||||
'git rev-parse --short HEAD 2> '
|
||||
|> Kernel.++(null)
|
||||
|> :os.cmd()
|
||||
|> strip
|
||||
# Tries to run "git describe --always --tags". In the case of success returns
|
||||
# the most recent tag. If that is not available, tries to read the commit hash
|
||||
# from .git/HEAD. If that fails, returns an empty string.
|
||||
defmacrop get_describe do
|
||||
dirpath = :filename.join(__DIR__, "../../../.git")
|
||||
case :file.read_file_info(dirpath) do
|
||||
{:ok, _} ->
|
||||
if :os.find_executable('git') do
|
||||
data = :os.cmd('git describe --always --tags')
|
||||
strip_re(data, "\n")
|
||||
else
|
||||
read_stripped(:filename.join(".git", "HEAD"))
|
||||
end
|
||||
_ -> ""
|
||||
end
|
||||
end
|
||||
|
||||
defp revision, do: get_revision
|
||||
|
||||
# Get the date at compilation time.
|
||||
defmacrop get_date do
|
||||
IO.iodata_to_binary :httpd_util.rfc1123_date
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the endianness.
|
||||
"""
|
||||
def endianness do
|
||||
:erlang.system_info(:endian)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the endianness the system was compiled with.
|
||||
"""
|
||||
@endianness :erlang.system_info(:endian)
|
||||
def compiled_endianness do
|
||||
@endianness
|
||||
end
|
||||
|
||||
@doc """
|
||||
Elixir version information.
|
||||
|
||||
@@ -157,26 +58,11 @@ defmodule System do
|
||||
@doc """
|
||||
Elixir build information.
|
||||
|
||||
Returns a keyword list with Elixir version, Git short revision hash and compilation date.
|
||||
Returns a keyword list with Elixir version, git tag info and compilation date.
|
||||
"""
|
||||
@spec build_info() :: map
|
||||
def build_info do
|
||||
%{build: build,
|
||||
date: get_date,
|
||||
revision: revision,
|
||||
version: version}
|
||||
end
|
||||
|
||||
# Returns a string of the build info
|
||||
defp build do
|
||||
{:ok, v} = Version.parse(version)
|
||||
|
||||
cond do
|
||||
([] == v.pre) or ("" == revision) ->
|
||||
version
|
||||
true ->
|
||||
"#{version} (#{revision})"
|
||||
end
|
||||
%{version: version, tag: get_describe, date: get_date}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -320,7 +206,7 @@ defmodule System do
|
||||
The function must receive the exit status code as an argument.
|
||||
"""
|
||||
def at_exit(fun) when is_function(fun, 1) do
|
||||
:elixir_config.update :at_exit, &[fun | &1]
|
||||
:elixir_config.update :at_exit, &[fun|&1]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -334,7 +220,7 @@ defmodule System do
|
||||
"""
|
||||
@spec find_executable(binary) :: binary | nil
|
||||
def find_executable(program) when is_binary(program) do
|
||||
case :os.find_executable(String.to_charlist(program)) do
|
||||
case :os.find_executable(String.to_char_list(program)) do
|
||||
false -> nil
|
||||
other -> List.to_string(other)
|
||||
end
|
||||
@@ -346,7 +232,7 @@ defmodule System do
|
||||
Returns a list of all environment variables. Each variable is given as a
|
||||
`{name, value}` tuple where both `name` and `value` are strings.
|
||||
"""
|
||||
@spec get_env() :: %{optional(String.t) => String.t}
|
||||
@spec get_env() :: %{String.t => String.t}
|
||||
def get_env do
|
||||
Enum.into(:os.getenv, %{}, fn var ->
|
||||
var = IO.chardata_to_string var
|
||||
@@ -364,7 +250,7 @@ defmodule System do
|
||||
"""
|
||||
@spec get_env(binary) :: binary | nil
|
||||
def get_env(varname) when is_binary(varname) do
|
||||
case :os.getenv(String.to_charlist(varname)) do
|
||||
case :os.getenv(String.to_char_list(varname)) do
|
||||
false -> nil
|
||||
other -> List.to_string(other)
|
||||
end
|
||||
@@ -388,7 +274,7 @@ defmodule System do
|
||||
"""
|
||||
@spec put_env(binary, binary) :: :ok
|
||||
def put_env(varname, value) when is_binary(varname) and is_binary(value) do
|
||||
:os.putenv String.to_charlist(varname), String.to_charlist(value)
|
||||
:os.putenv String.to_char_list(varname), String.to_char_list(value)
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -398,9 +284,9 @@ defmodule System do
|
||||
Sets a new value for each environment variable corresponding
|
||||
to each key in `dict`.
|
||||
"""
|
||||
@spec put_env(Enumerable.t) :: :ok
|
||||
def put_env(enum) do
|
||||
Enum.each enum, fn {key, val} -> put_env key, val end
|
||||
@spec put_env(Dict.t) :: :ok
|
||||
def put_env(dict) do
|
||||
Enum.each dict, fn {key, val} -> put_env key, val end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -410,7 +296,7 @@ defmodule System do
|
||||
"""
|
||||
@spec delete_env(String.t) :: :ok
|
||||
def delete_env(varname) do
|
||||
:os.unsetenv(String.to_charlist(varname))
|
||||
:os.unsetenv(String.to_char_list(varname))
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -463,7 +349,7 @@ defmodule System do
|
||||
end
|
||||
|
||||
def halt(status) when is_binary(status) do
|
||||
:erlang.halt(String.to_charlist(status))
|
||||
:erlang.halt(String.to_char_list(status))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -472,25 +358,22 @@ defmodule System do
|
||||
`command` is expected to be an executable available in PATH
|
||||
unless an absolute path is given.
|
||||
|
||||
`args` must be a list of binaries which the executable will receive
|
||||
as its arguments as is. This means that:
|
||||
|
||||
* environment variables will not be interpolated
|
||||
* wildcard expansion will not happen (unless `Path.wildcard/2` is used
|
||||
explicitly)
|
||||
* arguments do not need to be escaped or quoted for shell safety
|
||||
`args` must be a list of strings which are not expanded
|
||||
in any way. For example, this means wildcard expansion will
|
||||
not happen unless `Path.wildcard/2` is used. On Windows though,
|
||||
wildcard expansion is up to the program.
|
||||
|
||||
This function returns a tuple containing the collected result
|
||||
and the command exit status.
|
||||
|
||||
## Examples
|
||||
|
||||
|
||||
iex> System.cmd "echo", ["hello"]
|
||||
{"hello\n", 0}
|
||||
|
||||
iex> System.cmd "echo", ["hello"], env: [{"MIX_ENV", "test"}]
|
||||
{"hello\n", 0}
|
||||
|
||||
|
||||
iex> System.cmd "echo", ["hello"], into: IO.stream(:stdio, :line)
|
||||
hello
|
||||
{%IO.Stream{}, 0}
|
||||
@@ -543,7 +426,7 @@ defmodule System do
|
||||
@spec cmd(binary, [binary], Keyword.t) ::
|
||||
{Collectable.t, exit_status :: non_neg_integer}
|
||||
def cmd(command, args, opts \\ []) when is_binary(command) and is_list(args) do
|
||||
cmd = String.to_charlist(command)
|
||||
cmd = String.to_char_list(command)
|
||||
|
||||
cmd =
|
||||
if Path.type(cmd) == :absolute do
|
||||
@@ -554,16 +437,7 @@ defmodule System do
|
||||
|
||||
{into, opts} = cmd_opts(opts, [:use_stdio, :exit_status, :binary, :hide, args: args], "")
|
||||
{initial, fun} = Collectable.into(into)
|
||||
try do
|
||||
do_cmd Port.open({:spawn_executable, cmd}, opts), initial, fun
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
fun.(initial, :halt)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{acc, status} -> {fun.(acc, :done), status}
|
||||
end
|
||||
do_cmd Port.open({:spawn_executable, cmd}, opts), initial, fun
|
||||
end
|
||||
|
||||
defp do_cmd(port, acc, fun) do
|
||||
@@ -571,32 +445,32 @@ defmodule System do
|
||||
{^port, {:data, data}} ->
|
||||
do_cmd(port, fun.(acc, {:cont, data}), fun)
|
||||
{^port, {:exit_status, status}} ->
|
||||
{acc, status}
|
||||
{fun.(acc, :done), status}
|
||||
end
|
||||
end
|
||||
|
||||
defp cmd_opts([{:into, any} | t], opts, _into),
|
||||
defp cmd_opts([{:into, any}|t], opts, _into),
|
||||
do: cmd_opts(t, opts, any)
|
||||
|
||||
defp cmd_opts([{:cd, bin} | t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:cd, bin} | opts], into)
|
||||
defp cmd_opts([{:cd, bin}|t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:cd, bin}|opts], into)
|
||||
|
||||
defp cmd_opts([{:arg0, bin} | t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:arg0, bin} | opts], into)
|
||||
defp cmd_opts([{:arg0, bin}|t], opts, into) when is_binary(bin),
|
||||
do: cmd_opts(t, [{:arg0, bin}|opts], into)
|
||||
|
||||
defp cmd_opts([{:stderr_to_stdout, true} | t], opts, into),
|
||||
do: cmd_opts(t, [:stderr_to_stdout | opts], into)
|
||||
defp cmd_opts([{:stderr_to_stdout, true}|t], opts, into),
|
||||
do: cmd_opts(t, [:stderr_to_stdout|opts], into)
|
||||
|
||||
defp cmd_opts([{:stderr_to_stdout, false} | t], opts, into),
|
||||
defp cmd_opts([{:stderr_to_stdout, false}|t], opts, into),
|
||||
do: cmd_opts(t, opts, into)
|
||||
|
||||
defp cmd_opts([{:parallelism, bool} | t], opts, into) when is_boolean(bool),
|
||||
do: cmd_opts(t, [{:parallelism, bool} | opts], into)
|
||||
defp cmd_opts([{:parallelism, bool}|t], opts, into) when is_boolean(bool),
|
||||
do: cmd_opts(t, [{:parallelism, bool}|opts], into)
|
||||
|
||||
defp cmd_opts([{:env, enum} | t], opts, into),
|
||||
do: cmd_opts(t, [{:env, validate_env(enum)} | opts], into)
|
||||
defp cmd_opts([{:env, enum}|t], opts, into),
|
||||
do: cmd_opts(t, [{:env, validate_env(enum)}|opts], into)
|
||||
|
||||
defp cmd_opts([{key, val} | _], _opts, _into),
|
||||
defp cmd_opts([{key, val}|_], _opts, _into),
|
||||
do: raise(ArgumentError, "invalid option #{inspect key} with value #{inspect val}")
|
||||
|
||||
defp cmd_opts([], opts, into),
|
||||
@@ -604,213 +478,10 @@ defmodule System do
|
||||
|
||||
defp validate_env(enum) do
|
||||
Enum.map enum, fn
|
||||
{k, nil} ->
|
||||
{String.to_charlist(k), false}
|
||||
{k, v} ->
|
||||
{String.to_charlist(k), String.to_charlist(v)}
|
||||
{String.to_char_list(k), String.to_char_list(v)}
|
||||
other ->
|
||||
raise ArgumentError, "invalid environment key-value #{inspect other}"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current monotonic time in the `:native` time unit.
|
||||
|
||||
This time is monotonically increasing and starts in an unspecified
|
||||
point in time.
|
||||
|
||||
Inlined by the compiler into `:erlang.monotonic_time/0`.
|
||||
"""
|
||||
@spec monotonic_time() :: integer
|
||||
def monotonic_time do
|
||||
:erlang.monotonic_time()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current monotonic time in the given time unit.
|
||||
|
||||
This time is monotonically increasing and starts in an unspecified
|
||||
point in time.
|
||||
"""
|
||||
@spec monotonic_time(time_unit) :: integer
|
||||
def monotonic_time(unit) do
|
||||
:erlang.monotonic_time(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current system time in the `:native` time unit.
|
||||
|
||||
It is the VM view of the `os_time/0`. They may not match in
|
||||
case of time warps although the VM works towards aligning
|
||||
them. This time is not monotonic.
|
||||
|
||||
Inlined by the compiler into `:erlang.system_time/0`.
|
||||
"""
|
||||
@spec system_time() :: integer
|
||||
def system_time do
|
||||
:erlang.system_time()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current system time in the given time unit.
|
||||
|
||||
It is the VM view of the `os_time/0`. They may not match in
|
||||
case of time warps although the VM works towards aligning
|
||||
them. This time is not monotonic.
|
||||
"""
|
||||
@spec system_time(time_unit) :: integer
|
||||
def system_time(unit) do
|
||||
:erlang.system_time(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `time` from time unit `from_unit` to time unit `to_unit`.
|
||||
|
||||
The result is rounded via the floor function.
|
||||
|
||||
`convert_time_unit/3` accepts an additional time unit (other than the
|
||||
ones in the `time_unit` type) called `:native`. `:native` is the time
|
||||
unit used by the Erlang runtime system. It's determined when the runtime
|
||||
starts and stays the same until the runtime is stopped. To determine what
|
||||
the `:native` unit amounts to in a system, you can call this function to
|
||||
convert 1 second to the `:native` time unit (i.e.,
|
||||
`System.convert_time_unit(1, :seconds, :native)`).
|
||||
"""
|
||||
@spec convert_time_unit(integer, time_unit | :native, time_unit | :native) :: integer
|
||||
def convert_time_unit(time, from_unit, to_unit) do
|
||||
:erlang.convert_time_unit(time, normalize_time_unit(from_unit), normalize_time_unit(to_unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current time offset between the Erlang VM monotonic
|
||||
time and the Erlang VM system time.
|
||||
|
||||
The result is returned in the `:native` time unit.
|
||||
|
||||
See `time_offset/1` for more information.
|
||||
|
||||
Inlined by the compiler into `:erlang.time_offset/0`.
|
||||
"""
|
||||
@spec time_offset() :: integer
|
||||
def time_offset do
|
||||
:erlang.time_offset()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current time offset between the Erlang VM monotonic
|
||||
time and the Erlang VM system time.
|
||||
|
||||
The result is returned in the given time unit `unit`. The returned
|
||||
offset, added to an Erlang monotonic time (e.g., obtained with
|
||||
`monotonic_time/1`), gives the Erlang system time that corresponds
|
||||
to that monotonic time.
|
||||
"""
|
||||
@spec time_offset(time_unit) :: integer
|
||||
def time_offset(unit) do
|
||||
:erlang.time_offset(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current OS time.
|
||||
|
||||
The result is returned in the `:native` time unit.
|
||||
|
||||
This time may be adjusted forwards or backwards in time
|
||||
with no limitation and is not monotonic.
|
||||
|
||||
Inlined by the compiler into `:os.system_time/0`.
|
||||
"""
|
||||
@spec os_time() :: integer
|
||||
def os_time do
|
||||
:os.system_time()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current OS time in the given time `unit`.
|
||||
|
||||
This time may be adjusted forwards or backwards in time
|
||||
with no limitation and is not monotonic.
|
||||
"""
|
||||
@spec os_time(time_unit) :: integer
|
||||
def os_time(unit) do
|
||||
:os.system_time(normalize_time_unit(unit))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the OTP release number.
|
||||
"""
|
||||
@spec otp_release :: String.t
|
||||
def otp_release do
|
||||
:erlang.list_to_binary :erlang.system_info(:otp_release)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of schedulers in the VM.
|
||||
"""
|
||||
@spec schedulers :: pos_integer
|
||||
def schedulers do
|
||||
:erlang.system_info(:schedulers)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the number of schedulers online in the VM.
|
||||
"""
|
||||
@spec schedulers_online :: pos_integer
|
||||
def schedulers_online do
|
||||
:erlang.system_info(:schedulers_online)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates and returns an integer that is unique in the current runtime
|
||||
instance.
|
||||
|
||||
"Unique" means that this function, called with the same list of `modifiers`,
|
||||
will never return the same integer more than once on the current runtime
|
||||
instance.
|
||||
|
||||
If `modifiers` is `[]`, then a unique integer (that can be positive or negative) is returned.
|
||||
Other modifiers can be passed to change the properties of the returned integer:
|
||||
|
||||
* `:positive` - the returned integer is guaranteed to be positive.
|
||||
* `:monotonic` - the returned integer is monotonically increasing. This
|
||||
means that, on the same runtime instance (but even on different
|
||||
processes), integers returned using the `:monotonic` modifier will always
|
||||
be strictly less than integers returned by successive calls with the
|
||||
`:monotonic` modifier.
|
||||
|
||||
All modifiers listed above can be combined; repeated modifiers in `modifiers`
|
||||
will be ignored.
|
||||
|
||||
Inlined by the compiler into `:erlang.unique_integer/1`.
|
||||
"""
|
||||
@spec unique_integer([:positive | :monotonic]) :: integer
|
||||
def unique_integer(modifiers \\ []) do
|
||||
:erlang.unique_integer(modifiers)
|
||||
end
|
||||
|
||||
defp normalize_time_unit(:native),
|
||||
do: :native
|
||||
defp normalize_time_unit(:seconds),
|
||||
do: :seconds
|
||||
defp normalize_time_unit(:milliseconds),
|
||||
do: :milli_seconds
|
||||
defp normalize_time_unit(:microseconds),
|
||||
do: :micro_seconds
|
||||
defp normalize_time_unit(:nanoseconds),
|
||||
do: :nano_seconds
|
||||
defp normalize_time_unit(unit) when is_integer(unit) and unit > 0,
|
||||
do: unit
|
||||
|
||||
# TODO: Warn on Elixir 1.5
|
||||
defp normalize_time_unit(erlang_unit)
|
||||
when erlang_unit in [:milli_seconds, :micro_seconds, :nano_seconds] do
|
||||
erlang_unit
|
||||
end
|
||||
|
||||
defp normalize_time_unit(other) do
|
||||
raise ArgumentError,
|
||||
"unsupported time unit. Expected :seconds, :milliseconds, " <>
|
||||
":microseconds, :nanoseconds, or a positive integer, " <>
|
||||
"got #{inspect other}"
|
||||
end
|
||||
end
|
||||
|
||||
+148
-294
@@ -1,62 +1,60 @@
|
||||
defmodule Task do
|
||||
@moduledoc """
|
||||
Conveniences for spawning and awaiting tasks.
|
||||
Conveniences for spawning and awaiting for tasks.
|
||||
|
||||
Tasks are processes meant to execute one particular
|
||||
action throughout their lifetime, often with little or no
|
||||
action throughout their life-cycle, often with little or no
|
||||
communication with other processes. The most common use case
|
||||
for tasks is to convert sequential code into concurrent code
|
||||
by computing a value asynchronously:
|
||||
for tasks is to compute a value asynchronously:
|
||||
|
||||
task = Task.async(fn -> do_some_work() end)
|
||||
res = do_some_other_work()
|
||||
res + Task.await(task)
|
||||
|
||||
Tasks spawned with `async` can be awaited on by their caller
|
||||
process (and only their caller) as shown in the example above.
|
||||
Tasks spawned with `async` can be awaited on by its caller
|
||||
process (and only its caller) as shown in the example above.
|
||||
They are implemented by spawning a process that sends a message
|
||||
to the caller once the given computation is performed.
|
||||
|
||||
Besides `async/1` and `await/2`, tasks can also be
|
||||
started as part of a supervision tree and dynamically spawned
|
||||
on remote nodes. We will explore all three scenarios next.
|
||||
started as part of supervision trees and dynamically spawned
|
||||
in remote nodes. We will explore all three scenarios next.
|
||||
|
||||
## async and await
|
||||
|
||||
One of the common uses of tasks is to convert sequential code
|
||||
into concurrent code with `Task.async/1` while keeping its semantics.
|
||||
When invoked, a new process will be created, linked and monitored
|
||||
by the caller. Once the task action finishes, a message will be sent
|
||||
to the caller with the result.
|
||||
The most common way to spawn a task is with `Task.async/1`. A new
|
||||
process will be created, linked and monitored by the caller. Once
|
||||
the task action finishes, a message will be sent to the caller
|
||||
with the result.
|
||||
|
||||
`Task.await/2` is used to read the message sent by the task.
|
||||
`await` will check the monitor setup by the call to `async/1` to
|
||||
verify if the process exited for any abnormal reason (or in case
|
||||
exits are being trapped by the caller).
|
||||
|
||||
There are two important things to consider when using `async`:
|
||||
There are two important things to consider when using async:
|
||||
|
||||
1. If you are using async tasks, you must await a reply
|
||||
1. If you are using async tasks, you must await for a reply
|
||||
as they are *always* sent. If you are not expecting a reply,
|
||||
consider using `Task.start_link/1` detailed below.
|
||||
consider using `Task.start_link/1` detailed below
|
||||
|
||||
2. async tasks link the caller and the spawned process. This
|
||||
means that, if the caller crashes, the task will crash
|
||||
too and vice-versa. This is on purpose: if the process
|
||||
too and vice-versa. This is on purpose, if the process
|
||||
meant to receive the result no longer exists, there is
|
||||
no purpose in completing the computation.
|
||||
|
||||
If this is not desired, use `Task.start/1` or consider starting
|
||||
the task under a `Task.Supervisor` using `async_nolink` or
|
||||
`start_child`.
|
||||
no purpose in computing the result until the end. If this
|
||||
is not desired, consider using `Task.start_link/1` as well
|
||||
|
||||
`Task.yield/2` is an alternative to `await/2` where the caller will
|
||||
temporarily block, waiting until the task replies or crashes. If the
|
||||
result does not arrive within the timeout, it can be called again at a
|
||||
later moment. This allows checking for the result of a task multiple
|
||||
times. If a reply does not arrive within the desired time,
|
||||
`Task.shutdown/2` can be used to stop the task.
|
||||
temporarily block waiting for a task's result. If the result does not
|
||||
arrive within the timeout it can be called again at later moment. This
|
||||
allows checking for the result of a task multiple times or to handle
|
||||
a timeout. If a reply does not arrive within the desired time, and the
|
||||
caller is not going exit, `Task.shutdown/2` can be used to stop the task.
|
||||
|
||||
## Supervised tasks
|
||||
|
||||
It is also possible to spawn a task under a supervisor
|
||||
It is also possible to spawn a task inside a supervision tree
|
||||
with `start_link/1` and `start_link/3`:
|
||||
|
||||
Task.start_link(fn -> IO.puts "ok" end)
|
||||
@@ -75,7 +73,7 @@ defmodule Task do
|
||||
the result expected by supervision trees).
|
||||
|
||||
By default, most supervision strategies will try to restart
|
||||
a worker after it exits regardless of the reason. If you design the
|
||||
a worker after it exits regardless of reason. If you design the
|
||||
task to terminate normally (as in the example with `IO.puts/2` above),
|
||||
consider passing `restart: :transient` in the options to `worker/3`.
|
||||
|
||||
@@ -113,17 +111,18 @@ defmodule Task do
|
||||
# Do something
|
||||
end) |> Task.await()
|
||||
|
||||
Finally, check `Task.Supervisor` for other supported operations.
|
||||
Finally, check `Task.Supervisor` for other operations supported by the
|
||||
Task supervisor.
|
||||
|
||||
## Distributed tasks
|
||||
|
||||
Since Elixir provides a Task supervisor, it is easy to use one
|
||||
to dynamically spawn tasks across nodes:
|
||||
Since Elixir provides a Task supervisor, it is easy to use a task
|
||||
supervisor to dynamically spawn tasks across nodes:
|
||||
|
||||
# On the remote node
|
||||
# In the remote node
|
||||
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
|
||||
|
||||
# On the client
|
||||
# In the client
|
||||
Task.Supervisor.async({MyApp.DistSupervisor, :remote@local},
|
||||
MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
@@ -132,23 +131,21 @@ defmodule Task do
|
||||
works with anonymous functions. That's because anonymous functions expect
|
||||
the same module version to exist on all involved nodes. Check the `Agent` module
|
||||
documentation for more information on distributed processes as the limitations
|
||||
described there apply to the whole ecosystem.
|
||||
described in the agents documentation apply to the whole ecosystem.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
The Task struct.
|
||||
|
||||
It contains these fields:
|
||||
It contains two fields:
|
||||
|
||||
* `:pid` - the PID of the task process; `nil` if the task does
|
||||
not use a task process
|
||||
* `:pid` - the process reference of the task process; `nil` if the task does
|
||||
not use a task process.
|
||||
|
||||
* `:ref` - the task monitor reference
|
||||
|
||||
* `:owner` - the PID of the process that started the task
|
||||
|
||||
"""
|
||||
defstruct pid: nil, ref: nil, owner: nil
|
||||
defstruct pid: nil, ref: nil
|
||||
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
@@ -172,7 +169,7 @@ defmodule Task do
|
||||
Starts a task.
|
||||
|
||||
This is only used when the task is used for side-effects
|
||||
(i.e. no interest in the returned result) and it should not
|
||||
(i.e. no interest in its return result) and it should not
|
||||
be linked to the current process.
|
||||
"""
|
||||
@spec start(fun) :: {:ok, pid}
|
||||
@@ -184,7 +181,7 @@ defmodule Task do
|
||||
Starts a task.
|
||||
|
||||
This is only used when the task is used for side-effects
|
||||
(i.e. no interest in the returned result) and it should not
|
||||
(i.e. no interest in its return result) and it should not
|
||||
be linked to the current process.
|
||||
"""
|
||||
@spec start(module, atom, [term]) :: {:ok, pid}
|
||||
@@ -193,7 +190,7 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that must be awaited on.
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
This function spawns a process that is linked to and monitored
|
||||
by the caller process. A `Task` struct is returned containing
|
||||
@@ -202,7 +199,10 @@ defmodule Task do
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
See also `async/3`.
|
||||
## Task's message format
|
||||
|
||||
The reply sent by the task will be in the format `{ref, msg}`,
|
||||
where `ref` is the monitoring reference held by the task.
|
||||
"""
|
||||
@spec async(fun) :: t
|
||||
def async(fun) do
|
||||
@@ -210,76 +210,27 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that must be awaited on.
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
A `Task` struct is returned containing the relevant information.
|
||||
Developers must eventually call `Task.await/2` or `Task.yield/2`
|
||||
followed by `Task.shutdown/2` on the returned task.
|
||||
This function spawns a process that is linked to and monitored
|
||||
by the caller process. A `Task` struct is returned containing
|
||||
the relevant information.
|
||||
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
## Linking
|
||||
## Task's message format
|
||||
|
||||
This function spawns a process that is linked to and monitored
|
||||
by the caller process. The linking part is important because it
|
||||
aborts the task if the parent process dies. It also guarantees
|
||||
the code before async/await has the same properties after you
|
||||
add the async call. For example, imagine you have this:
|
||||
|
||||
x = heavy_fun()
|
||||
y = some_fun()
|
||||
x + y
|
||||
|
||||
Now you want to make the `heavy_fun()` async:
|
||||
|
||||
x = Task.async(&heavy_fun/0)
|
||||
y = some_fun()
|
||||
Task.await(x) + y
|
||||
|
||||
As before, if `heavy_fun/0` fails, the whole computation will
|
||||
fail, including the parent process. If you don't want the task
|
||||
to fail then you must change the `heavy_fun/0` code in the
|
||||
same way you would achieve it if you didn't have the async call.
|
||||
For example, to either return `{:ok, val} | :error` results or,
|
||||
in more extreme cases, by using `try/rescue`. In other words,
|
||||
an asynchronous task should be thought of as an extension of a
|
||||
process rather than a mechanism to isolate it from all errors.
|
||||
|
||||
If you don't want to link the caller to the task, then you
|
||||
must use a supervised task with `Task.Supervisor` and call
|
||||
`Task.Supervisor.async_nolink/2`.
|
||||
|
||||
In any case, avoid any of the following:
|
||||
|
||||
* Setting `:trap_exit` to `true` - trapping exits should be
|
||||
used only in special circumstances as it would make your
|
||||
process immune to not only exits from the task but from
|
||||
any other processes.
|
||||
|
||||
Moreover, even when trapping exists, calling `await` will
|
||||
still exit if the task has terminated without sending its
|
||||
result back.
|
||||
|
||||
* Unlinking the task process started with `async`/`await`.
|
||||
If you unlink the processes and the task does not belong
|
||||
to any supervisor, you may leave dangling tasks in case
|
||||
the parent dies.
|
||||
|
||||
## Message format
|
||||
|
||||
The reply sent by the task will be in the format `{ref, result}`,
|
||||
where `ref` is the monitor reference held by the task struct
|
||||
and `result` is the return value of the task function.
|
||||
The reply sent by the task will be in the format `{ref, msg}`,
|
||||
where `ref` is the monitoring reference held by the task.
|
||||
"""
|
||||
@spec async(module, atom, [term]) :: t
|
||||
def async(mod, fun, args) do
|
||||
mfa = {mod, fun, args}
|
||||
owner = self()
|
||||
pid = Task.Supervised.spawn_link(owner, get_info(owner), mfa)
|
||||
pid = :proc_lib.spawn_link(Task.Supervised, :async, [self, get_info(self), mfa])
|
||||
ref = Process.monitor(pid)
|
||||
send(pid, {owner, ref})
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
send(pid, {self(), ref})
|
||||
%Task{pid: pid, ref: ref}
|
||||
end
|
||||
|
||||
defp get_info(self) do
|
||||
@@ -297,33 +248,20 @@ defmodule Task do
|
||||
of `5000`. In case the task process dies, this function will
|
||||
exit with the same reason as the task.
|
||||
|
||||
If the timeout is exceeded, `await` will exit; however,
|
||||
If the timeout is exceeded, `await` will exit, however,
|
||||
the task will continue to run. When the calling process exits, its
|
||||
exit signal will terminate the task if it is not trapping exits.
|
||||
exit signal will close the task if it is not trapping exits.
|
||||
|
||||
This function assumes the task's monitor is still active or the monitor's
|
||||
`:DOWN` message is in the message queue. If it has been demonitored, or the
|
||||
message already received, this function will wait for the duration of the
|
||||
message already received, this function may wait for the duration of the
|
||||
timeout awaiting the message.
|
||||
|
||||
This function can only be called once for any given task. If you want
|
||||
to be able to check multiple times if a long-running task has finished
|
||||
its computation, use `yield/2` instead.
|
||||
|
||||
## Compatibility with OTP behaviours
|
||||
|
||||
It is not recommended to `await` a long-running task inside an OTP
|
||||
behaviour such as `GenServer`. Instead, you should match on the message
|
||||
coming from a task inside your `handle_info` callback.
|
||||
This function will always demonitor the task and so the task can not be used
|
||||
again. To await the task's reply multiple times use `yield/2` instead.
|
||||
"""
|
||||
@spec await(t, timeout) :: term | no_return
|
||||
def await(task, timeout \\ 5000)
|
||||
|
||||
def await(%Task{owner: owner} = task, _) when owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def await(%Task{ref: ref} = task, timeout) do
|
||||
def await(%Task{ref: ref}=task, timeout \\ 5000) do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -337,74 +275,101 @@ defmodule Task do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
# TODO: Remove on 2.0
|
||||
def find(tasks, msg) do
|
||||
IO.warn "Task.find/2 is deprecated, please match on the message directly"
|
||||
do_find(tasks, msg)
|
||||
end
|
||||
@doc """
|
||||
Receives a group of tasks and a message and finds
|
||||
a task that matches the given message.
|
||||
|
||||
defp do_find(tasks, {ref, reply}) when is_reference(ref) do
|
||||
This function returns a tuple with the returned value
|
||||
in case the message matches a task that exited with
|
||||
success alongside the matching task. It raises in case
|
||||
the found task failed or `nil` if no task was found.
|
||||
|
||||
This function is useful in situations where multiple
|
||||
tasks are spawned and their results are collected
|
||||
later on. For example, a `GenServer` can spawn tasks,
|
||||
store the tasks in a list and later use `Task.find/2`
|
||||
to see if incoming messages are from any of the tasks.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule TaskFinder do
|
||||
def run do
|
||||
task1 = Task.async fn -> :timer.sleep(1000); 1 end
|
||||
task2 = Task.async fn -> :timer.sleep(5000); 2 end
|
||||
await [task1, task2]
|
||||
end
|
||||
|
||||
# Be careful, this will receive all messages sent
|
||||
# to this process. It will return the first task
|
||||
# reply and the list of tasks that came second.
|
||||
def await(tasks) do
|
||||
receive do
|
||||
message ->
|
||||
case Task.find(tasks, message) do
|
||||
{reply, task} ->
|
||||
{reply, List.delete(tasks, task)}
|
||||
nil ->
|
||||
await(tasks)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
TaskFinder.run
|
||||
"""
|
||||
@spec find([t], any) :: {term, t} | nil | no_return
|
||||
def find(tasks, msg)
|
||||
|
||||
def find(tasks, {ref, reply}) when is_reference(ref) do
|
||||
Enum.find_value tasks, fn
|
||||
%Task{ref: ^ref} = task ->
|
||||
%Task{ref: task_ref} = t when ref == task_ref ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{reply, task}
|
||||
{reply, t}
|
||||
%Task{} ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp do_find(tasks, {:DOWN, ref, _, proc, reason} = msg) when is_reference(ref) do
|
||||
find = fn %Task{ref: task_ref} -> task_ref == ref end
|
||||
if Enum.find(tasks, find) do
|
||||
exit({reason(reason, proc), {__MODULE__, :find, [tasks, msg]}})
|
||||
def find(tasks, {:DOWN, ref, _, proc, reason} = msg) when is_reference(ref) do
|
||||
find = fn(%Task{ref: task_ref}) -> task_ref == ref end
|
||||
case Enum.find(tasks, find) do
|
||||
%Task{} ->
|
||||
exit({reason(reason, proc), {__MODULE__, :find, [tasks, msg]}})
|
||||
nil ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
defp do_find(_tasks, _msg) do
|
||||
def find(_tasks, _msg) do
|
||||
nil
|
||||
end
|
||||
|
||||
@doc """
|
||||
Temporarily blocks the current process waiting for a task reply.
|
||||
Yields, temporarily, for a task reply.
|
||||
|
||||
Returns `{:ok, reply}` if the reply is received, `nil` if
|
||||
no reply has arrived, or `{:exit, reason}` if the task has already
|
||||
exited. Keep in mind that normally a task failure also causes
|
||||
the process owning the task to exit. Therefore this function can
|
||||
return `{:exit, reason}` only if
|
||||
|
||||
* the task process exited with the reason `:normal`
|
||||
* it isn't linked to the caller
|
||||
* the caller is trapping exits
|
||||
Returns `{:ok, reply}` if the reply is received.
|
||||
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
of `5000`. If the time runs out before a message from
|
||||
the task is received, this function will return `nil`
|
||||
of `5000`. In case of the timeout, this function will return `nil`
|
||||
and the monitor will remain active. Therefore `yield/2` can be
|
||||
called multiple times on the same task.
|
||||
|
||||
This function assumes the task's monitor is still active or the
|
||||
monitor's `:DOWN` message is in the message queue. If it has been
|
||||
demonitored or the message already received, this function will wait
|
||||
for the duration of the timeout awaiting the message.
|
||||
In case the task process dies, this function will exit with the
|
||||
same reason as the task.
|
||||
|
||||
This function assumes the task's monitor is still active or the monitor's
|
||||
`:DOWN` message is in the message queue. If it has been demonitored, or the
|
||||
message already received, this function wait for the duration of the timeout
|
||||
awaiting the message.
|
||||
"""
|
||||
@spec yield(t, timeout) :: {:ok, term} | {:exit, term} | nil
|
||||
def yield(task, timeout \\ 5_000)
|
||||
|
||||
def yield(%Task{owner: owner} = task, _) when owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def yield(%Task{ref: ref} = task, timeout) do
|
||||
@spec yield(t, timeout) :: {:ok, term} | nil
|
||||
def yield(%Task{ref: ref} = task, timeout \\ 5_000) do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
{:ok, reply}
|
||||
{:DOWN, ^ref, _, proc, :noconnection} ->
|
||||
exit({reason(:noconnection, proc), {__MODULE__, :yield, [task, timeout]}})
|
||||
{:DOWN, ^ref, _, _, reason} ->
|
||||
{:exit, reason}
|
||||
{:DOWN, ^ref, _, proc, reason} ->
|
||||
exit({reason(reason, proc), {__MODULE__, :yield, [task, timeout]}})
|
||||
after
|
||||
timeout ->
|
||||
nil
|
||||
@@ -412,146 +377,41 @@ defmodule Task do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Yields to multiple tasks in the given time interval.
|
||||
|
||||
This function receives a list of tasks and waits for their
|
||||
replies in the given time interval. It returns a list
|
||||
of tuples of two elements, with the task as the first element
|
||||
and the yielded result as the second.
|
||||
|
||||
Similarly to `yield/2`, each task's result will be
|
||||
|
||||
* `{:ok, term}` if the task has successfully reported its
|
||||
result back in the given time interval
|
||||
* `{:exit, reason}` if the task has died
|
||||
* `nil` if the task keeps running past the timeout
|
||||
|
||||
Check `yield/2` for more information.
|
||||
|
||||
## Example
|
||||
|
||||
`Task.yield_many/2` allows developers to spawn multiple tasks
|
||||
and retrieve the results received in a given timeframe.
|
||||
If we combine it with `Task.shutdown/2`, it allows us to gather
|
||||
those results and cancel the tasks that have not replied in time.
|
||||
|
||||
Let's see an example.
|
||||
|
||||
tasks =
|
||||
for i <- 1..10 do
|
||||
Task.async(fn ->
|
||||
:timer.sleep(i * 1000)
|
||||
i
|
||||
end)
|
||||
end
|
||||
|
||||
tasks_with_results = Task.yield_many(tasks, 5000)
|
||||
|
||||
results = Enum.map(tasks_with_results, fn {task, res} ->
|
||||
# Shutdown the tasks that did not reply nor exit
|
||||
res || Task.shutdown(task, :brutal_kill)
|
||||
end)
|
||||
|
||||
# Here we are matching only on {:ok, value} and
|
||||
# ignoring {:exit, _} (crashed tasks) and `nil` (no replies)
|
||||
for {:ok, value} <- results do
|
||||
IO.inspect value
|
||||
end
|
||||
|
||||
In the example above, we create tasks that sleep from 1
|
||||
up to 10 seconds and return the amount of seconds they slept.
|
||||
If you execute the code all at once, you should see 1 up to 5
|
||||
printed, as those were the tasks that have replied in the
|
||||
given time. All other tasks will have been shut down using
|
||||
the `Task.shutdown/2` call.
|
||||
"""
|
||||
@spec yield_many([t], timeout) :: [{t, {:ok, term} | {:exit, term} | nil}]
|
||||
def yield_many(tasks, timeout \\ 5000) do
|
||||
timeout_ref = make_ref()
|
||||
timer_ref = Process.send_after(self(), timeout_ref, timeout)
|
||||
try do
|
||||
yield_many(tasks, timeout_ref, :infinity)
|
||||
catch
|
||||
{:noconnection, reason} ->
|
||||
exit({reason, {__MODULE__, :yield_many, [tasks, timeout]}})
|
||||
after
|
||||
Process.cancel_timer(timer_ref)
|
||||
receive do: (^timeout_ref -> :ok), after: (0 -> :ok)
|
||||
end
|
||||
end
|
||||
|
||||
defp yield_many([%Task{ref: ref, owner: owner}=task | rest], timeout_ref, timeout) do
|
||||
if owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
[{task, {:ok, reply}} | yield_many(rest, timeout_ref, timeout)]
|
||||
|
||||
{:DOWN, ^ref, _, proc, :noconnection} ->
|
||||
throw({:noconnection, reason(:noconnection, proc)})
|
||||
|
||||
{:DOWN, ^ref, _, _, reason} ->
|
||||
[{task, {:exit, reason}} | yield_many(rest, timeout_ref, timeout)]
|
||||
|
||||
^timeout_ref ->
|
||||
[{task, nil} | yield_many(rest, timeout_ref, 0)]
|
||||
|
||||
after
|
||||
timeout ->
|
||||
[{task, nil} | yield_many(rest, timeout_ref, 0)]
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
defp yield_many([], _timeout_ref, _timeout) do
|
||||
[]
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unlinks and shuts down the task, and then checks for a reply.
|
||||
Unlinks and shutdowns the task, and then checks for a reply.
|
||||
|
||||
Returns `{:ok, reply}` if the reply is received while shutting down the task,
|
||||
`{:exit, reason}` if the task died, otherwise `nil`.
|
||||
otherwise `nil`.
|
||||
|
||||
The shutdown method is either a timeout or `:brutal_kill`. In case
|
||||
The shutdown method is either a timeout or `:brutal_kill`. In the case
|
||||
of a `timeout`, a `:shutdown` exit signal is sent to the task process
|
||||
and if it does not exit within the timeout, it is killed. With `:brutal_kill`
|
||||
the task is killed straight away. In case the task terminates abnormally
|
||||
(possibly killed by another process), this function will exit with the same reason.
|
||||
and if it does not exit within the timeout it is killed. With `:brutal_kill`
|
||||
the task is killed straight away. In the case that the task exits abnormal,
|
||||
or a timeout shutdown kills the task, this function will exit with the same
|
||||
reason.
|
||||
|
||||
It is not required to call this function when terminating the caller, unless
|
||||
exiting with reason `:normal` or if the task is trapping exits. If the caller is
|
||||
exiting with a reason other than `:normal` and the task is not trapping exits, the
|
||||
exiting with reason `:normal` or the task is trapping exits. If the caller is
|
||||
exiting with a reason other than `:normal` and the task is not trapping exits the
|
||||
caller's exit signal will stop the task. The caller can exit with reason
|
||||
`:shutdown` to shutdown all of its linked processes, including tasks, that
|
||||
are not trapping exits without generating any log messages.
|
||||
`:shutdown` to shutdown linked processes, such as tasks, that are not trapping
|
||||
exits without generating any log messages.
|
||||
|
||||
This function assumes the task's monitor is still active or the monitor's
|
||||
`:DOWN` message is in the message queue. If it has been demonitored, or the
|
||||
message already received, this function will block forever awaiting the message.
|
||||
"""
|
||||
@spec shutdown(t, timeout | :brutal_kill) :: {:ok, term} | {:exit, term} | nil
|
||||
@spec shutdown(t, timeout | :brutal_kill) :: {:ok, term} | nil
|
||||
def shutdown(task, shutdown \\ 5_000)
|
||||
|
||||
def shutdown(%Task{pid: nil} = task, _) do
|
||||
raise ArgumentError, "task #{inspect task} does not have an associated task process"
|
||||
end
|
||||
|
||||
def shutdown(%Task{owner: owner} = task, _) when owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
raise ArgumentError, "task #{inspect task} does not have an associated task process."
|
||||
end
|
||||
|
||||
def shutdown(%Task{pid: pid} = task, :brutal_kill) do
|
||||
exit(pid, :kill)
|
||||
|
||||
case shutdown_receive(task, :brutal_kill, :infinity) do
|
||||
{:down, proc, :noconnection} ->
|
||||
exit({reason(:noconnection, proc), {__MODULE__, :shutdown, [task, :brutal_kill]}})
|
||||
{:down, _, reason} ->
|
||||
{:exit, reason}
|
||||
{:error, reason} ->
|
||||
exit({reason, {__MODULE__, :shutdown, [task, :brutal_kill]}})
|
||||
result ->
|
||||
result
|
||||
end
|
||||
@@ -560,10 +420,8 @@ defmodule Task do
|
||||
def shutdown(%Task{pid: pid} = task, timeout) do
|
||||
exit(pid, :shutdown)
|
||||
case shutdown_receive(task, :shutdown, timeout) do
|
||||
{:down, proc, :noconnection} ->
|
||||
exit({reason(:noconnection, proc), {__MODULE__, :shutdown, [task, timeout]}})
|
||||
{:down, _, reason} ->
|
||||
{:exit, reason}
|
||||
{:error, reason} ->
|
||||
exit({reason, {__MODULE__, :shutdown, [task, timeout]}})
|
||||
result ->
|
||||
result
|
||||
end
|
||||
@@ -604,7 +462,7 @@ defmodule Task do
|
||||
{:DOWN, ^ref, _, _, :killed} when type == :brutal_kill ->
|
||||
flush_reply(ref)
|
||||
{:DOWN, ^ref, _, proc, reason} ->
|
||||
flush_reply(ref) || {:down, proc, reason}
|
||||
flush_reply(ref) || {:error, reason(reason, proc)}
|
||||
after
|
||||
timeout ->
|
||||
Process.exit(task.pid, :kill)
|
||||
@@ -619,8 +477,4 @@ defmodule Task do
|
||||
0 -> nil
|
||||
end
|
||||
end
|
||||
|
||||
defp invalid_owner_error(task) do
|
||||
"task #{inspect task} must be queried from the owner but was queried from #{inspect self()}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
defmodule Task.Supervised do
|
||||
@moduledoc false
|
||||
|
||||
@ref_timeout 5_000
|
||||
|
||||
def start(info, fun) do
|
||||
{:ok, :proc_lib.spawn(__MODULE__, :noreply, [info, fun])}
|
||||
end
|
||||
@@ -11,43 +9,24 @@ defmodule Task.Supervised do
|
||||
{:ok, :proc_lib.spawn_link(__MODULE__, :noreply, [info, fun])}
|
||||
end
|
||||
|
||||
def start_link(caller, link, info, fun) do
|
||||
{:ok, spawn_link(caller, link, info, fun)}
|
||||
def start_link(caller, info, fun) do
|
||||
:proc_lib.start_link(__MODULE__, :reply, [caller, info, fun])
|
||||
end
|
||||
|
||||
def spawn_link(caller, link \\ :nolink, info, fun) do
|
||||
:proc_lib.spawn_link(__MODULE__, :reply, [caller, link, info, fun])
|
||||
end
|
||||
|
||||
def reply(caller, link, info, mfa) do
|
||||
def async(caller, info, mfa) do
|
||||
initial_call(mfa)
|
||||
case link do
|
||||
:link ->
|
||||
try do
|
||||
Process.link(caller)
|
||||
catch
|
||||
:error, :noproc ->
|
||||
exit({:shutdown, :noproc})
|
||||
end
|
||||
reply(caller, nil, @ref_timeout, info, mfa)
|
||||
:monitor ->
|
||||
mref = Process.monitor(caller)
|
||||
reply(caller, mref, @ref_timeout, info, mfa)
|
||||
:nolink ->
|
||||
reply(caller, nil, :infinity, info, mfa)
|
||||
end
|
||||
ref = receive do: ({^caller, ref} -> ref)
|
||||
send caller, {ref, do_apply(info, mfa)}
|
||||
end
|
||||
|
||||
defp reply(caller, mref, timeout, info, mfa) do
|
||||
receive do
|
||||
{^caller, ref} ->
|
||||
_ = if mref, do: Process.demonitor(mref, [:flush])
|
||||
send caller, {ref, do_apply(info, mfa)}
|
||||
{:DOWN, ^mref, _, _, reason} when is_reference(mref) ->
|
||||
exit({:shutdown, reason})
|
||||
after
|
||||
def reply(caller, info, mfa) do
|
||||
initial_call(mfa)
|
||||
:erlang.link(caller)
|
||||
:proc_lib.init_ack({:ok, self()})
|
||||
|
||||
ref =
|
||||
# There is a race condition on this operation when working across
|
||||
# node that manifests if a "Task.Supervisor.async/2" call is made
|
||||
# node that manifests if a "Task.Supervisor.async/1" call is made
|
||||
# while the supervisor is busy spawning previous tasks.
|
||||
#
|
||||
# Imagine the following workflow:
|
||||
@@ -62,12 +41,13 @@ defmodule Task.Supervised do
|
||||
# Given no work is done in the client between the task start and
|
||||
# sending the reference, 5000 should be enough to not raise false
|
||||
# negatives unless the nodes are indeed not available.
|
||||
#
|
||||
# The same situation could occur with "Task.Supervisor.async_nolink/2",
|
||||
# except a monitor is used instead of a link.
|
||||
timeout ->
|
||||
exit(:timeout)
|
||||
end
|
||||
receive do
|
||||
{^caller, ref} -> ref
|
||||
after
|
||||
5000 -> exit(:timeout)
|
||||
end
|
||||
|
||||
send caller, {ref, do_apply(info, mfa)}
|
||||
end
|
||||
|
||||
def noreply(info, mfa) do
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
defmodule Task.Supervisor do
|
||||
@moduledoc """
|
||||
A task supervisor.
|
||||
A tasks supervisor.
|
||||
|
||||
This module defines a supervisor which can be used to dynamically
|
||||
supervise tasks. Behind the scenes, this module is implemented as a
|
||||
@@ -12,7 +12,7 @@ defmodule Task.Supervisor do
|
||||
## Name Registration
|
||||
|
||||
A `Task.Supervisor` is bound to the same name registration rules as a
|
||||
`GenServer`. Read more about them in the `GenServer` docs.
|
||||
`GenServer`. Read more about it in the `GenServer` docs.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -26,8 +26,8 @@ defmodule Task.Supervisor do
|
||||
|
||||
* `:restart` - the restart strategy, may be `:temporary` (the default),
|
||||
`:transient` or `:permanent`. Check `Supervisor.Spec` for more info.
|
||||
Defaults to `:temporary` so tasks aren't automatically restarted when
|
||||
they complete nor in case of crashes;
|
||||
Defaults to temporary as most tasks can't be effectively restarted after
|
||||
a crash;
|
||||
|
||||
* `:shutdown` - `:brutal_kill` if the tasks must be killed directly on shutdown
|
||||
or an integer indicating the timeout value, defaults to 5000 milliseconds;
|
||||
@@ -48,8 +48,7 @@ defmodule Task.Supervisor do
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
The `supervisor` must be a reference as defined in `Task.Supervisor`.
|
||||
The task will still be linked to the caller, see `Task.async/3` for
|
||||
more information and `async_nolink/2` for a non-linked variant.
|
||||
For more information on tasks, check the `Task` module.
|
||||
"""
|
||||
@spec async(Supervisor.supervisor, fun) :: Task.t
|
||||
def async(supervisor, fun) do
|
||||
@@ -60,51 +59,15 @@ defmodule Task.Supervisor do
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
The `supervisor` must be a reference as defined in `Task.Supervisor`.
|
||||
The task will still be linked to the caller, see `Task.async/3` for
|
||||
more information and `async_nolink/2` for a non-linked variant.
|
||||
For more information on tasks, check the `Task` module.
|
||||
"""
|
||||
@spec async(Supervisor.supervisor, module, atom, [term]) :: Task.t
|
||||
def async(supervisor, module, fun, args) do
|
||||
do_async(supervisor, module, fun, args, :link)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
The `supervisor` must be a reference as defined in `Task.Supervisor`.
|
||||
The task won't be linked to the caller, see `Task.async/3` for
|
||||
more information.
|
||||
|
||||
## Compatibility with OTP behaviours
|
||||
|
||||
If you create a task using `async_nolink` inside an OTP behaviour
|
||||
like `GenServer`, you should match on the message coming from the
|
||||
task inside your `handle_info` callback.
|
||||
|
||||
The reply sent by the task will be in the format `{ref, result}`,
|
||||
where `ref` is the monitor reference held by the task struct
|
||||
and `result` is the return value of the task function.
|
||||
|
||||
Keep in mind that, regardless of how the task created with `async_nolink`
|
||||
terminates, the caller's process will always receive a `:DOWN` message
|
||||
with the same `ref` value that is held by the task struct. If the task
|
||||
terminates normally, the reason in the `:DOWN` message will be `:normal`.
|
||||
"""
|
||||
@spec async_nolink(Supervisor.supervisor, fun) :: Task.t
|
||||
def async_nolink(supervisor, fun) do
|
||||
async_nolink(supervisor, :erlang, :apply, [fun, []])
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task that can be awaited on.
|
||||
|
||||
The `supervisor` must be a reference as defined in `Task.Supervisor`.
|
||||
The task won't be linked to the caller, see `Task.async/3` for
|
||||
more information.
|
||||
"""
|
||||
@spec async_nolink(Supervisor.supervisor, module, atom, [term]) :: Task.t
|
||||
def async_nolink(supervisor, module, fun, args) do
|
||||
do_async(supervisor, module, fun, args, :monitor)
|
||||
args = [self, get_info(self), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {self(), ref}
|
||||
%Task{pid: pid, ref: ref}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -112,7 +75,7 @@ defmodule Task.Supervisor do
|
||||
"""
|
||||
@spec terminate_child(Supervisor.supervisor, pid) :: :ok
|
||||
def terminate_child(supervisor, pid) when is_pid(pid) do
|
||||
Supervisor.terminate_child(supervisor, pid)
|
||||
:supervisor.terminate_child(supervisor, pid)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -120,11 +83,11 @@ defmodule Task.Supervisor do
|
||||
"""
|
||||
@spec children(Supervisor.supervisor) :: [pid]
|
||||
def children(supervisor) do
|
||||
for {_, pid, _, _} <- Supervisor.which_children(supervisor), is_pid(pid), do: pid
|
||||
:supervisor.which_children(supervisor) |> Enum.map(&elem(&1, 1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task as a child of the given `supervisor`.
|
||||
Starts a task as child of the given `supervisor`.
|
||||
|
||||
Note that the spawned process is not linked to the caller, but
|
||||
only to the supervisor. This command is useful in case the
|
||||
@@ -137,7 +100,7 @@ defmodule Task.Supervisor do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Starts a task as a child of the given `supervisor`.
|
||||
Starts a task as child of the given `supervisor`.
|
||||
|
||||
Similar to `start_child/2` except the task is specified
|
||||
by the given `module`, `fun` and `args`.
|
||||
@@ -154,14 +117,4 @@ defmodule Task.Supervisor do
|
||||
{:registered_name, name} -> name
|
||||
end}
|
||||
end
|
||||
|
||||
defp do_async(supervisor, module, fun, args, link_type) do
|
||||
owner = self()
|
||||
args = [owner, link_type, get_info(owner), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
if link_type == :link, do: Process.link(pid)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {owner, ref}
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
end
|
||||
end
|
||||
|
||||
+9
-11
@@ -2,14 +2,14 @@ defmodule Tuple do
|
||||
@moduledoc """
|
||||
Functions for working with tuples.
|
||||
|
||||
See also `Kernel.elem/2`, `Kernel.is_tuple/1`,
|
||||
`Kernel.put_elem/3`, and `Kernel.tuple_size/1`.
|
||||
See also `Kernel.elem/2`, `Kernel.is_tuple/1`, `Kernel.put_elem/3`, and
|
||||
`Kernel.tuple_size/1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Creates a new tuple.
|
||||
|
||||
Creates a tuple of `size` containing the
|
||||
Creates a tuple of size `size` containing the
|
||||
given `data` at every position.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -28,9 +28,9 @@ defmodule Tuple do
|
||||
@doc """
|
||||
Inserts an element into a tuple.
|
||||
|
||||
Inserts `value` into `tuple` at the given `index`.
|
||||
Inserts `value` into `tuple` at the given zero-based `index`.
|
||||
Raises an `ArgumentError` if `index` is negative or greater than the
|
||||
length of `tuple`. Index is zero-based.
|
||||
length of `tuple`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -49,9 +49,9 @@ defmodule Tuple do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts an element at the end of a tuple.
|
||||
Inserts an element into the end of a tuple.
|
||||
|
||||
Returns a new tuple with the element appended at the end, and contains
|
||||
Returns a new tuple which has one element more than `tuple`, and contains
|
||||
the elements in `tuple` followed by `value` as the last element.
|
||||
|
||||
Inlined by the compiler.
|
||||
@@ -70,9 +70,9 @@ defmodule Tuple do
|
||||
@doc """
|
||||
Removes an element from a tuple.
|
||||
|
||||
Deletes the element at the given `index` from `tuple`.
|
||||
Deletes the element at the zero-based `index` from `tuple`.
|
||||
Raises an `ArgumentError` if `index` is negative or greater than
|
||||
or equal to the length of `tuple`. Index is zero-based.
|
||||
or equal to the length of `tuple`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -91,8 +91,6 @@ defmodule Tuple do
|
||||
@doc """
|
||||
Converts a tuple to a list.
|
||||
|
||||
Returns a new list with all the tuple elements.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
+133
-308
@@ -1,35 +1,21 @@
|
||||
defmodule URI do
|
||||
@moduledoc """
|
||||
Utilities for working with URIs.
|
||||
|
||||
This module provides functions for working with URIs (for example, parsing
|
||||
URIs or encoding query strings). For reference, most of the functions in this
|
||||
module refer to [RFC 3986](https://tools.ietf.org/html/rfc3986).
|
||||
Utilities for working with and creating URIs.
|
||||
"""
|
||||
|
||||
defstruct scheme: nil, path: nil, query: nil,
|
||||
fragment: nil, authority: nil,
|
||||
userinfo: nil, host: nil, port: nil
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
scheme: nil | binary,
|
||||
path: nil | binary,
|
||||
query: nil | binary,
|
||||
fragment: nil | binary,
|
||||
authority: nil | binary,
|
||||
userinfo: nil | binary,
|
||||
host: nil | binary,
|
||||
port: nil | :inet.port_number,
|
||||
}
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
import Bitwise
|
||||
|
||||
@doc """
|
||||
Returns the default port for a given scheme.
|
||||
|
||||
If the scheme is unknown to the `URI` module, this function returns
|
||||
`nil`. The default port for any scheme can be configured globally
|
||||
via `default_port/2`.
|
||||
If the scheme is unknown to URI, returns `nil`.
|
||||
Any scheme may be registered via `default_port/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -40,35 +26,27 @@ defmodule URI do
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec default_port(binary) :: nil | pos_integer
|
||||
def default_port(scheme) when is_binary(scheme) do
|
||||
:elixir_config.get({:uri, scheme})
|
||||
end
|
||||
|
||||
@doc """
|
||||
Registers the default port `port` for the given `scheme`.
|
||||
|
||||
After this function is called, `port` will be returned by
|
||||
`default_port/1` for the given scheme `scheme`. Note that this function
|
||||
changes the default port for the given `scheme` *globally*, meaning for
|
||||
every application.
|
||||
Registers a scheme with a default port.
|
||||
|
||||
It is recommended for this function to be invoked in your
|
||||
application's start callback in case you want to register
|
||||
application start callback in case you want to register
|
||||
new URIs.
|
||||
"""
|
||||
@spec default_port(binary, pos_integer) :: :ok
|
||||
def default_port(scheme, port) when is_binary(scheme) and is_integer(port) and port > 0 do
|
||||
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
|
||||
:elixir_config.put({:uri, scheme}, port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes an enumerable into a query string.
|
||||
|
||||
Takes an enumerable that enumerates as a list of two-element
|
||||
tuples (e.g., a map or a keyword list) and returns a string
|
||||
in the form of `key1=value1&key2=value2...` where keys and
|
||||
values are URL encoded as per `encode_www_form/1`.
|
||||
Takes an enumerable (containing a sequence of two-item tuples)
|
||||
and returns a string of the form "key1=value1&key2=value2..." where
|
||||
keys and values are URL encoded as per `encode/2`.
|
||||
|
||||
Keys and values can be any term that implements the `String.Chars`
|
||||
protocol, except lists which are explicitly forbidden.
|
||||
@@ -79,39 +57,15 @@ defmodule URI do
|
||||
iex> URI.encode_query(hd)
|
||||
"bar=2&foo=1"
|
||||
|
||||
iex> query = %{"key" => "value with spaces"}
|
||||
iex> URI.encode_query(query)
|
||||
"key=value+with+spaces"
|
||||
|
||||
iex> URI.encode_query %{key: [:a, :list]}
|
||||
** (ArgumentError) encode_query/1 values cannot be lists, got: [:a, :list]
|
||||
|
||||
"""
|
||||
@spec encode_query(term) :: binary
|
||||
def encode_query(enumerable) do
|
||||
Enum.map_join(enumerable, "&", &encode_kv_pair/1)
|
||||
end
|
||||
|
||||
defp encode_kv_pair({key, _}) when is_list(key) do
|
||||
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect key}"
|
||||
end
|
||||
|
||||
defp encode_kv_pair({_, value}) when is_list(value) do
|
||||
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect value}"
|
||||
end
|
||||
|
||||
defp encode_kv_pair({key, value}) do
|
||||
encode_www_form(Kernel.to_string(key)) <>
|
||||
"=" <> encode_www_form(Kernel.to_string(value))
|
||||
end
|
||||
def encode_query(l), do: Enum.map_join(l, "&", &pair/1)
|
||||
|
||||
@doc """
|
||||
Decodes a query string into a map.
|
||||
Decodes a query string into a dictionary (by default uses a map).
|
||||
|
||||
Given a query string of the form of `key1=value1&key2=value2...`, this
|
||||
function inserts each key-value pair in the query string as one entry in the
|
||||
given `map`. Keys and values in the resulting map will be binaries. Keys and
|
||||
values will be percent-unescaped.
|
||||
Given a query string of the form "key1=value1&key2=value2...", produces a
|
||||
map with one entry for each key-value pair. Each key and value will be a
|
||||
binary. Keys and values will be percent-unescaped.
|
||||
|
||||
Use `query_decoder/1` if you want to iterate over each value manually.
|
||||
|
||||
@@ -120,119 +74,82 @@ defmodule URI do
|
||||
iex> URI.decode_query("foo=1&bar=2")
|
||||
%{"bar" => "2", "foo" => "1"}
|
||||
|
||||
iex> URI.decode_query("percent=oh+yes%21", %{"starting" => "map"})
|
||||
%{"percent" => "oh yes!", "starting" => "map"}
|
||||
|
||||
"""
|
||||
@spec decode_query(binary, map) :: map
|
||||
def decode_query(query, map \\ %{})
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
def decode_query(query, %{__struct__: _} = dict) when is_binary(query) do
|
||||
IO.warn "URI.decode_query/2 is deprecated, please use URI.decode_query/1"
|
||||
decode_query_into_dict(query, dict)
|
||||
end
|
||||
|
||||
def decode_query(query, map) when is_binary(query) and is_map(map) do
|
||||
decode_query_into_map(query, map)
|
||||
end
|
||||
|
||||
# TODO: Remove on 2.0
|
||||
def decode_query(query, dict) when is_binary(query) do
|
||||
IO.warn "URI.decode_query/2 is deprecated, please use URI.decode_query/1"
|
||||
decode_query_into_dict(query, dict)
|
||||
end
|
||||
|
||||
defp decode_query_into_map(query, map) do
|
||||
case decode_next_query_pair(query) do
|
||||
nil ->
|
||||
map
|
||||
{{key, value}, rest} ->
|
||||
decode_query_into_map(rest, Map.put(map, key, value))
|
||||
end
|
||||
end
|
||||
|
||||
defp decode_query_into_dict(query, dict) do
|
||||
case decode_next_query_pair(query) do
|
||||
nil ->
|
||||
dict
|
||||
{{key, value}, rest} ->
|
||||
decode_query_into_dict(rest, Dict.put(dict, key, value))
|
||||
def decode_query(q, dict \\ %{}) when is_binary(q) do
|
||||
case do_decode_query(q) do
|
||||
nil -> dict
|
||||
{{k, v}, q} -> decode_query(q, Dict.put(dict, k, v))
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a stream of two-element tuples representing key-value pairs in the
|
||||
given `query`.
|
||||
|
||||
Key and value in each tuple will be binaries and will be percent-unescaped.
|
||||
Returns an iterator function over the query string that decodes
|
||||
the query string in steps.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.to_list()
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map(&(&1))
|
||||
[{"foo", "1"}, {"bar", "2"}]
|
||||
|
||||
"""
|
||||
@spec query_decoder(binary) :: Enumerable.t
|
||||
def query_decoder(query) when is_binary(query) do
|
||||
Stream.unfold(query, &decode_next_query_pair/1)
|
||||
def query_decoder(q) when is_binary(q) do
|
||||
Stream.unfold(q, &do_decode_query/1)
|
||||
end
|
||||
|
||||
defp decode_next_query_pair("") do
|
||||
defp do_decode_query("") do
|
||||
nil
|
||||
end
|
||||
|
||||
defp decode_next_query_pair(query) do
|
||||
{undecoded_next_pair, rest} =
|
||||
case :binary.split(query, "&") do
|
||||
[next_pair, rest] -> {next_pair, rest}
|
||||
[next_pair] -> {next_pair, ""}
|
||||
defp do_decode_query(q) do
|
||||
{first, next} =
|
||||
case :binary.split(q, "&") do
|
||||
[first, rest] -> {first, rest}
|
||||
[first] -> {first, ""}
|
||||
end
|
||||
|
||||
next_pair =
|
||||
case :binary.split(undecoded_next_pair, "=") do
|
||||
[key, value] -> {decode_www_form(key), decode_www_form(value)}
|
||||
[key] -> {decode_www_form(key), nil}
|
||||
current =
|
||||
case :binary.split(first, "=") do
|
||||
[key, value] ->
|
||||
{decode_www_form(key), decode_www_form(value)}
|
||||
[key] ->
|
||||
{decode_www_form(key), nil}
|
||||
end
|
||||
|
||||
{next_pair, rest}
|
||||
{current, next}
|
||||
end
|
||||
|
||||
defp pair({k, _}) when is_list(k) do
|
||||
raise ArgumentError, "encode_query/1 keys cannot be lists, got: #{inspect k}"
|
||||
end
|
||||
|
||||
defp pair({_, v}) when is_list(v) do
|
||||
raise ArgumentError, "encode_query/1 values cannot be lists, got: #{inspect v}"
|
||||
end
|
||||
|
||||
defp pair({k, v}) do
|
||||
encode_www_form(Kernel.to_string(k)) <>
|
||||
"=" <> encode_www_form(Kernel.to_string(v))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the character is a "reserved" character in a URI.
|
||||
|
||||
Reserved characters are specified in
|
||||
[RFC 3986, section 2.2](http://tools.ietf.org/html/rfc3986#section-2.2).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_reserved?(?+)
|
||||
true
|
||||
|
||||
Reserved characters are specified in [RFC3986, section 2.2](http://tools.ietf.org/html/rfc3986#section-2.2).
|
||||
"""
|
||||
@spec char_reserved?(char) :: boolean
|
||||
def char_reserved?(char) when char in 0..0x10FFFF do
|
||||
char in ':/?#[]@!$&\'()*+,;='
|
||||
def char_reserved?(c) do
|
||||
c in ':/?#[]@!$&\'()*+,;='
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the character is a "unreserved" character in a URI.
|
||||
|
||||
Unreserved characters are specified in
|
||||
[RFC 3986, section 2.3](http://tools.ietf.org/html/rfc3986#section-2.3).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_unreserved?(?_)
|
||||
true
|
||||
|
||||
Unreserved characters are specified in [RFC3986, section 2.3](http://tools.ietf.org/html/rfc3986#section-2.3).
|
||||
"""
|
||||
@spec char_unreserved?(char) :: boolean
|
||||
def char_unreserved?(char) when char in 0..0x10FFFF do
|
||||
char in ?0..?9 or
|
||||
char in ?a..?z or
|
||||
char in ?A..?Z or
|
||||
char in '~_-.'
|
||||
def char_unreserved?(c) do
|
||||
c in ?0..?9 or
|
||||
c in ?a..?z or
|
||||
c in ?A..?Z or
|
||||
c in '~_-.'
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -240,43 +157,27 @@ defmodule URI do
|
||||
|
||||
This is the default used by `URI.encode/2` where both
|
||||
reserved and unreserved characters are kept unescaped.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.char_unescaped?(?{)
|
||||
false
|
||||
|
||||
"""
|
||||
@spec char_unescaped?(char) :: boolean
|
||||
def char_unescaped?(char) when char in 0..0x10FFFF do
|
||||
char_reserved?(char) or char_unreserved?(char)
|
||||
def char_unescaped?(c) do
|
||||
char_reserved?(c) or char_unreserved?(c)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Percent-escapes the given string.
|
||||
|
||||
This function accepts a `predicate` function as an optional argument; if
|
||||
passed, this function will be called with each character (byte) in `str` as
|
||||
its argument and should return `true` if that character should not be escaped
|
||||
and left as is.
|
||||
Percent-escapes a URI.
|
||||
Accepts `predicate` function as an argument to specify if char can be left as is.
|
||||
|
||||
## Example
|
||||
|
||||
iex> URI.encode("ftp://s-ite.tld/?value=put it+й")
|
||||
"ftp://s-ite.tld/?value=put%20it+%D0%B9"
|
||||
|
||||
iex> URI.encode("a string", &(&1 != ?i))
|
||||
"a str%69ng"
|
||||
|
||||
"""
|
||||
@spec encode(binary, (byte -> boolean)) :: binary
|
||||
def encode(string, predicate \\ &char_unescaped?/1)
|
||||
when is_binary(string) and is_function(predicate, 1) do
|
||||
for <<char <- string>>, into: "", do: percent(char, predicate)
|
||||
def encode(str, predicate \\ &char_unescaped?/1) when is_binary(str) do
|
||||
for <<c <- str>>, into: "", do: percent(c, predicate)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a string as "x-www-form-urlencoded".
|
||||
Encodes a string as "x-www-urlencoded".
|
||||
|
||||
## Example
|
||||
|
||||
@@ -284,26 +185,25 @@ defmodule URI do
|
||||
"put%3A+it%2B%D0%B9"
|
||||
|
||||
"""
|
||||
@spec encode_www_form(binary) :: binary
|
||||
def encode_www_form(string) when is_binary(string) do
|
||||
for <<char <- string>>, into: "" do
|
||||
case percent(char, &char_unreserved?/1) do
|
||||
def encode_www_form(str) when is_binary(str) do
|
||||
for <<c <- str>>, into: "" do
|
||||
case percent(c, &char_unreserved?/1) do
|
||||
"%20" -> "+"
|
||||
percent -> percent
|
||||
pct -> pct
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp percent(char, predicate) do
|
||||
if predicate.(char) do
|
||||
<<char>>
|
||||
defp percent(c, predicate) do
|
||||
if predicate.(c) do
|
||||
<<c>>
|
||||
else
|
||||
<<"%", hex(bsr(char, 4)), hex(band(char, 15))>>
|
||||
"%" <> hex(bsr(c, 4)) <> hex(band(c, 15))
|
||||
end
|
||||
end
|
||||
|
||||
defp hex(n) when n <= 9, do: n + ?0
|
||||
defp hex(n), do: n + ?A - 10
|
||||
defp hex(n) when n <= 9, do: <<n + ?0>>
|
||||
defp hex(n), do: <<n + ?A - 10>>
|
||||
|
||||
@doc """
|
||||
Percent-unescapes a URI.
|
||||
@@ -314,7 +214,6 @@ defmodule URI do
|
||||
"http://elixir-lang.org"
|
||||
|
||||
"""
|
||||
@spec decode(binary) :: binary
|
||||
def decode(uri) do
|
||||
unpercent(uri, "", false)
|
||||
catch
|
||||
@@ -323,7 +222,7 @@ defmodule URI do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a string as "x-www-form-urlencoded".
|
||||
Decodes a string as "x-www-urlencoded".
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -331,12 +230,11 @@ defmodule URI do
|
||||
"<all in/"
|
||||
|
||||
"""
|
||||
@spec decode_www_form(binary) :: binary
|
||||
def decode_www_form(string) do
|
||||
unpercent(string, "", true)
|
||||
def decode_www_form(str) do
|
||||
unpercent(str, "", true)
|
||||
catch
|
||||
:malformed_uri ->
|
||||
raise ArgumentError, "malformed URI #{inspect string}"
|
||||
raise ArgumentError, "malformed URI #{inspect str}"
|
||||
end
|
||||
|
||||
defp unpercent(<<?+, tail::binary>>, acc, spaces = true) do
|
||||
@@ -362,17 +260,14 @@ defmodule URI do
|
||||
Parses a well-formed URI reference into its components.
|
||||
|
||||
Note this function expects a well-formed URI and does not perform
|
||||
any validation. See the "Examples" section below for examples of how
|
||||
`URI.parse/1` can be used to parse a wide range of URIs.
|
||||
any validation. See the examples section below of how `URI.parse/1`
|
||||
can be used to parse a wide range of relative URIs.
|
||||
|
||||
This function uses the parsing regular expression as defined
|
||||
in [RFC 3986, Appendix B](http://tools.ietf.org/html/rfc3986#appendix-B).
|
||||
in the [Appendix B of RFC3986](http://tools.ietf.org/html/rfc3986#appendix-B).
|
||||
|
||||
When a URI is given without a port, the value returned by
|
||||
`URI.default_port/1` for the URI's scheme is used for the `:port` field.
|
||||
|
||||
If a `%URI{}` struct is given to this function, this function returns it
|
||||
unmodified.
|
||||
When a URI is given without a port, the values registered via
|
||||
`URI.default_port/1` and `URI.default_port/2` are used.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -394,21 +289,29 @@ defmodule URI do
|
||||
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||
|
||||
"""
|
||||
@spec parse(t | binary) :: t
|
||||
def parse(uri)
|
||||
|
||||
def parse(%URI{} = uri), do: uri
|
||||
|
||||
def parse(string) when is_binary(string) do
|
||||
def parse(s) when is_binary(s) do
|
||||
# From http://tools.ietf.org/html/rfc3986#appendix-B
|
||||
regex = ~r/^(([a-z][a-z0-9\+\-\.]*):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/i
|
||||
parts = nillify(Regex.run(regex, string))
|
||||
parts = nillify(Regex.run(regex, s))
|
||||
|
||||
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
|
||||
{userinfo, host, port} = split_authority(authority)
|
||||
|
||||
scheme = scheme && String.downcase(scheme)
|
||||
port = port || (scheme && default_port(scheme))
|
||||
if authority do
|
||||
authority = ""
|
||||
|
||||
if userinfo, do: authority = authority <> userinfo <> "@"
|
||||
if host, do: authority = authority <> host
|
||||
if port, do: authority = authority <> ":" <> Integer.to_string(port)
|
||||
end
|
||||
|
||||
scheme = normalize_scheme(scheme)
|
||||
|
||||
if is_nil(port) and not is_nil(scheme) do
|
||||
port = default_port(scheme)
|
||||
end
|
||||
|
||||
%URI{
|
||||
scheme: scheme, path: path, query: query,
|
||||
@@ -418,141 +321,63 @@ defmodule URI do
|
||||
end
|
||||
|
||||
# Split an authority into its userinfo, host and port parts.
|
||||
defp split_authority(string) do
|
||||
components = Regex.run(~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, string || "")
|
||||
defp split_authority(s) do
|
||||
s = s || ""
|
||||
components = Regex.run ~r/(^(.*)@)?(\[[a-zA-Z0-9:.]*\]|[^:]*)(:(\d*))?/, s
|
||||
|
||||
destructure [_, _, userinfo, host, _, port], nillify(components)
|
||||
host = if host, do: host |> String.trim_leading("[") |> String.trim_trailing("]")
|
||||
port = if port, do: String.to_integer(port)
|
||||
host = if host, do: host |> String.lstrip(?[) |> String.rstrip(?])
|
||||
|
||||
{userinfo, host, port}
|
||||
end
|
||||
|
||||
defp normalize_scheme(nil), do: nil
|
||||
defp normalize_scheme(scheme), do: String.downcase(scheme)
|
||||
|
||||
# Regex.run returns empty strings sometimes. We want
|
||||
# to replace those with nil for consistency.
|
||||
defp nillify(list) do
|
||||
for string <- list do
|
||||
if byte_size(string) > 0, do: string
|
||||
defp nillify(l) do
|
||||
for s <- l do
|
||||
if byte_size(s) > 0, do: s, else: nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the string representation of the given `URI` struct.
|
||||
Converts the URI to string.
|
||||
|
||||
iex> URI.to_string(URI.parse("http://google.com"))
|
||||
"http://google.com"
|
||||
|
||||
iex> URI.to_string(%URI{scheme: "foo", host: "bar.baz"})
|
||||
"foo://bar.baz"
|
||||
|
||||
"""
|
||||
@spec to_string(t) :: binary
|
||||
defdelegate to_string(uri), to: String.Chars.URI
|
||||
|
||||
@doc ~S"""
|
||||
Merges two URIs.
|
||||
|
||||
This function merges two URIs as per
|
||||
[RFC 3986, section 5.2](http://tools.ietf.org/html/rfc3986#section-5.2).
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.merge(URI.parse("http://google.com"), "/query") |> to_string
|
||||
"http://google.com/query"
|
||||
|
||||
iex> URI.merge("http://example.com", "http://google.com") |> to_string
|
||||
"http://google.com"
|
||||
|
||||
"""
|
||||
@spec merge(t | binary, t | binary) :: t
|
||||
def merge(uri, rel)
|
||||
|
||||
def merge(%URI{authority: nil}, _rel) do
|
||||
raise ArgumentError, "you must merge onto an absolute URI"
|
||||
end
|
||||
def merge(_base, %URI{scheme: rel_scheme} = rel) when rel_scheme != nil do
|
||||
rel
|
||||
end
|
||||
def merge(%URI{} = base, %URI{path: rel_path} = rel) when rel_path in ["", nil] do
|
||||
%{base | query: rel.query || base.query, fragment: rel.fragment}
|
||||
end
|
||||
def merge(%URI{} = base, %URI{} = rel) do
|
||||
new_path = merge_paths(base.path, rel.path)
|
||||
%{base | path: new_path, query: rel.query, fragment: rel.fragment}
|
||||
end
|
||||
def merge(base, rel) do
|
||||
merge(parse(base), parse(rel))
|
||||
end
|
||||
|
||||
defp merge_paths(nil, rel_path),
|
||||
do: merge_paths("/", rel_path)
|
||||
defp merge_paths(_, "/" <> _ = rel_path),
|
||||
do: rel_path
|
||||
defp merge_paths(base_path, rel_path) do
|
||||
[_ | base_segments] = path_to_segments(base_path)
|
||||
path_to_segments(rel_path)
|
||||
|> Kernel.++(base_segments)
|
||||
|> remove_dot_segments([])
|
||||
|> Enum.join("/")
|
||||
end
|
||||
|
||||
defp remove_dot_segments([], [head, ".." | acc]),
|
||||
do: remove_dot_segments([], [head | acc])
|
||||
defp remove_dot_segments([], acc),
|
||||
do: acc
|
||||
defp remove_dot_segments(["." | tail], acc),
|
||||
do: remove_dot_segments(tail, acc)
|
||||
defp remove_dot_segments([head | tail], ["..", ".." | _] = acc),
|
||||
do: remove_dot_segments(tail, [head | acc])
|
||||
defp remove_dot_segments(segments, [_, ".." | acc]),
|
||||
do: remove_dot_segments(segments, acc)
|
||||
defp remove_dot_segments([head | tail], acc),
|
||||
do: remove_dot_segments(tail, [head | acc])
|
||||
|
||||
def path_to_segments(path) do
|
||||
[head | tail] = String.split(path, "/")
|
||||
reverse_and_discard_empty(tail, [head])
|
||||
end
|
||||
|
||||
defp reverse_and_discard_empty([], acc),
|
||||
do: acc
|
||||
defp reverse_and_discard_empty([head], acc),
|
||||
do: [head | acc]
|
||||
defp reverse_and_discard_empty(["" | tail], acc),
|
||||
do: reverse_and_discard_empty(tail, acc)
|
||||
defp reverse_and_discard_empty([head | tail], acc),
|
||||
do: reverse_and_discard_empty(tail, [head | acc])
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: URI do
|
||||
def to_string(%{scheme: scheme, port: port, path: path,
|
||||
query: query, fragment: fragment} = uri) do
|
||||
uri =
|
||||
case scheme && URI.default_port(scheme) do
|
||||
^port -> %{uri | port: nil}
|
||||
_ -> uri
|
||||
end
|
||||
def to_string(uri) do
|
||||
scheme = uri.scheme
|
||||
|
||||
if scheme && (port = URI.default_port(scheme)) do
|
||||
if uri.port == port, do: uri = %{uri | port: nil}
|
||||
end
|
||||
|
||||
# Based on http://tools.ietf.org/html/rfc3986#section-5.3
|
||||
authority = extract_authority(uri)
|
||||
|
||||
if(scheme, do: scheme <> ":", else: "") <>
|
||||
if(authority, do: "//" <> authority, else: "") <>
|
||||
if(path, do: path, else: "") <>
|
||||
if(query, do: "?" <> query, else: "") <>
|
||||
if(fragment, do: "#" <> fragment, else: "")
|
||||
end
|
||||
if uri.host do
|
||||
authority = uri.host
|
||||
if uri.userinfo, do: authority = uri.userinfo <> "@" <> authority
|
||||
if uri.port, do: authority = authority <> ":" <> Integer.to_string(uri.port)
|
||||
else
|
||||
authority = uri.authority
|
||||
end
|
||||
|
||||
defp extract_authority(%{host: nil, authority: authority}) do
|
||||
authority
|
||||
end
|
||||
defp extract_authority(%{host: host, userinfo: userinfo, port: port}) do
|
||||
# According to the grammar at
|
||||
# https://tools.ietf.org/html/rfc3986#appendix-A, a "host" can have a colon
|
||||
# in it only if it's an IPv6 or "IPvFuture" address), so if there's a colon
|
||||
# in the host we can safely surround it with [].
|
||||
if(userinfo, do: userinfo <> "@", else: "") <>
|
||||
if(String.contains?(host, ":"), do: "[" <> host <> "]", else: host) <>
|
||||
if(port, do: ":" <> Integer.to_string(port), else: "")
|
||||
result = ""
|
||||
|
||||
if uri.scheme, do: result = result <> uri.scheme <> ":"
|
||||
if authority, do: result = result <> "//" <> authority
|
||||
if uri.path, do: result = result <> uri.path
|
||||
if uri.query, do: result = result <> "?" <> uri.query
|
||||
if uri.fragment, do: result = result <> "#" <> uri.fragment
|
||||
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user