Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
34df817638 | ||
|
|
311a5790d6 | ||
|
|
b4c75f97b4 | ||
|
|
70692e6618 | ||
|
|
529d584262 | ||
|
|
c0fb275d5b | ||
|
|
24f46be144 | ||
|
|
0fe3da9dea | ||
|
|
03d0ad3f03 | ||
|
|
a87f43701f | ||
|
|
dce3a3a2a5 | ||
|
|
7b20084b28 | ||
|
|
542808102f | ||
|
|
fd18b6a41f | ||
|
|
4c9c95f80f | ||
|
|
89460154bb | ||
|
|
b026e8877a | ||
|
|
597381b2e0 | ||
|
|
df70729b14 | ||
|
|
61b116c1a5 | ||
|
|
d37a10bbd0 | ||
|
|
5d75c5c861 | ||
|
|
db4089cc71 | ||
|
|
f9f4f14edf | ||
|
|
58946ce879 | ||
|
|
de1d5ff793 | ||
|
|
a4be8fb260 | ||
|
|
031a27f5fa | ||
|
|
f3c1931cac | ||
|
|
2e48d81c20 | ||
|
|
e7ef3fc95d | ||
|
|
2d01aa1c6a | ||
|
|
ae07124550 | ||
|
|
1f744f4dac | ||
|
|
ad88ac642f | ||
|
|
f7956e9d80 | ||
|
|
da6892aa96 | ||
|
|
7a3bd20804 | ||
|
|
f7015166dc | ||
|
|
c6f346a355 | ||
|
|
911a3ba97f | ||
|
|
a1668a6b28 | ||
|
|
a4cb74a83f | ||
|
|
bbf9552186 | ||
|
|
e621b5828e | ||
|
|
68e282c50a | ||
|
|
403a50fbcd | ||
|
|
8e98cd4f41 | ||
|
|
499c385000 | ||
|
|
aecafc099d | ||
|
|
785c7d700a | ||
|
|
cecc8a0f95 |
+5
-9
@@ -1,17 +1,13 @@
|
||||
/doc
|
||||
/.eunit
|
||||
/.release
|
||||
/docs
|
||||
/ebin
|
||||
/lib/*/ebin/*
|
||||
/lib/*/tmp
|
||||
/lib/elixir/src/elixir.app.src
|
||||
/lib/elixir/src/*_lexer.erl
|
||||
/lib/elixir/src/*_parser.erl
|
||||
/lib/elixir/src/elixir.app.src
|
||||
/lib/elixir/test/ebin
|
||||
/man/elixir.1
|
||||
/man/iex.1
|
||||
/rel/elixir
|
||||
/Docs-v*.zip
|
||||
/Precompiled-v*.zip
|
||||
/.eunit
|
||||
/.release
|
||||
.elixir.plt
|
||||
erl_crash.dump
|
||||
.elixir.plt
|
||||
|
||||
+4
-8
@@ -1,15 +1,11 @@
|
||||
language: erlang
|
||||
|
||||
otp_release:
|
||||
- 18.0
|
||||
|
||||
sudo: false
|
||||
|
||||
script: "make compile && rm -rf .git && make test"
|
||||
|
||||
notifications:
|
||||
irc: "irc.freenode.org#elixir-lang"
|
||||
recipients:
|
||||
- jose.valim@plataformatec.com.br
|
||||
- eric.meadows.jonsson@gmail.com
|
||||
|
||||
otp_release:
|
||||
- 17.0
|
||||
- 17.1
|
||||
- 17.3
|
||||
|
||||
+1352
-257
File diff suppressed because it is too large
Load Diff
@@ -1,22 +0,0 @@
|
||||
# Contributor Code of Conduct
|
||||
|
||||
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.
|
||||
|
||||
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.
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* 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.
|
||||
|
||||
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.
|
||||
|
||||
This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community.
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by sending an e-mail to elixir-lang-conduct@googlegroups.com.
|
||||
|
||||
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/)
|
||||
+63
-73
@@ -3,23 +3,20 @@
|
||||
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)
|
||||
* [bug reports](#bugs-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)
|
||||
* [the elixir-talk mailing list](http://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/)
|
||||
* [#elixir-lang](irc://chat.freenode.net/elixir-lang)
|
||||
|
||||
Feature requests can be discussed on [the elixir-core mailing list](https://groups.google.com/group/elixir-lang-core).
|
||||
Feature requests can be discussed on [the elixir-core mailing list](http://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
|
||||
@@ -33,8 +30,8 @@ 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).
|
||||
1. **Use the GitHub issue search** — check if the issue has already been
|
||||
reported.
|
||||
|
||||
2. **Check if the issue has been fixed** — try to reproduce it using the
|
||||
`master` branch in the repository.
|
||||
@@ -67,7 +64,7 @@ Example:
|
||||
|
||||
## 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
|
||||
Feature requests are welcome and should be discussed on [the elixir-core mailing list](http://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.
|
||||
@@ -85,9 +82,9 @@ the `lib` folder:
|
||||
|
||||
* `ex_unit` - Simple test framework that ships with Elixir
|
||||
|
||||
* `iex` - IEx, Elixir's interactive shell
|
||||
* `iex` — IEx, Elixir's interactive shell
|
||||
|
||||
* `mix` - Elixir's build tool
|
||||
* `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,
|
||||
@@ -97,10 +94,8 @@ 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
|
||||
```
|
||||
$ 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`.
|
||||
@@ -122,7 +117,7 @@ something like:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
Returns only those elements for which `fun` is true.
|
||||
|
||||
...
|
||||
"""
|
||||
@@ -148,7 +143,7 @@ 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
|
||||
Try to keep unneccesary 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.
|
||||
@@ -158,7 +153,7 @@ example:
|
||||
|
||||
```elixir
|
||||
@doc """
|
||||
Returns only those elements for which `fun` is `true`.
|
||||
Return only those elements for which `fun` is true.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -178,11 +173,6 @@ 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.
|
||||
|
||||
@@ -193,83 +183,83 @@ 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
|
||||
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:
|
||||
1. [Fork](http://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
|
||||
```
|
||||
```bash
|
||||
# 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
|
||||
```
|
||||
```bash
|
||||
git checkout master
|
||||
git pull upstream master
|
||||
```
|
||||
|
||||
3. Create a new topic branch (off of `master`) to contain your feature, change,
|
||||
or fix.
|
||||
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.
|
||||
**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>
|
||||
```
|
||||
```bash
|
||||
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.
|
||||
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
|
||||
```
|
||||
```bash
|
||||
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.
|
||||
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>
|
||||
```
|
||||
```bash
|
||||
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.
|
||||
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.
|
||||
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.
|
||||
**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
|
||||
```
|
||||
```bash
|
||||
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)
|
||||
* https://github.com/elixir-lang/elixir/pull/992
|
||||
* https://github.com/elixir-lang/elixir/pull/1058
|
||||
* https://github.com/elixir-lang/elixir/pull/1059
|
||||
|
||||
Thank you for your contributions!
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
LEGAL NOTICE INFORMATION
|
||||
------------------------
|
||||
|
||||
All the files in this distribution are covered under either Elixir's
|
||||
license (see the file LICENSE) except the files mentioned below that
|
||||
contains sections that are under Erlang's License (EPL):
|
||||
|
||||
lib/elixir/src/elixir_parser.erl (generated by build scripts)
|
||||
@@ -1,4 +1,4 @@
|
||||
Copyright 2012 Plataformatec
|
||||
Copyright 2012-2013 Plataformatec.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
REBAR ?= "$(CURDIR)/rebar"
|
||||
PREFIX ?= /usr/local
|
||||
DOCS := v1.2
|
||||
CANONICAL := stable
|
||||
REBAR := rebar
|
||||
DOCS := v1.0
|
||||
ELIXIRC := bin/elixirc --verbose --ignore-module-conflict
|
||||
ERLC := erlc -I lib/elixir/include
|
||||
ERL := erl -I lib/elixir/include -noshell -pa lib/elixir/ebin
|
||||
VERSION := $(strip $(shell cat VERSION))
|
||||
Q := @
|
||||
PREFIX := /usr/local
|
||||
LIBDIR := lib
|
||||
INSTALL = install
|
||||
INSTALL_DIR = $(INSTALL) -m755 -d
|
||||
INSTALL_DATA = $(INSTALL) -m644
|
||||
INSTALL_PROGRAM = $(INSTALL) -m755
|
||||
|
||||
.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
|
||||
.PHONY: install compile erlang elixir build_plt clean_plt dialyze test clean docs release_docs release_zip check_erlang_release
|
||||
.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
|
||||
|
||||
@@ -41,7 +42,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
|
||||
@@ -63,7 +64,7 @@ lib/elixir/src/elixir.app.src: src/elixir.app.src
|
||||
$(Q) cat src/elixir.app.src >>lib/elixir/src/elixir.app.src
|
||||
|
||||
erlang:
|
||||
$(Q) cd lib/elixir && $(REBAR) compile
|
||||
$(Q) cd lib/elixir && ../../$(REBAR) compile
|
||||
|
||||
# Since Mix depends on EEx and EEx depends on
|
||||
# Mix, we first compile EEx without the .app
|
||||
@@ -81,7 +82,7 @@ $(KERNEL): lib/elixir/lib/*.ex lib/elixir/lib/*/*.ex lib/elixir/lib/*/*/*.ex
|
||||
$(Q) cd lib/elixir && ../../$(ELIXIRC) "lib/**/*.ex" -o ebin;
|
||||
$(Q) $(MAKE) unicode
|
||||
$(Q) rm -rf lib/elixir/ebin/elixir.app
|
||||
$(Q) cd lib/elixir && $(REBAR) compile
|
||||
$(Q) cd lib/elixir && ../../$(REBAR) compile
|
||||
|
||||
unicode: $(UNICODE)
|
||||
$(UNICODE): lib/elixir/unicode/*
|
||||
@@ -98,7 +99,6 @@ $(eval $(call APP_TEMPLATE,iex,IEx))
|
||||
install: compile
|
||||
@ echo "==> elixir (install)"
|
||||
$(Q) for dir in lib/*; do \
|
||||
rm -Rf $(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin; \
|
||||
$(INSTALL_DIR) "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
$(INSTALL_DATA) $$dir/ebin/* "$(DESTDIR)$(PREFIX)/$(LIBDIR)/elixir/$$dir/ebin"; \
|
||||
done
|
||||
@@ -108,10 +108,9 @@ install: compile
|
||||
$(Q) for file in "$(DESTDIR)$(PREFIX)"/$(LIBDIR)/elixir/bin/* ; do \
|
||||
ln -sf "../$(LIBDIR)/elixir/bin/$${file##*/}" "$(DESTDIR)$(PREFIX)/bin/" ; \
|
||||
done
|
||||
$(MAKE) install_man
|
||||
|
||||
clean:
|
||||
cd lib/elixir && $(REBAR) clean
|
||||
cd lib/elixir && ../../$(REBAR) clean
|
||||
rm -rf ebin
|
||||
rm -rf lib/*/ebin
|
||||
rm -rf lib/elixir/test/ebin
|
||||
@@ -120,72 +119,46 @@ clean:
|
||||
rm -rf lib/mix/test/fixtures/deps_on_git_repo
|
||||
rm -rf lib/mix/test/fixtures/git_rebar
|
||||
rm -rf lib/elixir/src/elixir.app.src
|
||||
$(MAKE) clean_man
|
||||
|
||||
clean_exbeam:
|
||||
$(Q) rm -f lib/*/ebin/Elixir.*.beam
|
||||
|
||||
#==> Create Documentation
|
||||
#==> Release tasks
|
||||
|
||||
LOGO_PATH = $(shell test -f ../docs/logo.png && echo "--logo ../docs/logo.png")
|
||||
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 $(4)
|
||||
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)" -o docs/$(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/Typespecs.md" -e "lib/elixir/pages/Writing Documentation.md")
|
||||
|
||||
docs_eex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (eex)"
|
||||
$(Q) rm -rf doc/eex
|
||||
$(call COMPILE_DOCS,EEx,eex,EEx)
|
||||
|
||||
docs_mix: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (mix)"
|
||||
$(Q) rm -rf doc/mix
|
||||
$(call COMPILE_DOCS,Mix,mix,Mix)
|
||||
|
||||
docs_iex: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (iex)"
|
||||
$(Q) rm -rf doc/iex
|
||||
$(call COMPILE_DOCS,IEx,iex,IEx)
|
||||
|
||||
docs_ex_unit: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (ex_unit)"
|
||||
$(Q) rm -rf doc/ex_unit
|
||||
$(call COMPILE_DOCS,ExUnit,ex_unit,ExUnit)
|
||||
|
||||
docs_logger: compile ../ex_doc/bin/ex_doc
|
||||
@ echo "==> ex_doc (logger)"
|
||||
$(Q) rm -rf doc/logger
|
||||
$(call COMPILE_DOCS,Logger,logger,Logger)
|
||||
docs: compile ../ex_doc/bin/ex_doc
|
||||
$(Q) rm -rf docs
|
||||
$(call DOCS,Elixir,elixir,Kernel)
|
||||
$(call DOCS,EEx,eex,EEx)
|
||||
$(call DOCS,Mix,mix,Mix)
|
||||
$(call DOCS,IEx,iex,IEx)
|
||||
$(call DOCS,ExUnit,ex_unit,ExUnit)
|
||||
$(call DOCS,Logger,logger,Logger)
|
||||
|
||||
../ex_doc/bin/ex_doc:
|
||||
@ echo "ex_doc is not found in ../ex_doc as expected. See README for more information."
|
||||
@ false
|
||||
|
||||
#==> Zips
|
||||
release_zip: compile
|
||||
rm -rf v$(VERSION).zip
|
||||
zip -9 -r v$(VERSION).zip bin CHANGELOG.md LEGAL lib/*/ebin LICENSE Makefile README.md VERSION
|
||||
|
||||
Docs.zip: docs
|
||||
rm -rf Docs-v$(VERSION).zip
|
||||
zip -9 -r Docs-v$(VERSION).zip CHANGELOG.md doc NOTICE LICENSE README.md
|
||||
@ echo "Docs file created $(CURDIR)/Docs-v$(VERSION).zip"
|
||||
release_docs: docs
|
||||
cd ../docs
|
||||
rm -rf ../docs/$(DOCS)
|
||||
mv docs ../docs/$(DOCS)
|
||||
|
||||
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
|
||||
@ echo "Precompiled file created $(CURDIR)/Precompiled-v$(VERSION).zip"
|
||||
# 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
|
||||
|
||||
publish_zips: Precompiled.zip Docs.zip
|
||||
|
||||
publish_docs: docs
|
||||
rm -rf ../docs/$(DOCS)/*/
|
||||
cp -R doc/* ../docs/$(DOCS)
|
||||
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
|
||||
|
||||
@@ -204,7 +177,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)"
|
||||
@@ -232,31 +209,3 @@ build_plt: clean_plt $(PLT)
|
||||
dialyze: compile $(PLT)
|
||||
@ echo "==> Dialyzing Elixir..."
|
||||
$(Q) dialyzer --plt $(PLT) $(DIALYZER_OPTS) lib/*/ebin
|
||||
|
||||
#==> Man page tasks
|
||||
|
||||
build_man: man/iex.1 man/elixir.1
|
||||
|
||||
man/iex.1:
|
||||
$(Q) cp man/iex.1.in man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r common" man/iex.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/iex.1
|
||||
$(Q) rm man/iex.1.bak
|
||||
|
||||
man/elixir.1:
|
||||
$(Q) cp man/elixir.1.in man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/r common" man/elixir.1
|
||||
$(Q) sed -i.bak "/{COMMON}/d" man/elixir.1
|
||||
$(Q) rm man/elixir.1.bak
|
||||
|
||||
clean_man:
|
||||
rm -f man/elixir.1
|
||||
rm -f man/iex.1
|
||||
|
||||
install_man: build_man
|
||||
$(Q) mkdir -p $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixir.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/elixirc.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/iex.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(Q) $(INSTALL_DATA) man/mix.1 $(DESTDIR)$(PREFIX)/share/man/man1
|
||||
$(MAKE) clean_man
|
||||
|
||||
@@ -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,89 +1,59 @@
|
||||

|
||||
=========
|
||||
[](https://travis-ci.org/elixir-lang/elixir)
|
||||
[](http://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
|
||||
|
||||
If you want to contribute to Elixir or run it 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
|
||||
cd elixir
|
||||
make clean test
|
||||
```
|
||||
$ git clone https://github.com/elixir-lang/elixir.git
|
||||
$ 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, feel free to
|
||||
[open an issue][2].
|
||||
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 [ex_doc](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:
|
||||
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.
|
||||
# After cloning and compiling Elixir
|
||||
$ git clone git://github.com/elixir-lang/ex_doc.git
|
||||
$ cd ex_doc && ../elixir/bin/mix compile
|
||||
$ cd ../elixir && make docs
|
||||
|
||||
## Contributing
|
||||
|
||||
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].
|
||||
We appreciate any contribution to Elixir, so check out our [CONTRIBUTING.md](CONTRIBUTING.md) guide for more information. We usually keep a list of features and bugs [in the issue tracker][2].
|
||||
|
||||
## Important links
|
||||
|
||||
* [Elixir Website][1]
|
||||
* [Elixir Documentation][7]
|
||||
* **[#elixir-lang][5]** on [Freenode][6] IRC
|
||||
* \#elixir-lang on freenode IRC
|
||||
* [Website][1]
|
||||
* [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-talk
|
||||
[4]: https://groups.google.com/group/elixir-lang-core
|
||||
[5]: https://webchat.freenode.net/?channels=#elixir-lang
|
||||
[6]: http://www.freenode.net
|
||||
[7]: http://elixir-lang.org/docs.html
|
||||
[3]: http://groups.google.com/group/elixir-lang-talk
|
||||
[4]: http://groups.google.com/group/elixir-lang-core
|
||||
|
||||
## 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.
|
||||
|
||||
+9
-7
@@ -14,13 +14,13 @@ This document simply outlines the release process:
|
||||
|
||||
5. Run `make clean test` to ensure all tests pass from scratch and the CI is green
|
||||
|
||||
6. Recompile an existing project (for example, Ecto) to ensure manifests can be upgraded
|
||||
6. Ensure minimum supported Hex works with new release (instructions upcoming)
|
||||
|
||||
7. Push branch and the new tag
|
||||
7. Push master and the new tag
|
||||
|
||||
8. Publish new docs with `make publish_docs`, copy docs to `docs/stable` if appropriate, and push to GitHub Pages
|
||||
8. Release new docs with `make release_docs`, move docs to `docs/stable` if appropriate, and push
|
||||
|
||||
9. Publish new zips with `make publish_zips`, upload `Precompiled.zip` and `Docs.zip` to GitHub Releases
|
||||
9. Release new zip with `make release_zip`, push `Precompiled.zip` to GitHub Releases
|
||||
|
||||
10. Add the release to `elixir.csv` file in `elixir-lang/elixir-lang.github.com`
|
||||
|
||||
@@ -28,11 +28,13 @@ This document simply outlines the release process:
|
||||
|
||||
## New vMAJOR.MINOR releases
|
||||
|
||||
12. Create a new branch "vMAJOR.MINOR"
|
||||
11. Create a new branch "vMAJOR.MINOR"
|
||||
|
||||
13. Move docs generation to `docs/vMAJOR.MINOR` in Makefile and copy them from `docs/stable` (change index.html accordingly)
|
||||
12. Move docs generation to `docs/vOLD-MAJOR.OLD-MINOR` and copy them to `docs/stable`
|
||||
|
||||
14. In master, bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
13. Bump versions, start new CHANGELOG, add `-dev` back and commit "Start vVERSION+1"
|
||||
|
||||
14. `make release_docs` and push it to `elixir-lang/docs`
|
||||
|
||||
## Places where version is mentioned
|
||||
|
||||
|
||||
+5
-14
@@ -2,7 +2,7 @@
|
||||
if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
echo "Usage: `basename $0` [options] [.exs file] [data]
|
||||
|
||||
-v Prints version and exits
|
||||
-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
|
||||
@@ -10,18 +10,17 @@ if [ $# -eq 0 ] || [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
|
||||
-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 (*)
|
||||
--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
|
||||
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
** Options can be passed to the erlang runtime using ELIXIR_ERL_OPTIONS or --erl" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -36,7 +35,6 @@ readlink_f () {
|
||||
}
|
||||
|
||||
MODE="elixir"
|
||||
ERL_EXEC="erl"
|
||||
ERL=""
|
||||
I=1
|
||||
|
||||
@@ -73,9 +71,6 @@ while [ $I -le $# ]; do
|
||||
eval "VAL=\${$I}"
|
||||
ERL="$ERL "$VAL""
|
||||
;;
|
||||
--werl)
|
||||
USE_WERL=true
|
||||
;;
|
||||
*)
|
||||
break
|
||||
;;
|
||||
@@ -94,15 +89,11 @@ if [ "$OS" != "Windows_NT" ]; then
|
||||
if test -t 1 -a -t 2; then ERL="-elixir ansi_enabled true $ERL"; fi
|
||||
fi
|
||||
|
||||
if [ "$OS" = "Windows_NT" ] && [ $USE_WERL ]; then
|
||||
ERL_EXEC="werl"
|
||||
fi
|
||||
|
||||
if [ -z "$ERL_PATH" ]; then
|
||||
if [ -f "$SCRIPT_PATH/../releases/RELEASES" ] && [ -f "$SCRIPT_PATH/erl" ]; then
|
||||
ERL_PATH="$SCRIPT_PATH"/"$ERL_EXEC"
|
||||
ERL_PATH="$SCRIPT_PATH"/erl
|
||||
else
|
||||
ERL_PATH="$ERL_EXEC"
|
||||
ERL_PATH=erl
|
||||
fi
|
||||
fi
|
||||
|
||||
|
||||
+8
-8
@@ -1,15 +1,15 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@echo off
|
||||
setlocal
|
||||
if ""%1""=="""" goto :documentation
|
||||
if ""%1""==""--help"" goto :documentation
|
||||
if ""%1""==""-h"" goto :documentation
|
||||
if ""%1""==""/h"" 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 -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
|
||||
@@ -95,12 +95,12 @@ for /d %%d in ("%originPath%..\lib\*.") do (
|
||||
SETLOCAL disabledelayedexpansion
|
||||
:run
|
||||
IF NOT %runMode% == "iex" (
|
||||
set beforeExtra=-noshell -s elixir start_cli %beforeExtra%
|
||||
set beforeExtra=-s elixir start_cli %beforeExtra%
|
||||
)
|
||||
IF %useWerl% EQU 1 (
|
||||
start werl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
) ELSE (
|
||||
erl.exe %ext_libs% %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
erl.exe %ext_libs% -noshell %ELIXIR_ERL_OPTIONS% %parsErlang% %beforeExtra% -extra %*
|
||||
)
|
||||
:end
|
||||
endlocal
|
||||
|
||||
+3
-3
@@ -1,4 +1,4 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@echo off
|
||||
set argc=0
|
||||
for %%A in (%*) do (
|
||||
if "%%A"=="--help" goto documentation
|
||||
@@ -20,7 +20,7 @@ echo --warnings-as-errors Treat warnings as errors and return non-zero exit cod
|
||||
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 >&2
|
||||
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 >&2
|
||||
:run
|
||||
call "%~dp0\elixir.bat" +elixirc %*
|
||||
|
||||
@@ -10,12 +10,11 @@ if [ $# -gt 0 ] && ([ "$1" = "--help" ] || [ "$1" = "-h" ]); then
|
||||
-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 (*)
|
||||
--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;
|
||||
@@ -39,4 +38,6 @@ readlink_f () {
|
||||
|
||||
SELF=$(readlink_f "$0")
|
||||
SCRIPT_PATH=$(dirname "$SELF")
|
||||
exec "$SCRIPT_PATH"/elixir --no-halt --erl "-user Elixir.IEx.CLI" +iex "$@"
|
||||
|
||||
if [ "$OS" = "Windows_NT" ]; then NOSHELL="-noshell "; fi
|
||||
exec "$SCRIPT_PATH"/elixir --no-halt --erl "$NOSHELL -user Elixir.IEx.CLI" +iex "$@"
|
||||
|
||||
+2
-4
@@ -1,4 +1,2 @@
|
||||
@if defined ELIXIR_CLI_ECHO (@echo on) else (@echo off)
|
||||
@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% %*
|
||||
@set __ELIXIR_IEX_FLAGS=
|
||||
@echo off
|
||||
call "%~dp0\elixir.bat" +iex --erl "-user Elixir.IEx.CLI" --no-halt %*
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env elixir
|
||||
# Reminder: apply any changes in this file to bin\mix.bat
|
||||
Mix.start
|
||||
Mix.CLI.main
|
||||
|
||||
+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 %*
|
||||
|
||||
+12
-12
@@ -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"
|
||||
@@ -62,9 +62,9 @@ defmodule EEx do
|
||||
|
||||
All expressions that output something to the template
|
||||
**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` clauses, they are treated the same in EEx and
|
||||
Elixir is a macro, there are no exceptions for this rule.
|
||||
For example, while some template languages would special-
|
||||
case `if` clauses, they are treated the same in EEx and
|
||||
also require `=` in order to have their result printed:
|
||||
|
||||
<%= if true do %>
|
||||
@@ -85,9 +85,9 @@ 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
|
||||
required by the template is not specified at compilation time.
|
||||
@@ -162,7 +162,7 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and generate a quoted expression
|
||||
Get a string `source` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
def compile_string(source, options \\ []) do
|
||||
@@ -170,7 +170,7 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and generate a quoted expression
|
||||
Get a `filename` and generate a quoted expression
|
||||
that can be evaluated by Elixir or compiled to a function.
|
||||
"""
|
||||
def compile_file(filename, options \\ []) do
|
||||
@@ -179,7 +179,7 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a string `source` and evaluate the values using the `bindings`.
|
||||
Get a string `source` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -193,15 +193,15 @@ defmodule EEx do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a `filename` and evaluate the values using the `bindings`.
|
||||
Get a `filename` and evaluate the values using the `bindings`.
|
||||
|
||||
## Examples
|
||||
|
||||
# sample.eex
|
||||
# sample.ex
|
||||
foo <%= bar %>
|
||||
|
||||
# iex
|
||||
EEx.eval_file "sample.eex", [bar: "baz"] #=> "foo baz"
|
||||
EEx.eval_file "sample.ex", [bar: "baz"] #=> "foo baz"
|
||||
|
||||
"""
|
||||
def eval_file(filename, bindings \\ [], options \\ []) do
|
||||
|
||||
@@ -12,8 +12,7 @@ defmodule EEx.Compiler do
|
||||
def compile(source, opts) do
|
||||
file = opts[:file] || "nofile"
|
||||
line = opts[:line] || 1
|
||||
trim = opts[:trim] || false
|
||||
case EEx.Tokenizer.tokenize(source, line, trim: trim) do
|
||||
case EEx.Tokenizer.tokenize(source, line) do
|
||||
{:ok, tokens} ->
|
||||
state = %{engine: opts[:engine] || @default_engine,
|
||||
file: file, line: line, quoted: [], start_line: nil}
|
||||
@@ -32,7 +31,7 @@ defmodule EEx.Compiler 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)
|
||||
buffer = state.engine.handle_expr(buffer, mark, expr)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
@@ -40,7 +39,7 @@ defmodule EEx.Compiler do
|
||||
{contents, line, t} = look_ahead_text(t, start_line, chars)
|
||||
{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)
|
||||
buffer = state.engine.handle_expr(buffer, mark, contents)
|
||||
generate_buffer(t, buffer, scope, state)
|
||||
end
|
||||
|
||||
|
||||
+16
-32
@@ -25,9 +25,11 @@ defmodule EEx.Engine do
|
||||
default implementations for the functions above.
|
||||
"""
|
||||
|
||||
@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
|
||||
use Behaviour
|
||||
|
||||
defcallback handle_body(Macro.t) :: Macro.t
|
||||
defcallback handle_text(Macro.t, binary) :: Macro.t
|
||||
defcallback handle_expr(Macro.t, binary, Macro.t) :: Macro.t
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
@@ -42,8 +44,8 @@ defmodule EEx.Engine do
|
||||
EEx.Engine.handle_text(buffer, text)
|
||||
end
|
||||
|
||||
def handle_expr(buffer, marker, expr) do
|
||||
EEx.Engine.handle_expr(buffer, marker, expr)
|
||||
def handle_expr(buffer, mark, expr) do
|
||||
EEx.Engine.handle_expr(buffer, mark, expr)
|
||||
end
|
||||
|
||||
defoverridable [handle_body: 1, handle_expr: 3, handle_text: 2]
|
||||
@@ -53,11 +55,8 @@ defmodule EEx.Engine do
|
||||
@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`:
|
||||
`handle_assign/3` with `Macro.prewalk/1`:
|
||||
|
||||
def handle_expr(buffer, token, expr) do
|
||||
expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
|
||||
@@ -67,30 +66,16 @@ 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 1.3 or 1.4
|
||||
def fetch_assign!(assigns, key) do
|
||||
case Access.fetch(assigns, key) do
|
||||
{:ok, val} ->
|
||||
val
|
||||
:error ->
|
||||
keys = Enum.map(assigns, &elem(&1, 0))
|
||||
IO.write :stderr, "warning: assign @#{key} not available in eex template. " <>
|
||||
"Please ensure all assigns are given as options. " <>
|
||||
"Available assigns: #{inspect keys}\n" <>
|
||||
Exception.format_stacktrace
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
The default implementation simply returns the given expression.
|
||||
The default implementation implementation simply returns the
|
||||
given expression.
|
||||
"""
|
||||
def handle_body(quoted) do
|
||||
quoted
|
||||
@@ -111,19 +96,18 @@ defmodule EEx.Engine do
|
||||
|
||||
All other markers are not implemented by this engine.
|
||||
"""
|
||||
@spec handle_expr(Macro.t, String.t, Macro.t) :: Macro.t
|
||||
def handle_expr(buffer, "=", expr) do
|
||||
quote do
|
||||
tmp1 = unquote(buffer)
|
||||
tmp1 <> String.Chars.to_string(unquote(expr))
|
||||
tmp = unquote(buffer)
|
||||
tmp <> String.Chars.to_string(unquote(expr))
|
||||
end
|
||||
end
|
||||
|
||||
def handle_expr(buffer, "", expr) do
|
||||
quote do
|
||||
tmp2 = unquote(buffer)
|
||||
tmp = unquote(buffer)
|
||||
unquote(expr)
|
||||
tmp2
|
||||
tmp
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -11,9 +11,9 @@ defmodule EEx.SmartEngine do
|
||||
"1"
|
||||
|
||||
In the example above, we can access the value `foo` under
|
||||
the binding `assigns` using `@foo`. This is useful because
|
||||
a template, after being compiled, can receive different
|
||||
assigns and would not require recompilation for each
|
||||
the binding `assigns` using `@foo`. This is useful when
|
||||
a template, after compiled, may receive different assigns
|
||||
and the developer don't want to recompile it for each
|
||||
variable set.
|
||||
|
||||
Assigns can also be used when compiled to a function:
|
||||
|
||||
@@ -14,63 +14,63 @@ defmodule EEx.Tokenizer do
|
||||
|
||||
Or `{:error, line, error}` in case of errors.
|
||||
"""
|
||||
def tokenize(bin, line, opts \\ [])
|
||||
|
||||
def tokenize(bin, line, opts) when is_binary(bin) do
|
||||
tokenize(String.to_char_list(bin), line, opts)
|
||||
def tokenize(bin, line) when is_binary(bin) do
|
||||
tokenize(String.to_char_list(bin), line)
|
||||
end
|
||||
|
||||
def tokenize(list, line, opts) do
|
||||
tokenize(list, line, opts, [], [])
|
||||
def tokenize(list, line) do
|
||||
tokenize(list, line, [], [])
|
||||
end
|
||||
|
||||
defp tokenize('<%%' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line, opts, [?%, ?<|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize('<%#' ++ t, line, opts, buffer, acc) do
|
||||
case expr(t, line, []) do
|
||||
defp tokenize('<%%' ++ t, line, buffer, acc) do
|
||||
case expr(t, line, [?%, ?<|buffer]) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, _, new_line, rest} ->
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
tokenize rest, new_line, opts, buffer, acc
|
||||
{:ok, buffer, new_line, rest} ->
|
||||
tokenize rest, new_line, [?>, ?%|buffer], acc
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('<%' ++ t, line, opts, buffer, acc) do
|
||||
defp tokenize('<%#' ++ t, line, buffer, acc) do
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, _, new_line, rest} ->
|
||||
tokenize rest, new_line, buffer, acc
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('<%' ++ t, line, buffer, acc) do
|
||||
{marker, t} = retrieve_marker(t)
|
||||
|
||||
case expr(t, line, []) do
|
||||
{:error, _, _} = error -> error
|
||||
{:ok, expr, new_line, rest} ->
|
||||
token = token_name(expr)
|
||||
{rest, new_line, buffer} = trim_if_needed(rest, new_line, opts, buffer, acc)
|
||||
acc = tokenize_text(buffer, acc)
|
||||
final = {token, line, marker, Enum.reverse(expr)}
|
||||
tokenize rest, new_line, opts, [], [final | acc]
|
||||
tokenize rest, new_line, [], [final | acc]
|
||||
end
|
||||
end
|
||||
|
||||
defp tokenize('\n' ++ t, line, opts, buffer, acc) do
|
||||
tokenize t, line + 1, opts, [?\n|buffer], acc
|
||||
defp tokenize('\n' ++ t, line, buffer, acc) do
|
||||
tokenize t, line + 1, [?\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, buffer, acc) do
|
||||
tokenize t, line, [h|buffer], acc
|
||||
end
|
||||
|
||||
defp tokenize([], _line, _opts, buffer, acc) do
|
||||
defp tokenize([], _line, buffer, acc) do
|
||||
{:ok, Enum.reverse(tokenize_text(buffer, acc))}
|
||||
end
|
||||
|
||||
# Retrieve marker for <%
|
||||
|
||||
defp retrieve_marker('=' ++ t) do
|
||||
{'=', t}
|
||||
{"=", t}
|
||||
end
|
||||
|
||||
defp retrieve_marker(t) do
|
||||
{'', t}
|
||||
{"", t}
|
||||
end
|
||||
|
||||
# Tokenize an expression until we find %>
|
||||
@@ -94,11 +94,11 @@ defmodule EEx.Tokenizer do
|
||||
# Receive an expression content and check
|
||||
# if it is a start, middle or an end token.
|
||||
#
|
||||
# Start tokens finish with "do" and "fn ->"
|
||||
# Middle tokens are marked with "->" or keywords
|
||||
# End tokens contain only the end word and optionally ")"
|
||||
# Start tokens finish with `do` and `fn ->`
|
||||
# Middle tokens are marked with `->` or keywords
|
||||
# End tokens contain only the end word
|
||||
|
||||
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
|
||||
|
||||
@@ -115,7 +115,7 @@ defmodule EEx.Tokenizer do
|
||||
# token and, if so, it is not followed by an "end"
|
||||
# token. If this is the case, we are on a start expr.
|
||||
case :elixir_tokenizer.tokenize(rest, 1, file: "eex", check_terminators: false) do
|
||||
{:ok, _line, _column, tokens} ->
|
||||
{:ok, _line, tokens} ->
|
||||
tokens = Enum.reverse(tokens)
|
||||
fn_index = fn_index(tokens)
|
||||
|
||||
@@ -169,47 +169,4 @@ defmodule EEx.Tokenizer do
|
||||
defp tokenize_text(buffer, acc) do
|
||||
[{:text, Enum.reverse(buffer)} | acc]
|
||||
end
|
||||
|
||||
# If trim mode is enabled and the token is on a line with
|
||||
# only itself and whitespace, trim the whitespace around it,
|
||||
# including the line break following it if there is one.
|
||||
|
||||
defp trim_if_needed(rest, line, opts, buffer, acc) do
|
||||
original = {rest, line, buffer}
|
||||
if opts[:trim] do
|
||||
case {trim_left(buffer, acc), trim_right(rest, line)} do
|
||||
{{true, new_buffer}, {true, new_rest, new_line}} ->
|
||||
{new_rest, new_line, new_buffer}
|
||||
_ ->
|
||||
original
|
||||
end
|
||||
else
|
||||
original
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_left(buffer, acc) do
|
||||
case {trim_whitespace(buffer), acc} do
|
||||
{[?\n|_] = trimmed_buffer, _} -> {true, trimmed_buffer}
|
||||
{[], []} -> {true, []}
|
||||
_ -> {false, buffer}
|
||||
end
|
||||
end
|
||||
|
||||
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}
|
||||
[] -> {true, [], line}
|
||||
_ -> {false, rest, line}
|
||||
end
|
||||
end
|
||||
|
||||
defp trim_whitespace([h|t]) when h == ?\s or h == ?\t do
|
||||
trim_whitespace(t)
|
||||
end
|
||||
|
||||
defp trim_whitespace(list) do
|
||||
list
|
||||
end
|
||||
end
|
||||
|
||||
@@ -14,17 +14,17 @@ defmodule EEx.TokenizerTest do
|
||||
|
||||
test "strings with embedded code" do
|
||||
assert T.tokenize('foo <% bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '', ' bar '}]}
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, "", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with embedded equals code" do
|
||||
assert T.tokenize('foo <%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, '=', ' bar '}]}
|
||||
{:ok, [{:text, 'foo '}, {:expr, 1, "=", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line" do
|
||||
assert T.tokenize('foo\n<%= bar %>', 1) ==
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, '=', ' bar '}]}
|
||||
{:ok, [{:text, 'foo\n'}, {:expr, 2, "=", ' bar '}]}
|
||||
end
|
||||
|
||||
test "strings with more than one line and expression with more than one line" do
|
||||
@@ -37,9 +37,9 @@ baz %>
|
||||
|
||||
assert T.tokenize(string, 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:expr, 1, '=', ' bar\n\nbaz '},
|
||||
{:expr, 1, "=", ' bar\n\nbaz '},
|
||||
{:text, '\n'},
|
||||
{:expr, 4, '', ' foo '},
|
||||
{:expr, 4, "", ' foo '},
|
||||
{:text, '\n'}
|
||||
]}
|
||||
end
|
||||
@@ -56,20 +56,6 @@ baz %>
|
||||
]}
|
||||
end
|
||||
|
||||
test "quotation with interpolation" do
|
||||
assert T.tokenize('a <%% b <%= c %> <%= d %> e %> f', 1) == {:ok, [
|
||||
{:text, 'a <% b '},
|
||||
{:expr, 1, '=', ' c '},
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' d '},
|
||||
{:text, ' e %> f'}
|
||||
]}
|
||||
|
||||
assert T.tokenize('<%%% a <%%= b %> c %>', 1) == {:ok, [
|
||||
{:text, '<%% a <%= b %> c %>'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "comments" do
|
||||
assert T.tokenize('foo <%# true %>', 1) == {:ok, [
|
||||
{:text, 'foo '}
|
||||
@@ -85,77 +71,35 @@ baz %>
|
||||
test "strings with embedded do end" do
|
||||
assert T.tokenize('foo <% if true do %>bar<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:start_expr, 1, "", ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded -> end" do
|
||||
assert T.tokenize('foo <% cond do %><% false -> %>bar<% true -> %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' cond do '},
|
||||
{:middle_expr, 1, '', ' false -> '},
|
||||
{:start_expr, 1, "", ' cond do '},
|
||||
{:middle_expr, 1, "", ' false -> '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' true -> '},
|
||||
{:middle_expr, 1, "", ' true -> '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "strings with embedded keywords blocks" do
|
||||
assert T.tokenize('foo <% if true do %>bar<% else %>baz<% end %>', 1) == {:ok, [
|
||||
{:text, 'foo '},
|
||||
{:start_expr, 1, '', ' if true do '},
|
||||
{:start_expr, 1, "", ' if true do '},
|
||||
{:text, 'bar'},
|
||||
{:middle_expr, 1, '', ' else '},
|
||||
{:middle_expr, 1, "", ' else '},
|
||||
{:text, 'baz'},
|
||||
{:end_expr, 1, '', ' end '}
|
||||
{:end_expr, 1, "", ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
template = '\t<%= if true do %> \n TRUE \n <% else %>\n FALSE \n <% end %> '
|
||||
assert T.tokenize(template, 1, trim: true) == {:ok, [
|
||||
{:start_expr, 1, '=', ' if true do '},
|
||||
{:text, ' TRUE \n'},
|
||||
{:middle_expr, 3, '', ' else '},
|
||||
{:text, ' FALSE \n'},
|
||||
{:end_expr, 5, '', ' end '}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode with comment" do
|
||||
assert T.tokenize(' <%# comment %> \n123', 1, trim: true) == {:ok, [
|
||||
{:text, '123'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode with CRLF" do
|
||||
assert T.tokenize('0\r\n <%= 12 %> \r\n34', 1, trim: true) == {:ok, [
|
||||
{:text, '0\r\n'},
|
||||
{:expr, 2, '=', ' 12 '},
|
||||
{:text, '34'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode set to false" do
|
||||
assert T.tokenize(' <%= 12 %> \n', 1, trim: false) == {:ok, [
|
||||
{:text, ' '},
|
||||
{:expr, 1, '=', ' 12 '},
|
||||
{:text, ' \n'}
|
||||
]}
|
||||
end
|
||||
|
||||
test "trim mode no false positives" do
|
||||
assert_not_trimmed = fn x -> assert T.tokenize(x, 1, trim: true) == T.tokenize(x, 1) end
|
||||
|
||||
assert_not_trimmed.('foo <%= "bar" %> ')
|
||||
assert_not_trimmed.('\n <%= "foo" %>bar')
|
||||
assert_not_trimmed.(' <%% hello %> ')
|
||||
assert_not_trimmed.(' <%= 01 %><%= 23 %>\n')
|
||||
end
|
||||
|
||||
test "raise syntax error when there is start mark and no end mark" do
|
||||
assert T.tokenize('foo <% :bar', 1) == {:error, 1, "missing token '%>'"}
|
||||
end
|
||||
|
||||
@@ -98,10 +98,6 @@ defmodule EExTest do
|
||||
assert_eval "foo baz", "foo <%= if true do %><%= if false do %>bar<% else %>baz<% end %><% end %>"
|
||||
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) ) %>"
|
||||
end
|
||||
|
||||
test "evaluates with defined variable" do
|
||||
assert_eval "foo 1", "foo <% bar = 1 %><%= bar %>"
|
||||
end
|
||||
@@ -236,10 +232,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 = """
|
||||
|
||||
@@ -260,7 +252,7 @@ Number <%= x %>
|
||||
assert_eval expected, string
|
||||
end
|
||||
|
||||
test "properly handle functions on the left side of clauses" do
|
||||
test "do not consider already finished functions" do
|
||||
expected = """
|
||||
foo
|
||||
|
||||
@@ -295,18 +287,6 @@ foo
|
||||
assert_eval "\n\n Good\n \n", string
|
||||
end
|
||||
|
||||
test "evaluates expressions with buffers" do
|
||||
string = """
|
||||
<%= 123 %>
|
||||
<% if true do %>
|
||||
<%= 456 %>
|
||||
<% end %>
|
||||
<%= 789 %>
|
||||
"""
|
||||
|
||||
assert_eval "123\n\n789\n", string
|
||||
end
|
||||
|
||||
test "for comprehensions" do
|
||||
string = """
|
||||
<%= for _name <- packages || [] do %>
|
||||
@@ -325,12 +305,6 @@ foo
|
||||
assert result == " • • •\n Jößé Vâlìm Jößé Vâlìm\n"
|
||||
end
|
||||
|
||||
test "trim mode" do
|
||||
string = "<%= 123 %> \n456\n <%= 789 %>"
|
||||
expected = "123456\n789"
|
||||
assert_eval expected, string, [], trim: true
|
||||
end
|
||||
|
||||
test "evaluates the source from a given file" do
|
||||
filename = Path.join(__DIR__, "fixtures/eex_template.eex")
|
||||
result = EEx.eval_file(filename)
|
||||
@@ -413,9 +387,8 @@ foo
|
||||
assert {:wrapped, "foo"} = EEx.eval_string("foo", [], engine: TestEngine)
|
||||
end
|
||||
|
||||
defp assert_eval(expected, actual, binding \\ [], opts \\ []) do
|
||||
opts = Enum.into [file: __ENV__.file, engine: EEx.Engine], opts
|
||||
result = EEx.eval_string(actual, binding, opts)
|
||||
defp assert_eval(expected, actual, binding \\ []) do
|
||||
result = EEx.eval_string(actual, binding, file: __ENV__.file, engine: EEx.Engine)
|
||||
assert result == expected
|
||||
end
|
||||
end
|
||||
|
||||
+107
-151
@@ -1,16 +1,15 @@
|
||||
defmodule Access do
|
||||
defprotocol Access do
|
||||
@moduledoc """
|
||||
Key-based access to data structures via the `foo[bar]` syntax.
|
||||
The Access protocol is used by `foo[bar]` and also
|
||||
empowers the nested update functions in Kernel.
|
||||
|
||||
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.
|
||||
For instance, `foo[bar]` translates `Access.get(foo, bar)`.
|
||||
`Kernel.get_in/2`, `Kernel.put_in/3`, `Kernel.update_in/3` and
|
||||
`Kernel.get_and_update_in/3` are also all powered by the Access
|
||||
protocol.
|
||||
|
||||
## Key-based lookups
|
||||
|
||||
Out of the box, Access works with `Keyword` and `Map`:
|
||||
This protocol is implemented by default for keywords, maps
|
||||
and dictionary like types:
|
||||
|
||||
iex> keywords = [a: 1, b: 2]
|
||||
iex> keywords[:a]
|
||||
@@ -24,171 +23,128 @@ 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. Access requires the key comparison to be
|
||||
implemented using the `===` operator.
|
||||
|
||||
## Field-based lookups
|
||||
|
||||
The Access syntax (`foo[bar]`) cannot be used to access fields in
|
||||
structs. That's by design, as Access is meant to be used for
|
||||
dynamic key-value structures, like maps and keywords, and not
|
||||
by static ones like structs.
|
||||
|
||||
However Elixir already provides a field-based lookup for structs.
|
||||
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` cannot be extended by
|
||||
the developers, and will be always 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
|
||||
|
||||
The key comparison must be implemented using the `===` operator.
|
||||
"""
|
||||
|
||||
@type t :: list | map | nil
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
|
||||
@callback fetch(t, key) :: {:ok, value} | :error
|
||||
@callback get_and_update(t, key, (value -> {value, value})) :: {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
|
||||
|
||||
@doc """
|
||||
Fetches the container's value for the given key.
|
||||
Accesses the given key in the container.
|
||||
"""
|
||||
@spec fetch(t, term) :: {:ok, term} | :error
|
||||
def fetch(container, key)
|
||||
|
||||
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
|
||||
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
|
||||
@spec get(t, term) :: t
|
||||
def get(container, key)
|
||||
|
||||
@doc """
|
||||
Gets the container's value for the given key.
|
||||
"""
|
||||
@spec get(t, term, term) :: term
|
||||
def get(container, key, default \\ nil) do
|
||||
case fetch(container, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> default
|
||||
end
|
||||
end
|
||||
Gets a value and updates the given `key` in one pass.
|
||||
|
||||
@doc """
|
||||
Gets and updates the container's value for the given key, in a single pass.
|
||||
|
||||
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`.
|
||||
|
||||
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`.
|
||||
The function must receive the value for the given `key`
|
||||
(or `nil` if the key doesn't exist in `container`) and
|
||||
the function must return a tuple containing the `get`
|
||||
value and the new value to be stored in the `container`.
|
||||
"""
|
||||
@spec get_and_update(t, term, (term -> {get, term})) :: {get, t} when get: var
|
||||
def get_and_update(container, key, fun)
|
||||
end
|
||||
|
||||
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], _}
|
||||
defimpl Access, for: List do
|
||||
def get(dict, key) when is_atom(key) do
|
||||
case :lists.keyfind(key, 1, dict) do
|
||||
{^key, value} -> value
|
||||
false -> nil
|
||||
end
|
||||
end
|
||||
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
current_value = case :maps.find(key, map) do
|
||||
def get(_dict, key) do
|
||||
raise ArgumentError,
|
||||
"the access protocol for lists expect the key to be an atom, got: #{inspect key}"
|
||||
end
|
||||
|
||||
def get_and_update(dict, key, fun) when is_atom(key) do
|
||||
get_and_update(dict, [], key, fun)
|
||||
end
|
||||
|
||||
defp get_and_update([{key, value}|t], acc, key, fun) do
|
||||
{get, update} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, update}|t])}
|
||||
end
|
||||
|
||||
defp get_and_update([h|t], acc, key, fun) do
|
||||
get_and_update(t, [h|acc], key, fun)
|
||||
end
|
||||
|
||||
defp get_and_update([], acc, key, fun) do
|
||||
{get, update} = fun.(nil)
|
||||
{get, [{key, update}|:lists.reverse(acc)]}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Access, for: Map do
|
||||
def get(map, key) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
end
|
||||
|
||||
{get, update} = fun.(current_value)
|
||||
def get_and_update(map, key, fun) do
|
||||
value =
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> nil
|
||||
end
|
||||
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
end
|
||||
|
||||
def get_and_update(list, key, fun) when is_list(list) do
|
||||
Keyword.get_and_update(list, key, fun)
|
||||
def get!(%{} = map, key) do
|
||||
case :maps.find(key, map) do
|
||||
{:ok, value} -> value
|
||||
:error -> raise KeyError, key: key, term: map
|
||||
end
|
||||
end
|
||||
|
||||
def get!(other, key) do
|
||||
raise ArgumentError,
|
||||
"could not get key #{inspect key}. Expected map/struct, got: #{inspect other}"
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
defimpl Access, for: Atom do
|
||||
def get(nil, _) do
|
||||
nil
|
||||
end
|
||||
|
||||
def get(atom, _) do
|
||||
undefined(atom)
|
||||
end
|
||||
|
||||
def get_and_update(nil, key, _fun) do
|
||||
raise ArgumentError,
|
||||
"could not put/update key #{inspect key} on a nil value"
|
||||
end
|
||||
|
||||
def get_and_update(atom, _key, _fun) do
|
||||
undefined(atom)
|
||||
end
|
||||
|
||||
defp undefined(atom) do
|
||||
raise Protocol.UndefinedError,
|
||||
protocol: @protocol,
|
||||
value: atom,
|
||||
description: "only the nil atom is supported"
|
||||
end
|
||||
end
|
||||
|
||||
+18
-38
@@ -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,14 +31,7 @@ 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))
|
||||
end
|
||||
|
||||
@doc "Resets the executed tasks and returns the previous list of tasks"
|
||||
def take_all() do
|
||||
Agent.get_and_update(__MODULE__, fn set ->
|
||||
{Enum.into(set, []), MapSet.new}
|
||||
end)
|
||||
Agent.update(__MODULE__, &Set.put(&1, item))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -61,11 +54,11 @@ defmodule Agent do
|
||||
Agent.get(agent, &(&1)) |> do_something_expensive()
|
||||
end
|
||||
|
||||
The first function blocks the agent. The second function copies
|
||||
all the state to the client and then executes the operation in the
|
||||
client. The difference is whether the data is large enough to require
|
||||
processing in the server, at least initially, or small enough to be
|
||||
sent to the client cheaply.
|
||||
The first one blocks the agent while the second one copies
|
||||
all the state to the client and executes the operation in the client.
|
||||
The trade-off here is exactly if the data is small enough to be
|
||||
sent to the client cheaply or large enough to require processing on
|
||||
the server (or at least some initial processing).
|
||||
|
||||
## Name Registration
|
||||
|
||||
@@ -75,8 +68,8 @@ defmodule Agent do
|
||||
## A word on distributed agents
|
||||
|
||||
It is important to consider the limitations of distributed agents. Agents
|
||||
provide two APIs, one that works with anonymous functions and another
|
||||
that expects an explicit module, function, and arguments.
|
||||
provides two APIs, one that works with anonymous functions and another
|
||||
that expects explicit module, function and arguments.
|
||||
|
||||
In a distributed setup with multiple nodes, the API that accepts anonymous
|
||||
functions only works if the caller (client) and the agent have the same
|
||||
@@ -89,13 +82,13 @@ defmodule Agent do
|
||||
In this setup, part of your environment will have one version of a given
|
||||
module and the other part another version (the newer one) of the same module.
|
||||
|
||||
The best solution is to simply use the explicit module, function, and arguments
|
||||
The best solution is to simply use the explicit module, function and arguments
|
||||
APIs when working with distributed agents.
|
||||
|
||||
## Hot code swapping
|
||||
|
||||
An agent can have its code hot swapped live by simply passing a module,
|
||||
function, and args tuple to the update instruction. For example, imagine
|
||||
function and args tuple to the update instruction. For example, imagine
|
||||
you have an agent named `:sample` and you want to convert its inner state
|
||||
from some dict structure to a map. It can be done with the following
|
||||
instruction:
|
||||
@@ -144,8 +137,8 @@ defmodule Agent do
|
||||
## Return values
|
||||
|
||||
If the server is successfully created and initialized, the function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If an agent with the
|
||||
specified name already exists, the function returns
|
||||
`{:ok, pid}`, where `pid` is the pid of the server. If there already exists
|
||||
an agent with the specified name, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the given function callback fails with `reason`, the function returns
|
||||
@@ -295,25 +288,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
|
||||
if is_integer(reason) or reason == :infinity do
|
||||
IO.write :stderr, "warning: Agent.stop(agent, timeout) is deprecated, " <>
|
||||
"please use Agent.stop(agent, :normal, timeout) instead\n" <>
|
||||
Exception.format_stacktrace
|
||||
:gen.stop(agent, :normal, reason)
|
||||
else
|
||||
:gen.stop(agent, reason, timeout)
|
||||
end
|
||||
@spec stop(agent, timeout) :: :ok
|
||||
def stop(agent, timeout \\ 5000) do
|
||||
GenServer.call(agent, :stop, timeout)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,6 @@ defmodule Agent.Server do
|
||||
use GenServer
|
||||
|
||||
def init(fun) do
|
||||
_ = initial_call(fun)
|
||||
{:ok, run(fun, [])}
|
||||
end
|
||||
|
||||
@@ -13,16 +12,18 @@ defmodule Agent.Server do
|
||||
end
|
||||
|
||||
def handle_call({:get_and_update, fun}, _from, state) do
|
||||
case run(fun, [state]) do
|
||||
{reply, state} -> {:reply, reply, state}
|
||||
other -> {:stop, {:bad_return_value, other}, state}
|
||||
end
|
||||
{reply, state} = run(fun, [state])
|
||||
{:reply, reply, state}
|
||||
end
|
||||
|
||||
def handle_call({:update, fun}, _from, state) 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,21 +40,17 @@ defmodule Agent.Server do
|
||||
{:ok, run(fun, [state])}
|
||||
end
|
||||
|
||||
defp initial_call(mfa) do
|
||||
_ = Process.put(:"$initial_call", get_initial_call(mfa))
|
||||
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 get_initial_call(fun) when is_function(fun, 0) do
|
||||
{:module, module} = :erlang.fun_info(fun, :module)
|
||||
{:name, name} = :erlang.fun_info(fun, :name)
|
||||
{module, name, 0}
|
||||
end
|
||||
|
||||
defp get_initial_call({mod, fun, args}) do
|
||||
{mod, fun, length(args)}
|
||||
end
|
||||
|
||||
defp run({m, f, a}, extra), do: apply(m, f, extra ++ a)
|
||||
defp run(fun, extra), do: apply(fun, extra)
|
||||
end
|
||||
|
||||
+37
-114
@@ -4,29 +4,29 @@ defmodule Application do
|
||||
|
||||
In Elixir (actually, in Erlang/OTP), an application is a component
|
||||
implementing some specific functionality, that can be started and stopped
|
||||
as a unit, and which can be re-used in other systems.
|
||||
as a unit, and which can be re-used in other systems as well.
|
||||
|
||||
Applications are defined with an application file named `APP.app` where
|
||||
`APP` is the application name, usually in `underscore_case`. The application
|
||||
file must reside in the same `ebin` directory as the compiled modules of the
|
||||
application.
|
||||
`APP` is the APP name, usually in `underscore_case` convention. The
|
||||
application file must reside in the same `ebin` directory as the
|
||||
application's modules bytecode.
|
||||
|
||||
In Elixir, Mix is responsible for compiling your source code and
|
||||
generating your application `.app` file. Furthermore, Mix is also
|
||||
responsible for configuring, starting and stopping your application
|
||||
and its dependencies. For this reason, this documentation will focus
|
||||
on the remaining aspects of your application: the application environment
|
||||
on the remaining aspects of your application: the application environment,
|
||||
and the application callback module.
|
||||
|
||||
You can learn more about Mix generation of `.app` files by typing
|
||||
You can learn more about Mix compilation of `.app` files by typing
|
||||
`mix help compile.app`.
|
||||
|
||||
## Application environment
|
||||
|
||||
Once an application is started, OTP provides an application environment
|
||||
that can be used to configure the application.
|
||||
that can be used to configure applications.
|
||||
|
||||
Assuming you are inside a Mix project, you can edit the `application`
|
||||
Assuming you are inside a Mix project, you can edit your application
|
||||
function in the `mix.exs` file to the following:
|
||||
|
||||
def application do
|
||||
@@ -38,15 +38,18 @@ defmodule Application do
|
||||
can access the default value:
|
||||
|
||||
Application.get_env(:APP_NAME, :hello)
|
||||
#=> :world
|
||||
#=> {:ok, :hello}
|
||||
|
||||
It is also possible to put and delete values from the application value,
|
||||
including new values that are not defined in the environment file (although
|
||||
this should be avoided).
|
||||
those should be avoided).
|
||||
|
||||
In the future, we plan to support configuration files which allows
|
||||
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
|
||||
Do not use the functions in this module for directly access or modify
|
||||
the environment of other application (as it may lead to inconsistent
|
||||
data in the application environment).
|
||||
|
||||
## Application module callback
|
||||
@@ -61,8 +64,8 @@ defmodule Application do
|
||||
end
|
||||
|
||||
Our application now requires the `MyApp` module to provide an application
|
||||
callback. This can be done by invoking `use Application` in that module and
|
||||
defining a `start/2` callback, for example:
|
||||
callback. This can be done by invoking `use Application` in that module
|
||||
and defining a `start/2` callback, for example:
|
||||
|
||||
defmodule MyApp do
|
||||
use Application
|
||||
@@ -72,22 +75,21 @@ defmodule Application do
|
||||
end
|
||||
end
|
||||
|
||||
`start/2` typically returns `{:ok, pid}` or `{:ok, pid, state}` where
|
||||
`pid` identifies the supervision tree and `state` is the application state.
|
||||
`args` is the second element of the tuple given to the `:mod` option.
|
||||
`start/2` most commonly returns `{:ok, pid}` or `{:ok, pid, state}` where
|
||||
`pid` identifies the supervision tree and the state is the application state.
|
||||
`args` is second element of the tuple given to the `:mod` option.
|
||||
|
||||
The `type` argument passed to `start/2` is usually `:normal` unless in a
|
||||
distributed setup where application takeovers and failovers are configured.
|
||||
This particular aspect of applications is explained in more detail in the
|
||||
OTP documentation:
|
||||
The `type` passed into `start/2` is usually `:normal` unless in a distributed
|
||||
setup where applications takeover and failovers are configured. This particular
|
||||
aspect of applications can be read with more detail in the OTP documentation:
|
||||
|
||||
* [`:application` module](http://www.erlang.org/doc/man/application.html)
|
||||
* [Applications – OTP Design Principles](http://www.erlang.org/doc/design_principles/applications.html)
|
||||
* http://www.erlang.org/doc/man/application.html
|
||||
* http://www.erlang.org/doc/design_principles/applications.html
|
||||
|
||||
A developer may also implement the `stop/1` callback (automatically defined
|
||||
by `use Application`) which does any application cleanup. It receives the
|
||||
application state and can return any value. Note that shutting down the
|
||||
supervisor is automatically handled by the VM.
|
||||
application state and can return any value. Notice that shutting down the
|
||||
supervisor is automatically handled by the VM;
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@@ -109,61 +111,10 @@ defmodule Application do
|
||||
@type value :: 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 """
|
||||
Get 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`.
|
||||
"""
|
||||
@spec get_all_env(app) :: [{key, value}]
|
||||
@spec get_all_env(app) :: [{key,value}]
|
||||
def get_all_env(app) do
|
||||
:application.get_all_env(app)
|
||||
end
|
||||
@@ -171,18 +122,22 @@ defmodule Application do
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, the function returns the
|
||||
`default` value.
|
||||
If the specified application is not loaded, or the configuration parameter
|
||||
does not exist, the function returns the `default` value.
|
||||
"""
|
||||
@spec get_env(app, key, value) :: value
|
||||
def get_env(app, key, default \\ nil) do
|
||||
:application.get_env(app, key, default)
|
||||
case :application.get_env(app, key) do
|
||||
{:ok, value} -> value
|
||||
:undefined -> default
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment in a tuple.
|
||||
|
||||
If the configuration parameter does not exist, the function returns `:error`.
|
||||
If the specified application is not loaded, or the configuration parameter
|
||||
does not exist, the function returns `:error`.
|
||||
"""
|
||||
@spec fetch_env(app, key) :: {:ok, value} | :error
|
||||
def fetch_env(app, key) do
|
||||
@@ -192,22 +147,6 @@ defmodule Application do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the value for `key` in `app`'s environment.
|
||||
|
||||
If the configuration parameter does not exist, raises `ArgumentError`.
|
||||
"""
|
||||
@spec fetch_env!(app, key) :: value | no_return
|
||||
def fetch_env!(app, key) do
|
||||
case fetch_env(app, key) do
|
||||
{:ok, value} -> value
|
||||
:error ->
|
||||
raise ArgumentError,
|
||||
"application #{inspect app} is not loaded, " <>
|
||||
"or the configuration parameter #{inspect key} is not set"
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the `value` in `key` for the given `app`.
|
||||
|
||||
@@ -220,7 +159,7 @@ defmodule Application do
|
||||
environment values specified in the `.app` file will override the ones
|
||||
previously set.
|
||||
|
||||
The persistent option can be set to `true` when there is a need to guarantee
|
||||
The persistent option can be set to true when there is a need to guarantee
|
||||
parameters set with this function will not be overridden by the ones defined
|
||||
in the application resource file on load. This means persistent values will
|
||||
stick after the application is loaded and also on application reload.
|
||||
@@ -381,22 +320,6 @@ defmodule Application do
|
||||
Path.join(app_dir(app), path)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which are currently running.
|
||||
"""
|
||||
@spec started_applications(timeout) :: [tuple]
|
||||
def started_applications(timeout \\ 5000) do
|
||||
:application.which_applications(timeout)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with information about the applications which have been loaded.
|
||||
"""
|
||||
@spec loaded_applications :: [tuple]
|
||||
def loaded_applications do
|
||||
:application.loaded_applications
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the error reason returned by `start/2`,
|
||||
`ensure_started/2`, `stop/1`, `load/1` and `unload/1`,
|
||||
|
||||
+2
-16
@@ -1,20 +1,12 @@
|
||||
defmodule Atom do
|
||||
@moduledoc """
|
||||
@doc """
|
||||
Convenience functions for working with atoms.
|
||||
|
||||
See also `Kernel.is_atom/1`.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Converts an atom to a string.
|
||||
Converts an atom to string.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_string(:foo)
|
||||
"foo"
|
||||
|
||||
"""
|
||||
@spec to_string(atom) :: String.t
|
||||
def to_string(atom) do
|
||||
@@ -25,12 +17,6 @@ defmodule Atom do
|
||||
Converts an atom to a char list.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Atom.to_char_list(:"An atom")
|
||||
'An atom'
|
||||
|
||||
"""
|
||||
@spec to_char_list(atom) :: char_list
|
||||
def to_char_list(atom) do
|
||||
|
||||
+119
-381
@@ -98,55 +98,49 @@ 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.
|
||||
|
||||
@@ -166,9 +160,10 @@ 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.
|
||||
|
||||
@@ -190,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
|
||||
@@ -220,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_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode64(data, pad_flag)
|
||||
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
|
||||
@@ -291,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.
|
||||
@@ -305,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_flag = Keyword.get(opts, :padding, true)
|
||||
string |> filter_ignored(opts[:ignore]) |> do_decode64(pad_flag)
|
||||
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>>)
|
||||
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_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode64url(data, pad_flag)
|
||||
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>>}
|
||||
{: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
|
||||
@@ -379,32 +306,18 @@ 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.
|
||||
|
||||
## Examples
|
||||
|
||||
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>>
|
||||
<<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_flag = Keyword.get(opts, :padding, true)
|
||||
string |> filter_ignored(opts[:ignore]) |> do_decode64url(pad_flag)
|
||||
def url_decode64!(string) when is_binary(string) do
|
||||
do_decode64(string, &dec64url/1)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -413,9 +326,6 @@ defmodule Base do
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.encode32("foobar")
|
||||
@@ -424,16 +334,12 @@ 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_flag = Keyword.get(opts, :padding, true)
|
||||
do_encode32(case, data, pad_flag)
|
||||
do_encode32(data, encode_case(case, &enc32/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -443,9 +349,6 @@ defmodule Base do
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.decode32("MZXW6YTBOI======")
|
||||
@@ -457,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
|
||||
@@ -476,9 +377,6 @@ defmodule Base do
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
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.
|
||||
|
||||
@@ -493,16 +391,12 @@ 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_flag = Keyword.get(opts, :padding, true)
|
||||
do_decode32(case, string, pad_flag)
|
||||
do_decode32(string, decode_case(case, &dec32/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -512,9 +406,6 @@ defmodule Base do
|
||||
Accepts an atom `:upper` (default) for encoding to upper case characters or
|
||||
`:lower` for lower case characters.
|
||||
|
||||
Accepts `padding: false` option which will omit padding from
|
||||
the output string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_encode32("foobar")
|
||||
@@ -523,16 +414,12 @@ 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_flag = Keyword.get(opts, :padding, true)
|
||||
do_hex_encode32(case, data, pad_flag)
|
||||
do_encode32(data, encode_case(case, &enc32hex/1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -543,9 +430,6 @@ defmodule Base do
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
Accepts `padding: false` option which will ignore padding from
|
||||
the input string.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Base.hex_decode32("CPNMUOJ1E8======")
|
||||
@@ -557,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
|
||||
@@ -577,9 +459,6 @@ defmodule Base do
|
||||
`:lower` for lower case characters. `:mixed` can be given for mixed case
|
||||
characters.
|
||||
|
||||
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.
|
||||
|
||||
@@ -594,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_flag = Keyword.get(opts, :padding, true)
|
||||
do_hex_decode32(case, string, pad_flag)
|
||||
do_decode32(string, decode_case(case, &dec32hex/1))
|
||||
end
|
||||
|
||||
defp filter_ignored(string, nil), do: string
|
||||
defp filter_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_flag) 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_flag, 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_flag) 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_flag) 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_flag, 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_flag) 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_flag) 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_flag, 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_flag) 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_flag) 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_flag, 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_flag) 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
|
||||
|
||||
+85
-33
@@ -1,27 +1,60 @@
|
||||
defmodule Behaviour do
|
||||
@moduledoc """
|
||||
This module has been deprecated.
|
||||
Utilities for defining behaviour interfaces.
|
||||
|
||||
Instead of `defcallback`, one can simply use `@callback`.
|
||||
Instead of `defmacrocallback`, one can simply use `@macrocallback`.
|
||||
Instead of `__behaviour__(:callbacks)`, one can simply use `behaviour_info(:callbacks)`.
|
||||
Behaviours can be referenced by other modules
|
||||
to ensure they implement required callbacks.
|
||||
|
||||
For example, you can specify the `URI.Parser`
|
||||
behaviour as follows:
|
||||
|
||||
defmodule URI.Parser do
|
||||
use Behaviour
|
||||
|
||||
@doc "Parses the given URL"
|
||||
defcallback parse(uri_info :: URI.t) :: URI.t
|
||||
|
||||
@doc "Defines a default port"
|
||||
defcallback default_port() :: integer
|
||||
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
|
||||
end
|
||||
|
||||
If the behaviour changes or `URI.HTTP` does
|
||||
not implement one of the callbacks, a warning
|
||||
will be raised.
|
||||
|
||||
## Implementation
|
||||
|
||||
Since Erlang R15, behaviours must be defined via
|
||||
`@callback` attributes. `defcallback` is a simple
|
||||
mechanism that defines the `@callback` attribute
|
||||
according to the given type specification. `defcallback` allows
|
||||
documentation to be created for the callback and defines
|
||||
a custom function signature.
|
||||
|
||||
The callbacks and their documentation can be retrieved
|
||||
via the `__behaviour__` callback function.
|
||||
"""
|
||||
|
||||
# TODO: Deprecate by 1.2
|
||||
# TODO: Remove by 2.0
|
||||
|
||||
@doc """
|
||||
Defines a function callback according to the given type specification.
|
||||
Define a function callback according to the given type specification.
|
||||
"""
|
||||
defmacro defcallback(spec) do
|
||||
do_defcallback(:def, split_spec(spec, quote(do: term)))
|
||||
do_defcallback(split_spec(spec, quote(do: term)), __CALLER__)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a macro callback according to the given type specification.
|
||||
Define a macro callback according to the given type specification.
|
||||
"""
|
||||
defmacro defmacrocallback(spec) do
|
||||
do_defcallback(:defmacro, split_spec(spec, quote(do: Macro.t)))
|
||||
do_defmacrocallback(split_spec(spec, quote(do: Macro.t)), __CALLER__)
|
||||
end
|
||||
|
||||
defp split_spec({:when, _, [{:::, _, [spec, return]}, guard]}, _default) do
|
||||
@@ -40,17 +73,27 @@ defmodule Behaviour do
|
||||
{spec, default, []}
|
||||
end
|
||||
|
||||
defp do_defcallback(kind, {spec, return, guards}) do
|
||||
defp do_defcallback({spec, return, guards}, caller) do
|
||||
case Macro.decompose_call(spec) do
|
||||
{name, args} ->
|
||||
do_callback(kind, name, args, return, guards)
|
||||
do_callback(:def, name, args, name, length(args), args, return, guards, caller)
|
||||
_ ->
|
||||
raise ArgumentError, "invalid syntax in #{kind}callback #{Macro.to_string(spec)}"
|
||||
raise ArgumentError, "invalid syntax in defcallback #{Macro.to_string(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp do_callback(kind, name, args, return, guards) do
|
||||
:lists.foreach fn
|
||||
defp do_defmacrocallback({spec, return, guards}, caller) do
|
||||
case Macro.decompose_call(spec) do
|
||||
{name, args} ->
|
||||
do_callback(:defmacro, :"MACRO-#{name}", [quote(do: env :: Macro.Env.t)|args],
|
||||
name, length(args), args, return, guards, caller)
|
||||
_ ->
|
||||
raise ArgumentError, "invalid syntax in defmacrocallback #{Macro.to_string(spec)}"
|
||||
end
|
||||
end
|
||||
|
||||
defp do_callback(kind, name, args, docs_name, docs_arity, _docs_args, return, guards, caller) do
|
||||
Enum.each args, fn
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
@@ -58,16 +101,12 @@ defmodule Behaviour do
|
||||
other ->
|
||||
ensure_not_default(other)
|
||||
other
|
||||
end, args
|
||||
end
|
||||
|
||||
spec =
|
||||
quote do
|
||||
unquote(name)(unquote_splicing(args)) :: unquote(return) when unquote(guards)
|
||||
end
|
||||
|
||||
case kind do
|
||||
:def -> quote(do: @callback unquote(spec))
|
||||
:defmacro -> quote(do: @macrocallback unquote(spec))
|
||||
quote do
|
||||
@callback unquote(name)(unquote_splicing(args)) :: unquote(return) when unquote(guards)
|
||||
Behaviour.store_docs(__MODULE__, unquote(caller.line), unquote(kind),
|
||||
unquote(docs_name), unquote(docs_arity))
|
||||
end
|
||||
end
|
||||
|
||||
@@ -77,8 +116,28 @@ defmodule Behaviour do
|
||||
|
||||
defp ensure_not_default(_), do: :ok
|
||||
|
||||
@doc false
|
||||
def store_docs(module, line, kind, name, arity) do
|
||||
doc = Module.get_attribute module, :doc
|
||||
Module.delete_attribute module, :doc
|
||||
Module.put_attribute module, :behaviour_docs, {{name, arity}, line, kind, doc}
|
||||
end
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote do
|
||||
Module.register_attribute(__MODULE__, :behaviour_docs, accumulate: true)
|
||||
@before_compile unquote(__MODULE__)
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
defmacro __before_compile__(env) do
|
||||
docs = if Code.compiler_options[:docs] do
|
||||
Enum.reverse Module.get_attribute(env.module, :behaviour_docs)
|
||||
end
|
||||
|
||||
quote do
|
||||
@doc false
|
||||
def __behaviour__(:callbacks) do
|
||||
@@ -86,15 +145,8 @@ defmodule Behaviour do
|
||||
end
|
||||
|
||||
def __behaviour__(:docs) do
|
||||
for {tuple, line, kind, docs} <- Code.get_docs(__MODULE__, :callback_docs) do
|
||||
case kind do
|
||||
:callback -> {tuple, line, :def, docs}
|
||||
:macrocallback -> {tuple, line, :defmacro, docs}
|
||||
end
|
||||
end
|
||||
unquote(Macro.escape(docs))
|
||||
end
|
||||
|
||||
import unquote(__MODULE__)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+25
-104
@@ -1,9 +1,10 @@
|
||||
defmodule Bitwise do
|
||||
@moduledoc """
|
||||
This module provides macro-based operators that perform calculations
|
||||
on (sets of) bits.
|
||||
This module provides macros and operators for bitwise operators.
|
||||
These macros can be used in guards.
|
||||
|
||||
In general, you should `use` the Bitwise module as a whole:
|
||||
The easiest way to use is to simply import them into
|
||||
your module:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> bnot 1
|
||||
@@ -11,26 +12,22 @@ defmodule Bitwise do
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
When used, it accepts the following options:
|
||||
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
You can select to include only or skip operators by passing options:
|
||||
|
||||
iex> use Bitwise, only_operators: true
|
||||
iex> 1 &&& 1
|
||||
1
|
||||
|
||||
|
||||
These macros can be used in guards:
|
||||
|
||||
iex> use Bitwise
|
||||
iex> odd? = fn(int) when band(int, 1) == 1 -> true; (_) -> false end
|
||||
iex> odd?.(1)
|
||||
true
|
||||
|
||||
"""
|
||||
|
||||
@doc false
|
||||
@doc """
|
||||
Allow a developer to use this module in their programs with
|
||||
the following options:
|
||||
|
||||
* `:only_operators` - include only operators
|
||||
* `:skip_operators` - skip operators
|
||||
|
||||
"""
|
||||
defmacro __using__(options) do
|
||||
except = cond do
|
||||
Keyword.get(options, :only_operators) ->
|
||||
@@ -46,160 +43,84 @@ defmodule Bitwise do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> bnot(2)
|
||||
-3
|
||||
iex> bnot(2) &&& 3
|
||||
1
|
||||
|
||||
Bitwise not.
|
||||
"""
|
||||
defmacro bnot(expr) do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Prefix (unary) operator; calculates the bitwise NOT of its argument.
|
||||
|
||||
iex> ~~~2
|
||||
-3
|
||||
iex> ~~~2 &&& 3
|
||||
1
|
||||
|
||||
Bitwise not as operator.
|
||||
"""
|
||||
defmacro ~~~expr do
|
||||
quote do: :erlang.bnot(unquote(expr))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise AND of its arguments.
|
||||
|
||||
iex> band(9, 3)
|
||||
1
|
||||
|
||||
Bitwise and.
|
||||
"""
|
||||
defmacro band(left, right) do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise AND of its arguments.
|
||||
|
||||
iex> 9 &&& 3
|
||||
1
|
||||
|
||||
Bitwise and as operator.
|
||||
"""
|
||||
defmacro left &&& right do
|
||||
quote do: :erlang.band(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise OR of its arguments.
|
||||
|
||||
iex> bor(9, 3)
|
||||
11
|
||||
|
||||
Bitwise or.
|
||||
"""
|
||||
defmacro bor(left, right) do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise OR of its arguments.
|
||||
|
||||
iex> 9 ||| 3
|
||||
11
|
||||
|
||||
Bitwise or as operator.
|
||||
"""
|
||||
defmacro left ||| right do
|
||||
quote do: :erlang.bor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the bitwise XOR of its arguments.
|
||||
|
||||
iex> bxor(9, 3)
|
||||
10
|
||||
|
||||
Bitwise xor.
|
||||
"""
|
||||
defmacro bxor(left, right) do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the bitwise XOR of its arguments.
|
||||
|
||||
iex> 9 ^^^ 3
|
||||
10
|
||||
|
||||
Bitwise xor as operator.
|
||||
"""
|
||||
defmacro left ^^^ right do
|
||||
quote do: :erlang.bxor(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic left bitshift.
|
||||
|
||||
iex> bsl(1, 2)
|
||||
4
|
||||
iex> bsl(1, -2)
|
||||
0
|
||||
iex> bsl(-1, 2)
|
||||
-4
|
||||
iex> bsl(-1, -2)
|
||||
-1
|
||||
|
||||
Arithmetic bitshift left.
|
||||
"""
|
||||
defmacro bsl(left, right) do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the result of an arithmetic left bitshift.
|
||||
|
||||
iex> 1 <<< 2
|
||||
4
|
||||
iex> 1 <<< -2
|
||||
0
|
||||
iex> -1 <<< 2
|
||||
-4
|
||||
iex> -1 <<< -2
|
||||
-1
|
||||
|
||||
Arithmetic bitshift left as operator.
|
||||
"""
|
||||
defmacro left <<< right do
|
||||
quote do: :erlang.bsl(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Calculates the result of an arithmetic right bitshift.
|
||||
|
||||
iex> bsr(1, 2)
|
||||
0
|
||||
iex> bsr(1, -2)
|
||||
4
|
||||
iex> bsr(-1, 2)
|
||||
-1
|
||||
iex> bsr(-1, -2)
|
||||
-4
|
||||
|
||||
Arithmetic bitshift right.
|
||||
"""
|
||||
defmacro bsr(left, right) do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Infix operator; calculates the result of an arithmetic right bitshift.
|
||||
|
||||
iex> 1 >>> 2
|
||||
0
|
||||
iex> 1 >>> -2
|
||||
4
|
||||
iex> -1 >>> 2
|
||||
-1
|
||||
iex> -1 >>> -2
|
||||
-4
|
||||
|
||||
Arithmetic bitshift right as operator.
|
||||
"""
|
||||
defmacro left >>> right do
|
||||
quote do: :erlang.bsr(unquote(left), unquote(right))
|
||||
|
||||
+48
-155
@@ -2,112 +2,84 @@ defmodule Code do
|
||||
@moduledoc """
|
||||
Utilities for managing code compilation, code evaluation and code loading.
|
||||
|
||||
This module complements Erlang's [`:code` module](http://www.erlang.org/doc/man/code.html)
|
||||
to add behaviour which is specific to Elixir. Almost all of the functions in this module
|
||||
have global side effects on the behaviour of Elixir.
|
||||
This module complements [Erlang's code module](http://www.erlang.org/doc/man/code.html)
|
||||
to add behaviour which is specific to Elixir.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Lists all loaded files.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.require_file("../eex/test/eex_test.exs")
|
||||
List.first(Code.loaded_files) =~ "eex_test.exs" #=> true
|
||||
|
||||
List all loaded files.
|
||||
"""
|
||||
def loaded_files do
|
||||
:elixir_code_server.call :loaded
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes files from the loaded files list.
|
||||
Remove files from the loaded files list.
|
||||
|
||||
The modules defined in the file are not removed;
|
||||
calling this function only removes them from the list,
|
||||
allowing them to be required again.
|
||||
|
||||
## Examples
|
||||
|
||||
# Load EEx test code, unload file, check for functions still available
|
||||
Code.load_file("../eex/test/eex_test.exs")
|
||||
Code.unload_files(Code.loaded_files)
|
||||
function_exported?(EExTest.Compiled, :before_compile, 0) #=> true
|
||||
|
||||
"""
|
||||
def unload_files(files) do
|
||||
:elixir_code_server.cast {:unload_files, files}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Appends a path to the end of the Erlang VM code path list.
|
||||
|
||||
This is the list of directories the Erlang VM uses for
|
||||
finding module code.
|
||||
Append a path to the Erlang VM code path.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being appended.
|
||||
If this path does not exist, an error is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.append_path(".") #=> true
|
||||
|
||||
Code.append_path("/does_not_exist") #=> {:error, :bad_directory}
|
||||
|
||||
"""
|
||||
def append_path(path) do
|
||||
:code.add_pathz(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
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.
|
||||
Prepend a path to the Erlang VM code path.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being prepended.
|
||||
If this path does not exist, an error is returned.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.prepend_path(".") #=> true
|
||||
|
||||
Code.prepend_path("/does_not_exist") #=> {:error, :bad_directory}
|
||||
|
||||
"""
|
||||
def prepend_path(path) do
|
||||
:code.add_patha(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a path from the Erlang VM code path list. This is the list of
|
||||
directories the Erlang VM uses for finding module code.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being deleted. If the
|
||||
path does not exist it returns `false`.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.prepend_path(".")
|
||||
Code.delete_path(".") #=> true
|
||||
|
||||
Code.delete_path("/does_not_exist") #=> false
|
||||
Delete a path from the Erlang VM code path.
|
||||
|
||||
The path is expanded with `Path.expand/1` before being deleted.
|
||||
"""
|
||||
def delete_path(path) do
|
||||
:code.del_path(to_char_list(Path.expand path))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the contents given by `string`.
|
||||
Re-add paths given to the command line to keep their position
|
||||
on the overall code path.
|
||||
|
||||
Some tools may change the code path by prepending new items but
|
||||
still want the paths given by the user to have higher priority.
|
||||
Calling this function guarantees the paths are re-added on
|
||||
top of the user given ones.
|
||||
"""
|
||||
@spec readd_paths() :: :ok
|
||||
def readd_paths() do
|
||||
{pa, pz} = :elixir_code_server.call(:paths)
|
||||
:code.add_pathsa(pa)
|
||||
:code.add_pathsz(pz)
|
||||
:ok
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluate the contents given by `string`.
|
||||
|
||||
The `binding` argument is a keyword list of variable bindings.
|
||||
The `opts` argument is a keyword list of environment options.
|
||||
|
||||
Those options can be:
|
||||
|
||||
* `:file` - the file to be considered in the evaluation
|
||||
* `:line` - the line on which the script starts
|
||||
* `:file` - the file to be considered in the evaluation
|
||||
* `:line` - the line on which the script starts
|
||||
* `:delegate_locals_to` - delegate local calls to the given module,
|
||||
the default is to not delegate
|
||||
|
||||
Additionally, the following scope values can be configured:
|
||||
|
||||
@@ -170,7 +142,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Evaluates the quoted contents.
|
||||
Evaluate the quoted contents.
|
||||
|
||||
See `eval_string/3` for a description of arguments and return values.
|
||||
|
||||
@@ -239,7 +211,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
Convert the given string to its quoted form.
|
||||
|
||||
Returns `{:ok, quoted_form}`
|
||||
if it succeeds, `{:error, {line, error, token}}` otherwise.
|
||||
@@ -267,7 +239,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the given string to its quoted form.
|
||||
Convert the given string to its quoted form.
|
||||
|
||||
It returns the ast if it succeeds,
|
||||
raises an exception otherwise. The exception is a `TokenMissingError`
|
||||
@@ -297,7 +269,7 @@ defmodule Code do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Loads the given file.
|
||||
Load the given file.
|
||||
|
||||
Accepts `relative_to` as an argument to tell where the file is located.
|
||||
If the file was already required/loaded, loads it again.
|
||||
@@ -308,12 +280,6 @@ defmodule Code do
|
||||
Notice that if `load_file` is invoked by different processes concurrently,
|
||||
the target file will be loaded concurrently many times. Check `require_file/2`
|
||||
if you don't want a file to be loaded concurrently.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.load_file("eex_test.exs","../eex/test") |> List.first
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
"""
|
||||
def load_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
@@ -336,20 +302,7 @@ defmodule Code do
|
||||
N times with a given file, it will be loaded only once. The first process to
|
||||
call `require_file` will get the list of loaded modules, others will get `nil`.
|
||||
|
||||
Check `load_file/2` if you want a file to be loaded multiple times. See also
|
||||
`unload_files/1`
|
||||
|
||||
## Examples
|
||||
|
||||
If the code is already loaded, it returns `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
|
||||
#=> {EExTest.Compiled, <<70, 79, 82, 49, ...>>}
|
||||
|
||||
Check `load_file/2` if you want a file to be loaded multiple times.
|
||||
"""
|
||||
def require_file(file, relative_to \\ nil) when is_binary(file) do
|
||||
file = find_file(file, relative_to)
|
||||
@@ -370,28 +323,15 @@ defmodule Code do
|
||||
Gets the compilation options from the code server.
|
||||
|
||||
Check `compiler_options/1` for more information.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: false}
|
||||
|
||||
"""
|
||||
def compiler_options do
|
||||
:elixir_config.get :compiler_options
|
||||
:elixir_code_server.call :compiler_options
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list with the available compiler options.
|
||||
|
||||
See `Code.compiler_options/1` for more info.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.available_compiler_options
|
||||
#=> [:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
|
||||
"""
|
||||
def available_compiler_options do
|
||||
[:docs, :debug_info, :ignore_module_conflict, :warnings_as_errors]
|
||||
@@ -417,23 +357,14 @@ defmodule Code do
|
||||
* `:warnings_as_errors` - cause compilation to fail when warnings are
|
||||
generated
|
||||
|
||||
It returns the new list of compiler options.
|
||||
|
||||
## Examples
|
||||
|
||||
Code.compiler_options(debug_info: true)
|
||||
#=> %{debug_info: true, docs: true,
|
||||
warnings_as_errors: false, ignore_module_conflict: 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
|
||||
:elixir_code_server.cast {:compiler_options, opts}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -480,7 +411,7 @@ defmodule Code do
|
||||
module uses this function to check if a specific parser exists for a given
|
||||
URI scheme.
|
||||
|
||||
## Code.ensure_compiled/1
|
||||
## `Code.ensure_compiled/1`
|
||||
|
||||
Elixir also contains an `ensure_compiled/1` function that is a
|
||||
superset of `ensure_loaded/1`.
|
||||
@@ -495,15 +426,6 @@ defmodule Code do
|
||||
In most cases, `ensure_loaded/1` is enough. `ensure_compiled/1`
|
||||
must be used in rare cases, usually involving macros that need to
|
||||
invoke a module for callback information.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.ensure_loaded(Atom)
|
||||
{:module, Atom}
|
||||
|
||||
iex> Code.ensure_loaded(DoesNotExist)
|
||||
{:error, :nofile}
|
||||
|
||||
"""
|
||||
def ensure_loaded(module) when is_atom(module) do
|
||||
:code.ensure_loaded(module)
|
||||
@@ -515,12 +437,6 @@ defmodule Code do
|
||||
Similar to `ensure_loaded/1`, but returns `true` if the module
|
||||
is already loaded or was successfully loaded. Returns `false`
|
||||
otherwise.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Code.ensure_loaded?(Atom)
|
||||
true
|
||||
|
||||
"""
|
||||
def ensure_loaded?(module) do
|
||||
match?({:module, ^module}, ensure_loaded(module))
|
||||
@@ -567,7 +483,7 @@ defmodule Code do
|
||||
match?({:module, ^module}, ensure_compiled(module))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@doc """
|
||||
Returns the docs for the given module.
|
||||
|
||||
When given a module name, it finds its BEAM code and reads the docs from it.
|
||||
@@ -584,33 +500,10 @@ defmodule Code do
|
||||
which module definition starts and `doc` is the string
|
||||
attached to the module using the `@moduledoc` attribute
|
||||
|
||||
* `:callback_docs` - list of all docstrings attached to
|
||||
`@callbacks` using the `@doc` attribute
|
||||
|
||||
* `:type_docs` - list of all docstrings attached to
|
||||
`@type` callbacks using the `@typedoc` attribute
|
||||
|
||||
* `:all` - a keyword list with `:docs` and `:moduledoc`, `:callback_docs`,
|
||||
and `:type_docs`.
|
||||
|
||||
If the module cannot be found, it returns `nil`.
|
||||
|
||||
## Examples
|
||||
|
||||
# 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)
|
||||
"Converts an atom to a char list."
|
||||
|
||||
# Module doesn't exist
|
||||
iex> Code.get_docs(ModuleNotGood, :all)
|
||||
nil
|
||||
* `:all` - a keyword list with both `:docs` and `:moduledoc`
|
||||
|
||||
"""
|
||||
@doc_kinds [:docs, :moduledoc, :callback_docs, :type_docs, :all]
|
||||
|
||||
def get_docs(module, kind) when is_atom(module) and kind in @doc_kinds do
|
||||
def get_docs(module, kind) when is_atom(module) do
|
||||
case :code.get_object_code(module) do
|
||||
{_module, bin, _beam_path} ->
|
||||
do_get_docs(bin, kind)
|
||||
@@ -619,7 +512,7 @@ defmodule Code do
|
||||
end
|
||||
end
|
||||
|
||||
def get_docs(binpath, kind) when is_binary(binpath) and kind in @doc_kinds do
|
||||
def get_docs(binpath, kind) when is_binary(binpath) do
|
||||
do_get_docs(String.to_char_list(binpath), kind)
|
||||
end
|
||||
|
||||
@@ -641,7 +534,7 @@ defmodule Code do
|
||||
defp lookup_docs(_, _), do: nil
|
||||
|
||||
defp do_lookup_docs(docs, :all), do: docs
|
||||
defp do_lookup_docs(docs, kind),
|
||||
defp do_lookup_docs(docs, kind) when kind in [:docs, :moduledoc],
|
||||
do: Keyword.get(docs, kind)
|
||||
|
||||
## Helpers
|
||||
|
||||
+397
-102
@@ -1,55 +1,136 @@
|
||||
defmodule Dict do
|
||||
@moduledoc ~S"""
|
||||
WARNING: this module is deprecated.
|
||||
This module specifies the Dict API expected to be
|
||||
implemented by different dictionaries. It also provides
|
||||
functions that redirect to the underlying Dict, allowing
|
||||
a developer to work with different Dict implementations
|
||||
using one API.
|
||||
|
||||
If you need a general dictionary, use the `Map` module.
|
||||
If you need to manipulate keyword lists, use `Keyword`.
|
||||
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`.
|
||||
|
||||
## 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 implemtented 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`
|
||||
* `has_key?/2`
|
||||
* `keys/1`
|
||||
* `merge/2`
|
||||
* `merge/3`
|
||||
* `pop/2`
|
||||
* `pop/3`
|
||||
* `put_new/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.
|
||||
"""
|
||||
|
||||
use Behaviour
|
||||
|
||||
@type key :: any
|
||||
@type value :: any
|
||||
@type t :: list | map
|
||||
|
||||
# TODO: Remove callbacks on 1.3
|
||||
# TODO: Deprecate every function on 1.3
|
||||
@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)
|
||||
defcallback new :: t
|
||||
defcallback delete(t, key) :: t
|
||||
defcallback drop(t, Enum.t) :: t
|
||||
defcallback equal?(t, t) :: boolean
|
||||
defcallback get(t, key) :: value
|
||||
defcallback get(t, key, value) :: value
|
||||
defcallback fetch(t, key) :: {:ok, value} | :error
|
||||
defcallback fetch!(t, key) :: value | no_return
|
||||
defcallback has_key?(t, key) :: boolean
|
||||
defcallback keys(t) :: [key]
|
||||
defcallback merge(t, t) :: t
|
||||
defcallback merge(t, t, (key, value, value -> value)) :: t
|
||||
defcallback pop(t, key) :: {value, t}
|
||||
defcallback pop(t, key, value) :: {value, t}
|
||||
defcallback put(t, key, value) :: t
|
||||
defcallback put_new(t, key, value) :: t
|
||||
defcallback size(t) :: non_neg_integer()
|
||||
defcallback split(t, Enum.t) :: {t, t}
|
||||
defcallback take(t, Enum.t) :: t
|
||||
defcallback to_list(t) :: list()
|
||||
defcallback update(t, key, value, (value -> value)) :: t
|
||||
defcallback update!(t, key, (value -> value)) :: t | no_return
|
||||
defcallback 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
|
||||
|
||||
@@ -60,19 +141,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
def get_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
:error -> fun.()
|
||||
end
|
||||
end
|
||||
|
||||
def get_and_update(dict, key, fun) do
|
||||
current_value = get(dict, key)
|
||||
{get, new_value} = fun.(current_value)
|
||||
{get, put(dict, key, new_value)}
|
||||
end
|
||||
|
||||
def fetch!(dict, key) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} -> value
|
||||
@@ -91,13 +159,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
def put_new_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case has_key?(dict, key) do
|
||||
true -> dict
|
||||
false -> put(dict, key, fun.())
|
||||
end
|
||||
end
|
||||
|
||||
def drop(dict, keys) do
|
||||
Enum.reduce(keys, dict, &delete(&2, &1))
|
||||
end
|
||||
@@ -187,15 +248,6 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
def pop_lazy(dict, key, fun) when is_function(fun, 0) do
|
||||
case fetch(dict, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(dict, key)}
|
||||
:error ->
|
||||
{fun.(), dict}
|
||||
end
|
||||
end
|
||||
|
||||
def split(dict, keys) do
|
||||
Enum.reduce(keys, {new, dict}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
@@ -210,11 +262,11 @@ defmodule Dict do
|
||||
defoverridable merge: 2, merge: 3, equal?: 2, to_list: 1, keys: 1,
|
||||
values: 1, take: 2, drop: 2, get: 2, get: 3, fetch!: 2,
|
||||
has_key?: 2, put_new: 3, pop: 2, pop: 3, split: 2,
|
||||
update: 4, update!: 3, get_and_update: 3, get_lazy: 3,
|
||||
pop_lazy: 3, put_new_lazy: 3
|
||||
update: 4, update!: 3
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
defmacrop target(dict) do
|
||||
quote do
|
||||
case unquote(dict) do
|
||||
@@ -230,141 +282,380 @@ 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
|
||||
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
def get_lazy(dict, key, fun) do
|
||||
target(dict).get_lazy(dict, key, fun)
|
||||
end
|
||||
@doc """
|
||||
Returns `{:ok, value}` associated with `key` in `dict`.
|
||||
If `dict` does not contain `key`, returns `:error`.
|
||||
|
||||
@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
|
||||
## 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` already exists.
|
||||
|
||||
## 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
|
||||
|
||||
@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
|
||||
|
||||
@spec merge(t, t) :: t
|
||||
def merge(dict1, dict2) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
@doc """
|
||||
Merges the dict `dict2` into dict `dict1`.
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fn(_k, _v1, v2) -> v2 end)
|
||||
end
|
||||
end
|
||||
If one of the `dict2` entries already exists in `dict1`, the
|
||||
functions in entries in `dict2` have higher precedence unless a
|
||||
function is given to resolve conflicts.
|
||||
|
||||
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]
|
||||
|
||||
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
|
||||
def merge(dict1, dict2, fun \\ fn(_k, _v1, v2) -> v2 end) do
|
||||
target1 = target(dict1)
|
||||
target2 = target(dict2)
|
||||
|
||||
if target1 == target2 do
|
||||
target1.merge(dict1, dict2, fun)
|
||||
else
|
||||
do_merge(target1, dict1, dict2, fun)
|
||||
Enumerable.reduce(dict2, {:cont, dict1}, fn({k, v}, acc) ->
|
||||
{:cont, target1.update(acc, k, v, fn(other) -> fun.(k, other, v) end)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
end
|
||||
|
||||
defp do_merge(target1, dict1, dict2, fun) do
|
||||
Enumerable.reduce(dict2, {:cont, dict1}, fn({k, v}, acc) ->
|
||||
{:cont, target1.update(acc, k, v, fn(other) -> fun.(k, other, v) end)}
|
||||
end) |> elem(1)
|
||||
end
|
||||
@doc """
|
||||
Returns the value associated with `key` in `dict` as
|
||||
well as the `dict` without `key`.
|
||||
|
||||
## 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
|
||||
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(dict, key, fun) do
|
||||
target(dict).pop_lazy(dict, key, fun)
|
||||
end
|
||||
@doc """
|
||||
Update 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 """
|
||||
Update 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`
|
||||
|
||||
Any 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`.
|
||||
Any 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.
|
||||
|
||||
Any 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 """
|
||||
Check 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)
|
||||
@@ -378,7 +669,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)
|
||||
|
||||
@@ -387,6 +678,10 @@ defmodule Dict do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list of key-value pairs stored in `dict`.
|
||||
No particular order is enforced.
|
||||
"""
|
||||
@spec to_list(t) :: list
|
||||
def to_list(dict) do
|
||||
target(dict).to_list(dict)
|
||||
|
||||
+534
-1065
File diff suppressed because it is too large
Load Diff
+57
-99
@@ -4,7 +4,7 @@ defmodule Exception do
|
||||
|
||||
Note that stacktraces in Elixir are updated on throw,
|
||||
errors and exits. For example, at any given moment,
|
||||
`System.stacktrace/0` will return the stacktrace for the
|
||||
`System.stacktrace` will return the stacktrace for the
|
||||
last throw/error/exit that occurred in the current process.
|
||||
|
||||
Do not rely on the particular format returned by the `format`
|
||||
@@ -22,8 +22,8 @@ defmodule Exception do
|
||||
|
||||
@type stacktrace :: [stacktrace_entry]
|
||||
@type stacktrace_entry ::
|
||||
{module, atom, arity_or_args, location} |
|
||||
{(... -> any), arity_or_args, location}
|
||||
{module, function, arity_or_args, location} |
|
||||
{function, arity_or_args, location}
|
||||
|
||||
@typep arity_or_args :: non_neg_integer | list
|
||||
@typep location :: Keyword.t
|
||||
@@ -32,31 +32,22 @@ defmodule Exception do
|
||||
@callback message(t) :: String.t
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is an exception.
|
||||
Returns true if the given argument is an exception.
|
||||
"""
|
||||
def exception?(term)
|
||||
|
||||
def exception?(%{__struct__: struct, __exception__: true}) when is_atom(struct),
|
||||
do: true
|
||||
|
||||
def exception?(%{__struct__: struct, __exception__: true}) when is_atom(struct), do: true
|
||||
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
|
||||
module.message(exception)
|
||||
rescue
|
||||
e ->
|
||||
"got #{inspect e.__struct__} with message #{inspect message(e)} " <>
|
||||
"while retrieving Exception.message/1 for #{inspect(exception)}"
|
||||
else
|
||||
x when is_binary(x) -> x
|
||||
x ->
|
||||
"got #{inspect(x)} " <>
|
||||
"while retrieving Exception.message/1 for #{inspect(exception)} " <>
|
||||
"(expected a string)"
|
||||
raise ArgumentError,
|
||||
"Got #{inspect e.__struct__} with message " <>
|
||||
"\"#{message(e)}\" while retrieving message for #{inspect(exception)}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -95,7 +86,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.
|
||||
@@ -128,7 +119,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.
|
||||
@@ -155,7 +146,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
|
||||
@@ -262,7 +253,7 @@ defmodule Exception do
|
||||
|
||||
# :supervisor.start_link error reasons
|
||||
|
||||
# If value is a list will be formatted by mfa exit in format_exit/1
|
||||
# If value is a list will be be formatted by mfa exit in format_exit/1
|
||||
defp format_exit_reason({:bad_return, {mod, :init, value}})
|
||||
when is_atom(mod) do
|
||||
format_mfa(mod, :init, 1) <> " returned a bad value: " <> inspect(value)
|
||||
@@ -365,10 +356,9 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
defp format_application(module) do
|
||||
if app = Application.get_application(module) do
|
||||
"(" <> Atom.to_string(app) <> ") "
|
||||
else
|
||||
""
|
||||
case :application.get_application(module) do
|
||||
{:ok, app} -> "(" <> Atom.to_string(app) <> ") "
|
||||
:undefined -> ""
|
||||
end
|
||||
end
|
||||
|
||||
@@ -395,7 +385,7 @@ defmodule Exception do
|
||||
|
||||
## Examples
|
||||
|
||||
Exception.format_fa(fn -> nil end, 1)
|
||||
Exception.format_fa(fn -> end, 1)
|
||||
#=> "#Function<...>/1"
|
||||
|
||||
"""
|
||||
@@ -448,8 +438,8 @@ defmodule Exception do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Formats the given `file` and `line` as shown in stacktraces.
|
||||
If any of the values are `nil`, they are omitted.
|
||||
Formats the given file and line as shown in stacktraces.
|
||||
If any of the values are nil, they are omitted.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -484,17 +474,35 @@ defmodule Exception do
|
||||
end
|
||||
end
|
||||
|
||||
# Some exceptions implement "message/1" instead of "exception/1" mostly
|
||||
# Some exceptions implement `message/1` instead of `exception/1` mostly
|
||||
# for bootstrap reasons. It is recommended for applications to implement
|
||||
# "exception/1" instead of "message/1" as described in "defexception/1"
|
||||
# `exception/1` instead of `message/1` as described in `defexception/1`
|
||||
# docs.
|
||||
|
||||
defmodule RuntimeError do
|
||||
defexception message: "runtime error"
|
||||
|
||||
@spec exception(String.t) :: Exception.t
|
||||
def exception(msg) when is_binary(msg) do
|
||||
%RuntimeError{message: msg}
|
||||
end
|
||||
|
||||
def exception(arg) do
|
||||
super(arg)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule ArgumentError do
|
||||
defexception message: "argument error"
|
||||
|
||||
@spec exception(String.t) :: Exception.t
|
||||
def exception(msg) when is_binary(msg) do
|
||||
%ArgumentError{message: msg}
|
||||
end
|
||||
|
||||
def exception(arg) do
|
||||
super(arg)
|
||||
end
|
||||
end
|
||||
|
||||
defmodule ArithmeticError do
|
||||
@@ -556,14 +564,6 @@ defmodule BadStructError do
|
||||
end
|
||||
end
|
||||
|
||||
defmodule BadMapError do
|
||||
defexception [term: nil]
|
||||
|
||||
def message(exception) do
|
||||
"expected a map, got: #{inspect(exception.term)}"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule MatchError do
|
||||
defexception [term: nil]
|
||||
|
||||
@@ -613,37 +613,21 @@ defmodule BadArityError do
|
||||
end
|
||||
|
||||
defmodule UndefinedFunctionError do
|
||||
defexception [module: nil, function: nil, arity: nil, reason: nil]
|
||||
defexception [module: nil, function: nil, arity: nil, self: false]
|
||||
|
||||
def message(%{reason: nil, module: module, function: function, arity: arity} = e) do
|
||||
cond do
|
||||
is_nil(function) or is_nil(arity) ->
|
||||
"undefined function"
|
||||
not is_nil(module) and :code.is_loaded(module) === false ->
|
||||
message(%{e | reason: :"module could not be loaded"})
|
||||
true ->
|
||||
message(%{e | reason: :"function not exported"})
|
||||
def message(%{function: function, module: module, arity: arity, self: self}) do
|
||||
if function do
|
||||
formatted = Exception.format_mfa module, function, arity
|
||||
suffix = if self or is_nil(module) or :code.is_loaded(module) do
|
||||
""
|
||||
else
|
||||
" (module #{inspect module} is not available)"
|
||||
end
|
||||
"undefined function: #{formatted}" <> suffix
|
||||
else
|
||||
"undefined function"
|
||||
end
|
||||
end
|
||||
|
||||
def message(%{reason: :"module could not be loaded", module: module, function: function, arity: arity}) do
|
||||
"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
|
||||
"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)
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <>
|
||||
" (function #{fa} is not available)"
|
||||
end
|
||||
|
||||
def message(%{reason: reason, module: module, function: function, arity: arity}) do
|
||||
"undefined function " <> Exception.format_mfa(module, function, arity) <> " (#{reason})"
|
||||
end
|
||||
end
|
||||
|
||||
defmodule FunctionClauseError do
|
||||
@@ -685,12 +669,7 @@ defmodule KeyError do
|
||||
defexception key: nil, term: nil
|
||||
|
||||
def message(exception) do
|
||||
msg = "key #{inspect exception.key} not found"
|
||||
if exception.term != nil do
|
||||
msg <> " in: #{inspect exception.term}"
|
||||
else
|
||||
msg
|
||||
end
|
||||
"key #{inspect exception.key} not found in: #{inspect exception.term}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -708,12 +687,8 @@ defmodule UnicodeConversionError do
|
||||
"encoding starting at #{inspect rest}"
|
||||
end
|
||||
|
||||
defp detail([h|_]) when is_integer(h) do
|
||||
"code point #{h}"
|
||||
end
|
||||
|
||||
defp detail([h|_]) do
|
||||
detail(h)
|
||||
"code point #{h}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -793,26 +768,6 @@ defmodule ErlangError do
|
||||
%MatchError{term: term}
|
||||
end
|
||||
|
||||
def normalize({:badmap, term}, _stacktrace) do
|
||||
%BadMapError{term: term}
|
||||
end
|
||||
|
||||
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
|
||||
_ -> 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
|
||||
@@ -824,7 +779,7 @@ defmodule ErlangError do
|
||||
def normalize(:undef, stacktrace) do
|
||||
stacktrace = stacktrace || :erlang.get_stacktrace
|
||||
{mod, fun, arity} = from_stacktrace(stacktrace)
|
||||
%UndefinedFunctionError{module: mod, function: fun, arity: arity}
|
||||
%UndefinedFunctionError{module: mod, function: fun, arity: arity, self: from_self(stacktrace)}
|
||||
end
|
||||
|
||||
def normalize(:function_clause, stacktrace) do
|
||||
@@ -851,4 +806,7 @@ defmodule ErlangError do
|
||||
defp from_stacktrace(_) do
|
||||
{nil, nil, nil}
|
||||
end
|
||||
|
||||
defp from_self([{module, _, _, _}, {module, _, _, _}|_]), do: true
|
||||
defp from_self(_), do: false
|
||||
end
|
||||
|
||||
+123
-232
@@ -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 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:
|
||||
|
||||
@@ -47,15 +47,15 @@ defmodule File do
|
||||
File.read!("invalid.txt")
|
||||
#=> raises File.Error
|
||||
|
||||
In general, a developer should use the former in case they want
|
||||
In general, a developer should use the former in case he wants
|
||||
to react if the file does not exist. The latter should be used
|
||||
when the developer expects their software to fail in case the
|
||||
when the developer expects his software to fail in case the
|
||||
file cannot be read (i.e. it is literally an exception).
|
||||
|
||||
## 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,10 +63,10 @@ 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
|
||||
Check http://www.erlang.org/doc/man/file.html#open-2 for more information
|
||||
about such options and other performance considerations.
|
||||
"""
|
||||
|
||||
@@ -77,7 +77,7 @@ defmodule File do
|
||||
@type stat_options :: [time: :local | :universal | :posix]
|
||||
@type mode :: :append | :binary | :compressed | :delayed_write | :exclusive |
|
||||
:raw | :read | :read_ahead | :sync | :write |
|
||||
{:encoding, :latin1 | :unicode | :utf16 | :utf32 | :utf8 |
|
||||
{:encoding , :latin1 | :unicode | :utf16 | :utf32 | :utf8 |
|
||||
{:utf16, :big | :little} | {:utf32, :big | :little}} |
|
||||
{:read_ahead, pos_integer} |
|
||||
{:delayed_write, non_neg_integer, non_neg_integer}
|
||||
@@ -132,12 +132,12 @@ defmodule File do
|
||||
Typical error reasons are:
|
||||
|
||||
* `:eacces` - missing search or write permissions for the parent
|
||||
directories of `path`
|
||||
directories of `path`
|
||||
* `:eexist` - there is already a file or directory named `path`
|
||||
* `:enoent` - a component of `path` does not exist
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:enotdir` - a component of `path` is not a directory;
|
||||
on some platforms, `:enoent` is returned instead
|
||||
on some platforms, `:enoent` is returned instead
|
||||
"""
|
||||
@spec mkdir(Path.t) :: :ok | {:error, posix}
|
||||
def mkdir(path) do
|
||||
@@ -149,11 +149,11 @@ defmodule File do
|
||||
"""
|
||||
@spec mkdir!(Path.t) :: :ok | no_return
|
||||
def mkdir!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case mkdir(path) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "make directory",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "make directory", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -164,7 +164,7 @@ defmodule File do
|
||||
Typical error reasons are:
|
||||
|
||||
* `:eacces` - missing search or write permissions for the parent
|
||||
directories of `path`
|
||||
directories of `path`
|
||||
* `:enospc` - there is a no space left on the device
|
||||
* `:enotdir` - a component of `path` is not a directory
|
||||
"""
|
||||
@@ -187,7 +187,7 @@ defmodule File do
|
||||
{:error, :einval}
|
||||
else
|
||||
_ = do_mkdir_p(parent)
|
||||
case F.make_dir(path) do
|
||||
case :file.make_dir(path) do
|
||||
{:error, :eexist} = error ->
|
||||
if dir?(path), do: :ok, else: error
|
||||
other ->
|
||||
@@ -202,11 +202,11 @@ defmodule File do
|
||||
"""
|
||||
@spec mkdir_p!(Path.t) :: :ok | no_return
|
||||
def mkdir_p!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case mkdir_p(path) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "make directory (with -p)",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "make directory (with -p)", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -218,10 +218,10 @@ defmodule File do
|
||||
|
||||
* `:enoent` - the file does not exist
|
||||
* `:eacces` - missing permission for reading the file,
|
||||
or for searching one of the parent directories
|
||||
or for searching one of the parent directories
|
||||
* `:eisdir` - the named file is a directory
|
||||
* `: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
|
||||
* `:enomem` - there is not enough memory for the contents of the file
|
||||
|
||||
You can use `:file.format_error/1` to get a descriptive string of the error.
|
||||
@@ -232,17 +232,17 @@ 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
|
||||
def read!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case read(path) do
|
||||
{:ok, binary} ->
|
||||
binary
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "read file",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "read file", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -256,19 +256,11 @@ defmodule File do
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:time` - configures how the file timestamps are returned
|
||||
|
||||
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
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
* `:time` - `:local | :universal | :posix`; default: `:local`
|
||||
|
||||
"""
|
||||
@spec stat(Path.t, stat_options) :: {:ok, File.Stat.t} | {:error, posix}
|
||||
def stat(path, opts \\ []) do
|
||||
opts = Keyword.put_new(opts, :time, :universal)
|
||||
case F.read_file_info(IO.chardata_to_string(path), opts) do
|
||||
{:ok, fileinfo} ->
|
||||
{:ok, File.Stat.from_record(fileinfo)}
|
||||
@@ -283,56 +275,11 @@ defmodule File do
|
||||
"""
|
||||
@spec stat!(Path.t, stat_options) :: File.Stat.t | no_return
|
||||
def stat!(path, opts \\ []) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case stat(path, opts) do
|
||||
{:ok, info} -> info
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "read file stats",
|
||||
path: IO.chardata_to_string(path)
|
||||
end
|
||||
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
|
||||
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).
|
||||
|
||||
## Options
|
||||
|
||||
The accepted options are:
|
||||
|
||||
* `:time` - configures how the file timestamps are returned
|
||||
|
||||
The values for `:time` can be:
|
||||
|
||||
* `:universal` - returns a `{date, time}` tuple in UTC (default)
|
||||
* `:local` - returns a `{date, time}` tuple using the machine time
|
||||
* `:posix` - returns the time as integer seconds since epoch
|
||||
|
||||
"""
|
||||
@spec lstat(Path.t, stat_options) :: {:ok, File.Stat.t} | {:error, posix}
|
||||
def lstat(path, opts \\ []) do
|
||||
opts = Keyword.put_new(opts, :time, :universal)
|
||||
case F.read_link_info(IO.chardata_to_string(path), opts) do
|
||||
{:ok, fileinfo} ->
|
||||
{:ok, File.Stat.from_record(fileinfo)}
|
||||
error ->
|
||||
error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `lstat/2` but returns the `File.Stat` struct directly and
|
||||
throws `File.Error` if an error is returned.
|
||||
"""
|
||||
@spec lstat!(Path.t, stat_options) :: File.Stat.t | no_return
|
||||
def lstat!(path, opts \\ []) do
|
||||
case lstat(path, opts) do
|
||||
{:ok, info} -> info
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "read file stats",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "read file stats", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -342,7 +289,6 @@ defmodule File do
|
||||
"""
|
||||
@spec write_stat(Path.t, File.Stat.t, stat_options) :: :ok | {:error, posix}
|
||||
def write_stat(path, stat, opts \\ []) do
|
||||
opts = Keyword.put_new(opts, :time, :universal)
|
||||
F.write_file_info(IO.chardata_to_string(path), File.Stat.to_record(stat), opts)
|
||||
end
|
||||
|
||||
@@ -352,24 +298,22 @@ defmodule File do
|
||||
"""
|
||||
@spec write_stat!(Path.t, File.Stat.t, stat_options) :: :ok | no_return
|
||||
def write_stat!(path, stat, opts \\ []) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case write_stat(path, stat, opts) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "write file stats",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "write file stats", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
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.
|
||||
the given file. File is created if it doesn’t exist.
|
||||
"""
|
||||
@spec touch(Path.t, :calendar.datetime) :: :ok | {:error, posix}
|
||||
def touch(path, time \\ :calendar.universal_time) do
|
||||
def touch(path, time \\ :calendar.local_time) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case :elixir_utils.change_universal_time(path, time) do
|
||||
case F.change_time(path, time) do
|
||||
{:error, :enoent} -> touch_new(path, time)
|
||||
other -> other
|
||||
end
|
||||
@@ -377,23 +321,22 @@ defmodule File do
|
||||
|
||||
defp touch_new(path, time) do
|
||||
case write(path, "", [:append]) do
|
||||
:ok -> :elixir_utils.change_universal_time(path, time)
|
||||
:ok -> F.change_time(path, time)
|
||||
{:error, _reason} = error -> error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Same as `touch/2` but raises an exception if it fails.
|
||||
|
||||
Returns `:ok` otherwise. Requires datetime in UTC.
|
||||
Returns `:ok` otherwise.
|
||||
"""
|
||||
@spec touch!(Path.t, :calendar.datetime) :: :ok | no_return
|
||||
def touch!(path, time \\ :calendar.universal_time) do
|
||||
def touch!(path, time \\ :calendar.local_time) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case touch(path, time) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "touch",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "touch", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -441,47 +384,24 @@ defmodule File do
|
||||
"""
|
||||
@spec copy!(Path.t, Path.t, pos_integer | :infinity) :: non_neg_integer | no_return
|
||||
def copy!(source, destination, bytes_count \\ :infinity) do
|
||||
source = IO.chardata_to_string(source)
|
||||
destination = IO.chardata_to_string(destination)
|
||||
case copy(source, destination, bytes_count) do
|
||||
{:ok, bytes_count} -> bytes_count
|
||||
{:error, reason} ->
|
||||
raise File.CopyError, reason: reason, action: "copy",
|
||||
source: IO.chardata_to_string(source), destination: IO.chardata_to_string(destination)
|
||||
source: source, destination: destination
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renames the `source` file to `destination` file. It 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 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.
|
||||
We have chosen to explicitly disallow this behaviour.
|
||||
|
||||
## Examples
|
||||
|
||||
# Rename file "a.txt" to "b.txt"
|
||||
File.rename "a.txt", "b.txt"
|
||||
|
||||
# Rename directory "samples" to "tmp"
|
||||
File.rename "samples", "tmp"
|
||||
"""
|
||||
@spec rename(Path.t, Path.t) :: :ok | {:error, posix}
|
||||
def rename(source, destination) do
|
||||
F.rename(source, destination)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Copies the contents in `source` to `destination` preserving its mode.
|
||||
|
||||
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
|
||||
@@ -504,24 +424,20 @@ defmodule File do
|
||||
end
|
||||
end
|
||||
|
||||
defp path_differs?(path, path),
|
||||
do: false
|
||||
|
||||
defp path_differs?(p1, p2) do
|
||||
Path.expand(p1) !== Path.expand(p2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
The same as `cp/3`, but raises `File.CopyError` if it fails.
|
||||
Returns `:ok` otherwise.
|
||||
Returns the list of copied files otherwise.
|
||||
"""
|
||||
@spec cp!(Path.t, Path.t, (Path.t, Path.t -> boolean)) :: :ok | no_return
|
||||
def cp!(source, destination, callback \\ fn(_, _) -> true end) do
|
||||
source = IO.chardata_to_string(source)
|
||||
destination = IO.chardata_to_string(destination)
|
||||
|
||||
case cp(source, destination, callback) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.CopyError, reason: reason, action: "copy",
|
||||
source: IO.chardata_to_string(source), destination: IO.chardata_to_string(destination)
|
||||
raise File.CopyError, reason: reason, action: "copy recursively",
|
||||
source: source, destination: destination
|
||||
end
|
||||
end
|
||||
|
||||
@@ -535,20 +451,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.
|
||||
@@ -556,15 +472,15 @@ defmodule File do
|
||||
|
||||
## Examples
|
||||
|
||||
# Copies file "a.txt" to "b.txt"
|
||||
File.cp_r "a.txt", "b.txt"
|
||||
# Copies "a.txt" to "tmp"
|
||||
File.cp_r "a.txt", "tmp.txt"
|
||||
|
||||
# Copies all files in "samples" to "tmp"
|
||||
File.cp_r "samples", "tmp"
|
||||
|
||||
# Same as before, but asks the user how to proceed in case of conflicts
|
||||
File.cp_r "samples", "tmp", fn(source, destination) ->
|
||||
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm. ") == "y\n"
|
||||
IO.gets("Overwriting #{destination} by #{source}. Type y to confirm.") == "y"
|
||||
end
|
||||
|
||||
"""
|
||||
@@ -585,11 +501,14 @@ defmodule File do
|
||||
"""
|
||||
@spec cp_r!(Path.t, Path.t, (Path.t, Path.t -> boolean)) :: [binary] | no_return
|
||||
def cp_r!(source, destination, callback \\ fn(_, _) -> true end) do
|
||||
source = IO.chardata_to_string(source)
|
||||
destination = IO.chardata_to_string(destination)
|
||||
|
||||
case cp_r(source, destination, callback) do
|
||||
{:ok, files} -> files
|
||||
{:error, reason, file} ->
|
||||
raise File.CopyError, reason: reason, action: "copy recursively", on: file,
|
||||
source: IO.chardata_to_string(source), destination: IO.chardata_to_string(destination)
|
||||
raise File.CopyError, reason: reason, action: "copy recursively",
|
||||
source: source, destination: destination, on: file
|
||||
end
|
||||
end
|
||||
|
||||
@@ -638,7 +557,9 @@ defmodule File do
|
||||
copy_file_mode!(src, dest)
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
if callback.(src, dest) do
|
||||
# If rm/1 fails, copy/2 will fail
|
||||
_ = rm(dest)
|
||||
case copy(src, dest) do
|
||||
{:ok, _} ->
|
||||
copy_file_mode!(src, dest)
|
||||
@@ -658,8 +579,8 @@ defmodule File do
|
||||
:ok ->
|
||||
[dest|acc]
|
||||
{:error, :eexist} ->
|
||||
if path_differs?(src, dest) and callback.(src, dest) do
|
||||
# If rm/1 fails, F.make_symlink/2 will fail
|
||||
if callback.(src, dest) do
|
||||
# If rm/1 fails, iF.make_symlink/2 will fail
|
||||
_ = rm(dest)
|
||||
case F.make_symlink(link, dest) do
|
||||
:ok -> [dest|acc]
|
||||
@@ -683,16 +604,16 @@ 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
|
||||
the parent directories
|
||||
* `:eisdir` - the named file is a directory
|
||||
|
||||
Check `File.open/2` for other available options.
|
||||
@@ -707,11 +628,11 @@ defmodule File do
|
||||
"""
|
||||
@spec write!(Path.t, iodata, [mode]) :: :ok | no_return
|
||||
def write!(path, content, modes \\ []) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case F.write_file(path, content, modes) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "write to file",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "write to file", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -719,7 +640,6 @@ defmodule File do
|
||||
Tries to delete the file `path`.
|
||||
|
||||
Returns `:ok` if successful, or `{:error, reason}` if an error occurs.
|
||||
|
||||
Note the file is deleted even if in read-only mode.
|
||||
|
||||
Typical error reasons are:
|
||||
@@ -728,15 +648,15 @@ 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
|
||||
|
||||
File.rm("file.txt")
|
||||
File.rm('file.txt')
|
||||
#=> :ok
|
||||
|
||||
File.rm("tmp_dir/")
|
||||
File.rm('tmp_dir/')
|
||||
#=> {:error, :eperm}
|
||||
|
||||
"""
|
||||
@@ -755,7 +675,7 @@ defmodule File do
|
||||
|
||||
defp change_mode_windows(path) do
|
||||
if match? {:win32, _}, :os.type do
|
||||
case F.read_file_info(path) do
|
||||
case F.read_file_info(IO.chardata_to_string(path)) do
|
||||
{:ok, file_info} when elem(file_info, 3) in [:read, :none] ->
|
||||
change_mode_windows(path, file_info)
|
||||
_ ->
|
||||
@@ -765,7 +685,7 @@ defmodule File do
|
||||
end
|
||||
|
||||
defp change_mode_windows(path, file_info) do
|
||||
case chmod(path, (elem(file_info, 7) + 0o200)) do
|
||||
case File.chmod(path, (elem(file_info, 7) + 0o200)) do
|
||||
:ok -> F.delete(path)
|
||||
{:error, _reason} = error -> error
|
||||
end
|
||||
@@ -776,11 +696,11 @@ defmodule File do
|
||||
"""
|
||||
@spec rm!(Path.t) :: :ok | no_return
|
||||
def rm!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case rm(path) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "remove file",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "remove file", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -807,16 +727,16 @@ defmodule File do
|
||||
"""
|
||||
@spec rmdir!(Path.t) :: :ok | {:error, posix}
|
||||
def rmdir!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case rmdir(path) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "remove directory",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "remove directory", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes files and directories recursively at the given `path`.
|
||||
Remove files and directories recursively at the given `path`.
|
||||
Symlinks are not followed but simply removed, non-existing
|
||||
files are simply ignored (i.e. doesn't make this function fail).
|
||||
|
||||
@@ -910,10 +830,11 @@ defmodule File do
|
||||
"""
|
||||
@spec rm_rf!(Path.t) :: [binary] | no_return
|
||||
def rm_rf!(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case rm_rf(path) do
|
||||
{:ok, files} -> files
|
||||
{:error, reason, _} ->
|
||||
raise File.Error, reason: reason, path: IO.chardata_to_string(path),
|
||||
raise File.Error, reason: reason, path: path,
|
||||
action: "remove files and directories recursively from"
|
||||
end
|
||||
end
|
||||
@@ -922,7 +843,7 @@ defmodule File do
|
||||
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
|
||||
@@ -956,15 +877,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.
|
||||
|
||||
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).
|
||||
Check http://www.erlang.org/doc/man/file.html#open-2 for more information about
|
||||
other options like `:read_ahead` and `:delayed_write`.
|
||||
|
||||
This function returns:
|
||||
|
||||
@@ -1000,9 +921,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.
|
||||
|
||||
@@ -1041,10 +962,11 @@ defmodule File do
|
||||
"""
|
||||
@spec open!(Path.t, [mode]) :: io_device | no_return
|
||||
def open!(path, modes \\ []) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case open(path, modes) do
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "open", path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "open", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1055,10 +977,11 @@ defmodule File do
|
||||
"""
|
||||
@spec open!(Path.t, [mode | :ram], (io_device -> res)) :: res | no_return when res: var
|
||||
def open!(path, modes, function) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case open(path, modes, function) do
|
||||
{:ok, device} -> device
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "open", path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "open", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1066,7 +989,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.
|
||||
"""
|
||||
@@ -1095,7 +1018,7 @@ defmodule File do
|
||||
case cwd() do
|
||||
{:ok, cwd} -> cwd
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "get current working directory"
|
||||
raise File.Error, reason: reason, action: "get current working directory"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1114,18 +1037,18 @@ defmodule File do
|
||||
"""
|
||||
@spec cd!(Path.t) :: :ok | no_return
|
||||
def cd!(path) do
|
||||
case cd(path) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case F.set_cwd(path) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "set current working directory to",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "set current working directory to", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@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.
|
||||
@@ -1142,7 +1065,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.
|
||||
@@ -1161,11 +1084,11 @@ defmodule File do
|
||||
"""
|
||||
@spec ls!(Path.t) :: [binary] | no_return
|
||||
def ls!(path \\ ".") do
|
||||
path = IO.chardata_to_string(path)
|
||||
case ls(path) do
|
||||
{:ok, value} -> value
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "list directory",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "list directory", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1192,9 +1115,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
|
||||
|
||||
@@ -1202,23 +1125,10 @@ defmodule File do
|
||||
device cannot be shared and as such it is convenient to open the file
|
||||
in raw mode for performance reasons. Therefore, Elixir **will** open
|
||||
streams in `:raw` mode with the `:read_ahead` option unless an encoding
|
||||
is specified. This means any data streamed into the file must be
|
||||
converted to `iodata` type. If you pass `[:utf8]` in the modes parameter,
|
||||
the underlying stream will use `IO.write/2` and the `String.Chars` protocol
|
||||
to convert the data. See `IO.binwrite/2` and `IO.write/2` .
|
||||
is specified.
|
||||
|
||||
One may also consider passing the `:delayed_write` option if the stream
|
||||
is meant to be written to under a tight loop.
|
||||
|
||||
## Examples
|
||||
|
||||
# Read in 2048 byte chunks rather than lines
|
||||
File.stream!("./test/test.data", [], 2048)
|
||||
#=> %File.Stream{line_or_bytes: 2048, modes: [:raw, :read_ahead, :binary],
|
||||
#=> path: "./test/test.data", raw: true}
|
||||
|
||||
See `Stream.run/1` for an example of streaming into a file.
|
||||
|
||||
"""
|
||||
def stream!(path, modes \\ [], line_or_bytes \\ :line) do
|
||||
modes = open_defaults(modes, true)
|
||||
@@ -1226,28 +1136,9 @@ defmodule File do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Changes the `mode` for a given `file`.
|
||||
|
||||
Returns `:ok` on success, or `{:error, reason}` on failure.
|
||||
|
||||
## Permissions
|
||||
|
||||
* 0o400 - read permission: owner
|
||||
* 0o200 - write permission: owner
|
||||
* 0o100 - execute permission: owner
|
||||
|
||||
* 0o040 - read permission: group
|
||||
* 0o020 - write permission: group
|
||||
* 0o010 - execute permission: group
|
||||
|
||||
* 0o004 - read permission: other
|
||||
* 0o002 - write permission: other
|
||||
* 0o001 - execute permission: other
|
||||
|
||||
For example, setting the mode 0o755 gives it
|
||||
write, read and execute permission to the owner
|
||||
and both read and execute permission to group
|
||||
and others.
|
||||
Changes the unix file `mode` for a given `file`.
|
||||
Returns `:ok` on success, or `{:error, reason}`
|
||||
on failure.
|
||||
"""
|
||||
@spec chmod(Path.t, non_neg_integer) :: :ok | {:error, posix}
|
||||
def chmod(path, mode) do
|
||||
@@ -1259,16 +1150,16 @@ defmodule File do
|
||||
"""
|
||||
@spec chmod!(Path.t, non_neg_integer) :: :ok | no_return
|
||||
def chmod!(path, mode) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case chmod(path, mode) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "change mode for",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "change mode for", path: path
|
||||
end
|
||||
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.
|
||||
"""
|
||||
@@ -1282,11 +1173,11 @@ defmodule File do
|
||||
"""
|
||||
@spec chgrp!(Path.t, non_neg_integer) :: :ok | no_return
|
||||
def chgrp!(path, gid) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case chgrp(path, gid) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "change group for",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "change group for", path: path
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1305,11 +1196,11 @@ defmodule File do
|
||||
"""
|
||||
@spec chown!(Path.t, non_neg_integer) :: :ok | no_return
|
||||
def chown!(path, uid) do
|
||||
path = IO.chardata_to_string(path)
|
||||
case chown(path, uid) do
|
||||
:ok -> :ok
|
||||
{:error, reason} ->
|
||||
raise File.Error, reason: reason, action: "change owner for",
|
||||
path: IO.chardata_to_string(path)
|
||||
raise File.Error, reason: reason, action: "change owner for", path: path
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -2,11 +2,11 @@ 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
|
||||
between the Erlang record and the Elixir struct.
|
||||
in between the Erlang record and the Elixir struct.
|
||||
|
||||
Its fields are:
|
||||
|
||||
@@ -41,15 +41,14 @@ 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`,
|
||||
`:universal`, or `:posix`. Default is `:universal`.
|
||||
`:universal`, or `:posix`. Default is `:local`.
|
||||
"""
|
||||
|
||||
record = Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
|
||||
@@ -70,7 +69,6 @@ defmodule File.Stat do
|
||||
@doc """
|
||||
Converts a `:file_info` record into a `File.Stat`.
|
||||
"""
|
||||
def from_record(file_info)
|
||||
def from_record({:file_info, unquote_splicing(vals)}) do
|
||||
%File.Stat{unquote_splicing(pairs)}
|
||||
end
|
||||
|
||||
@@ -34,6 +34,10 @@ defmodule File.Stream do
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def empty(stream) do
|
||||
stream
|
||||
end
|
||||
|
||||
def into(%{path: path, modes: modes, raw: raw} = stream) do
|
||||
modes = for mode <- modes, not mode in [:read], do: mode
|
||||
|
||||
|
||||
+84
-59
@@ -8,26 +8,19 @@ defmodule Float do
|
||||
@doc """
|
||||
Parses a binary into a float.
|
||||
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`;
|
||||
when the binary cannot be coerced into a valid float, the atom `:error` is
|
||||
returned.
|
||||
|
||||
If the size of float exceeds the maximum size of `1.7976931348623157e+308`,
|
||||
the `ArgumentError` exception is raised.
|
||||
|
||||
If a float formatted string wants to be directly converted to a float,
|
||||
`String.to_float/1` can be used instead.
|
||||
If successful, returns a tuple of the form `{float, remainder_of_binary}`.
|
||||
Otherwise `:error`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Float.parse("34")
|
||||
{34.0, ""}
|
||||
{34.0,""}
|
||||
|
||||
iex> Float.parse("34.25")
|
||||
{34.25, ""}
|
||||
{34.25,""}
|
||||
|
||||
iex> Float.parse("56.5xyz")
|
||||
{56.5, "xyz"}
|
||||
{56.5,"xyz"}
|
||||
|
||||
iex> Float.parse("pi")
|
||||
:error
|
||||
@@ -35,51 +28,83 @@ defmodule Float do
|
||||
"""
|
||||
@spec parse(binary) :: {float, binary} | :error
|
||||
def parse("-" <> binary) do
|
||||
case parse_unsigned(binary) do
|
||||
case parse_unsign(binary) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
def parse("+" <> binary) do
|
||||
parse_unsigned(binary)
|
||||
end
|
||||
|
||||
def parse(binary) do
|
||||
parse_unsigned(binary)
|
||||
parse_unsign(binary)
|
||||
end
|
||||
|
||||
defp parse_unsigned(<<digit, rest::binary>>) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, false, false, <<digit>>)
|
||||
defp parse_unsign("-" <> _), do: :error
|
||||
defp parse_unsign(binary) when is_binary(binary) do
|
||||
case Integer.parse binary do
|
||||
:error -> :error
|
||||
{integer_part, after_integer} -> parse_unsign after_integer, integer_part
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_unsigned(binary) when is_binary(binary), do:
|
||||
:error
|
||||
# Dot followed by digit is required afterwards or we are done
|
||||
defp parse_unsign(<< ?., char, rest :: binary >>, int) when char in ?0..?9 do
|
||||
parse_unsign(rest, char - ?0, 1, int)
|
||||
end
|
||||
|
||||
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_unsign(rest, int) do
|
||||
{:erlang.float(int), rest}
|
||||
end
|
||||
|
||||
defp parse_unsigned(<<?., digit, rest::binary>>, false, false, acc) when digit in ?0..?9, do:
|
||||
parse_unsigned(rest, true, false, <<acc::binary, ?., digit>>)
|
||||
# Handle decimal points
|
||||
defp parse_unsign(<< char, rest :: binary >>, float, decimal, int) when char in ?0..?9 do
|
||||
parse_unsign rest, 10 * float + (char - ?0), decimal + 1, int
|
||||
end
|
||||
|
||||
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_unsign(<< ?e, after_e :: binary >>, float, decimal, int) do
|
||||
case Integer.parse after_e do
|
||||
:error ->
|
||||
# Note we rebuild the binary here instead of breaking it apart at
|
||||
# the function clause because the current approach copies a binary
|
||||
# just on this branch. If we broke it apart in the function clause,
|
||||
# the copy would happen when calling Integer.parse/1.
|
||||
{floatify(int, float, decimal), << ?e, after_e :: binary >>}
|
||||
{exponential, after_exponential} ->
|
||||
{floatify(int, float, decimal, exponential), after_exponential}
|
||||
end
|
||||
end
|
||||
|
||||
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_unsign(bitstring, float, decimal, int) do
|
||||
{floatify(int, float, decimal), bitstring}
|
||||
end
|
||||
|
||||
defp parse_unsigned(rest, dot?, _e?, acc), do:
|
||||
{:erlang.binary_to_float(add_dot(acc, dot?)), rest}
|
||||
defp floatify(int, float, decimal, exponential \\ 0) do
|
||||
multiplier = if int < 0, do: -1.0, else: 1.0
|
||||
|
||||
defp add_dot(acc, true), do: acc
|
||||
defp add_dot(acc, false), do: acc <> ".0"
|
||||
# Try to ensure the minimum amount of rounding errors
|
||||
result = multiplier * (abs(int) * :math.pow(10, decimal) + float) * :math.pow(10, exponential - decimal)
|
||||
|
||||
# Try avoiding stuff like this:
|
||||
# iex(1)> 0.0001 * 75
|
||||
# 0.007500000000000001
|
||||
# Due to IEEE 754 floating point standard
|
||||
# http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
|
||||
|
||||
final_decimal_places = decimal - exponential
|
||||
if final_decimal_places > 0 do
|
||||
decimal_power_round = :math.pow(10, final_decimal_places)
|
||||
trunc(result * decimal_power_round) / decimal_power_round
|
||||
else
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Rounds a float to the largest integer less than or equal to `num`.
|
||||
|
||||
`floor/2` also accepts a precision to round a floating point value down
|
||||
Floor also accepts a precision to round a floating point value down
|
||||
to an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
This function always returns a float. `Kernel.trunc/1` may be used instead to
|
||||
This function always returns floats. One may use `Kernel.trunc/1` to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Examples
|
||||
@@ -90,7 +115,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,12 +129,12 @@ 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).
|
||||
Ceil also accepts a precision to round a floating point value down to
|
||||
an arbitrary number of fractional digits (between 0 and 15).
|
||||
|
||||
This function always returns floats. `Kernel.trunc/1` may be used instead to
|
||||
This function always returns floats. One may use `Kernel.trunc/1` to
|
||||
truncate the result to an integer afterwards.
|
||||
|
||||
## Examples
|
||||
@@ -120,7 +145,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
|
||||
|
||||
"""
|
||||
@@ -137,9 +162,9 @@ defmodule Float do
|
||||
Rounds a floating point value to an arbitrary number of fractional digits
|
||||
(between 0 and 15).
|
||||
|
||||
This function only accepts floats and always returns a float. Use
|
||||
`Kernel.round/1` if you want a function that accepts both floats and integers
|
||||
and always returns an integer.
|
||||
This function only accepts floats and returns floats. Use `Kernel.round/1`
|
||||
if you want a function that accepts both floats and integers and always
|
||||
returns an integer.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -179,20 +204,20 @@ defmodule Float do
|
||||
|
||||
"""
|
||||
@spec to_char_list(float) :: char_list
|
||||
def to_char_list(float) do
|
||||
:erlang.float_to_list(float)
|
||||
def to_char_list(number) do
|
||||
:erlang.float_to_list(number)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a list which corresponds to the text representation
|
||||
of the given float.
|
||||
of `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)
|
||||
* `: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
|
||||
|
||||
@@ -207,7 +232,7 @@ defmodule Float do
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of the given float.
|
||||
of `some_float`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -218,8 +243,8 @@ defmodule Float do
|
||||
|
||||
"""
|
||||
@spec to_string(float) :: String.t
|
||||
def to_string(float) do
|
||||
:erlang.float_to_binary(float)
|
||||
def to_string(some_float) do
|
||||
:erlang.float_to_binary(some_float)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -228,10 +253,10 @@ defmodule Float do
|
||||
|
||||
## 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)
|
||||
* `: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
|
||||
|
||||
|
||||
+95
-206
@@ -8,7 +8,7 @@ defmodule GenEvent do
|
||||
|
||||
An event manager 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.
|
||||
error reporting. It will also fit into an supervision tree.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -22,7 +22,6 @@ defmodule GenEvent do
|
||||
As an example, let's have a GenEvent that accumulates messages until
|
||||
they are collected by an explicit call.
|
||||
|
||||
# Define a Event Handler
|
||||
defmodule LoggerHandler do
|
||||
use GenEvent
|
||||
|
||||
@@ -37,40 +36,84 @@ defmodule GenEvent do
|
||||
end
|
||||
end
|
||||
|
||||
# Start a new event manager.
|
||||
{:ok, pid} = GenEvent.start_link([])
|
||||
{: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]
|
||||
|
||||
GenEvent.call(pid, LoggerHandler, :messages)
|
||||
#=> []
|
||||
|
||||
We start a new event manager by calling `GenEvent.start_link/1`.
|
||||
We start a new event manager by calling `GenEvent.start_link/0`.
|
||||
Notifications can be sent to the event manager which will then
|
||||
invoke `handle_event/2` for each registered handler.
|
||||
|
||||
We can add new handlers with `add_handler/3` and `add_mon_handler/3`.
|
||||
Calls can also be made to specific handlers by using `call/3`.
|
||||
We can add new handlers with `add_handler/3`. Calls can also
|
||||
be made to specific handlers by using `call/3`.
|
||||
|
||||
## Callbacks
|
||||
|
||||
There are 6 callbacks required to be implemented in a `GenEvent`. By
|
||||
adding `use GenEvent` to your module, Elixir will automatically define
|
||||
all 6 callbacks for you, leaving it up to you to implement the ones
|
||||
you want to customize.
|
||||
you want to customize. The callbacks are:
|
||||
|
||||
* `init(args)` - invoked when the event handler is added.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, state}`
|
||||
- `{:ok, state, :hibernate}`
|
||||
- `{:error, reason}`
|
||||
|
||||
* `handle_event(msg, state)` - invoked whenever an event is sent via
|
||||
`notify/2`, `ack_notify/2` or `sync_notify/2`.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, new_state}`
|
||||
- `{:ok, new_state, :hibernate}`
|
||||
- `:remove_handler`
|
||||
|
||||
* `handle_call(msg, state)` - invoked when a `call/3` is done to a specific
|
||||
handler.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, reply, new_state}`
|
||||
- `{:ok, reply, new_state, :hibernate}`
|
||||
- `{:remove_handler, reply}`
|
||||
|
||||
* `handle_info(msg, state)` - invoked to handle all other messages which
|
||||
are received by the process. Must return the same values as
|
||||
`handle_event/2`.
|
||||
|
||||
* `terminate(reason, state)` - called when the event handler is removed or
|
||||
the event manager is terminating. It can return any term.
|
||||
|
||||
The reason is one of:
|
||||
|
||||
- `:stop` - manager is terminating
|
||||
- `{:stop, reason}` - monitored process terminated (for monitored handlers)
|
||||
- `:remove_handler` - handler is being removed
|
||||
- `{:error, term}` - handler crashed or returned a bad value
|
||||
- `term` - any term passed to functions like `GenEvent.remove_handler/2`
|
||||
|
||||
* `code_change(old_vsn, state, extra)` - called when the application
|
||||
code is being upgraded live (hot code swapping).
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, new_state}`
|
||||
|
||||
## Name Registration
|
||||
|
||||
@@ -82,35 +125,32 @@ defmodule GenEvent do
|
||||
GenEvent stream supports three different notifications.
|
||||
|
||||
On `GenEvent.ack_notify/2`, the manager acknowledges each event,
|
||||
providing backpressure, but processing of the message happens
|
||||
providing back pressure, 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).
|
||||
|
||||
## Streaming
|
||||
|
||||
`GenEvent` messages can be streamed with the help of `stream/2`.
|
||||
You will need to start another process to consume the stream:
|
||||
`GenEvent`s can be streamed from and streamed with the help of `stream/2`.
|
||||
Here are some examples:
|
||||
|
||||
Task.start_link fn ->
|
||||
stream = GenEvent.stream(pid)
|
||||
stream = GenEvent.stream(pid)
|
||||
|
||||
# Discard the next 3 events
|
||||
_ = Enum.drop(stream, 3)
|
||||
# Take the next 10 events
|
||||
Enum.take(stream, 10)
|
||||
|
||||
# Print all remaining events
|
||||
for event <- stream do
|
||||
IO.inspect event
|
||||
end
|
||||
# Print all remaining events
|
||||
for event <- stream do
|
||||
IO.inspect event
|
||||
end
|
||||
|
||||
Now call `GenEvent.notify/2` multiple times. You will see the
|
||||
first three events will be skipped while the rest will be
|
||||
continuously printed.
|
||||
A stream may also be given an id, which allows all streams with the given
|
||||
id to be cancelled at any moment via `cancel_streams/1`.
|
||||
|
||||
## Learn more and compatibility
|
||||
|
||||
@@ -118,9 +158,9 @@ defmodule GenEvent do
|
||||
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)
|
||||
* http://elixir-lang.org/getting_started/mix/1.html
|
||||
* http://www.erlang.org/doc/man/gen_event.html
|
||||
* http://learnyousomeerlang.com/event-handlers
|
||||
|
||||
Keep in mind though Elixir and Erlang gen events are not 100% compatible.
|
||||
The `:gen_event.add_sup_handler/3` is not supported by Elixir's GenEvent,
|
||||
@@ -130,158 +170,11 @@ 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
|
||||
Futhermore, 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`,
|
||||
(and `add_mon_handler/3`) will block until it returns.
|
||||
|
||||
`args` is the argument term (third argument) passed to `add_handler/3`.
|
||||
|
||||
Returning `{:ok, state}` will cause `add_handler/3` to return `:ok` and the
|
||||
handler to become part of the `GenEvent` loop with state `state`.
|
||||
|
||||
Returning `{:ok, state, :hibernate}` is similar to
|
||||
`{:ok, state}` except the `GenEvent` process is hibernated before continuing
|
||||
its loop. See `handle_event/2` for more information on hibernation.
|
||||
|
||||
Returning `{:error, reason}` will cause `add_handler/3` to return
|
||||
`{:error, reason}` and the handler is not added to `GenEvent` loop.
|
||||
"""
|
||||
@callback init(args :: term) ::
|
||||
{:ok, state} |
|
||||
{:ok, state, :hibernate} |
|
||||
{:error, reason :: any} when state: any
|
||||
|
||||
@doc """
|
||||
Invoked to handle `notify/2`, `ack_notify/2` or `sync_notify/2` messages.
|
||||
|
||||
`event` is the event message and `state` is the current state of the handler.
|
||||
|
||||
Returning `{:ok, new_state}` sets the handler's state to `new_state` and the
|
||||
`GenEvent` loop continues.
|
||||
|
||||
Returning `{:ok, new_state, :hibernate}` is similar to
|
||||
`{:ok, new_state}` except the process is hibernated once all handlers have
|
||||
handled the events. The `GenEvent` process will 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 `GenEvent` 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
|
||||
expected soon and minimising the memory of the process is shown to be
|
||||
beneficial.
|
||||
|
||||
Returning `:remove_handler` removes the handler from the `GenEvent` loop and
|
||||
calls `terminate/2` with reason `:remove_handler` and state `state`.
|
||||
"""
|
||||
@callback handle_event(event :: term, state :: term) ::
|
||||
{:ok, new_state} |
|
||||
{:ok, new_state, :hibernate} |
|
||||
:remove_handler when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle synchronous `call/4` messages to a specific handler.
|
||||
|
||||
`request` is the request message sent by a `call/4` and `state` is the current
|
||||
state of the handler.
|
||||
|
||||
Returning `{:ok, reply, new_state}` sends `reply` as a response to the call
|
||||
and sets the handler's state to `new_state`.
|
||||
|
||||
Returning `{:ok, reply, new_state, :hibernate}` is similar to
|
||||
`{: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 reponse to the call,
|
||||
removes the handler from the `GenEvent` loop and calls `terminate/2` with
|
||||
reason `:remove_handler` and state `state`.
|
||||
"""
|
||||
|
||||
@callback handle_call(request :: term, state :: term) ::
|
||||
{:ok, reply, new_state} |
|
||||
{:ok, reply, new_state, :hibernate} |
|
||||
{:remove_handler, reply} when reply: term, new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle all other messages. All handlers are run in the `GenEvent`
|
||||
process so messages intended for other handlers should be ignored with a catch
|
||||
all clause.
|
||||
|
||||
`msg` is the message and `state` is the current state of the handler.
|
||||
|
||||
Return values are the same as `handle_event/2`.
|
||||
"""
|
||||
@callback handle_info(msg :: term, state :: term) ::
|
||||
{:ok, new_state} |
|
||||
{:ok, new_state, :hibernate} |
|
||||
:remove_handler when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked when the server is about to exit. It should do any cleanup required.
|
||||
|
||||
`reason` is removal reason and `state` is the current state of the handler.
|
||||
The return value is returned to `GenEvent.remove_handler/3` or ignored if
|
||||
removing for another reason.
|
||||
|
||||
`reason` is one of:
|
||||
|
||||
- `:stop` - manager is terminating
|
||||
- `{:stop, term}` - monitored process terminated (for monitored handlers)
|
||||
- `:remove_handler` - handler is being removed
|
||||
- `{:error, term}` - handler crashed or returned a bad value and an error is
|
||||
logged
|
||||
- `term` - any term passed to functions like `GenEvent.remove_handler/3`
|
||||
|
||||
If part of a supervision tree, a `GenEvent`'s `Supervisor` will send an exit
|
||||
signal when shutting it down. The exit signal is based on the shutdown
|
||||
strategy in the child's specification. If it is `:brutal_kill` the `GenEvent`
|
||||
is killed and so `terminate/2` is not called for its handlers. However if it is
|
||||
a timeout the `Supervisor` will send the exit signal `:shutdown` and the
|
||||
`GenEvent` will have the duration of the timeout to call `terminate/2` on all
|
||||
of its handlers - if the process is still alive after the timeout it is
|
||||
killed.
|
||||
|
||||
If the `GenEvent` receives an exit signal (that is not `:normal`) from any
|
||||
process when it is not trapping exits it will exit abruptly with the same
|
||||
reason and so not call the handlers' `terminate/2`. Note that a process does
|
||||
*NOT* trap exits by default and an exit signal is sent when a linked process
|
||||
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 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
|
||||
process is not exiting so will not be automatically cleaned up.
|
||||
"""
|
||||
@callback terminate(reason, state :: term) ::
|
||||
term when reason: :stop | {:stop, term} | :remove_handler | {:error, term} | term
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the handler when a different version of the
|
||||
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
|
||||
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.
|
||||
|
||||
If `code_change/3` raises, the code change fails and the handler will continue
|
||||
with its previous state. Therefore this callback does not usually contain side
|
||||
effects.
|
||||
"""
|
||||
@callback code_change(old_vsn, state :: term, extra :: term) ::
|
||||
{:ok, new_state :: term} when old_vsn: term | {:down, term}
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | {:error, {:already_started, pid}}
|
||||
|
||||
@@ -295,7 +188,7 @@ defmodule GenEvent do
|
||||
@type manager :: pid | name | {atom, node}
|
||||
|
||||
@typedoc "Supported values for new handlers"
|
||||
@type handler :: atom | {atom, term}
|
||||
@type handler :: atom | {atom, term} | {pid, reference}
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
@@ -315,10 +208,9 @@ defmodule GenEvent do
|
||||
@doc false
|
||||
def handle_call(msg, state) do
|
||||
# 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)
|
||||
1 -> {:remove_handler, reason}
|
||||
case :random.uniform(1) do
|
||||
1 -> exit({:bad_call, msg})
|
||||
2 -> {:remove_handler, :ok}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -351,8 +243,8 @@ defmodule GenEvent do
|
||||
section in the `GenServer` module docs.
|
||||
|
||||
If the event manager 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
|
||||
returns `{:ok, pid}`, where pid is the pid of the server. If there already
|
||||
exists a process with the specified server name, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
Note that a `GenEvent` started with `start_link/1` is linked to the
|
||||
@@ -422,10 +314,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
|
||||
@@ -517,7 +405,7 @@ defmodule GenEvent do
|
||||
Sends a sync event notification to the event `manager`.
|
||||
|
||||
In other words, this function only returns `:ok` after the event manager
|
||||
invokes the `handle_event/2` callback on each installed event handler.
|
||||
invokes the `handle_event/2` on each installed event handler.
|
||||
|
||||
See `notify/2` for more info.
|
||||
"""
|
||||
@@ -581,7 +469,7 @@ defmodule GenEvent do
|
||||
|
||||
First, the old event handler is deleted by calling `terminate/2` with
|
||||
the given `args1` and collects the return value. Then the new event handler
|
||||
is added and initiated by calling `init({args2, term})`, where `term` is the
|
||||
is added and initiated by calling `init({args2, state}), where term is the
|
||||
return value of calling `terminate/2` in the old handler. This makes it
|
||||
possible to transfer information from one handler to another.
|
||||
|
||||
@@ -616,24 +504,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 once patch is accepted by OTP
|
||||
{:ok, reply} = :gen.call(module, self(), cmd, :infinity)
|
||||
reply
|
||||
end
|
||||
@@ -742,6 +624,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)
|
||||
@@ -767,7 +656,7 @@ defmodule GenEvent do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def system_code_change([name, handlers, hib], module, old_vsn, extra) do
|
||||
def system_code_change([name, handlers, hib], module , old_vsn, extra) do
|
||||
handlers =
|
||||
for handler <- handlers do
|
||||
if handler(handler, :module) == module do
|
||||
@@ -1124,9 +1013,9 @@ 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 ->
|
||||
:code.is_loaded(m) ->
|
||||
{:"module could not be loaded", mfas}
|
||||
function_exported?(m, f, length(a)) ->
|
||||
reason
|
||||
|
||||
@@ -2,11 +2,11 @@ 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`
|
||||
* `:timeout` - the timeout between events, defaults to `:infinity`
|
||||
* `:timeout` - the timeout in between events, defaults to `:infinity`
|
||||
|
||||
"""
|
||||
defstruct manager: nil, timeout: :infinity
|
||||
@@ -22,21 +22,12 @@ 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.
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit({:bad_event, event})
|
||||
1 -> :remove_handler
|
||||
end
|
||||
exit({:bad_event, event})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _state) do
|
||||
# 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)
|
||||
1 -> {:remove_handler, reason}
|
||||
end
|
||||
exit({:bad_call, msg})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -152,9 +143,15 @@ defimpl Enumerable, for: GenEvent.Stream do
|
||||
|
||||
defp wait_for_handler_removal(pid, ref, mon_ref) do
|
||||
receive do
|
||||
{:gen_event_EXIT, {^pid, ^ref}, _reason} ->
|
||||
{:gen_event_EXIT, {^pid, ^ref}, reason}
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 3 and elem(reason, 0) == :swapped ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
:ok
|
||||
{:gen_event_EXIT, {^pid, ^ref}, reason} ->
|
||||
Process.demonitor(mon_ref, [:flush])
|
||||
{:error, reason}
|
||||
{:DOWN, ^mon_ref, _, _, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
|
||||
+71
-321
@@ -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
|
||||
@@ -61,7 +61,50 @@ defmodule GenServer do
|
||||
There are 6 callbacks required to be implemented in a `GenServer`. By
|
||||
adding `use GenServer` to your module, Elixir will automatically define
|
||||
all 6 callbacks for you, leaving it up to you to implement the ones
|
||||
you want to customize.
|
||||
you want to customize. The callbacks are:
|
||||
|
||||
* `init(args)` - invoked when the server is started.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, state}`
|
||||
- `{:ok, state, timeout}`
|
||||
- `:ignore`
|
||||
- `{:stop, reason}`
|
||||
|
||||
* `handle_call(msg, {from, ref}, state)` and `handle_cast(msg, state)` -
|
||||
invoked to handle call (sync) and cast (async) messages.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:reply, reply, new_state}`
|
||||
- `{:reply, reply, new_state, timeout}`
|
||||
- `{:reply, reply, new_state, :hibernate}`
|
||||
- `{:noreply, new_state}`
|
||||
- `{:noreply, new_state, timeout}`
|
||||
- `{:noreply, new_state, :hibernate}`
|
||||
- `{:stop, reason, new_state}`
|
||||
- `{:stop, reason, reply, new_state}`
|
||||
|
||||
* `handle_info(msg, state)` - invoked to handle all other messages which
|
||||
are received by the process.
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:noreply, state}`
|
||||
- `{:noreply, state, timeout}`
|
||||
- `{:stop, reason, state}`
|
||||
|
||||
* `terminate(reason, state)` - called when the server is about to
|
||||
terminate, useful for cleaning up. It must return `:ok`.
|
||||
|
||||
* `code_change(old_vsn, state, extra)` - called when the application
|
||||
code is being upgraded live (hot code swapping).
|
||||
|
||||
It must return:
|
||||
|
||||
- `{:ok, new_state}`
|
||||
- `{:error, reason}`
|
||||
|
||||
## Name Registration
|
||||
|
||||
@@ -76,11 +119,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:
|
||||
|
||||
@@ -151,228 +191,18 @@ defmodule GenServer do
|
||||
the same module. If the server and/or client implementations are growing
|
||||
complex, you may want to have them in different modules.
|
||||
|
||||
## Receiving custom messages
|
||||
|
||||
The goal of a `GenServer` is to abstract the "receive" loop for developers,
|
||||
automatically handling system messages, support code change, synchronous
|
||||
calls and more. Therefore, you should never call your own "receive" inside
|
||||
the GenServer callbacks as doing so will cause the GenServer to misbehave.
|
||||
If you want to receive custom messages, always receive them in `handle_info/2`.
|
||||
|
||||
## Learn more
|
||||
|
||||
If you wish to find out more about gen servers, the Elixir Getting Started
|
||||
guide provides a tutorial-like introduction. The documentation and links
|
||||
If you wish to find out more about gen servers, Elixir getting started
|
||||
guides provide a tutorial-like introduction. The documentation and links
|
||||
in Erlang can also provide extra insight.
|
||||
|
||||
* [GenServer – Elixir's Getting Started Guide](http://elixir-lang.org/getting-started/mix-otp/genserver.html)
|
||||
* [`:gen_server` module documentation](http://www.erlang.org/doc/man/gen_server.html)
|
||||
* [gen_server Behaviour – OTP Design Principles](http://www.erlang.org/doc/design_principles/gen_server_concepts.html)
|
||||
* [Clients and Servers – Learn You Some Erlang for Great Good!](http://learnyousomeerlang.com/clients-and-servers)
|
||||
* http://elixir-lang.org/getting_started/mix/1.html
|
||||
* http://www.erlang.org/doc/man/gen_server.html
|
||||
* http://www.erlang.org/doc/design_principles/gen_server_concepts.html
|
||||
* http://learnyousomeerlang.com/clients-and-servers
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Invoked when the server is started. `start_link/3` (or `start/3`) will
|
||||
block until it returns.
|
||||
|
||||
`args` is the argument term (second argument) passed to `start_link/3`.
|
||||
|
||||
Returning `{:ok, state}` will cause `start_link/3` to return
|
||||
`{:ok, pid}` and the process to enter its loop.
|
||||
|
||||
Returning `{:ok, state, timeout}` is similar to `{:ok, state}`
|
||||
except `handle_info(:timeout, state)` will be called after `timeout`
|
||||
milliseconds if no messages are received within the timeout.
|
||||
|
||||
Returning `{:ok, state, :hibernate}` is similar to
|
||||
`{:ok, state}` except the process is hibernated before entering the loop. See
|
||||
`handle_call/3` for more information on hibernation.
|
||||
|
||||
Returning `:ignore` will cause `start_link/3` to return `:ignore` and the
|
||||
process will exit normally without entering the loop or calling `terminate/2`.
|
||||
If used when part of a supervision tree the parent supervisor will not fail
|
||||
to start nor immediately try to restart the `GenServer`. The remainder of the
|
||||
supervision tree will be (re)started and so the `GenServer` should not be
|
||||
required by other processes. It can be started later with
|
||||
`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 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
|
||||
entering the loop or calling `terminate/2`.
|
||||
"""
|
||||
@callback init(args :: term) ::
|
||||
{:ok, state} |
|
||||
{:ok, state, timeout | :hibernate} |
|
||||
:ignore |
|
||||
{:stop, reason :: any} when state: any
|
||||
|
||||
@doc """
|
||||
Invoked to handle synchronous `call/3` messages. `call/3` will block until a
|
||||
reply is received (unless the call times out or nodes are disconnected).
|
||||
|
||||
`request` is the request message sent by a `call/3`, `from` is a 2-tuple
|
||||
containing the caller's pid and a term that uniquely identifies the call, and
|
||||
`state` is the current state of the `GenServer`.
|
||||
|
||||
Returning `{:reply, reply, new_state}` sends the response `reply` to the
|
||||
caller and continues the loop with new state `new_state`.
|
||||
|
||||
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.
|
||||
|
||||
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 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
|
||||
expected soon and minimising the memory of the process is shown to be
|
||||
beneficial.
|
||||
|
||||
Returning `{:noreply, new_state}` does not send a response to the caller and
|
||||
continues the loop with new state `new_state`. The response must be sent with
|
||||
`reply/2`.
|
||||
|
||||
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.
|
||||
|
||||
When replying from another process the `GenServer` should exit if the other
|
||||
process exits without replying as the caller will be blocking awaiting a
|
||||
reply.
|
||||
|
||||
Returning `{:noreply, new_state, timeout | :hibernate}` is similar to
|
||||
`{:noreply, new_state}` except a timeout or hibernation occurs as with a
|
||||
`:reply` tuple.
|
||||
|
||||
Returning `{:stop, reason, reply, new_state}` stops the loop and `terminate/2`
|
||||
is called with reason `reason` and state `new_state`. Then the `reply` is sent
|
||||
as the response to call and the process exits with reason `reason`.
|
||||
|
||||
Returning `{:stop, reason, new_state}` is similar to
|
||||
`{:stop, reason, reply, new_state}` except a reply is not sent.
|
||||
"""
|
||||
@callback handle_call(request :: term, from, state :: term) ::
|
||||
{:reply, reply, new_state} |
|
||||
{:reply, reply, new_state, timeout | :hibernate} |
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason, reply, new_state} |
|
||||
{:stop, reason, new_state} when reply: term, new_state: term, reason: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle asynchronous `cast/2` messages.
|
||||
|
||||
`request` is the request message sent by a `cast/2` and `state` is the current
|
||||
state of the `GenServer`.
|
||||
|
||||
Returning `{:noreply, new_state}` continues the loop with new state `new_state`.
|
||||
|
||||
Returning `{:noreply, new_state, timeout}` is similar to
|
||||
`{: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
|
||||
loop. See `handle_call/3` for more information.
|
||||
|
||||
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`.
|
||||
"""
|
||||
@callback handle_cast(request :: term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason :: term, new_state} when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked to handle all other messages.
|
||||
|
||||
`msg` is the message and `state` is the current state of the `GenServer`. When
|
||||
a timeout occurs the message is `:timeout`.
|
||||
|
||||
Return values are the same as `handle_cast/2`.
|
||||
"""
|
||||
@callback handle_info(msg :: :timeout | term, state :: term) ::
|
||||
{:noreply, new_state} |
|
||||
{:noreply, new_state, timeout | :hibernate} |
|
||||
{:stop, reason :: term, new_state} when new_state: term
|
||||
|
||||
@doc """
|
||||
Invoked when the server is about to exit. It should do any cleanup required.
|
||||
|
||||
`reason` is exit reason and `state` is the current state of the `GenServer`.
|
||||
The return value is ignored.
|
||||
|
||||
`terminate/2` is called if a callback (except `init/1`) returns a `:stop`
|
||||
tuple, raises, calls `Kernel.exit/1` or returns an invalid value. It may also
|
||||
be called if the `GenServer` traps exits using `Process.flag/2` *and* the
|
||||
parent process sends an exit signal.
|
||||
|
||||
If part of a supervision tree a `GenServer`'s `Supervisor` will send an exit
|
||||
signal when shutting it down. The exit signal is based on the shutdown
|
||||
strategy in the child's specification. If it is `:brutal_kill` the `GenServer`
|
||||
is killed and so `terminate/2` is not called. However if it is a timeout the
|
||||
`Supervisor` will send the exit signal `:shutdown` and the `GenServer` will
|
||||
have the duration of the timeout to call `terminate/2` - if the process is
|
||||
still alive after the timeout it is killed.
|
||||
|
||||
If the `GenServer` receives an exit signal (that is not `:normal`) from any
|
||||
process when it is not trapping exits it will exit abruptly with the same
|
||||
reason and so not call `terminate/2`. Note that a process does *NOT* trap
|
||||
exits by default and an exit signal is sent when a linked process exits or its
|
||||
node is disconnected.
|
||||
|
||||
Therefore it is not guaranteed that `terminate/2` is called when a `GenServer`
|
||||
exits. For such reasons, we usually recommend important clean-up rules to
|
||||
happen in separated processes either by use of monitoring or by links
|
||||
themselves. For example if the `GenServer` controls a `port` (e.g.
|
||||
`:gen_tcp.socket`) or `File.io_device`, they will be closed on receiving a
|
||||
`GenServer`'s exit signal and do not need to be closed in `terminate/2`.
|
||||
|
||||
If `reason` is not `:normal`, `:shutdown` nor `{:shutdown, term}` an error is
|
||||
logged.
|
||||
"""
|
||||
@callback terminate(reason, state :: term) ::
|
||||
term when reason: :normal | :shutdown | {:shutdown, term} | term
|
||||
|
||||
@doc """
|
||||
Invoked to change the state of the `GenServer` when a different version of a
|
||||
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
|
||||
`GenServer` 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.
|
||||
|
||||
Returning `{:error, reason}` fails the code change with reason `reason` and
|
||||
the state remains as the previous state.
|
||||
|
||||
If `code_change/3` raises the code change fails and the loop will continue
|
||||
with its previous state. Therefore this callback does not usually contain side effects.
|
||||
"""
|
||||
@callback code_change(old_vsn, state :: term, extra :: term) ::
|
||||
{:ok, new_state :: term} |
|
||||
{:error, reason :: term} when old_vsn: term | {:down, term}
|
||||
|
||||
@typedoc "Return values of `start*` functions"
|
||||
@type on_start :: {:ok, pid} | :ignore | {:error, {:already_started, pid} | term}
|
||||
|
||||
@@ -380,28 +210,17 @@ defmodule GenServer do
|
||||
@type name :: atom | {:global, term} | {:via, module, term}
|
||||
|
||||
@typedoc "Options used by the `start*` functions"
|
||||
@type options :: [option]
|
||||
@type options :: [debug: debug,
|
||||
name: name,
|
||||
timeout: timeout,
|
||||
spawn_opt: Process.spawn_opt]
|
||||
|
||||
@typedoc "Option values used by the `start*` functions"
|
||||
@type option :: {:debug, debug} |
|
||||
{:name, name} |
|
||||
{:timeout, timeout} |
|
||||
{:spawn_opt, Process.spawn_opt}
|
||||
|
||||
@typedoc "Debug options supported by the `start*` functions"
|
||||
@typedoc "debug options supported by the `start*` functions"
|
||||
@type debug :: [:trace | :log | :statistics | {:log_to_file, Path.t}]
|
||||
|
||||
@typedoc "The server reference"
|
||||
@type server :: pid | name | {atom, node}
|
||||
|
||||
@typedoc """
|
||||
Tuple describing the client of a call request.
|
||||
|
||||
`pid` is the pid of the caller and `tag` is a unique term used to identify the
|
||||
call.
|
||||
"""
|
||||
@type from :: {pid, tag :: term}
|
||||
|
||||
@doc false
|
||||
defmacro __using__(_) do
|
||||
quote location: :keep do
|
||||
@@ -414,12 +233,7 @@ defmodule GenServer do
|
||||
|
||||
@doc false
|
||||
def handle_call(msg, _from, state) do
|
||||
# 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)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
{:stop, {:bad_call, msg}, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -429,12 +243,7 @@ defmodule GenServer do
|
||||
|
||||
@doc false
|
||||
def handle_cast(msg, state) do
|
||||
# We do this to trick Dialyzer to not complain about non-local returns.
|
||||
reason = {:bad_cast, msg}
|
||||
case :erlang.phash2(1, 1) do
|
||||
0 -> exit(reason)
|
||||
1 -> {:stop, reason, state}
|
||||
end
|
||||
{:stop, {:bad_cast, msg}, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -482,8 +291,8 @@ defmodule GenServer do
|
||||
## Return values
|
||||
|
||||
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
|
||||
`{:ok, pid}`, where pid is the pid of the server. If there already exists a
|
||||
process with the specified server name, the function returns
|
||||
`{:error, {:already_started, pid}}` with the pid of that process.
|
||||
|
||||
If the `init/1` callback fails with `reason`, the function returns
|
||||
@@ -517,23 +326,6 @@ defmodule GenServer do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the server with the given `reason`.
|
||||
|
||||
The `terminate/2` callback will be invoked before exiting.
|
||||
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.
|
||||
"""
|
||||
@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.
|
||||
|
||||
@@ -553,7 +345,7 @@ defmodule GenServer do
|
||||
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.
|
||||
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
|
||||
@@ -570,24 +362,17 @@ defmodule GenServer do
|
||||
@doc """
|
||||
Sends an asynchronous request to the `server`.
|
||||
|
||||
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. 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`.
|
||||
This function returns `:ok` immediately, regardless of
|
||||
whether the destination node or server does exists, unless
|
||||
the server is specified as an atom.
|
||||
|
||||
`handle_cast/2` will be called on the server to handle
|
||||
the request. In case the `server` is on a node which is
|
||||
not yet connected to the caller one, the call is going to
|
||||
the request. In case the server is a node which is not
|
||||
yet connected to the caller one, the call is going to
|
||||
block until a connection happens. This is different than
|
||||
the behaviour in OTP's `:gen_server` where the message
|
||||
is sent by another process in this case, which could cause
|
||||
messages to other nodes to arrive out of order.
|
||||
would be sent by another process, which could cause
|
||||
messages to arrive out of order.
|
||||
"""
|
||||
@spec cast(server, term) :: :ok
|
||||
def cast(server, request)
|
||||
@@ -675,7 +460,7 @@ defmodule GenServer do
|
||||
|
||||
This function always returns `:ok`.
|
||||
"""
|
||||
@spec reply(from, term) :: :ok
|
||||
@spec reply({pid, reference}, term) :: :ok
|
||||
def reply(client, reply)
|
||||
|
||||
def reply({to, tag}, reply) do
|
||||
@@ -687,41 +472,6 @@ defmodule GenServer do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the `pid` or `{name, node}` of a GenServer process.
|
||||
Returns `nil` if no process is associated with the given name.
|
||||
|
||||
For example, to lookup a server process, monitor it and send a cast:
|
||||
|
||||
process = GenServer.whereis(server)
|
||||
monitor = Process.monitor(process)
|
||||
GenServer.cast(process, :hello)
|
||||
|
||||
"""
|
||||
@spec whereis(server) :: pid | {atom, node} | nil
|
||||
def whereis(pid) when is_pid(pid), do: pid
|
||||
def whereis(name) when is_atom(name) do
|
||||
Process.whereis(name)
|
||||
end
|
||||
def whereis({:global, name}) do
|
||||
case :global.whereis_name(name) do
|
||||
pid when is_pid(pid) -> pid
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
def whereis({:via, mod, name}) do
|
||||
case apply(mod, :whereis_name, [name]) do
|
||||
pid when is_pid(pid) -> pid
|
||||
:undefined -> nil
|
||||
end
|
||||
end
|
||||
def whereis({name, local}) when is_atom(name) and local == node() do
|
||||
Process.whereis(name)
|
||||
end
|
||||
def whereis({name, node} = server) when is_atom(name) and is_atom(node) do
|
||||
server
|
||||
end
|
||||
|
||||
@compile {:inline, [nodes: 0]}
|
||||
|
||||
defp nodes do
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
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.
|
||||
"""
|
||||
|
||||
use Dict
|
||||
@@ -185,7 +194,7 @@ defmodule HashDict do
|
||||
end
|
||||
|
||||
defp do_reduce_each([k|v], {:cont, acc}, fun, next) do
|
||||
next.(fun.({k, v}, acc))
|
||||
next.(fun.({k,v}, acc))
|
||||
end
|
||||
|
||||
defp do_reduce_each({k, v, n}, {:cont, acc}, fun, next) do
|
||||
@@ -224,10 +233,25 @@ defimpl Enumerable, for: HashDict do
|
||||
def count(dict), do: {:ok, HashDict.size(dict)}
|
||||
end
|
||||
|
||||
defimpl Access, for: HashDict do
|
||||
def get(dict, key) do
|
||||
HashDict.get(dict, key, nil)
|
||||
end
|
||||
|
||||
def get_and_update(dict, key, fun) do
|
||||
{get, update} = fun.(HashDict.get(dict, key, nil))
|
||||
{get, HashDict.put(dict, key, update)}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashDict do
|
||||
def empty(_dict) do
|
||||
HashDict.new
|
||||
end
|
||||
|
||||
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}
|
||||
|
||||
@@ -1,8 +1,17 @@
|
||||
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.
|
||||
"""
|
||||
|
||||
@behaviour Set
|
||||
@@ -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{}
|
||||
@@ -241,6 +253,10 @@ defimpl Enumerable, for: HashSet do
|
||||
end
|
||||
|
||||
defimpl Collectable, for: HashSet do
|
||||
def empty(_dict) do
|
||||
HashSet.new
|
||||
end
|
||||
|
||||
def into(original) do
|
||||
{original, fn
|
||||
set, {:cont, x} -> HashSet.put(set, x)
|
||||
|
||||
+64
-68
@@ -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 `">"`.
|
||||
@@ -41,14 +41,13 @@ defprotocol Inspect do
|
||||
## Error handling
|
||||
|
||||
In case there is an error while your structure is being inspected,
|
||||
Elixir will raise an `ArgumentError` error and will automatically fall back
|
||||
to a raw representation for printing the structure.
|
||||
Elixir will automatically fall back to a raw representation.
|
||||
|
||||
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)
|
||||
|
||||
"""
|
||||
|
||||
@@ -88,7 +87,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 +103,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 +112,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 +128,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
|
||||
@@ -143,7 +142,7 @@ end
|
||||
defimpl Inspect, for: BitString do
|
||||
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, ?">>
|
||||
<<?", escape(thing, ?") :: binary, ?">>
|
||||
else
|
||||
inspect_bitstring(thing, opts)
|
||||
end
|
||||
@@ -160,54 +159,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 +217,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,7 +235,7 @@ 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
|
||||
@@ -249,11 +248,11 @@ defimpl Inspect, for: BitString do
|
||||
acc <> "..."
|
||||
end
|
||||
|
||||
defp each_bit(<<h, t::bitstring>>, counter, acc) when t != <<>> do
|
||||
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, acc) do
|
||||
defp each_bit(<<h :: size(8)>>, _counter, acc) do
|
||||
acc <> Integer.to_string(h)
|
||||
end
|
||||
|
||||
@@ -263,7 +262,7 @@ defimpl Inspect, for: BitString do
|
||||
|
||||
defp each_bit(bitstring, _counter, acc) do
|
||||
size = bit_size(bitstring)
|
||||
<<h::size(size)>> = bitstring
|
||||
<<h :: size(size)>> = bitstring
|
||||
acc <> Integer.to_string(h) <> "::size(" <> Integer.to_string(size) <> ")"
|
||||
end
|
||||
|
||||
@@ -277,7 +276,7 @@ defimpl Inspect, for: List do
|
||||
def inspect(thing, %Inspect.Opts{char_lists: lists} = opts) do
|
||||
cond do
|
||||
lists == :as_char_lists or (lists == :infer and printable?(thing)) ->
|
||||
<<?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?')::binary, ?'>>
|
||||
<< ?', Inspect.BitString.escape(IO.chardata_to_string(thing), ?') :: binary, ?' >>
|
||||
keyword?(thing) ->
|
||||
surround_many("[", thing, "]", opts, &keyword/2)
|
||||
true ->
|
||||
@@ -285,7 +284,6 @@ defimpl Inspect, for: List do
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword({key, value}, opts) do
|
||||
concat(
|
||||
key_to_binary(key) <> ": ",
|
||||
@@ -293,7 +291,6 @@ defimpl Inspect, for: List do
|
||||
)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
case Atom.to_char_list(key) do
|
||||
'Elixir.' ++ _ -> false
|
||||
@@ -304,19 +301,6 @@ defimpl Inspect, for: List do
|
||||
def keyword?([]), do: true
|
||||
def keyword?(_other), do: false
|
||||
|
||||
@doc false
|
||||
def printable?([c|cs]) when is_integer(c) and 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
|
||||
@@ -325,6 +309,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
|
||||
@@ -410,7 +406,7 @@ defimpl Inspect, for: Regex do
|
||||
defp escape(<<term>> <> rest, buf, term),
|
||||
do: escape(rest, buf <> <<?\\, term>>, term)
|
||||
|
||||
# the list of characters is from "String.printable?" impl
|
||||
# the list of characters is from `String.printable?` impl
|
||||
# minus characters treated specially by regex: \s, \d, \b, \e
|
||||
|
||||
defp escape(<<?\n>> <> rest, buf, term),
|
||||
|
||||
@@ -4,8 +4,8 @@ defmodule Inspect.Opts do
|
||||
|
||||
The following fields are available:
|
||||
|
||||
* `:structs` - when `false`, structs are not formatted by the inspect
|
||||
protocol, they are instead printed as maps, defaults to `true`.
|
||||
* `:structs` - when false, structs are not formatted by the inspect
|
||||
protocol, they are instead printed as maps, defaults to true.
|
||||
|
||||
* `:binaries` - when `:as_strings` all binaries will be printed as strings,
|
||||
non-printable bytes will be escaped.
|
||||
@@ -27,19 +27,14 @@ defmodule Inspect.Opts do
|
||||
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`.
|
||||
* `:pretty` - if set to true enables pretty printing, defaults to false.
|
||||
|
||||
* `:width` - defaults to the 80 characters, used when pretty is `true` or
|
||||
* `: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
|
||||
|
||||
* `:safe` - when `false`, failures while inspecting structs will be raised
|
||||
as errors instead of being wrapped in the Inspect.Error exception. This
|
||||
is useful when debugging failures and crashes for custom inspect
|
||||
implementations
|
||||
|
||||
"""
|
||||
|
||||
defstruct structs: true,
|
||||
@@ -48,36 +43,25 @@ defmodule Inspect.Opts do
|
||||
limit: 50,
|
||||
width: 80,
|
||||
base: :decimal,
|
||||
pretty: false,
|
||||
safe: true
|
||||
pretty: false
|
||||
|
||||
@type t :: %__MODULE__{
|
||||
structs: boolean,
|
||||
binaries: :infer | :as_binaries | :as_strings,
|
||||
char_lists: :infer | :as_lists | :as_char_lists,
|
||||
limit: pos_integer | :infinity,
|
||||
limit: pos_integer,
|
||||
width: pos_integer | :infinity,
|
||||
base: :decimal | :binary | :hex | :octal,
|
||||
pretty: boolean,
|
||||
safe: boolean}
|
||||
end
|
||||
|
||||
defmodule Inspect.Error do
|
||||
@moduledoc """
|
||||
Raised when a struct cannot be inspected.
|
||||
"""
|
||||
defexception [:message]
|
||||
pretty: boolean}
|
||||
end
|
||||
|
||||
defmodule Inspect.Algebra do
|
||||
@moduledoc ~S"""
|
||||
A set of functions for creating and manipulating algebra
|
||||
documents.
|
||||
documents, as described in ["Strictly Pretty" (2000) by Christian Lindig][0].
|
||||
|
||||
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.
|
||||
An algebra document is represented by an `Inspect.Algebra` node
|
||||
or a regular string.
|
||||
|
||||
iex> Inspect.Algebra.empty
|
||||
:doc_nil
|
||||
@@ -145,7 +129,7 @@ defmodule Inspect.Algebra do
|
||||
@nesting 1
|
||||
@break " "
|
||||
|
||||
# Functional interface to "doc" records
|
||||
# Functional interface to `doc` records
|
||||
|
||||
@type t :: :doc_nil | :doc_line | doc_cons | doc_nest | doc_break | doc_group | binary
|
||||
|
||||
@@ -201,8 +185,6 @@ defmodule Inspect.Algebra do
|
||||
Inspect.inspect(map, opts)
|
||||
rescue
|
||||
e ->
|
||||
stacktrace = System.stacktrace
|
||||
|
||||
# Because we try to raise a nice error message in case
|
||||
# we can't inspect a struct, there is a chance the error
|
||||
# message itself relies on the struct being printed, so
|
||||
@@ -214,20 +196,11 @@ defmodule Inspect.Algebra do
|
||||
else
|
||||
try do
|
||||
Process.put(:inspect_trap, true)
|
||||
|
||||
res = Inspect.Map.inspect(map, opts)
|
||||
res = IO.iodata_to_binary(format(res, :infinity))
|
||||
|
||||
exception = Inspect.Error.exception(
|
||||
message: "got #{inspect e.__struct__} with message " <>
|
||||
"#{inspect Exception.message(e)} while inspecting #{res}"
|
||||
)
|
||||
|
||||
if opts.safe do
|
||||
Inspect.inspect(exception, opts)
|
||||
else
|
||||
reraise(exception, stacktrace)
|
||||
end
|
||||
formatted = IO.iodata_to_binary(format(res, :infinity))
|
||||
raise ArgumentError,
|
||||
"Got #{inspect e.__struct__} with message " <>
|
||||
"\"#{Exception.message(e)}\" while inspecting #{formatted}"
|
||||
after
|
||||
Process.delete(:inspect_trap)
|
||||
end
|
||||
@@ -274,7 +247,7 @@ defmodule Inspect.Algebra do
|
||||
"""
|
||||
@spec concat([t]) :: doc_cons
|
||||
def concat(docs) do
|
||||
fold_doc(docs, &concat(&1, &2))
|
||||
folddoc(docs, &concat(&1, &2))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -405,18 +378,17 @@ defmodule Inspect.Algebra do
|
||||
## Examples
|
||||
|
||||
iex> doc = ["A", "B"]
|
||||
iex> doc = Inspect.Algebra.fold_doc(doc, fn(x, y) ->
|
||||
iex> doc = Inspect.Algebra.folddoc(doc, fn(x,y) ->
|
||||
...> Inspect.Algebra.concat [x, "!", y]
|
||||
...> end)
|
||||
iex> Inspect.Algebra.format(doc, 80)
|
||||
["A", "!", "B"]
|
||||
|
||||
"""
|
||||
@spec fold_doc([t], ((t, t) -> t)) :: t
|
||||
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))
|
||||
@spec folddoc([t], ((t, t) -> t)) :: t
|
||||
def folddoc([], _), do: empty
|
||||
def folddoc([doc], _), do: doc
|
||||
def folddoc([d|ds], f), do: f.(d, folddoc(ds, f))
|
||||
|
||||
# Elixir conveniences
|
||||
|
||||
@@ -472,41 +444,34 @@ defmodule Inspect.Algebra do
|
||||
concat(left, right)
|
||||
end
|
||||
|
||||
defp do_surround_many(left, docs, right, limit, opts, fun, sep) do
|
||||
surround(left, do_surround_many(docs, limit, opts, fun, sep), right)
|
||||
defp do_surround_many(left, docs, right, limit, _opts, fun, sep) do
|
||||
surround(left, do_surround_many(docs, limit, _opts, fun, sep), right)
|
||||
end
|
||||
|
||||
defp do_surround_many(_, 0, _opts, _fun, _sep) do
|
||||
"..."
|
||||
end
|
||||
|
||||
defp do_surround_many([], _limit, _opts, _fun, _sep) do
|
||||
:doc_nil
|
||||
end
|
||||
|
||||
defp do_surround_many([h], limit, opts, fun, _sep) do
|
||||
fun.(h, %{opts | limit: limit})
|
||||
end
|
||||
|
||||
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)
|
||||
glue(
|
||||
concat(fun.(h, %{opts | limit: limit}), sep),
|
||||
do_surround_many(t, limit, opts, fun, sep)
|
||||
)
|
||||
end
|
||||
|
||||
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})
|
||||
do_join(h, t, @tail_separator)
|
||||
glue(
|
||||
concat(fun.(h, %{opts | limit: limit}), @tail_separator),
|
||||
fun.(t, %{opts | limit: limit})
|
||||
)
|
||||
end
|
||||
|
||||
defp do_join(:doc_nil, :doc_nil, _), do: :doc_nil
|
||||
defp do_join(h, :doc_nil, _), do: h
|
||||
defp do_join(:doc_nil, t, _), do: t
|
||||
defp do_join(h, t, sep), do: glue(concat(h, sep), t)
|
||||
|
||||
defp decrement(:infinity), do: :infinity
|
||||
defp decrement(counter), do: counter - 1
|
||||
|
||||
|
||||
+17
-140
@@ -11,14 +11,6 @@ defmodule Integer do
|
||||
Returns `true` if `n` is an odd number, otherwise `false`.
|
||||
|
||||
Allowed in guard clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.is_odd(3)
|
||||
true
|
||||
|
||||
iex> Integer.is_odd(4)
|
||||
false
|
||||
"""
|
||||
defmacro is_odd(n) do
|
||||
quote do: (unquote(n) &&& 1) == 1
|
||||
@@ -30,171 +22,56 @@ defmodule Integer do
|
||||
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
|
||||
"""
|
||||
defmacro is_even(n) do
|
||||
quote do: (unquote(n) &&& 1) == 0
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the ordered digits for the given non-negative integer.
|
||||
|
||||
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(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(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(0, _base, acc), do: 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.
|
||||
|
||||
An optional base value may be provided representing the radix for the digits.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.undigits([1, 0, 1])
|
||||
101
|
||||
|
||||
iex> Integer.undigits([1, 4], 16)
|
||||
20
|
||||
"""
|
||||
@spec undigits([integer], integer) :: integer
|
||||
def undigits(digits, base \\ 10) when is_integer(base) do
|
||||
do_undigits(digits, base, 0)
|
||||
end
|
||||
|
||||
defp do_undigits([], _base, acc), do: acc
|
||||
defp do_undigits([digit | tail], base, acc) do
|
||||
do_undigits(tail, base, acc * base + digit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a binary from a text representation of an integer
|
||||
in an optional base `base` to the corresponding integer.
|
||||
|
||||
If the base `base` is not given, base 10 will be used.
|
||||
Converts a binary to an integer.
|
||||
|
||||
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.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Integer.parse("34")
|
||||
{34, ""}
|
||||
{34,""}
|
||||
|
||||
iex> Integer.parse("34.5")
|
||||
{34, ".5"}
|
||||
{34,".5"}
|
||||
|
||||
iex> Integer.parse("three")
|
||||
:error
|
||||
|
||||
iex> Integer.parse("34", 10)
|
||||
{34, ""}
|
||||
|
||||
iex> Integer.parse("f4", 16)
|
||||
{244, ""}
|
||||
|
||||
iex> Integer.parse("Awww++", 36)
|
||||
{509216, "++"}
|
||||
|
||||
iex> Integer.parse("fab", 10)
|
||||
:error
|
||||
|
||||
iex> Integer.parse("a2", 38)
|
||||
** (ArgumentError) invalid base 38
|
||||
|
||||
"""
|
||||
@spec parse(binary, 2..36) :: {integer, binary} | :error | no_return
|
||||
def parse(binary, base \\ 10)
|
||||
|
||||
def parse(binary, base) when is_integer(base) and base in 2..36 do
|
||||
parse_in_base(binary, base)
|
||||
end
|
||||
|
||||
def parse(_, base) do
|
||||
raise ArgumentError, "invalid base #{base}"
|
||||
end
|
||||
|
||||
defp parse_in_base("-" <> bin, base) do
|
||||
case do_parse(bin, base) do
|
||||
@spec parse(binary) :: {integer, binary} | :error
|
||||
def parse(<< ?-, bin :: binary >>) do
|
||||
case do_parse(bin) do
|
||||
:error -> :error
|
||||
{number, remainder} -> {-number, remainder}
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_in_base("+" <> bin, base) do
|
||||
do_parse(bin, base)
|
||||
def parse(<< ?+, bin :: binary >>) do
|
||||
do_parse(bin)
|
||||
end
|
||||
|
||||
defp parse_in_base(bin, base) when is_binary(bin) do
|
||||
do_parse(bin, base)
|
||||
def parse(bin) when is_binary(bin) do
|
||||
do_parse(bin)
|
||||
end
|
||||
|
||||
defp do_parse(<<char, rest::binary>>, base) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, parse_digit(char, base))
|
||||
else
|
||||
:error
|
||||
end
|
||||
defp do_parse(<< char, bin :: binary >>) when char in ?0..?9, do: do_parse(bin, char - ?0)
|
||||
defp do_parse(_), do: :error
|
||||
|
||||
defp do_parse(<< char, rest :: binary >>, acc) when char in ?0..?9 do
|
||||
do_parse rest, 10 * acc + (char - ?0)
|
||||
end
|
||||
|
||||
defp do_parse(_, _), do: :error
|
||||
|
||||
defp do_parse(<<char, rest::binary>> = bin, base, acc) do
|
||||
if valid_digit_in_base?(char, base) do
|
||||
do_parse(rest, base, base * acc + parse_digit(char, base))
|
||||
else
|
||||
{acc, bin}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_parse(bitstring, _, acc) do
|
||||
defp do_parse(bitstring, acc) do
|
||||
{acc, bitstring}
|
||||
end
|
||||
|
||||
defp parse_digit(char, _) do
|
||||
cond do
|
||||
char in ?0..?9 -> char - ?0
|
||||
char in ?A..?Z -> char - ?A + 10
|
||||
true -> char - ?a + 10
|
||||
end
|
||||
end
|
||||
|
||||
defp valid_digit_in_base?(char, base) do
|
||||
if base <= 10 do
|
||||
char in ?0..(?0 + base - 1)
|
||||
else
|
||||
char in ?0..?9 or char in ?A..(?A + base - 11) or char in ?a..(?a + base - 11)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a binary which corresponds to the text representation
|
||||
of `some_integer`.
|
||||
@@ -247,7 +124,7 @@ defmodule Integer do
|
||||
|
||||
@doc """
|
||||
Returns a char list which corresponds to the text representation of the
|
||||
given integer in the given base.
|
||||
given integer in the given case.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
|
||||
+16
-33
@@ -2,35 +2,31 @@ defmodule IO do
|
||||
@moduledoc """
|
||||
Functions handling IO.
|
||||
|
||||
Many functions in this module expect an IO device as an argument.
|
||||
Many functions in this module expects an IO device as 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 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*` expects iodata as 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 shorcuts:
|
||||
the atom must be the name of a registered process. However,
|
||||
there are three exceptions for this rule:
|
||||
|
||||
* `:stdio` - a shortcut for `:standard_io`, which maps to
|
||||
the current `Process.group_leader/0` in Erlang
|
||||
* `:standard_io` - when the `:standard_io` atom is given,
|
||||
it is treated as a shortcut for `Process.group_leader`
|
||||
|
||||
* `:stderr` - a shortcut for the named process `:standard_error`
|
||||
provided in Erlang
|
||||
* `:stdio` - is a shortcut for `:standard_io`
|
||||
|
||||
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.
|
||||
* `:stderr` - is a shortcut for `:standard_error`
|
||||
|
||||
"""
|
||||
|
||||
@@ -127,9 +123,8 @@ defmodule IO do
|
||||
end
|
||||
end
|
||||
|
||||
@read_all_size 4096
|
||||
defp do_binread_all(mapped_dev, acc) do
|
||||
case :file.read(mapped_dev, @read_all_size) do
|
||||
case :file.read_line(mapped_dev) do
|
||||
{:ok, data} -> do_binread_all(mapped_dev, acc <> data)
|
||||
:eof -> acc
|
||||
other -> other
|
||||
@@ -188,24 +183,20 @@ defmodule IO do
|
||||
80 characters. The width can be changed by explicitly
|
||||
passing the `:width` option.
|
||||
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
|
||||
## Examples
|
||||
|
||||
IO.inspect Process.list, width: 40
|
||||
|
||||
"""
|
||||
@spec inspect(item, Keyword.t) :: item when item: var
|
||||
@spec inspect(term, Keyword.t) :: term
|
||||
def inspect(item, opts \\ []) do
|
||||
inspect group_leader(), item, opts
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inspects the item with options using the given device.
|
||||
|
||||
See `Inspect.Opts` for a full list of options.
|
||||
"""
|
||||
@spec inspect(device, item, Keyword.t) :: item when item: var
|
||||
@spec inspect(device, term, Keyword.t) :: term
|
||||
def inspect(device, item, opts) when is_list(opts) do
|
||||
opts = struct(Inspect.Opts, opts)
|
||||
iodata = Inspect.Algebra.format(Inspect.Algebra.to_doc(item, opts), opts.width)
|
||||
@@ -252,24 +243,16 @@ defmodule IO do
|
||||
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)
|
||||
by a LF (or end of file)
|
||||
|
||||
* `:eof` - end of file was encountered
|
||||
|
||||
* `{: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?"
|
||||
"""
|
||||
@spec gets(device, chardata | String.Chars.t) :: chardata | nodata
|
||||
def gets(device \\ group_leader(), prompt) do
|
||||
@@ -379,11 +362,11 @@ defmodule IO do
|
||||
iex> bin2 = <<4, 5>>
|
||||
iex> bin3 = <<6>>
|
||||
iex> IO.iodata_to_binary([bin1, 1, [2, 3, bin2], 4|bin3])
|
||||
<<1, 2, 3, 1, 2, 3, 4, 5, 4, 6>>
|
||||
<<1,2,3,1,2,3,4,5,4,6>>
|
||||
|
||||
iex> bin = <<1, 2, 3>>
|
||||
iex> IO.iodata_to_binary(bin)
|
||||
<<1, 2, 3>>
|
||||
<<1,2,3>>
|
||||
|
||||
"""
|
||||
@spec iodata_to_binary(iodata) :: binary
|
||||
|
||||
+14
-45
@@ -1,7 +1,7 @@
|
||||
defmodule IO.ANSI.Sequence do
|
||||
@moduledoc false
|
||||
|
||||
defmacro defsequence(name, code, terminator \\ "m") do
|
||||
defmacro defsequence(name, code \\ "", terminator \\ "m") do
|
||||
quote bind_quoted: [name: name, code: code, terminator: terminator] do
|
||||
def unquote(name)() do
|
||||
"\e[#{unquote(code)}#{unquote(terminator)}"
|
||||
@@ -16,11 +16,10 @@ end
|
||||
|
||||
defmodule IO.ANSI do
|
||||
@moduledoc """
|
||||
Functionality to render ANSI escape sequences.
|
||||
|
||||
[ANSI escape sequences](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
||||
are characters embedded in text used to control formatting, color, and
|
||||
other output options on video text terminals.
|
||||
Functionality to render ANSI escape sequences
|
||||
(http://en.wikipedia.org/wiki/ANSI_escape_code) — characters embedded
|
||||
in text used to control formatting, color, and other output options
|
||||
on video text terminals.
|
||||
"""
|
||||
|
||||
import IO.ANSI.Sequence
|
||||
@@ -34,7 +33,7 @@ defmodule IO.ANSI do
|
||||
|
||||
This function simply reads the configuration value for
|
||||
`:ansi_enabled` in the `:elixir` application. The value is by
|
||||
default `false` unless Elixir can detect during startup that
|
||||
default false unless Elixir can detect during startup that
|
||||
both `stdout` and `stderr` are terminals.
|
||||
"""
|
||||
@spec enabled? :: boolean
|
||||
@@ -42,34 +41,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
|
||||
|
||||
@@ -79,7 +50,7 @@ defmodule IO.ANSI do
|
||||
@doc "Faint (decreased intensity), not widely supported"
|
||||
defsequence :faint, 2
|
||||
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse"
|
||||
@doc "Italic: on. Not widely supported. Sometimes treated as inverse."
|
||||
defsequence :italic, 3
|
||||
|
||||
@doc "Underline: Single"
|
||||
@@ -100,7 +71,7 @@ defmodule IO.ANSI do
|
||||
@doc "Conceal. Not widely supported"
|
||||
defsequence :conceal, 8
|
||||
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported"
|
||||
@doc "Crossed-out. Characters legible, but marked for deletion. Not widely supported."
|
||||
defsequence :crossed_out, 9
|
||||
|
||||
@doc "Sets primary (default) font"
|
||||
@@ -124,8 +95,9 @@ defmodule IO.ANSI do
|
||||
defsequence :blink_off, 25
|
||||
|
||||
colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
|
||||
colors = Enum.zip(0..(length(colors)-1), colors)
|
||||
|
||||
for {color, code} <- Enum.with_index(colors) do
|
||||
for {code, color} <- colors do
|
||||
@doc "Sets foreground color to #{color}"
|
||||
defsequence color, code + 30
|
||||
|
||||
@@ -154,17 +126,14 @@ defmodule IO.ANSI do
|
||||
@doc "Not overlined"
|
||||
defsequence :not_overlined, 55
|
||||
|
||||
@doc "Sends cursor home"
|
||||
@doc "Send cursor home"
|
||||
defsequence :home, "", "H"
|
||||
|
||||
@doc "Clears screen"
|
||||
@doc "Clear screen"
|
||||
defsequence :clear, "2", "J"
|
||||
|
||||
@doc "Clears line"
|
||||
defsequence :clear_line, "2", "K"
|
||||
|
||||
defp format_sequence(other) do
|
||||
raise ArgumentError, "invalid ANSI sequence specification: #{inspect other}"
|
||||
raise ArgumentError, "invalid ANSI sequence specification: #{other}"
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@@ -173,7 +142,7 @@ defmodule IO.ANSI do
|
||||
|
||||
The named sequences are represented by atoms.
|
||||
|
||||
It will also append an `IO.ANSI.reset/0` to the chardata when a conversion is
|
||||
It will also append an `IO.ANSI.reset` to the chardata when a conversion is
|
||||
performed. If you don't want this behaviour, use `format_fragment/2`.
|
||||
|
||||
An optional boolean parameter can be passed to enable or disable
|
||||
|
||||
+69
-150
@@ -25,10 +25,10 @@ defmodule IO.ANSI.Docs do
|
||||
[enabled: true,
|
||||
doc_bold: [:bright],
|
||||
doc_code: [:cyan, :bright],
|
||||
doc_headings: [:yellow],
|
||||
doc_headings: [:yellow, :bright],
|
||||
doc_inline_code: [:cyan],
|
||||
doc_table_heading: [:reverse],
|
||||
doc_title: [:reverse, :yellow],
|
||||
doc_title: [:reverse, :yellow, :bright],
|
||||
doc_underline: [:underline],
|
||||
width: 80]
|
||||
end
|
||||
@@ -57,7 +57,7 @@ defmodule IO.ANSI.Docs do
|
||||
def print(doc, options \\ []) do
|
||||
options = Keyword.merge(default_options, options)
|
||||
doc
|
||||
|> String.split(["\r\n", "\n"], trim: false)
|
||||
|> String.split(["\r\n","\n"], trim: false)
|
||||
|> Enum.map(&String.rstrip/1)
|
||||
|> process([], "", options)
|
||||
end
|
||||
@@ -94,25 +94,25 @@ defmodule IO.ANSI.Docs do
|
||||
process_code(rest, [line], indent, options)
|
||||
end
|
||||
|
||||
defp process(["```" <> _line | rest], text, indent, options) do
|
||||
process_fenced_code_block(rest, text, indent, options, _delimiter = "```")
|
||||
end
|
||||
|
||||
defp process(["~~~" <> _line | rest], text, indent, options) do
|
||||
process_fenced_code_block(rest, text, indent, options, _delimiter = "~~~")
|
||||
end
|
||||
|
||||
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,22 +135,6 @@ 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
|
||||
if indent == "", do: entry = " " <> entry
|
||||
@@ -178,11 +162,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
|
||||
@@ -210,7 +194,7 @@ defmodule IO.ANSI.Docs do
|
||||
lines
|
||||
|> Enum.join(" ")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> handle_inline(nil, [], [], options)
|
||||
|> String.split(~r{\s})
|
||||
|> write_with_wrap(options[:width] - byte_size(indent), indent, no_wrap)
|
||||
|
||||
@@ -237,23 +221,6 @@ defmodule IO.ANSI.Docs do
|
||||
process(rest, [], indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code_block(rest, text, indent, options, delimiter) do
|
||||
write_text(text, indent, options)
|
||||
process_fenced_code(rest, [], indent, options, delimiter)
|
||||
end
|
||||
|
||||
defp process_fenced_code([], code, indent, options, _delimiter) do
|
||||
write_code(code, indent, options)
|
||||
end
|
||||
|
||||
defp process_fenced_code([line | rest], code, indent, options, delimiter) do
|
||||
if line === delimiter do
|
||||
process_code(rest, code, indent, options)
|
||||
else
|
||||
process_fenced_code(rest, [line|code], indent, options, delimiter)
|
||||
end
|
||||
end
|
||||
|
||||
defp write_code(code, indent, options) do
|
||||
write(:doc_code, "#{indent}┃ #{Enum.join(Enum.reverse(code), "\n#{indent}┃ ")}", options)
|
||||
newline_after_block
|
||||
@@ -262,7 +229,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)
|
||||
@@ -295,7 +262,7 @@ defmodule IO.ANSI.Docs do
|
||||
col = col
|
||||
|> String.replace(~r/\\ \|/x, "|")
|
||||
|> handle_links
|
||||
|> handle_inline(options)
|
||||
|> handle_inline(nil, [], [], options)
|
||||
{col, length_without_escape(col, 0)}
|
||||
end
|
||||
|
||||
@@ -303,7 +270,7 @@ defmodule IO.ANSI.Docs do
|
||||
do: cols ++ List.duplicate({"", 0}, col_count - length(cols))
|
||||
|
||||
defp max_column_widths(cols, widths),
|
||||
do: Enum.zip(cols, widths) |> Enum.map(fn {a, b} -> max(a, b) end)
|
||||
do: Enum.zip(cols, widths) |> Enum.map(fn {a,b} -> max(a,b) end)
|
||||
|
||||
# If second line is heading separator, use the heading style on the first
|
||||
defp render_table([first, second | rest], widths, options) do
|
||||
@@ -342,7 +309,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 +319,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),
|
||||
@@ -401,11 +360,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,113 +382,73 @@ 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
|
||||
Regex.replace(~r{\[(.*?)\]\((.*?)\)}, text, "\\1 (\\2)")
|
||||
end
|
||||
|
||||
# Single inline quotes.
|
||||
@single [?`, ?_, ?*]
|
||||
|
||||
# We have four entries: **, *, _ and `.
|
||||
#
|
||||
# The first three behave the same while the last one is simpler
|
||||
# when it comes to delimiters. But, since the first has two
|
||||
# characters, we need to handle 3 cases:
|
||||
#
|
||||
# 1. **
|
||||
# 2. _ and *
|
||||
# 3. `
|
||||
#
|
||||
# Where the first two should have the same code but match differently.
|
||||
@single [?_, ?*]
|
||||
# ` does not require space in between
|
||||
@spaced [?_, ?*]
|
||||
|
||||
# Characters that can mark the beginning or the end of a word.
|
||||
# Only support the most common ones at this moment.
|
||||
@delimiters [?\s, ?', ?", ?!, ?@, ?#, ?$, ?%, ?^, ?&, ?-, ?+, ?(, ?), ?[, ?], ?{, ?}, ?<, ?>, ?.]
|
||||
|
||||
# Inline start
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, options) do
|
||||
handle_inline(rest, ?d, ["**"], [], options)
|
||||
# Clauses for handling spaces
|
||||
defp handle_inline(<<?*, ?*, ?\s, rest :: binary>>, nil, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [?\s, ?*, ?*|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<mark, rest::binary>>, options) when mark in @single do
|
||||
handle_inline(rest, mark, [<<mark>>], [], options)
|
||||
defp handle_inline(<<mark, ?\s, rest :: binary>>, nil, buffer, acc, options) when mark in @spaced do
|
||||
handle_inline(rest, nil, [?\s, mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
defp handle_inline(rest, options) do
|
||||
handle_inline(rest, nil, [], [], options)
|
||||
defp handle_inline(<<?\s, ?*, ?*, rest :: binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?*, ?*, ?\s|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Inline delimiters
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?`, ["`"], [Enum.reverse(buffer)|acc], options)
|
||||
defp handle_inline(<<?\s, mark, rest :: binary>>, limit, buffer, acc, options) when mark in @spaced do
|
||||
handle_inline(rest, limit, [mark, ?\s|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Clauses for handling escape
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, ?*, ?*, rest::binary>>, nil, buffer, acc, options)
|
||||
when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [?\\, Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?\\, ?\\, rest::binary>>, limit, buffer, acc, options) do
|
||||
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)
|
||||
when not(mark == limit and mark == ?`) do
|
||||
defp handle_inline(<<?\\, ?*, ?*, rest :: binary>>, limit, buffer, acc, options) do
|
||||
handle_inline(rest, limit, [?*, ?*|buffer], acc, options)
|
||||
end
|
||||
|
||||
# A escape is not valid inside `
|
||||
defp handle_inline(<<?\\, mark, rest :: binary>>, limit, buffer, acc, options)
|
||||
when mark in [?_, ?*, ?`] and not(mark == limit and mark == ?`) do
|
||||
handle_inline(rest, limit, [mark|buffer], acc, options)
|
||||
end
|
||||
|
||||
# Inline start
|
||||
defp handle_inline(<<?*, ?*, rest :: binary>>, nil, buffer, acc, options) when rest != "" do
|
||||
handle_inline(rest, ?d, ["**"], [Enum.reverse(buffer)|acc], options)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
# Inline end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?*, ?*, rest::binary>>, ?d, buffer, acc, options)
|
||||
when rest == "" do
|
||||
handle_inline(<<>>, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
defp handle_inline(<<?`, rest::binary>>, ?`, buffer, acc, options) do
|
||||
defp handle_inline(<<?*, ?*, rest :: binary>>, ?d, buffer, acc, options) do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
# Catch all
|
||||
defp handle_inline(<<mark, rest :: binary>>, mark, buffer, acc, options) when mark in @single do
|
||||
handle_inline(rest, nil, [], [inline_buffer(buffer, options)|acc], options)
|
||||
end
|
||||
|
||||
defp handle_inline(<<char, rest::binary>>, mark, buffer, acc, options) do
|
||||
defp handle_inline(<<char, rest :: binary>>, mark, buffer, acc, options) do
|
||||
handle_inline(rest, mark, [char|buffer], acc, options)
|
||||
end
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -30,6 +30,10 @@ defmodule IO.Stream do
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def empty(stream) do
|
||||
stream
|
||||
end
|
||||
|
||||
def into(%{device: device, raw: raw} = stream) do
|
||||
{:ok, into(stream, device, raw)}
|
||||
end
|
||||
|
||||
+521
-925
File diff suppressed because it is too large
Load Diff
@@ -12,6 +12,7 @@ defmodule Kernel.CLI do
|
||||
argv = for arg <- argv, do: IO.chardata_to_string(arg)
|
||||
|
||||
{config, argv} = parse_argv(argv)
|
||||
:elixir_code_server.cast({:paths, config.pa, config.pz})
|
||||
System.argv(argv)
|
||||
|
||||
run fn _ ->
|
||||
@@ -33,17 +34,10 @@ defmodule Kernel.CLI do
|
||||
by escripts generated by Elixir.
|
||||
"""
|
||||
def run(fun, halt \\ true) do
|
||||
{ok_or_shutdown, status} = exec_fun(fun, {:ok, 0})
|
||||
if ok_or_shutdown == :shutdown or halt do
|
||||
{_, status} = at_exit({ok_or_shutdown, status})
|
||||
|
||||
# Ensure Logger messages are flushed before halting
|
||||
case :erlang.whereis(Logger) do
|
||||
pid when is_pid(pid) -> Logger.flush()
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
System.halt(status)
|
||||
res = exec_fun(fun, {:ok, 0})
|
||||
if elem(res, 0) == :shutdown or halt do
|
||||
{_, int} = at_exit(res)
|
||||
System.halt(int)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -62,7 +56,7 @@ defmodule Kernel.CLI do
|
||||
## Helpers
|
||||
|
||||
defp at_exit(res) do
|
||||
hooks = :elixir_config.get_and_put(:at_exit, [])
|
||||
hooks = :elixir_code_server.call(:flush_at_exit)
|
||||
res = Enum.reduce(hooks, res, &exec_fun/2)
|
||||
if hooks == [], do: res, else: at_exit(res)
|
||||
end
|
||||
@@ -71,7 +65,7 @@ defmodule Kernel.CLI do
|
||||
parent = self()
|
||||
|
||||
{pid, ref} =
|
||||
spawn_monitor(fn ->
|
||||
spawn_monitor fn ->
|
||||
try do
|
||||
fun.(elem(res, 1))
|
||||
catch
|
||||
@@ -88,12 +82,12 @@ defmodule Kernel.CLI do
|
||||
stack = System.stacktrace
|
||||
print_error(kind, reason, stack)
|
||||
send parent, {self, {:shutdown, 1}}
|
||||
exit(to_exit(kind, reason, stack))
|
||||
:erlang.raise(kind, reason, stack)
|
||||
else
|
||||
_ ->
|
||||
send parent, {self, res}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
receive do
|
||||
{^pid, res} ->
|
||||
@@ -105,10 +99,6 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
end
|
||||
|
||||
defp to_exit(:throw, reason, stack), do: {{:nocatch, reason}, stack}
|
||||
defp to_exit(:error, reason, stack), do: {reason, stack}
|
||||
defp to_exit(:exit, reason, _stack), do: reason
|
||||
|
||||
defp shared_option?(list, config, callback) do
|
||||
case parse_shared(list, config) do
|
||||
{[h|hs], _} when h == hd(list) ->
|
||||
@@ -123,9 +113,8 @@ defmodule Kernel.CLI do
|
||||
IO.puts :stderr, Exception.format(kind, reason, prune_stacktrace(trace))
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def]
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_translator,
|
||||
:elixir_expand, :elixir_lexical]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
@@ -146,18 +135,7 @@ 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.version}"
|
||||
else
|
||||
IO.puts :erlang.system_info(:system_version)
|
||||
{:ok, v} = Version.parse(System.version)
|
||||
|
||||
case v.pre do
|
||||
[] -> IO.puts "Elixir #{System.version}"
|
||||
_ -> IO.puts "Elixir #{System.version} (#{System.build_info().revision})"
|
||||
end
|
||||
end
|
||||
|
||||
IO.puts "Elixir #{System.version}"
|
||||
System.halt 0
|
||||
end
|
||||
|
||||
@@ -404,10 +382,7 @@ defmodule Kernel.CLI do
|
||||
end
|
||||
|
||||
defp filter_patterns(pattern) do
|
||||
pattern
|
||||
|> Path.wildcard
|
||||
|> :lists.usort
|
||||
|> Enum.filter(&File.regular?/1)
|
||||
Enum.filter(Enum.uniq(Path.wildcard(pattern)), &File.regular?(&1))
|
||||
end
|
||||
|
||||
defp filter_multiple_patterns(patterns) do
|
||||
@@ -427,9 +402,9 @@ defmodule Kernel.CLI do
|
||||
&elem(&1, 1)
|
||||
|
||||
if missing_patterns == [] do
|
||||
{:ok, :lists.usort(Enum.concat(files))}
|
||||
{:ok, Enum.uniq(Enum.concat(files))}
|
||||
else
|
||||
{:missing, :lists.usort(missing_patterns)}
|
||||
{:missing, Enum.uniq(missing_patterns)}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ defmodule Kernel.ErrorHandler do
|
||||
end
|
||||
|
||||
def release() do
|
||||
# On release, no longer allow elixir_ensure_compiled
|
||||
# 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.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
# 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
|
||||
@@ -11,22 +11,17 @@ defmodule Kernel.LexicalTracker do
|
||||
@timeout 30_000
|
||||
@behaviour :gen_server
|
||||
|
||||
@import 2
|
||||
@alias 3
|
||||
|
||||
@doc """
|
||||
Returns all remotes linked to in this lexical scope.
|
||||
"""
|
||||
def remotes(arg) do
|
||||
:gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
|> :ets.match({{:mode, :'$1'}, :'$2'})
|
||||
|> partition([], [])
|
||||
ets = :gen_server.call(to_pid(arg), :ets, @timeout)
|
||||
:ets.match(ets, {:"$1", :_, :_}) |> List.flatten
|
||||
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
|
||||
compile to.
|
||||
@@ -66,8 +61,8 @@ defmodule Kernel.LexicalTracker do
|
||||
end
|
||||
|
||||
@doc false
|
||||
def remote_dispatch(pid, module, mode) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module, mode})
|
||||
def remote_dispatch(pid, module) do
|
||||
:gen_server.cast(pid, {:remote_dispatch, module})
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -82,24 +77,28 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
@doc false
|
||||
def collect_unused_imports(pid) do
|
||||
unused(pid, :import)
|
||||
unused(pid, @import)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def collect_unused_aliases(pid) do
|
||||
unused(pid, :alias)
|
||||
unused(pid, @alias)
|
||||
end
|
||||
|
||||
defp unused(pid, tag) do
|
||||
:gen_server.call(pid, :ets, @timeout)
|
||||
|> :ets.select([{{{tag, :"$1"}, :"$2"}, [is_integer: :"$2"], [{{:"$1", :"$2"}}]}])
|
||||
|> Enum.sort
|
||||
defp unused(pid, pos) do
|
||||
ets = :gen_server.call(pid, :ets, @timeout)
|
||||
:ets.foldl(fn
|
||||
{module, _, _} = tuple, acc when is_integer(:erlang.element(pos, tuple)) ->
|
||||
[{module, :erlang.element(pos, tuple)}|acc]
|
||||
_, acc ->
|
||||
acc
|
||||
end, [], ets) |> Enum.sort
|
||||
end
|
||||
|
||||
# Callbacks
|
||||
|
||||
def init(dest) do
|
||||
{:ok, {:ets.new(__MODULE__, [:protected]), dest}}
|
||||
{:ok, {:ets.new(:lexical, [:protected]), dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -111,31 +110,32 @@ defmodule Kernel.LexicalTracker do
|
||||
{:reply, dest, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module, mode}, {d, dest}) do
|
||||
add_compile(d, module, mode)
|
||||
def handle_call(request, _from, {d, dest}) do
|
||||
{:stop, {:bad_call, request}, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:remote_dispatch, module}, {d, dest}) do
|
||||
add_module(d, module)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
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)
|
||||
add_dispatch(d, module, @import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:alias_dispatch, module}, {d, dest}) do
|
||||
add_dispatch(d, module, :alias)
|
||||
add_dispatch(d, module, @alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_import, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :import)
|
||||
add_directive(d, module, line, warn, @import)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast({:add_alias, module, line, warn}, {d, dest}) do
|
||||
add_directive(d, module, line, warn, :alias)
|
||||
add_directive(d, module, line, warn, @alias)
|
||||
{:noreply, {d, dest}}
|
||||
end
|
||||
|
||||
@@ -143,6 +143,10 @@ defmodule Kernel.LexicalTracker do
|
||||
{:stop, :normal, {d, dest}}
|
||||
end
|
||||
|
||||
def handle_cast(msg, {d, dest}) do
|
||||
{:stop, {:bad_cast, msg}, {d, dest}}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, {d, dest}) do
|
||||
{:noreply, {d, dest}}
|
||||
@@ -162,17 +166,19 @@ defmodule Kernel.LexicalTracker do
|
||||
|
||||
# In the table we keep imports and aliases.
|
||||
# If the value is false, it was not imported/aliased
|
||||
# If the value is true, it was 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_dispatch(d, module, tag) do
|
||||
:ets.insert(d, {{tag, module}, true})
|
||||
defp add_module(d, module) do
|
||||
:ets.insert_new(d, {module, false, false})
|
||||
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_dispatch(d, module, pos) do
|
||||
:ets.update_element(d, module, {pos, true})
|
||||
end
|
||||
|
||||
defp add_directive(d, module, line, warn, tag) do
|
||||
defp add_directive(d, module, line, warn, pos) do
|
||||
add_module(d, module)
|
||||
marker = if warn, do: line, else: true
|
||||
:ets.insert(d, {{tag, module}, marker})
|
||||
:ets.update_element(d, module, {pos, marker})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,7 +25,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
* `:dest` - the destination directory for the beam files. When using `files/2`,
|
||||
this information is only used to properly annotate the beam files before
|
||||
they are loaded into memory. If you want a file to actually be written to
|
||||
they are loaded into memory. If you want a file to actually be writen to
|
||||
`dest`, use `files_to_path/3` instead.
|
||||
|
||||
Returns the modules generated by each compiled file.
|
||||
@@ -55,13 +55,10 @@ defmodule Kernel.ParallelCompiler do
|
||||
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 ->
|
||||
result
|
||||
:error ->
|
||||
IO.puts :stderr, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
:ok -> result
|
||||
:error -> exit({:shutdown, 1})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -99,7 +96,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
else
|
||||
:elixir_compiler.file(h, Keyword.get(options, :dest))
|
||||
end
|
||||
{:shutdown, h}
|
||||
{:compiled, h}
|
||||
catch
|
||||
kind, reason ->
|
||||
{:failure, kind, reason, System.stacktrace}
|
||||
@@ -169,7 +166,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
|
||||
spawn_compilers(entries, original, output, options, waiting, queued, schedulers, result)
|
||||
|
||||
{:DOWN, _down_ref, :process, down_pid, {:shutdown, file}} ->
|
||||
{:DOWN, _down_ref, :process, down_pid, {:compiled, file}} ->
|
||||
if callback = Keyword.get(options, :each_file) do
|
||||
callback.(file)
|
||||
end
|
||||
@@ -190,15 +187,9 @@ defmodule Kernel.ParallelCompiler do
|
||||
defp handle_failure(ref, reason, entries, waiting, queued) do
|
||||
if file = find_failure(ref, queued) do
|
||||
print_failure(file, reason)
|
||||
|
||||
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
|
||||
@@ -210,7 +201,7 @@ defmodule Kernel.ParallelCompiler do
|
||||
end
|
||||
end
|
||||
|
||||
defp print_failure(_file, {:shutdown, _}) do
|
||||
defp print_failure(_file, {:compiled, _}) do
|
||||
:ok
|
||||
end
|
||||
|
||||
@@ -224,9 +215,8 @@ defmodule Kernel.ParallelCompiler do
|
||||
IO.puts Exception.format(:exit, reason, [])
|
||||
end
|
||||
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_clauses,
|
||||
:elixir_translator, :elixir_expand, :elixir_lexical, :elixir_exp_clauses,
|
||||
:elixir_def]
|
||||
@elixir_internals [:elixir, :elixir_exp, :elixir_compiler, :elixir_module, :elixir_translator,
|
||||
:elixir_expand, :elixir_lexical]
|
||||
|
||||
defp prune_stacktrace([{mod, _, _, _}|t]) when mod in @elixir_internals do
|
||||
prune_stacktrace(t)
|
||||
|
||||
@@ -3,6 +3,8 @@ 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.
|
||||
|
||||
@@ -11,21 +13,9 @@ defmodule Kernel.ParallelRequire do
|
||||
|
||||
Returns the modules generated by each required file.
|
||||
"""
|
||||
def files(files, callback \\ fn x -> x end) 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, [], callback, 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, "Compilation failed due to warnings while using the --warnings-as-errors option"
|
||||
exit({:shutdown, 1})
|
||||
end
|
||||
spawn_requires(files, [], callback, schedulers, [])
|
||||
end
|
||||
|
||||
defp spawn_requires([], [], _callback, _schedulers, result), do: result
|
||||
@@ -39,9 +29,22 @@ defmodule Kernel.ParallelRequire do
|
||||
end
|
||||
|
||||
defp spawn_requires([h|t], waiting, callback, schedulers, result) do
|
||||
parent = self()
|
||||
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)
|
||||
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(h) || []
|
||||
@@ -72,14 +75,6 @@ defmodule Kernel.ParallelRequire do
|
||||
end
|
||||
end
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:module_available, child, ref, _, _, _} ->
|
||||
send(child, {ref, :ack})
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:struct_available, _} ->
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
{:waiting, :struct, child, ref, _} ->
|
||||
send(child, {ref, :release})
|
||||
spawn_requires(files, waiting, callback, schedulers, result)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
defmodule Kernel.SpecialForms do
|
||||
@moduledoc """
|
||||
Special forms are the basic building blocks of Elixir, and therefore
|
||||
they cannot be overridden by the developer.
|
||||
In this module we define Elixir special forms. Special forms
|
||||
cannot be overridden by the developer and are the basic
|
||||
building blocks of Elixir code.
|
||||
|
||||
We define them in this module. Some of these forms are lexical (like
|
||||
`alias`, `case`, etc). The macros `{}` and `<<>>` are also special
|
||||
forms used to define tuple and binary data structures respectively.
|
||||
Some of those forms are lexical (like `alias`, `case`, etc).
|
||||
The macros `{}` and `<<>>` are also special forms used to define
|
||||
tuple and binary data structures respectively.
|
||||
|
||||
This module also documents Elixir's pseudo variables (`__ENV__`,
|
||||
`__MODULE__`, `__DIR__` and `__CALLER__`). Pseudo variables return
|
||||
@@ -36,7 +37,7 @@ defmodule Kernel.SpecialForms do
|
||||
{1, 2, 3}
|
||||
|
||||
iex> quote do: {1, 2, 3}
|
||||
{:{}, [], [1, 2, 3]}
|
||||
{:{}, [], [1,2,3]}
|
||||
|
||||
"""
|
||||
defmacro unquote(:{})(args)
|
||||
@@ -87,7 +88,7 @@ defmodule Kernel.SpecialForms do
|
||||
%{:a => :c}
|
||||
|
||||
Notice the update syntax requires the given keys to exist.
|
||||
Trying to update a key that does not exist will raise an `KeyError`.
|
||||
Trying to update a key that does not exist will raise an `ArgumentError`.
|
||||
|
||||
## AST representation
|
||||
|
||||
@@ -95,8 +96,8 @@ defmodule Kernel.SpecialForms do
|
||||
always represented internally as a list of two-items tuples
|
||||
for simplicity:
|
||||
|
||||
iex> quote do: %{"a" => :b, c: :d}
|
||||
{:%{}, [], [{"a", :b}, {:c, :d}]}
|
||||
iex> quote do: %{:a => :b, c: :d}
|
||||
{:%{}, [], [{:a, :b}, {:c, :d}]}
|
||||
|
||||
"""
|
||||
defmacro unquote(:%{})(args)
|
||||
@@ -168,36 +169,31 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> <<1, 2, 3>>
|
||||
<<1, 2, 3>>
|
||||
iex> << 1, 2, 3 >>
|
||||
<< 1, 2, 3 >>
|
||||
|
||||
## Types
|
||||
## Bitstring types
|
||||
|
||||
A bitstring is made of many segments and each segment has a
|
||||
type. There are 9 types used in bitstrings:
|
||||
|
||||
- `integer`
|
||||
- `float`
|
||||
- `bits` (alias for bitstring)
|
||||
- `bitstring`
|
||||
- `binary`
|
||||
- `bytes` (alias for binary)
|
||||
- `utf8`
|
||||
- `utf16`
|
||||
- `utf32`
|
||||
|
||||
When no type is specified, the default is `integer`:
|
||||
A bitstring is made of many segments. Each segment has a
|
||||
type, which defaults to integer:
|
||||
|
||||
iex> <<1, 2, 3>>
|
||||
<<1, 2, 3>>
|
||||
|
||||
Elixir also accepts by default the segment to be a literal
|
||||
string or a literal char list, which are by default expanded to integers:
|
||||
string or a literal char list, which are by expanded to integers:
|
||||
|
||||
iex> <<0, "foo">>
|
||||
<<0, 102, 111, 111>>
|
||||
|
||||
Variables or any other type need to be explicitly tagged:
|
||||
Any other type needs to be explicitly tagged. For example,
|
||||
in order to store a float type in the binary, one has to do:
|
||||
|
||||
iex> <<3.14 :: float>>
|
||||
<<64, 9, 30, 184, 81, 235, 133, 31>>
|
||||
|
||||
This also means that variables need to be explicitly tagged,
|
||||
otherwise Elixir defaults to integer:
|
||||
|
||||
iex> rest = "oo"
|
||||
iex> <<102, rest>>
|
||||
@@ -205,160 +201,103 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
We can solve this by explicitly tagging it as a binary:
|
||||
|
||||
iex> rest = "oo"
|
||||
iex> <<102, rest::binary>>
|
||||
"foo"
|
||||
<<102, rest :: binary>>
|
||||
|
||||
The type can be integer, float, bitstring/bits, binary/bytes,
|
||||
utf8, utf16 or utf32, e.g.:
|
||||
|
||||
<<102 :: float, rest :: binary>>
|
||||
|
||||
An integer can be any arbitrary precision integer. A float is an
|
||||
IEEE 754 binary32 or binary64 floating point number. A bitstring
|
||||
is an arbitrary series of bits. A binary is a special case of
|
||||
bitstring that has a total size divisible by 8.
|
||||
|
||||
The utf8, utf16, and utf32 types are for unicode codepoints. They
|
||||
can also be applied to literal strings and char lists:
|
||||
|
||||
iex> <<"foo"::utf16>>
|
||||
<<0, 102, 0, 111, 0, 111>>
|
||||
iex> <<"foo"::utf32>>
|
||||
<<0, 0, 0, 102, 0, 0, 0, 111, 0, 0, 0, 111>>
|
||||
iex> <<"foo" :: utf16>>
|
||||
<<0,102,0,111,0,111>>
|
||||
|
||||
## Options
|
||||
The bits type is an alias for bitstring. The bytes type is an
|
||||
alias for binary.
|
||||
|
||||
Many options can be given by using `-` as separator. Order is
|
||||
arbitrary, so the following are all equivalent:
|
||||
The signedness can also be given as signed or unsigned. The
|
||||
signedness only matters for matching and relevant only for
|
||||
integers. If unspecified, it defaults to unsigned. Example:
|
||||
|
||||
<<102::integer-native, rest::binary>>
|
||||
<<102::native-integer, rest::binary>>
|
||||
<<102::unsigned-big-integer, rest::binary>>
|
||||
<<102::unsigned-big-integer-size(8), rest::binary>>
|
||||
<<102::unsigned-big-integer-8, rest::binary>>
|
||||
<<102::8-integer-big-unsigned, rest::binary>>
|
||||
<<102, rest::binary>>
|
||||
iex> <<-100 :: signed, _rest :: binary>> = <<-100, "foo">>
|
||||
<<156,102,111,111>>
|
||||
|
||||
### Unit and Size
|
||||
This match would have failed if we did not specify that the
|
||||
value -100 is signed. If we're matching into a variable instead
|
||||
of a value, the signedness won't be checked; rather, the number
|
||||
will simply be interpreted as having the given (or implied)
|
||||
signedness, e.g.:
|
||||
|
||||
The length of the match is equal to the `unit` (a number of bits) times the
|
||||
`size` (the number of repeated segnments of length `unit`).
|
||||
iex> <<val, _rest :: binary>> = <<-100, "foo">>
|
||||
iex> val
|
||||
156
|
||||
|
||||
Type | Default Unit
|
||||
--------- | ------------
|
||||
`integer` | 1 bit
|
||||
`float` | 1 bit
|
||||
`binary` | 8 bits
|
||||
Here, `val` is interpreted as unsigned.
|
||||
|
||||
Sizes for types are a bit more nuanced. The default size for integers is 8.
|
||||
The endianness of a segment can be big, little or native (the
|
||||
latter meaning it will be resolved at VM load time). Many options
|
||||
can be given by using `-` as separator:
|
||||
|
||||
For floats, it is 64. For floats, `size * unit` must result in 32 or 64,
|
||||
corresponding to [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point)
|
||||
binary32 and binary64, respectively.
|
||||
<<102 :: integer-native, rest :: binary>>
|
||||
|
||||
For binaries, the default is the size of the binary. Only the last binary in a
|
||||
match can use the default size. All others must have their size specified
|
||||
explicitly, even if the match is unambiguous. For example:
|
||||
Or:
|
||||
|
||||
iex> <<name::binary-size(5), " the ", species::binary>> = <<"Frank the Walrus">>
|
||||
"Frank the Walrus"
|
||||
iex> {name, species}
|
||||
{"Frank", "Walrus"}
|
||||
<<102 :: unsigned-big-integer, rest :: binary>>
|
||||
|
||||
Failing to specify the size for the non-last causes compilation to fail:
|
||||
And so on.
|
||||
|
||||
<<name::binary, " the ", species::binary>> = <<"Frank the Walrus">>
|
||||
** (CompileError): a binary field without size is only allowed at the end of a binary pattern
|
||||
Endianness only makes sense for integers and some UTF code
|
||||
point types (utf16 and utf32).
|
||||
|
||||
#### Shortcut Syntax
|
||||
Finally, we can also specify size and unit for each segment. The
|
||||
unit is multiplied by the size to give the effective size of
|
||||
the segment in bits. The default unit for integers, floats,
|
||||
and bitstrings is 1. For binaries, it is 8.
|
||||
|
||||
Since integers are default, the default unit is 1. The example below
|
||||
matches because the string "foo" takes 24 bits and we match it
|
||||
against a segment of 24 bits, 8 of which are taken by the integer
|
||||
102 and the remaining 16 bits are specified on the rest.
|
||||
|
||||
iex> <<102, _rest :: size(16)>> = "foo"
|
||||
"foo"
|
||||
|
||||
We can also match by specifying size and unit explicitly:
|
||||
|
||||
iex> <<102, _rest :: size(2)-unit(8)>> = "foo"
|
||||
"foo"
|
||||
|
||||
However, if we expect a size of 32, it won't match:
|
||||
|
||||
iex> <<102, _rest :: size(32)>> = "foo"
|
||||
** (MatchError) no match of right hand side value: "foo"
|
||||
|
||||
Size and unit are not applicable to utf8, utf16, and utf32.
|
||||
|
||||
The default size for integers is 8. For floats, it is 64. For
|
||||
binaries, it is the size of the binary. Only the last binary
|
||||
in a binary match can use the default size (all others must
|
||||
have their size specified explicitly).
|
||||
|
||||
Size and unit can also be specified using a syntax shortcut
|
||||
when passing integer values:
|
||||
|
||||
iex> x = 1
|
||||
iex> <<x::8>> == <<x::size(8)>>
|
||||
true
|
||||
iex> <<x::8 * 4>> == <<x::size(8)-unit(4)>>
|
||||
true
|
||||
<< x :: 8 >> == << x :: size(8) >>
|
||||
<< x :: 8 * 4 >> == << x :: size(8)-unit(4) >>
|
||||
<< x :: _ * 4 >> == << x :: unit(4) >>
|
||||
|
||||
This syntax reflects the fact the effective size is given by
|
||||
multiplying the size by the unit.
|
||||
|
||||
### Modifiers
|
||||
|
||||
Some types have associated modifiers to clear up ambiguity in byte
|
||||
representation.
|
||||
|
||||
Modifier | Relevant Type(s)
|
||||
-------------------- | ----------------
|
||||
`signed` | `integer`
|
||||
`unsigned` (default) | `integer`
|
||||
`little` | `integer`, `utf16`, `utf32`
|
||||
`big` (default) | `integer`, `utf16`, `utf32`
|
||||
`native` | `integer`, `utf16`, `utf32`
|
||||
|
||||
### Sign
|
||||
|
||||
Integers can be `signed` or `unsigned`, defaulting to `unsigned`.
|
||||
|
||||
iex> <<int::integer>> = <<-100>>
|
||||
<<156>>
|
||||
iex> int
|
||||
156
|
||||
iex> <<int::integer-signed>> = <<-100>>
|
||||
<<156>>
|
||||
iex> int
|
||||
-100
|
||||
|
||||
`signed` and `unsigned` are only used for matching binaries (see below) and
|
||||
are only used for integers.
|
||||
|
||||
iex> <<-100::signed, _rest::binary>> = <<-100, "foo">>
|
||||
<<156, 102, 111, 111>>
|
||||
|
||||
### Endianness
|
||||
|
||||
Elixir has three options for endianness: `big`, `little`, and `native`.
|
||||
The default is `big`:
|
||||
|
||||
iex> <<number::little-integer-size(16)>> = <<0, 1>>
|
||||
<<0, 1>>
|
||||
iex> number
|
||||
256
|
||||
iex> <<number::big-integer-size(16)>> = <<0, 1>>
|
||||
<<0, 1>>
|
||||
iex> number
|
||||
1
|
||||
|
||||
`native` is determined by the VM at startup and will depend on the
|
||||
host operating system.
|
||||
|
||||
## Binary/Bitstring Matching
|
||||
|
||||
Binary matching is a powerful feature in Elixir that is useful for extracting
|
||||
information from binaries as well as pattern matching.
|
||||
|
||||
Binary matching can be used by itself to extract information from binaries:
|
||||
|
||||
iex> <<"Hello, ", place::binary>> = "Hello, World"
|
||||
"Hello, World"
|
||||
iex> place
|
||||
"World"
|
||||
|
||||
Or as a part of function definitions to pattern match:
|
||||
|
||||
defmodule ImageTyper
|
||||
@png_signature <<137::size(8), 80::size(8), 78::size(8), 71::size(8),
|
||||
13::size(8), 10::size(8), 26::size(8), 10::size(8)>>
|
||||
@jpg_signature <<255::size(8), 216::size(8)>>
|
||||
|
||||
def type(<<@png_signature, rest::binary>>), do: :png
|
||||
def type(<<@jpg_signature, rest::binary>>), do: :jpg
|
||||
def type(_), do :unknown
|
||||
end
|
||||
|
||||
### Performance & Optimizations
|
||||
|
||||
The Erlang compiler can provide a number of optimizations on binary creation
|
||||
and matching. To see optimization output, set the `bin_opt_info` compiler
|
||||
option:
|
||||
|
||||
ERL_COMPILER_OPTIONS=bin_opt_info mix compile
|
||||
|
||||
To learn more about specific optimizations and performance considerations,
|
||||
check out
|
||||
[Erlang's Efficiency Guide on handling binaries](http://www.erlang.org/doc/efficiency_guide/binaryhandling.html).
|
||||
For floats, `size * unit` must result in 32 or 64, corresponding
|
||||
to binary32 and binary64, respectively.
|
||||
"""
|
||||
defmacro unquote(:<<>>)(args)
|
||||
|
||||
@@ -390,13 +329,13 @@ defmodule Kernel.SpecialForms do
|
||||
iex> Kernel.Sample
|
||||
Kernel.Sample
|
||||
|
||||
iex> Kernel.length([1, 2, 3])
|
||||
iex> Kernel.length([1,2,3])
|
||||
3
|
||||
|
||||
iex> Kernel.+(1, 2)
|
||||
3
|
||||
|
||||
iex> Kernel."length"([1, 2, 3])
|
||||
iex> Kernel."length"([1,2,3])
|
||||
3
|
||||
|
||||
iex> Kernel.'+'(1, 2)
|
||||
@@ -507,7 +446,7 @@ defmodule Kernel.SpecialForms do
|
||||
was not explicitly defined.
|
||||
|
||||
Both warning behaviours could be changed by explicitly
|
||||
setting the `:warn` option to `true` or `false`.
|
||||
setting the `:warn` option to true or false.
|
||||
"""
|
||||
defmacro alias(module, opts)
|
||||
|
||||
@@ -540,7 +479,7 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro require(module, opts)
|
||||
|
||||
@doc """
|
||||
Imports functions and macros from other modules.
|
||||
Imports function and macros from other modules.
|
||||
|
||||
`import` allows one to easily access functions or macros from
|
||||
others modules without using the qualified name.
|
||||
@@ -553,7 +492,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
iex> import List
|
||||
iex> flatten([1, [2], 3])
|
||||
[1, 2, 3]
|
||||
[1,2,3]
|
||||
|
||||
## Selector
|
||||
|
||||
@@ -579,20 +518,12 @@ defmodule Kernel.SpecialForms do
|
||||
Notice that calling `except` for a previously declared `import`
|
||||
simply filters the previously imported elements. For example:
|
||||
|
||||
import List, only: [flatten: 1, keyfind: 4]
|
||||
import List, only: [flatten: 1, keyfind: 3]
|
||||
import List, except: [flatten: 1]
|
||||
|
||||
After the two import calls above, only `List.keyfind/4` will be
|
||||
After the two import calls above, only `List.keyfind/3` will be
|
||||
imported.
|
||||
|
||||
## Underscore functions
|
||||
|
||||
By default functions starting with `_` are not imported. If you really want
|
||||
to import a function starting with `_` you must explicitly include it in the
|
||||
`:only` selector.
|
||||
|
||||
import File.Stream, only: [__build__: 3]
|
||||
|
||||
## Lexical scope
|
||||
|
||||
It is important to notice that `import` is lexical. This means you
|
||||
@@ -600,10 +531,10 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
defmodule Math do
|
||||
def some_function do
|
||||
# 1) Disable "if/2" from Kernel
|
||||
# 1) Disable `if/2` from Kernel
|
||||
import Kernel, except: [if: 2]
|
||||
|
||||
# 2) Require the new "if" macro from MyMacros
|
||||
# 2) Require the new `if` macro from MyMacros
|
||||
import MyMacros
|
||||
|
||||
# 3) Use the new macro
|
||||
@@ -627,7 +558,7 @@ defmodule Kernel.SpecialForms do
|
||||
was not explicitly defined.
|
||||
|
||||
Both warning behaviours could be changed by explicitly
|
||||
setting the `:warn` option to `true` or `false`.
|
||||
setting the `:warn` option to true or false.
|
||||
|
||||
## Ambiguous function/macro names
|
||||
|
||||
@@ -671,27 +602,26 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro __CALLER__
|
||||
|
||||
@doc """
|
||||
Accesses an already bound variable in match clauses. Also known as the pin operator.
|
||||
Accesses an already bound variable in match clauses.
|
||||
|
||||
## Examples
|
||||
|
||||
Elixir allows variables to be rebound via static single assignment:
|
||||
|
||||
iex> x = 1
|
||||
iex> x = x + 1
|
||||
iex> x = 2
|
||||
iex> x
|
||||
2
|
||||
|
||||
However, in some situations, it is useful to match against an existing
|
||||
value, instead of rebinding. This can be done with the `^` special form,
|
||||
colloquially known as the pin operator:
|
||||
value, instead of rebinding. This can be done with the `^` special form:
|
||||
|
||||
iex> x = 1
|
||||
iex> ^x = List.first([1])
|
||||
iex> ^x = List.first([2])
|
||||
** (MatchError) no match of right hand side value: 2
|
||||
|
||||
Note that `^x` always refers to the value of `x` prior to the match. The
|
||||
Note that `^` always refers to the value of x prior to the match. The
|
||||
following example will match:
|
||||
|
||||
iex> x = 0
|
||||
@@ -702,32 +632,6 @@ defmodule Kernel.SpecialForms do
|
||||
"""
|
||||
defmacro ^(var)
|
||||
|
||||
@doc """
|
||||
Matches the value on the right against the pattern on the left.
|
||||
"""
|
||||
defmacro left = right
|
||||
|
||||
@doc """
|
||||
Used by types and bitstrings to specify types.
|
||||
|
||||
This operator is used in two distinct occasions in Elixir.
|
||||
It is used in typespecs to specify the type of a variable,
|
||||
function or of a type itself:
|
||||
|
||||
@type number :: integer | float
|
||||
@spec add(number, number) :: number
|
||||
|
||||
It may also be used in bit strings to specify the type
|
||||
of a given bit segment:
|
||||
|
||||
<<int::integer-little, rest::bits>> = bits
|
||||
|
||||
Read the documentation for `Kernel.Typespec` and
|
||||
`<<>>/1` for more information on typespecs and
|
||||
bitstrings respectively.
|
||||
"""
|
||||
defmacro left :: right
|
||||
|
||||
@doc ~S"""
|
||||
Gets the representation of any expression.
|
||||
|
||||
@@ -758,7 +662,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
## Options
|
||||
|
||||
* `:unquote` - when `false`, disables unquoting. Useful when you have a quote
|
||||
* `:unquote` - when false, disables unquoting. Useful when you have a quote
|
||||
inside another quote and want to control what quote is able to unquote.
|
||||
|
||||
* `:location` - when set to `:keep`, keeps the current line and file from
|
||||
@@ -820,7 +724,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
Returning 5
|
||||
Returning 5
|
||||
Got 25
|
||||
25
|
||||
|
||||
Notice how "Returning 5" was printed twice, instead of just once. This is
|
||||
because a macro receives an expression and not a value (which is what we
|
||||
@@ -880,7 +784,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
import Math
|
||||
squared(5)
|
||||
x #=> ** (CompileError) undefined variable x or undefined function x/0
|
||||
x #=> ** (RuntimeError) undefined function or variable: x
|
||||
|
||||
We can see that `x` did not leak to the user context. This happens
|
||||
because Elixir macros are hygienic, a topic we will discuss at length
|
||||
@@ -904,7 +808,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
In the example above, `a` returns 10 even if the macro
|
||||
is apparently setting it to 1 because variables defined
|
||||
in the macro do not affect the context the macro is executed in.
|
||||
in the macro does not affect the context the macro is executed in.
|
||||
If you want to set or get a variable in the caller's context, you
|
||||
can do it with the help of the `var!` macro:
|
||||
|
||||
@@ -939,7 +843,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
Hygiene.write
|
||||
Hygiene.read
|
||||
#=> ** (RuntimeError) undefined variable a or undefined function a/0
|
||||
#=> ** (RuntimeError) undefined function or variable: a
|
||||
|
||||
For such, you can explicitly pass the current module scope as
|
||||
argument:
|
||||
@@ -968,34 +872,34 @@ defmodule Kernel.SpecialForms do
|
||||
Consider the following example:
|
||||
|
||||
defmodule Hygiene do
|
||||
alias Map, as: M
|
||||
alias HashDict, as: D
|
||||
|
||||
defmacro no_interference do
|
||||
quote do: M.new
|
||||
quote do: D.new
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
Hygiene.no_interference #=> %{}
|
||||
Hygiene.no_interference #=> #HashDict<[]>
|
||||
|
||||
Notice that, even though the alias `M` is not available
|
||||
Notice that, even though the alias `D` is not available
|
||||
in the context the macro is expanded, the code above works
|
||||
because `M` still expands to `Map`.
|
||||
because `D` still expands to `HashDict`.
|
||||
|
||||
Similarly, even if we defined an alias with the same name
|
||||
before invoking a macro, it won't affect the macro's result:
|
||||
|
||||
defmodule Hygiene do
|
||||
alias Map, as: M
|
||||
alias HashDict, as: D
|
||||
|
||||
defmacro no_interference do
|
||||
quote do: M.new
|
||||
quote do: D.new
|
||||
end
|
||||
end
|
||||
|
||||
require Hygiene
|
||||
alias SomethingElse, as: M
|
||||
Hygiene.no_interference #=> %{}
|
||||
alias SomethingElse, as: D
|
||||
Hygiene.no_interference #=> #HashDict<[]>
|
||||
|
||||
In some cases, you want to access an alias or a module defined
|
||||
in the caller. For such, you can use the `alias!` macro:
|
||||
@@ -1032,54 +936,54 @@ defmodule Kernel.SpecialForms do
|
||||
following code:
|
||||
|
||||
defmodule Hygiene do
|
||||
defmacrop get_length do
|
||||
defmacrop get_size do
|
||||
quote do
|
||||
length([1,2,3])
|
||||
size("hello")
|
||||
end
|
||||
end
|
||||
|
||||
def return_length do
|
||||
import Kernel, except: [length: 1]
|
||||
get_length
|
||||
def return_size do
|
||||
import Kernel, except: [size: 1]
|
||||
get_size
|
||||
end
|
||||
end
|
||||
|
||||
Hygiene.return_length #=> 3
|
||||
Hygiene.return_size #=> 5
|
||||
|
||||
Notice how `return_length` returns 5 even though the `length/1`
|
||||
function is not imported. In fact, even if `return_length`
|
||||
imported a function with the same name and arity from another
|
||||
module, it wouldn't affect the function result:
|
||||
Notice how `return_size` returns 5 even though the `size/1`
|
||||
function is not imported. In fact, even if `return_size` imported
|
||||
a function from another module, it wouldn't affect the function
|
||||
result:
|
||||
|
||||
def return_length do
|
||||
import String, only: [length: 1]
|
||||
get_length
|
||||
def return_size do
|
||||
import Dict, only: [size: 1]
|
||||
get_size
|
||||
end
|
||||
|
||||
Calling this new `return_length` will still return 3 as result.
|
||||
Calling this new `return_size` will still return 5 as result.
|
||||
|
||||
Elixir is smart enough to delay the resolution to the latest
|
||||
moment possible. So, if you call `length([1, 2, 3])` inside quote,
|
||||
but no `length/1` function is available, it is then expanded in
|
||||
moment possible. So, if you call `size("hello")` inside quote,
|
||||
but no `size/1` function is available, it is then expanded in
|
||||
the caller:
|
||||
|
||||
defmodule Lazy do
|
||||
defmacrop get_length do
|
||||
import Kernel, except: [length: 1]
|
||||
defmacrop get_size do
|
||||
import Kernel, except: [size: 1]
|
||||
|
||||
quote do
|
||||
length("hello")
|
||||
size([a: 1, b: 2])
|
||||
end
|
||||
end
|
||||
|
||||
def return_length do
|
||||
import Kernel, except: [length: 1]
|
||||
import String, only: [length: 1]
|
||||
get_length
|
||||
def return_size do
|
||||
import Kernel, except: [size: 1]
|
||||
import Dict, only: [size: 1]
|
||||
get_size
|
||||
end
|
||||
end
|
||||
|
||||
Lazy.return_length #=> 5
|
||||
Lazy.return_size #=> 2
|
||||
|
||||
## Stacktrace information
|
||||
|
||||
@@ -1160,7 +1064,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
If you try to run our new macro, you will notice it won't
|
||||
even compile, complaining that the variables `k` and `v`
|
||||
do not exist. This is because of the ambiguity: `unquote(k)`
|
||||
does not exist. This is because of the ambiguity: `unquote(k)`
|
||||
can either be an unquote fragment, as previously, or a regular
|
||||
unquote as in `unquote(kv)`.
|
||||
|
||||
@@ -1251,7 +1155,7 @@ defmodule Kernel.SpecialForms do
|
||||
Note generators can also be used to filter as it removes any value
|
||||
that doesn't match the left side of `<-`:
|
||||
|
||||
iex> for {:user, name} <- [user: "john", admin: "james", user: "meg"] do
|
||||
iex> for {:user, name} <- [user: "john", admin: "john", user: "meg"] do
|
||||
...> String.upcase(name)
|
||||
...> end
|
||||
["JOHN", "MEG"]
|
||||
@@ -1261,7 +1165,7 @@ defmodule Kernel.SpecialForms do
|
||||
|
||||
iex> pixels = <<213, 45, 132, 64, 76, 32, 76, 0, 0, 234, 32, 15>>
|
||||
iex> for <<r::8, g::8, b::8 <- pixels >>, do: {r, g, b}
|
||||
[{213, 45, 132}, {64, 76, 32}, {76, 0, 0}, {234, 32, 15}]
|
||||
[{213,45,132},{64,76,32},{76,0,0},{234,32,15}]
|
||||
|
||||
Variable assignments inside the comprehension, be it in generators,
|
||||
filters or inside the block, are not reflected outside of the
|
||||
@@ -1290,42 +1194,6 @@ defmodule Kernel.SpecialForms do
|
||||
"""
|
||||
defmacro for(args)
|
||||
|
||||
@doc """
|
||||
Used to combine matching clauses.
|
||||
|
||||
Let's start with an example:
|
||||
|
||||
iex> opts = %{width: 10, height: 15}
|
||||
iex> with {:ok, width} <- Map.fetch(opts, :width),
|
||||
...> {:ok, height} <- Map.fetch(opts, :height),
|
||||
...> do: {:ok, width * height}
|
||||
{:ok, 150}
|
||||
|
||||
If all clauses match, the `do` block is executed, returning its result.
|
||||
Otherwise the chain is aborted and a non-matched value is returned:
|
||||
|
||||
iex> opts = %{width: 10}
|
||||
iex> with {:ok, width} <- Map.fetch(opts, :width),
|
||||
...> {:ok, height} <- Map.fetch(opts, :height),
|
||||
...> do: {:ok, width * height}
|
||||
:error
|
||||
|
||||
Similarly to `for`/1, variables bound inside `with/1` won't leak,
|
||||
and also it allows "bare expressions":
|
||||
|
||||
iex> width = nil
|
||||
iex> opts = %{width: 10, height: 15}
|
||||
iex> with {:ok, width} <- Map.fetch(opts, :width),
|
||||
...> double_width = width * 2,
|
||||
...> {:ok, height} <- Map.fetch(opts, :height),
|
||||
...> do: {:ok, double_width * height}
|
||||
{:ok, 300}
|
||||
iex> width
|
||||
nil
|
||||
|
||||
"""
|
||||
defmacro with(args)
|
||||
|
||||
@doc """
|
||||
Defines an anonymous function.
|
||||
|
||||
@@ -1451,9 +1319,8 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro __aliases__(args)
|
||||
|
||||
@doc """
|
||||
Calls the overriden function when overriding it with `Kernel.defoverridable/1`.
|
||||
|
||||
See `Kernel.defoverridable/1` for more information and documentation.
|
||||
Calls the overriden function when overriding it with `defoverridable`.
|
||||
See `Kernel.defoverridable` for more information and documentation.
|
||||
"""
|
||||
defmacro super(args)
|
||||
|
||||
@@ -1500,15 +1367,15 @@ defmodule Kernel.SpecialForms do
|
||||
In the example above, value is going to be `7` or `13` depending on
|
||||
the value of `lucky?`. In case `value` has no previous value before
|
||||
case, clauses that do not explicitly bind a value have the variable
|
||||
bound to `nil`.
|
||||
bound to nil.
|
||||
"""
|
||||
defmacro case(condition, clauses)
|
||||
|
||||
@doc """
|
||||
Evaluates the expression corresponding to the first clause that
|
||||
evaluates to a truthy value.
|
||||
evaluates to truth value.
|
||||
|
||||
Raises an error if all conditions evaluate to `nil` or `false`.
|
||||
Raises an error if all conditions evaluate to to nil or false.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -1525,7 +1392,7 @@ defmodule Kernel.SpecialForms do
|
||||
defmacro cond(clauses)
|
||||
|
||||
@doc ~S"""
|
||||
Evaluates the given expressions and handle any error, exit
|
||||
Evaluate the given expressions and handle any error, exit
|
||||
or throw that may have happened.
|
||||
|
||||
## Examples
|
||||
@@ -1711,7 +1578,7 @@ defmodule Kernel.SpecialForms do
|
||||
This means the VM no longer needs to keep the stacktrace once inside
|
||||
an else clause and so tail recursion is possible when using a `try`
|
||||
with a tail call as the final call inside an else clause. The same
|
||||
is true for `rescue` and `catch` clauses.
|
||||
is true for rescue and catch clauses.
|
||||
|
||||
## Variable handling
|
||||
|
||||
@@ -1727,7 +1594,7 @@ defmodule Kernel.SpecialForms do
|
||||
_, _ -> :failed
|
||||
end
|
||||
|
||||
x #=> unbound variable "x"
|
||||
x #=> unbound variable `x`
|
||||
|
||||
In the example above, `x` cannot be accessed since it was defined
|
||||
inside the `try` clause. A common practice to address this issue
|
||||
|
||||
+269
-194
@@ -1,5 +1,211 @@
|
||||
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` and `@callback` 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`. Parametrized 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 `{...}`.
|
||||
|
||||
### Predefined types
|
||||
|
||||
Type :: any # the top type, the set of all terms
|
||||
| none # the bottom type, contains no terms
|
||||
| pid
|
||||
| port
|
||||
| reference
|
||||
| Atom
|
||||
| Bitstring
|
||||
| float
|
||||
| Fun
|
||||
| Integer
|
||||
| List
|
||||
| Map
|
||||
| Tuple
|
||||
| Union
|
||||
| UserDefined # Described in section "Defining a type"
|
||||
|
||||
Atom :: atom
|
||||
| ElixirAtom # `:foo`, `:bar`, ...
|
||||
|
||||
Bitstring :: <<>>
|
||||
| << _ :: M >> # M is a positive integer
|
||||
| << _ :: _ * N >> # N is a positive integer
|
||||
| << _ :: M, _ :: _ * N >>
|
||||
|
||||
Fun :: (... -> any) # any function
|
||||
| (... -> Type) # any arity, returning Type
|
||||
| (() -> Type))
|
||||
| (TList -> Type)
|
||||
|
||||
Integer :: integer
|
||||
| ElixirInteger # ..., -1, 0, 1, ... 42 ...
|
||||
| ElixirInteger..ElixirInteger # an integer range
|
||||
|
||||
List :: list(Type) # proper list ([]-terminated)
|
||||
| improper_list(Type1, Type2) # Type1=contents, Type2=termination
|
||||
| maybe_improper_list(Type1, Type2) # Type1 and Type2 as above
|
||||
| nonempty_list(Type) # proper non-empty list
|
||||
| [] # empty list
|
||||
| [Type] # shorthand for list(Type)
|
||||
| [...] # shorthand for nonempty_list()
|
||||
| [Type, ...] # shorthand for nonempty_list(Type)
|
||||
| [Keyword]
|
||||
|
||||
Map :: map() # map of any size
|
||||
| %{} # map of any size
|
||||
| %Struct{} # struct (see defstruct/1)
|
||||
| %Struct{Keyword}
|
||||
| %{Keyword}
|
||||
| %{Pairs}
|
||||
|
||||
Tuple :: tuple # a tuple of any size
|
||||
| {} # empty tuple
|
||||
| {TList}
|
||||
| record(Atom) # record (see Record)
|
||||
| record(Atom, Keyword)
|
||||
|
||||
Keyword :: ElixirAtom: Type
|
||||
| ElixirAtom: Type, Keyword
|
||||
|
||||
Pairs :: Type => Type
|
||||
| Type => Type, Pairs
|
||||
|
||||
TList :: Type
|
||||
| Type, TList
|
||||
|
||||
Union :: Type | Type
|
||||
|
||||
### Bit strings
|
||||
|
||||
Bit string with a base size of 3:
|
||||
|
||||
<< _ :: 3 >>
|
||||
|
||||
Bit string with a unit size of 8:
|
||||
|
||||
<< _ :: _ * 8 >>
|
||||
|
||||
### Anonymous functions
|
||||
|
||||
Any anonymous function:
|
||||
|
||||
((...) -> any)
|
||||
(... -> any)
|
||||
|
||||
Anonymous function with arity of zero:
|
||||
|
||||
(() -> type)
|
||||
|
||||
Anonymous function with some arity:
|
||||
|
||||
((type, type) -> type)
|
||||
(type, type -> type)
|
||||
|
||||
## Built-in 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`
|
||||
`mfa` | `{atom, atom, arity}`
|
||||
`arity` | `0..255`
|
||||
`node` | `atom`
|
||||
`timeout` | `:infinity` \| `non_neg_integer`
|
||||
`no_return` | `none`
|
||||
`fun` | `(... -> any)`
|
||||
|
||||
|
||||
Some built-in types cannot be expressed with valid syntax according to the
|
||||
language defined above.
|
||||
|
||||
Built-in type | Can be interpreted as
|
||||
:---------------- | :--------------------
|
||||
`non_neg_integer` | `0..`
|
||||
`pos_integer` | `1..`
|
||||
`neg_integer` | `..-1`
|
||||
|
||||
Types defined in other modules are referred to as "remote types", they are
|
||||
referenced as `Module.type_name` (ex. `Enum.t` or `String.t`).
|
||||
|
||||
## 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 parametrised 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
|
||||
|
||||
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.
|
||||
@@ -76,22 +282,6 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@doc """
|
||||
Defines a macro callback.
|
||||
This macro is responsible for handling the attribute `@macrocallback`.
|
||||
|
||||
## Examples
|
||||
|
||||
@macrocallback add(number, number) :: Macro.t
|
||||
|
||||
"""
|
||||
defmacro defmacrocallback(spec) do
|
||||
quote do
|
||||
Kernel.Typespec.defspec(:macrocallback, unquote(Macro.escape(spec, unquote: true)), __ENV__)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Defines a `type`, `typep` or `opaque` by receiving a typespec expression.
|
||||
"""
|
||||
@@ -103,7 +293,7 @@ defmodule Kernel.Typespec do
|
||||
Defines a `spec` by receiving a typespec expression.
|
||||
"""
|
||||
def define_spec(kind, expr, env) do
|
||||
defspec(kind, expr, env)
|
||||
Module.store_typespec(env.module, kind, {kind, expr, env})
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -112,9 +302,10 @@ defmodule Kernel.Typespec do
|
||||
for modules being compiled.
|
||||
"""
|
||||
def defines_type?(module, name, arity) do
|
||||
finder = fn {_kind, expr, _caller} ->
|
||||
finder = fn {_kind, expr, _doc, _caller} ->
|
||||
type_to_signature(expr) == {name, arity}
|
||||
end
|
||||
|
||||
:lists.any(finder, Module.get_attribute(module, :type)) or
|
||||
:lists.any(finder, Module.get_attribute(module, :opaque))
|
||||
end
|
||||
@@ -144,7 +335,6 @@ defmodule Kernel.Typespec do
|
||||
@doc """
|
||||
Converts a spec clause back to Elixir AST.
|
||||
"""
|
||||
def spec_to_ast(name, spec)
|
||||
def spec_to_ast(name, {:type, line, :fun, [{:type, _, :product, args}, result]}) do
|
||||
meta = [line: line]
|
||||
body = {name, meta, Enum.map(args, &typespec_to_ast/1)}
|
||||
@@ -192,7 +382,6 @@ defmodule Kernel.Typespec do
|
||||
@doc """
|
||||
Converts a type clause back to Elixir AST.
|
||||
"""
|
||||
def type_to_ast(type)
|
||||
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)
|
||||
@@ -205,12 +394,23 @@ 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
|
||||
@doc """
|
||||
Returns all type docs available from the module's beam code.
|
||||
|
||||
The result is returned as a list of tuples where the first element is the pair of type
|
||||
name and arity and the second element is the documentation.
|
||||
|
||||
The module must have a corresponding beam file which can be
|
||||
located by the runtime system.
|
||||
"""
|
||||
@spec beam_typedocs(module | binary) :: [tuple] | nil
|
||||
def beam_typedocs(module) when is_atom(module) or is_binary(module) do
|
||||
if docs = Code.get_docs(module, :type_docs) do
|
||||
for {tuple, _, _, doc} <- docs, do: {tuple, doc}
|
||||
case abstract_code(module) do
|
||||
{:ok, abstract_code} ->
|
||||
type_docs = for {:attribute, _, :typedoc, tup} <- abstract_code, do: tup
|
||||
:lists.flatten(type_docs)
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@@ -312,66 +512,27 @@ defmodule Kernel.Typespec do
|
||||
do: {name, 0}
|
||||
def type_to_signature({:::, _, [{name, _, args}, _]}) when is_atom(name),
|
||||
do: {name, length(args)}
|
||||
def type_to_signature(_),
|
||||
do: :error
|
||||
|
||||
## Macro callbacks
|
||||
|
||||
@doc false
|
||||
def defspec(kind, expr, caller) when kind in [:callback, :macrocallback] do
|
||||
case spec_to_signature(expr) do
|
||||
{name, arity} ->
|
||||
store_callbackdoc(caller, caller.module, kind, name, arity)
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
Module.store_typespec(caller.module, kind, {kind, expr, caller})
|
||||
end
|
||||
|
||||
@doc false
|
||||
def defspec(kind, expr, caller) do
|
||||
Module.store_typespec(caller.module, kind, {kind, expr, caller})
|
||||
end
|
||||
|
||||
defp store_callbackdoc(caller, module, kind, name, arity) do
|
||||
table = :elixir_module.data_table(module)
|
||||
{line, doc} = get_doc_info(table, :doc, caller)
|
||||
:ets.insert(table, {{:callbackdoc, {name, arity}}, line, kind, doc})
|
||||
end
|
||||
|
||||
defp get_doc_info(table, attr, caller) do
|
||||
case :ets.take(table, attr) do
|
||||
[{^attr, {line, doc}}] -> {line, doc}
|
||||
[] -> {caller.line, nil}
|
||||
end
|
||||
end
|
||||
|
||||
@doc false
|
||||
def deftype(kind, expr, caller) do
|
||||
module = caller.module
|
||||
case type_to_signature(expr) do
|
||||
{name, arity} -> store_typedoc(caller, caller.module, kind, name, arity)
|
||||
:error -> :error
|
||||
end
|
||||
Module.store_typespec(module, kind, {kind, expr, caller})
|
||||
end
|
||||
doc = Module.get_attribute(module, :typedoc)
|
||||
|
||||
defp store_typedoc(caller, module, kind, name, arity) do
|
||||
table = :elixir_module.data_table(module)
|
||||
{line, doc} = get_doc_info(table, :typedoc, caller)
|
||||
|
||||
if kind == :typep && doc do
|
||||
:elixir_errors.warn(caller.line, caller.file, "type #{name}/#{arity} is private, " <>
|
||||
"@typedoc's are always discarded for private types")
|
||||
end
|
||||
|
||||
:ets.insert(table, {{:typedoc, {name, arity}}, line, kind, doc})
|
||||
Module.delete_attribute(module, :typedoc)
|
||||
Module.store_typespec(module, kind, {kind, expr, doc, caller})
|
||||
end
|
||||
|
||||
## Translation from Elixir AST to typespec AST
|
||||
|
||||
@doc false
|
||||
def translate_type(kind, {:::, _, [{name, _, args}, definition]}, caller) when is_atom(name) and name != ::: do
|
||||
def translate_type(kind, {:::, _, [{name, _, args}, definition]}, doc, caller) when is_atom(name) and name != ::: do
|
||||
args =
|
||||
if is_atom(args) do
|
||||
[]
|
||||
@@ -379,11 +540,12 @@ defmodule Kernel.Typespec do
|
||||
for(arg <- args, do: variable(arg))
|
||||
end
|
||||
|
||||
vars = for {:var, _, var} <- args, do: var
|
||||
spec = typespec(definition, vars, caller)
|
||||
vars = for {:var, _, _} = var <- args, do: var
|
||||
type = {name, spec, vars}
|
||||
arity = length(vars)
|
||||
vars = for {:var, _, var} <- args, do: var
|
||||
spec = typespec(definition, vars, caller)
|
||||
|
||||
vars = for {:var, _, _} = var <- args, do: var
|
||||
type = {name, spec, vars}
|
||||
arity = length(vars)
|
||||
|
||||
{kind, export} =
|
||||
case kind do
|
||||
@@ -392,24 +554,19 @@ defmodule Kernel.Typespec do
|
||||
:opaque -> {:opaque, true}
|
||||
end
|
||||
|
||||
if elixir_builtin_type?(name, arity) do
|
||||
:elixir_errors.handle_file_error(caller.file,
|
||||
{caller.line, :erl_lint, {:builtin_type, {name, arity}}})
|
||||
if not export and doc do
|
||||
:elixir_errors.warn(caller.line, caller.file, "type #{name}/#{arity} is private, " <>
|
||||
"@typedoc's are always discarded for private types")
|
||||
end
|
||||
|
||||
{{kind, {name, arity}, type}, caller.line, export}
|
||||
{{kind, {name, arity}, type}, caller.line, export, doc}
|
||||
end
|
||||
|
||||
def translate_type(_kind, other, caller) do
|
||||
def translate_type(_kind, other, _doc, caller) do
|
||||
type_spec = Macro.to_string(other)
|
||||
compile_error caller, "invalid type specification: #{type_spec}"
|
||||
end
|
||||
|
||||
defp elixir_builtin_type?(:as_boolean, 1), do: true
|
||||
defp elixir_builtin_type?(:struct, 0), do: true
|
||||
defp elixir_builtin_type?(:char_list, 0), do: true
|
||||
defp elixir_builtin_type?(_, _), do: false
|
||||
|
||||
@doc false
|
||||
def translate_spec(kind, {:when, _meta, [spec, guard]}, caller) do
|
||||
translate_spec(kind, spec, guard, caller)
|
||||
@@ -422,17 +579,9 @@ defmodule Kernel.Typespec do
|
||||
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}"
|
||||
compile_error caller, "expected keywords as guard in function type specification, got: #{guard}"
|
||||
end
|
||||
|
||||
vars = Keyword.keys(guard)
|
||||
@@ -447,34 +596,11 @@ defmodule Kernel.Typespec do
|
||||
{{kind, {name, arity}, spec}, caller.line}
|
||||
end
|
||||
|
||||
defp translate_spec(_kind, {name, _meta, _args} = spec, _guard, caller) when is_atom(name) and name != ::: do
|
||||
spec = Macro.to_string(spec)
|
||||
compile_error caller, "type specification missing return type: #{spec}"
|
||||
end
|
||||
|
||||
defp translate_spec(_kind, spec, _guard, caller) do
|
||||
spec = Macro.to_string(spec)
|
||||
compile_error caller, "invalid type specification: #{spec}"
|
||||
compile_error caller, "invalid function type specification: #{spec}"
|
||||
end
|
||||
|
||||
defp ensure_no_defaults!(args) do
|
||||
:lists.foreach fn
|
||||
{:::, _, [left, right]} ->
|
||||
ensure_not_default(left)
|
||||
ensure_not_default(right)
|
||||
left
|
||||
other ->
|
||||
ensure_not_default(other)
|
||||
other
|
||||
end, args
|
||||
end
|
||||
|
||||
defp ensure_not_default({:\\, _, [_, _]}) do
|
||||
raise ArgumentError, "default arguments \\\\ not supported in type spec"
|
||||
end
|
||||
|
||||
defp ensure_not_default(_), do: :ok
|
||||
|
||||
defp guard_to_constraints(guard, vars, meta, caller) do
|
||||
line = line(meta)
|
||||
|
||||
@@ -518,10 +644,6 @@ defmodule Kernel.Typespec do
|
||||
[]
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:user_type, line, name, args}) do
|
||||
typespec_to_ast({:type, line, name, args})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:type, line, :tuple, :any}) do
|
||||
{:tuple, [line: line], []}
|
||||
end
|
||||
@@ -554,18 +676,9 @@ defmodule Kernel.Typespec do
|
||||
[typespec_to_ast(arg), {:..., [line: line], nil}]
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:type, line, :map, :any}) do
|
||||
{:map, [line: line], []}
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:type, line, :map, fields}) do
|
||||
fields = Enum.map fields, fn
|
||||
# OTP 18
|
||||
{:type, _, :map_field_assoc, [k, 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)}
|
||||
fields = Enum.map fields, fn {:type, _, :map_field_assoc, k, v} ->
|
||||
{typespec_to_ast(k), typespec_to_ast(v)}
|
||||
end
|
||||
|
||||
{struct, fields} = Keyword.pop(fields, :__struct__)
|
||||
@@ -634,10 +747,6 @@ defmodule Kernel.Typespec do
|
||||
typespec_to_ast({:type, line, :char_list, []})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :struct}, []]}) do
|
||||
typespec_to_ast({:type, line, :struct, []})
|
||||
end
|
||||
|
||||
defp typespec_to_ast({:remote_type, line, [{:atom, _, :elixir}, {:atom, _, :as_boolean}, [arg]]}) do
|
||||
typespec_to_ast({:type, line, :as_boolean, [arg]})
|
||||
end
|
||||
@@ -674,9 +783,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
|
||||
@@ -702,38 +811,19 @@ defmodule Kernel.Typespec do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta), 0}, {:integer, line(meta), 0}]}
|
||||
end
|
||||
|
||||
defp typespec({:<<>>, meta, [{:::, unit_meta, [{:_, _, ctx1}, {:*, _, [{:_, _, ctx2}, unit]}]}]}, _, _)
|
||||
when is_atom(ctx1) and is_atom(ctx2) and is_integer(unit) do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta), 0}, {:integer, line(unit_meta), unit}]}
|
||||
defp typespec({:<<>>, meta, [{:::, _, [{:_, meta1, atom}, {:*, _, [{:_, meta2, atom}, unit]}]}]}, _, _) when is_atom(atom) do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta1), 0}, {:integer, line(meta2), 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}]}
|
||||
defp typespec({:<<>>, meta, [{:::, meta1, [{:_, meta2, atom}, base]}]}, _, _) when is_atom(atom) do
|
||||
{:type, line(meta), :binary, [{:integer, line(meta1), base}, {:integer, line(meta2), 0}]}
|
||||
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
|
||||
fields =
|
||||
: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)
|
||||
|
||||
defp typespec({:%{}, meta, fields}, vars, caller) do
|
||||
fields = :lists.map(fn {k, v} ->
|
||||
{:type, line(meta), :map_field_assoc, typespec(k, vars, caller), typespec(v, vars, caller)}
|
||||
end, fields)
|
||||
{:type, line(meta), :map, fields}
|
||||
end
|
||||
|
||||
@@ -748,16 +838,11 @@ 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
|
||||
@@ -765,7 +850,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
|
||||
@@ -776,9 +862,9 @@ defmodule Kernel.Typespec do
|
||||
defp typespec({:record, meta, [atom, fields]}, vars, caller) do
|
||||
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, _} ->
|
||||
@@ -787,6 +873,9 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
end, fields)
|
||||
|
||||
fields = Keyword.merge(keyword, fields)
|
||||
types = Keyword.values(fields)
|
||||
|
||||
typespec({:{}, meta, [atom|types]}, vars, caller)
|
||||
_ ->
|
||||
compile_error(caller, "unknown record #{inspect atom}")
|
||||
@@ -795,8 +884,7 @@ defmodule Kernel.Typespec do
|
||||
|
||||
# Handle ranges
|
||||
defp typespec({:.., meta, args}, vars, caller) do
|
||||
args = for arg <- args, do: typespec(arg, vars, caller)
|
||||
{:type, line(meta), :range, args}
|
||||
typespec({:range, meta, args}, vars, caller)
|
||||
end
|
||||
|
||||
# Handle special forms
|
||||
@@ -805,9 +893,7 @@ defmodule Kernel.Typespec do
|
||||
end
|
||||
|
||||
defp typespec({:__aliases__, _, _} = alias, vars, caller) do
|
||||
# We set a function name to avoid tracking
|
||||
# aliases in typespecs as compile time dependencies.
|
||||
atom = Macro.expand(alias, %{caller | function: {:typespec, 0}})
|
||||
atom = Macro.expand alias, caller
|
||||
typespec(atom, vars, caller)
|
||||
end
|
||||
|
||||
@@ -878,24 +964,13 @@ defmodule Kernel.Typespec do
|
||||
typespec((quote do: :elixir.char_list()), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:struct, _meta, []}, vars, caller) do
|
||||
typespec((quote do: :elixir.struct()), vars, caller)
|
||||
end
|
||||
|
||||
defp typespec({:as_boolean, _meta, [arg]}, vars, caller) do
|
||||
typespec((quote do: :elixir.as_boolean(unquote(arg))), 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}
|
||||
end
|
||||
|
||||
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}
|
||||
{:type, line(meta), name, arguments}
|
||||
end
|
||||
|
||||
# Handle literals
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
import Kernel, except: [destructure: 2, defdelegate: 2, defstruct: 2]
|
||||
|
||||
defmodule Kernel.Utils do
|
||||
@moduledoc false
|
||||
|
||||
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)]
|
||||
|
||||
def defdelegate(fun, opts, env) 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
|
||||
|
||||
:ok = check_defdelegate_args(args, env)
|
||||
|
||||
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
|
||||
|
||||
# TODO: Convert this to an error on 1.3
|
||||
defp check_defdelegate_args([], _env),
|
||||
do: :ok
|
||||
defp check_defdelegate_args([{var, _, mod}|rest], env) when is_atom(var) and is_atom(mod),
|
||||
do: check_defdelegate_args(rest, env)
|
||||
defp check_defdelegate_args([code|_], env) do
|
||||
:elixir_errors.warn(env.line, env.file,
|
||||
"defdelegate/2 will only accept variable names in upcoming versions, " <>
|
||||
"got: #{Macro.to_string(code)}")
|
||||
end
|
||||
|
||||
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))
|
||||
end
|
||||
end
|
||||
+133
-375
@@ -1,15 +1,13 @@
|
||||
defmodule Keyword do
|
||||
@moduledoc """
|
||||
A set of functions for working with keywords.
|
||||
|
||||
A keyword is a list of 2-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 dictionary. 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.
|
||||
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.
|
||||
@@ -22,13 +20,12 @@ defmodule Keyword do
|
||||
a given key and `delete_first/2` deletes just one of the existing
|
||||
entries.
|
||||
|
||||
The functions in Keyword do not guarantee any property when
|
||||
it comes to ordering. However, since a keyword list is simply a
|
||||
list, all the operations defined in `Enum` and `List` can be
|
||||
applied too, specially when ordering is required.
|
||||
Since a keyword list is simply a list, all the operations defined
|
||||
in `Enum` and `List` can be applied.
|
||||
"""
|
||||
|
||||
@compile :inline_list_funcs
|
||||
@behaviour Dict
|
||||
|
||||
@type key :: atom
|
||||
@type value :: any
|
||||
@@ -37,62 +34,42 @@ defmodule Keyword do
|
||||
@type t(value) :: [{key, value}]
|
||||
|
||||
@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
|
||||
|
||||
Checks if the given argument is a keyword list or not.
|
||||
"""
|
||||
@spec keyword?(term) :: boolean
|
||||
def keyword?(term)
|
||||
def keyword?([{key, _value} | rest]) when is_atom(key) do
|
||||
keyword?(rest)
|
||||
end
|
||||
|
||||
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 :: t
|
||||
def new, do: []
|
||||
def new do
|
||||
[]
|
||||
end
|
||||
|
||||
@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]
|
||||
|
||||
iex> Keyword.new([{:a, 1}, {:a, 2}, {:a, 3}])
|
||||
[a: 3]
|
||||
[a: 2, b: 1]
|
||||
|
||||
"""
|
||||
@spec new(Enum.t) :: t
|
||||
def new(pairs) do
|
||||
new(pairs, fn pair -> pair end)
|
||||
Enum.reduce pairs, [], fn {k, v}, keywords ->
|
||||
put(keywords, k, v)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -104,46 +81,37 @@ 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]
|
||||
|
||||
"""
|
||||
@spec new(Enum.t, (term -> {key, value})) :: t
|
||||
@spec new(Enum.t, ({key, value} -> {key, value})) :: t
|
||||
def new(pairs, transform) do
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put_new(acc, k, v)
|
||||
Enum.reduce pairs, [], fn i, keywords ->
|
||||
{k, v} = transform.(i)
|
||||
put(keywords, k, v)
|
||||
end
|
||||
:lists.foldl(fun, [], Enum.reverse(pairs))
|
||||
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
|
||||
@@ -154,123 +122,6 @@ defmodule Keyword do
|
||||
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.
|
||||
|
||||
If duplicated entries exist, the first one is returned.
|
||||
Use `get_values/2` to retrieve all entries.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Keyword.get_lazy(keyword, :a, fun)
|
||||
1
|
||||
iex> Keyword.get_lazy(keyword, :b, fun)
|
||||
13
|
||||
|
||||
"""
|
||||
@spec get_lazy(t, key, (() -> value)) :: value
|
||||
def get_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_atom(key) and is_function(fun, 0) do
|
||||
case :lists.keyfind(key, 1, keywords) do
|
||||
{^key, value} -> value
|
||||
false -> fun.()
|
||||
end
|
||||
end
|
||||
|
||||
@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-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 ->
|
||||
...> {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]}
|
||||
|
||||
"""
|
||||
@spec get_and_update(t, key, (value -> {get, value})) :: {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, value}|t], acc, key, fun) do
|
||||
{get, new_value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, new_value}|t])}
|
||||
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
|
||||
{get, update} = fun.(nil)
|
||||
{get, [{key, update}|:lists.reverse(acc)]}
|
||||
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-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) ->
|
||||
...> {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)
|
||||
** (KeyError) key :b not found in: [a: 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, [])
|
||||
end
|
||||
|
||||
defp get_and_update!([{key, value}|keywords], key, fun, acc) do
|
||||
{get, value} = fun.(value)
|
||||
{get, :lists.reverse(acc, [{key, value}|delete(keywords, key)])}
|
||||
end
|
||||
|
||||
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)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for a specific `key` and returns it in a tuple.
|
||||
|
||||
@@ -280,6 +131,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.fetch([a: 1], :a)
|
||||
{:ok, 1}
|
||||
|
||||
iex> Keyword.fetch([a: 1], :b)
|
||||
:error
|
||||
|
||||
@@ -293,14 +145,15 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
Fetches the value for specific `key`.
|
||||
|
||||
If `key` does not exist, a `KeyError` is raised.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.fetch!([a: 1], :a)
|
||||
1
|
||||
|
||||
iex> Keyword.fetch!([a: 1], :b)
|
||||
** (KeyError) key :b not found in: [a: 1]
|
||||
|
||||
@@ -318,12 +171,8 @@ 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]
|
||||
[1,2]
|
||||
|
||||
"""
|
||||
@spec get_values(t, key) :: [value]
|
||||
@@ -332,20 +181,22 @@ defmodule Keyword do
|
||||
{k, v} when k === key -> {true, v}
|
||||
{_, _} -> false
|
||||
end
|
||||
|
||||
:lists.filtermap(fun, keywords)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all keys from the keyword list.
|
||||
|
||||
Returns all keys from the keyword list.
|
||||
|
||||
Duplicated keys appear duplicated in the final list of keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.keys([a: 1, b: 2])
|
||||
[:a, :b]
|
||||
[:a,:b]
|
||||
|
||||
iex> Keyword.keys([a: 1, b: 2, a: 3])
|
||||
[:a, :b, :a]
|
||||
[:a,:b,:a]
|
||||
|
||||
"""
|
||||
@spec keys(t) :: [key]
|
||||
@@ -356,14 +207,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]
|
||||
[1,2]
|
||||
|
||||
"""
|
||||
@spec values(t) :: [value]
|
||||
@@ -380,12 +227,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
|
||||
@@ -404,8 +251,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]
|
||||
|
||||
@@ -424,6 +273,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]
|
||||
|
||||
@@ -441,10 +291,9 @@ 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]
|
||||
|
||||
@@ -454,35 +303,6 @@ defmodule Keyword do
|
||||
[{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.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 3
|
||||
...> end
|
||||
iex> Keyword.put_new_lazy(keyword, :a, fun)
|
||||
[a: 1]
|
||||
iex> Keyword.put_new_lazy(keyword, :b, fun)
|
||||
[b: 3, a: 1]
|
||||
|
||||
"""
|
||||
@spec put_new_lazy(t, key, (() -> value)) :: t
|
||||
def put_new_lazy(keywords, key, fun)
|
||||
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]
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the given `value` under `key` unless the entry `key`
|
||||
already exists.
|
||||
@@ -491,6 +311,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]
|
||||
|
||||
@@ -504,8 +325,8 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if two keywords are equal.
|
||||
|
||||
Checks if two keywords are equal.
|
||||
|
||||
Two keywords are considered to be equal if they contain
|
||||
the same keys and those keys contain the same values.
|
||||
|
||||
@@ -513,10 +334,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
|
||||
@@ -525,74 +342,46 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@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.
|
||||
Merges two keyword lists into one.
|
||||
|
||||
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]
|
||||
iex> Keyword.merge([a: 1, b: 2], [a: 3, d: 4]) |> Enum.sort
|
||||
[a: 3, b: 2, d: 4]
|
||||
|
||||
"""
|
||||
@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
|
||||
def merge(d1, d2) when is_list(d1) and is_list(d2) do
|
||||
fun = fn {k, _v} -> not has_key?(d2, k) end
|
||||
d2 ++ :lists.filter(fun, d1)
|
||||
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.
|
||||
Merges two keyword lists into one.
|
||||
|
||||
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)
|
||||
def merge(d1, d2, fun) when is_list(d1) and is_list(d2) do
|
||||
do_merge(d2, d1, 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 """
|
||||
@@ -602,6 +391,7 @@ defmodule Keyword do
|
||||
|
||||
iex> Keyword.has_key?([a: 1], :a)
|
||||
true
|
||||
|
||||
iex> Keyword.has_key?([a: 1], :b)
|
||||
false
|
||||
|
||||
@@ -612,8 +402,8 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` with the given function.
|
||||
|
||||
Updates the `key` with the given function.
|
||||
|
||||
If the `key` does not exist, raises `KeyError`.
|
||||
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
@@ -623,8 +413,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]
|
||||
@@ -648,8 +436,8 @@ defmodule Keyword do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates the `key` in `keywords` with the given function.
|
||||
|
||||
Updates the `key` with the given function.
|
||||
|
||||
If the `key` does not exist, inserts the given `initial` value.
|
||||
|
||||
If there are duplicated keys, they are all removed and only the first one
|
||||
@@ -659,15 +447,12 @@ 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]
|
||||
|
||||
"""
|
||||
@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)]
|
||||
end
|
||||
@@ -682,8 +467,8 @@ defmodule Keyword do
|
||||
|
||||
@doc """
|
||||
Takes all entries corresponding to the given keys and extracts them into a
|
||||
separate keyword list.
|
||||
|
||||
separate keyword list.
|
||||
|
||||
Returns a tuple with the new list and the old list with removed keys.
|
||||
|
||||
Keys for which there are no entires in the keyword list are ignored.
|
||||
@@ -692,10 +477,13 @@ defmodule Keyword do
|
||||
|
||||
## 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
|
||||
@@ -719,9 +507,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]
|
||||
|
||||
"""
|
||||
@@ -736,9 +527,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]
|
||||
|
||||
"""
|
||||
@@ -747,108 +541,72 @@ 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)
|
||||
{1, []}
|
||||
iex> Keyword.pop([a: 1], :b)
|
||||
{nil, [a: 1]}
|
||||
iex> Keyword.pop([a: 1], :b, 3)
|
||||
{3, [a: 1]}
|
||||
iex> Keyword.pop([a: 1, a: 2], :a)
|
||||
{1, []}
|
||||
iex> Keyword.pop [a: 1], :a
|
||||
{1,[]}
|
||||
|
||||
iex> Keyword.pop [a: 1], :b
|
||||
{nil,[a: 1]}
|
||||
|
||||
iex> Keyword.pop [a: 1], :b, 3
|
||||
{3,[a: 1]}
|
||||
|
||||
iex> Keyword.pop [a: 1], :b, 3
|
||||
{3,[a: 1]}
|
||||
|
||||
iex> Keyword.pop [a: 1, a: 2], :a
|
||||
{1,[]}
|
||||
|
||||
"""
|
||||
@spec pop(t, key, value) :: {value, t}
|
||||
def pop(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
:error ->
|
||||
{default, keywords}
|
||||
end
|
||||
{get(keywords, key, default), delete(keywords, key)}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the keyword list.
|
||||
|
||||
This is useful if the default value is very expensive to calculate or
|
||||
generally difficult to setup and teardown again.
|
||||
|
||||
All duplicated keys are removed. See `pop_first/3` for
|
||||
removing only the first entry.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> keyword = [a: 1]
|
||||
iex> fun = fn ->
|
||||
...> # some expensive operation here
|
||||
...> 13
|
||||
...> end
|
||||
iex> Keyword.pop_lazy(keyword, :a, fun)
|
||||
{1, []}
|
||||
iex> Keyword.pop_lazy(keyword, :b, fun)
|
||||
{13, [a: 1]}
|
||||
|
||||
"""
|
||||
@spec pop_lazy(t, key, (() -> value)) :: {value, t}
|
||||
def pop_lazy(keywords, key, fun)
|
||||
when is_list(keywords) and is_function(fun, 0) do
|
||||
case fetch(keywords, key) do
|
||||
{:ok, value} ->
|
||||
{value, delete(keywords, key)}
|
||||
:error ->
|
||||
{fun.(), keywords}
|
||||
end
|
||||
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.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :a
|
||||
{1, []}
|
||||
{1,[]}
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :b
|
||||
{nil, [a: 1]}
|
||||
{nil,[a: 1]}
|
||||
|
||||
iex> Keyword.pop_first [a: 1], :b, 3
|
||||
{3, [a: 1]}
|
||||
{3,[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]}
|
||||
{1,[a: 2]}
|
||||
|
||||
"""
|
||||
@spec pop_first(t, key, value) :: {value, t}
|
||||
def pop_first(keywords, key, default \\ nil) when is_list(keywords) do
|
||||
case :lists.keytake(key, 1, keywords) do
|
||||
{:value, {^key, value}, rest} -> {value, rest}
|
||||
false -> {default, keywords}
|
||||
end
|
||||
{get(keywords, key, default), delete_first(keywords, key)}
|
||||
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
|
||||
end
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
def size(keyword) do
|
||||
length(keyword)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def to_list(keyword) do
|
||||
keyword
|
||||
end
|
||||
end
|
||||
|
||||
+45
-114
@@ -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.
|
||||
|
||||
## Char lists
|
||||
|
||||
If a list is made of non-negative integers, it can also
|
||||
be called as a char list. Elixir uses single quotes to
|
||||
define char lists:
|
||||
|
||||
iex> 'héllo'
|
||||
[104, 233, 108, 108, 111]
|
||||
|
||||
In particular, char lists 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 char lists
|
||||
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
|
||||
@@ -52,7 +26,7 @@ defmodule List do
|
||||
## Examples
|
||||
|
||||
iex> List.delete([1, 2, 3], 1)
|
||||
[2, 3]
|
||||
[2,3]
|
||||
|
||||
iex> List.delete([1, 2, 2, 3], 2)
|
||||
[1, 2, 3]
|
||||
@@ -69,11 +43,10 @@ defmodule List do
|
||||
## Examples
|
||||
|
||||
iex> List.duplicate("hello", 3)
|
||||
["hello", "hello", "hello"]
|
||||
["hello","hello","hello"]
|
||||
|
||||
iex> List.duplicate([1, 2], 2)
|
||||
[[1, 2], [1, 2]]
|
||||
|
||||
[[1,2],[1,2]]
|
||||
|
||||
"""
|
||||
@spec duplicate(elem, non_neg_integer) :: [elem] when elem: var
|
||||
@@ -87,7 +60,7 @@ defmodule List do
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]])
|
||||
[1, 2, 3]
|
||||
[1,2,3]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list) :: list when deep_list: [any | deep_list]
|
||||
@@ -103,7 +76,7 @@ defmodule List do
|
||||
## Examples
|
||||
|
||||
iex> List.flatten([1, [[2], 3]], [4, 5])
|
||||
[1, 2, 3, 4, 5]
|
||||
[1,2,3,4,5]
|
||||
|
||||
"""
|
||||
@spec flatten(deep_list, [elem]) :: [elem] when elem: var, deep_list: [elem | deep_list]
|
||||
@@ -112,7 +85,7 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the left with
|
||||
Folds (reduces) the given list to the left with
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
@@ -130,7 +103,7 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Folds (reduces) the given list from the right with
|
||||
Folds (reduces) the given list to the right with
|
||||
a function. Requires an accumulator.
|
||||
|
||||
## Examples
|
||||
@@ -186,7 +159,7 @@ defmodule List do
|
||||
@doc """
|
||||
Receives a list of tuples and returns the first tuple
|
||||
where the item at `position` in the tuple matches the
|
||||
given `key`.
|
||||
given `item`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -208,7 +181,7 @@ defmodule List do
|
||||
@doc """
|
||||
Receives a list of tuples and returns `true` if there is
|
||||
a tuple where the item at `position` in the tuple matches
|
||||
the given `key`.
|
||||
the given `item`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -282,7 +255,7 @@ defmodule List do
|
||||
@doc """
|
||||
Receives a list of tuples and deletes the first tuple
|
||||
where the item at `position` matches the
|
||||
given `key`. Returns the new list.
|
||||
given `item`. Returns the new list.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -301,33 +274,6 @@ defmodule List do
|
||||
:lists.keydelete(key, position + 1, list)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Receives a `list` of tuples and returns the first tuple
|
||||
where the element at `position` in the tuple matches the
|
||||
given `key`, as well as the `list` without found tuple.
|
||||
|
||||
If such a tuple is not found, `nil` will be returned.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> List.keytake([a: 1, b: 2], :a, 0)
|
||||
{{:a, 1}, [b: 2]}
|
||||
|
||||
iex> List.keytake([a: 1, b: 2], 2, 1)
|
||||
{{:b, 2}, [a: 1]}
|
||||
|
||||
iex> List.keytake([a: 1, b: 2], :c, 0)
|
||||
nil
|
||||
|
||||
"""
|
||||
@spec keytake([tuple], any, non_neg_integer) :: {tuple, [tuple]} | nil
|
||||
def keytake(list, key, position) do
|
||||
case :lists.keytake(key, position + 1, list) do
|
||||
{:value, item, list} -> {item, list}
|
||||
false -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Wraps the argument in a list.
|
||||
If the argument is already a list, returns the list.
|
||||
@@ -339,7 +285,7 @@ defmodule List do
|
||||
["hello"]
|
||||
|
||||
iex> List.wrap([1, 2, 3])
|
||||
[1, 2, 3]
|
||||
[1,2,3]
|
||||
|
||||
iex> List.wrap(nil)
|
||||
[]
|
||||
@@ -475,7 +421,7 @@ defmodule List do
|
||||
iex> List.delete_at([1, 2, 3], 0)
|
||||
[2, 3]
|
||||
|
||||
iex> List.delete_at([1, 2, 3], 10)
|
||||
iex List.delete_at([1, 2, 3], 10)
|
||||
[1, 2, 3]
|
||||
|
||||
iex> List.delete_at([1, 2, 3], -1)
|
||||
@@ -511,23 +457,12 @@ defmodule List do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts a char list to an existing atom. Raises an `ArgumentError`
|
||||
if the atom does not exist.
|
||||
Converts a char list to an existing atom.
|
||||
|
||||
Currently Elixir does not support conversions from char lists
|
||||
which contains Unicode codepoints greater than 0xFF.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> _ = :my_atom
|
||||
iex> List.to_existing_atom('my_atom')
|
||||
:my_atom
|
||||
|
||||
iex> List.to_existing_atom('this_atom_will_never_exist')
|
||||
** (ArgumentError) argument error
|
||||
|
||||
"""
|
||||
@spec to_existing_atom(char_list) :: atom
|
||||
def to_existing_atom(char_list) do
|
||||
@@ -602,9 +537,9 @@ defmodule List do
|
||||
Converts a list of integers representing codepoints, lists or
|
||||
strings into a string.
|
||||
|
||||
Notice that this function expects a list of integers representing
|
||||
Notice that this function expect a list of integer representing
|
||||
UTF-8 codepoints. If you have a list of bytes, you must instead use
|
||||
the [`:binary` module](http://www.erlang.org/doc/man/binary.html).
|
||||
[the `:binary` module](http://erlang.org/doc/man/binary.html).
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -617,12 +552,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 list to string. The list must contain only integers, strings or nested such lists; got: #{inspect list}"
|
||||
else
|
||||
case :unicode.characters_to_binary(list) do
|
||||
result when is_binary(result) ->
|
||||
result
|
||||
|
||||
@@ -646,40 +576,40 @@ defmodule List do
|
||||
list
|
||||
end
|
||||
|
||||
defp do_replace_at([_old | rest], 0, value) do
|
||||
[value | rest]
|
||||
defp do_replace_at([_old|rest], 0, value) do
|
||||
[ value | rest ]
|
||||
end
|
||||
|
||||
defp do_replace_at([h | t], index, value) do
|
||||
[h | do_replace_at(t, index - 1, value)]
|
||||
defp do_replace_at([h|t], index, value) do
|
||||
[ h | do_replace_at(t, index - 1, value) ]
|
||||
end
|
||||
|
||||
# insert_at
|
||||
|
||||
defp do_insert_at([], _index, value) do
|
||||
[value]
|
||||
[ value ]
|
||||
end
|
||||
|
||||
defp do_insert_at(list, index, value) when index <= 0 do
|
||||
[value | list]
|
||||
[ value | list ]
|
||||
end
|
||||
|
||||
defp do_insert_at([h | t], index, value) do
|
||||
[h | do_insert_at(t, index - 1, value)]
|
||||
defp do_insert_at([h|t], index, value) do
|
||||
[ h | do_insert_at(t, index - 1, value) ]
|
||||
end
|
||||
|
||||
# update_at
|
||||
|
||||
defp do_update_at([value | list], 0, fun) do
|
||||
[fun.(value) | list]
|
||||
defp do_update_at([value|list], 0, fun) do
|
||||
[ fun.(value) | list ]
|
||||
end
|
||||
|
||||
defp do_update_at(list, index, _fun) when index < 0 do
|
||||
list
|
||||
end
|
||||
|
||||
defp do_update_at([h | t], index, fun) do
|
||||
[h | do_update_at(t, index - 1, fun)]
|
||||
defp do_update_at([h|t], index, fun) do
|
||||
[ h | do_update_at(t, index - 1, fun) ]
|
||||
end
|
||||
|
||||
defp do_update_at([], _index, _fun) do
|
||||
@@ -692,7 +622,7 @@ defmodule List do
|
||||
[]
|
||||
end
|
||||
|
||||
defp do_delete_at([_ | t], 0) do
|
||||
defp do_delete_at([_|t], 0) do
|
||||
t
|
||||
end
|
||||
|
||||
@@ -700,18 +630,19 @@ defmodule List do
|
||||
list
|
||||
end
|
||||
|
||||
defp do_delete_at([h | t], index) do
|
||||
[h | do_delete_at(t, index - 1)]
|
||||
defp do_delete_at([h|t], index) do
|
||||
[h | do_delete_at(t, index-1)]
|
||||
end
|
||||
|
||||
# zip
|
||||
|
||||
defp do_zip(list, acc) do
|
||||
converter = fn x, acc -> do_zip_each(to_list(x), acc) end
|
||||
case :lists.mapfoldl(converter, [], list) do
|
||||
{_, nil} -> :lists.reverse(acc)
|
||||
{mlist, heads} ->
|
||||
do_zip(mlist, [to_tuple(:lists.reverse(heads)) | acc])
|
||||
{mlist, heads} = :lists.mapfoldl converter, [], list
|
||||
|
||||
case heads do
|
||||
nil -> :lists.reverse acc
|
||||
_ -> do_zip mlist, [:erlang.list_to_tuple(:lists.reverse(heads))|acc]
|
||||
end
|
||||
end
|
||||
|
||||
@@ -719,8 +650,8 @@ defmodule List do
|
||||
{nil, nil}
|
||||
end
|
||||
|
||||
defp do_zip_each([h | t], acc) do
|
||||
{t, [h | acc]}
|
||||
defp do_zip_each([h|t], acc) do
|
||||
{t, [h|acc]}
|
||||
end
|
||||
|
||||
defp do_zip_each([], _) do
|
||||
|
||||
+96
-295
@@ -3,23 +3,12 @@ import Kernel, except: [to_string: 1]
|
||||
defmodule Macro do
|
||||
@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 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.
|
||||
"""
|
||||
|
||||
@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 [:===, :!==,
|
||||
@@ -52,11 +41,12 @@ defmodule Macro do
|
||||
o when o in [:==, :!=, :=~, :===, :!==] -> {:left, 150}
|
||||
o when o in [:<, :<=, :>=, :>] -> {:left, 160}
|
||||
o when o in [:|>, :<<<, :>>>, :<~, :~>,
|
||||
:<<~, :~>>, :<~>, :<|>, :^^^] -> {:left, 170}
|
||||
:<<~, :~>>, :<~>, :<|>] -> {:left, 170}
|
||||
:in -> {:left, 180}
|
||||
o when o in [:++, :--, :.., :<>] -> {:right, 200}
|
||||
o when o in [:+, :-] -> {:left, 210}
|
||||
o when o in [:*, :/] -> {:left, 220}
|
||||
:^^^ -> {:left, 250}
|
||||
:. -> {:left, 310}
|
||||
end
|
||||
end
|
||||
@@ -86,17 +76,7 @@ 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
|
||||
|
||||
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"
|
||||
bad_pipe(expr, call_args)
|
||||
end
|
||||
|
||||
def pipe(expr, {call, line, atom}, integer) when is_atom(atom) do
|
||||
@@ -108,18 +88,18 @@ 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 """
|
||||
Applies the given function to the node metadata if it contains one.
|
||||
|
||||
This is often useful when used with `Macro.prewalk/2` to remove
|
||||
This is often useful when used with `Macro.prewalk/1` to remove
|
||||
information like lines and hygienic counters from the expression
|
||||
for either storage or comparison.
|
||||
|
||||
@@ -143,7 +123,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Generates an AST node representing the variable given
|
||||
Genrates a AST node representing the variable given
|
||||
by the atoms `var` and `context`.
|
||||
|
||||
## Examples
|
||||
@@ -167,52 +147,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) do
|
||||
unless is_atom(form) do
|
||||
{form, acc} = pre.(form, acc)
|
||||
{form, acc} = do_traverse(form, acc, pre, post)
|
||||
end
|
||||
|
||||
unless is_atom(args) do
|
||||
{args, acc} = Enum.map_reduce(args, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
end
|
||||
|
||||
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} = Enum.map_reduce(list, acc, fn x, acc ->
|
||||
{x, acc} = pre.(x, acc)
|
||||
do_traverse(x, acc, pre, post)
|
||||
end)
|
||||
post.(list, acc)
|
||||
end
|
||||
|
||||
defp do_traverse(x, acc, _pre, post) do
|
||||
post.(x, acc)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Performs a depth-first, pre-order traversal of quoted expressions.
|
||||
"""
|
||||
@@ -227,7 +161,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 """
|
||||
@@ -244,7 +214,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 """
|
||||
@@ -272,8 +269,6 @@ defmodule Macro do
|
||||
|
||||
"""
|
||||
@spec decompose_call(Macro.t) :: {atom, [Macro.t]} | {Macro.t, atom, [Macro.t]} | :error
|
||||
def decompose_call(ast)
|
||||
|
||||
def decompose_call({{:., _, [remote, function]}, _, args}) when is_tuple(remote) or is_atom(remote),
|
||||
do: {remote, function, args}
|
||||
|
||||
@@ -347,15 +342,15 @@ defmodule Macro do
|
||||
defp find_invalid(other), do: {:error, other}
|
||||
|
||||
@doc ~S"""
|
||||
Unescapes the given chars.
|
||||
Unescape the given chars.
|
||||
|
||||
This is the unescaping behaviour used by default in Elixir
|
||||
single- and double-quoted strings. Check `unescape_string/2`
|
||||
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`. Unicode codepoints
|
||||
can be given as hexadecimals via `\xNN` and `\x{NN...}` escapes.
|
||||
`\d`, `\e`, `\f`, `\n`, `\r`, `\s`, `\t` and `\v`. Hexadecimals
|
||||
are also supported via `\xNN` and `\x{NN...}` syntax.
|
||||
|
||||
This function is commonly used on sigil implementations
|
||||
(like `~r`, `~s` and others) which receive a raw, unescaped
|
||||
@@ -375,7 +370,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Unescapes the given chars according to the map given.
|
||||
Unescape the given chars according to the map given.
|
||||
|
||||
Check `unescape_string/1` if you want to use the same map
|
||||
as Elixir single- and double-quoted strings.
|
||||
@@ -419,7 +414,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unescapes the given tokens according to the default map.
|
||||
Unescape the given tokens according to the default map.
|
||||
|
||||
Check `unescape_string/1` and `unescape_string/2` for more
|
||||
information about unescaping.
|
||||
@@ -435,7 +430,7 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Unescapes the given tokens according to the given map.
|
||||
Unescape the given tokens according to the given map.
|
||||
|
||||
Check `unescape_tokens/1` and `unescape_string/2` for more information.
|
||||
"""
|
||||
@@ -478,20 +473,11 @@ defmodule Macro do
|
||||
end
|
||||
|
||||
# Bits containers
|
||||
def to_string({:<<>>, _, parts} = 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
|
||||
end)
|
||||
fun.(ast, "<<" <> result <> ">>")
|
||||
end
|
||||
def to_string({:<<>>, _, args} = ast, fun) do
|
||||
fun.(ast, case Enum.map_join(args, ", ", &to_string(&1, fun)) do
|
||||
"<" <> rest -> "<< <" <> rest <> " >>"
|
||||
rest -> "<<" <> rest <> ">>"
|
||||
end)
|
||||
end
|
||||
|
||||
# Tuple containers
|
||||
@@ -527,12 +513,6 @@ 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
|
||||
fun.(ast, "(" <> arrow_to_string(ast, fun, true) <> ")")
|
||||
@@ -560,21 +540,6 @@ defmodule Macro do
|
||||
fun.(ast, "(" <> Enum.map_join(left, ", ", &to_string(&1, fun)) <> ") when " <> to_string(right, fun))
|
||||
end
|
||||
|
||||
# Capture
|
||||
def to_string({:&, _, [{:/, _, [{name, _, ctx}, arity]}]} = ast, fun)
|
||||
when is_atom(name) and is_atom(ctx) and is_integer(arity) do
|
||||
fun.(ast, "&" <> Atom.to_string(name) <> "/" <> to_string(arity, fun))
|
||||
end
|
||||
|
||||
def to_string({:&, _, [{:/, _, [{{:., _, [mod, name]}, _, []}, arity]}]} = ast, fun)
|
||||
when is_atom(name) and is_integer(arity) 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
|
||||
@@ -590,11 +555,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
|
||||
@@ -634,30 +594,6 @@ 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]
|
||||
|
||||
@@ -666,41 +602,15 @@ defmodule Macro do
|
||||
end
|
||||
defp kw_blocks?(_), do: false
|
||||
|
||||
# Check if we have an interpolated string.
|
||||
defp interpolated?({:<<>>, _, [_|_] = parts}) do
|
||||
Enum.all?(parts, fn
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [_]},
|
||||
{:binary, _, _}]} -> true
|
||||
binary when is_binary(binary) -> true
|
||||
_ -> false
|
||||
end)
|
||||
end
|
||||
|
||||
defp interpolated?(_) do
|
||||
false
|
||||
end
|
||||
|
||||
defp interpolate({:<<>>, _, parts}, fun) do
|
||||
parts = Enum.map_join(parts, "", fn
|
||||
{:::, _, [{{:., _, [Kernel, :to_string]}, _, [arg]}, {:binary, _, _}]} ->
|
||||
"\#{" <> to_string(arg, fun) <> "}"
|
||||
binary when is_binary(binary) ->
|
||||
binary = inspect(binary, [])
|
||||
:binary.part(binary, 1, byte_size(binary) - 2)
|
||||
end)
|
||||
|
||||
<<?", parts::binary, ?">>
|
||||
end
|
||||
|
||||
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, _, [{:<<>>, _, [string]}, args]} = ast, fun) when is_list(args) do
|
||||
sigil =
|
||||
case Atom.to_string(func) do
|
||||
<<"sigil_", name>> ->
|
||||
"~" <> <<name>> <>
|
||||
interpolate(bin, fun) <>
|
||||
fun.(string, inspect(string, [])) <>
|
||||
sigil_args(args, fun)
|
||||
_ ->
|
||||
nil
|
||||
@@ -715,14 +625,10 @@ defmodule Macro do
|
||||
defp sigil_args([], _fun), do: ""
|
||||
defp sigil_args(args, fun), do: fun.(args, List.to_string(args))
|
||||
|
||||
defp call_to_string(atom, _fun) when is_atom(atom),
|
||||
do: Atom.to_string(atom)
|
||||
defp call_to_string({:., _, [arg]}, fun),
|
||||
do: module_to_string(arg, fun) <> "."
|
||||
defp call_to_string({:., _, [left, right]}, fun),
|
||||
do: module_to_string(left, fun) <> "." <> call_to_string(right, fun)
|
||||
defp call_to_string(other, fun),
|
||||
do: to_string(other, fun)
|
||||
defp call_to_string(atom, _fun) when is_atom(atom), do: Atom.to_string(atom)
|
||||
defp call_to_string({:., _, [arg]}, fun), do: module_to_string(arg, fun) <> "."
|
||||
defp call_to_string({:., _, [left, right]}, fun), do: module_to_string(left, fun) <> "." <> call_to_string(right, fun)
|
||||
defp call_to_string(other, fun), do: to_string(other, fun)
|
||||
|
||||
defp call_to_string_with_args(target, args, fun) do
|
||||
target = call_to_string(target, fun)
|
||||
@@ -924,15 +830,15 @@ defmodule Macro do
|
||||
defp do_expand_once({:__aliases__, _, _} = original, env) do
|
||||
case :elixir_aliases.expand(original, env.aliases, env.macro_aliases, env.lexical_tracker) do
|
||||
receiver when is_atom(receiver) ->
|
||||
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
|
||||
:elixir_lexical.record_remote(receiver, env.lexical_tracker)
|
||||
{receiver, true}
|
||||
aliases ->
|
||||
aliases = :lists.map(&elem(do_expand_once(&1, env), 0), aliases)
|
||||
aliases = for alias <- aliases, do: elem(do_expand_once(alias, env), 0)
|
||||
|
||||
case :lists.all(&is_atom/1, aliases) do
|
||||
true ->
|
||||
receiver = :elixir_aliases.concat(aliases)
|
||||
:elixir_lexical.record_remote(receiver, env.function, env.lexical_tracker)
|
||||
:elixir_lexical.record_remote(receiver, env.lexical_tracker)
|
||||
{receiver, true}
|
||||
false ->
|
||||
{original, false}
|
||||
@@ -1033,7 +939,7 @@ defmodule Macro do
|
||||
be expanded.
|
||||
|
||||
This function uses `expand_once/2` under the hood. Check
|
||||
it out for more information and examples.
|
||||
`expand_once/2` for more information and examples.
|
||||
"""
|
||||
def expand(tree, env) do
|
||||
expand_until({tree, true}, env)
|
||||
@@ -1046,109 +952,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.
|
||||
|
||||
## 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"
|
||||
|
||||
"""
|
||||
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.
|
||||
|
||||
## 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(<<?_>>),
|
||||
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,12 +24,12 @@ 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-item tuples, where the first
|
||||
(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
|
||||
@@ -39,8 +39,8 @@ defmodule Macro.Env do
|
||||
* `vars` - a list keeping all defined variables as `{var, context}`
|
||||
* `export_vars` - a list keeping all variables to be exported in a
|
||||
construct (may be `nil`)
|
||||
* `lexical_tracker` - PID of the lexical tracker which is responsible for
|
||||
keeping user info
|
||||
* `lexical_tracker` - PID of the lexical tracker which is responsible to
|
||||
keep user info
|
||||
* `local` - the module to expand local functions to
|
||||
"""
|
||||
|
||||
@@ -91,15 +91,14 @@ defmodule Macro.Env do
|
||||
context_modules: [],
|
||||
vars: [],
|
||||
export_vars: nil,
|
||||
lexical_tracker: nil}
|
||||
lexical_tracker: nil,
|
||||
local: nil}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a keyword list containing the file and line
|
||||
information as keys.
|
||||
"""
|
||||
@spec location(t) :: Keyword.t
|
||||
def location(env)
|
||||
def location(%{__struct__: Macro.Env, file: file, line: line}) do
|
||||
[file: file, line: line]
|
||||
end
|
||||
@@ -108,22 +107,17 @@ defmodule Macro.Env do
|
||||
Returns whether the compilation environment is currently
|
||||
inside a guard.
|
||||
"""
|
||||
@spec in_guard?(t) :: boolean
|
||||
def in_guard?(env)
|
||||
def in_guard?(%{__struct__: Macro.Env, context: context}), do: context == :guard
|
||||
|
||||
@doc """
|
||||
Returns whether the compilation environment is currently
|
||||
inside a match clause.
|
||||
"""
|
||||
@spec in_match?(t) :: boolean
|
||||
def in_match?(env)
|
||||
def in_match?(%{__struct__: Macro.Env, context: context}), do: context == :match
|
||||
|
||||
@doc """
|
||||
Returns the environment stacktrace.
|
||||
"""
|
||||
@spec stacktrace(t) :: list
|
||||
def stacktrace(%{__struct__: Macro.Env} = env) do
|
||||
cond do
|
||||
is_nil(env.module) ->
|
||||
|
||||
+12
-550
@@ -1,558 +1,43 @@
|
||||
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 the 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 the map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.values(%{a: 1, b: 2})
|
||||
[1, 2]
|
||||
|
||||
"""
|
||||
@spec values(map) :: [value]
|
||||
defdelegate values(map), to: :maps
|
||||
|
||||
@doc """
|
||||
Converts the 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) do
|
||||
Enum.reduce(enumerable, %{}, fn {k, v}, acc -> put(acc, k, v) end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a map from an enumerable via the transformation function.
|
||||
|
||||
Duplicated entries 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
|
||||
fun = fn el, acc ->
|
||||
{k, v} = transform.(el)
|
||||
put(acc, k, v)
|
||||
end
|
||||
Enum.reduce(enumerable, %{}, fun)
|
||||
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, [key]) :: map
|
||||
def take(map, keys) do
|
||||
Enum.reduce(keys, new, fn key, acc ->
|
||||
case fetch(map, key) do
|
||||
{:ok, value} -> put(acc, key, value)
|
||||
:error -> acc
|
||||
end
|
||||
end)
|
||||
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 the map for a specific `key`.
|
||||
|
||||
If the `key` does not exist, returns the 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.
|
||||
|
||||
## 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)
|
||||
end, map1, map2
|
||||
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}
|
||||
|
||||
"""
|
||||
@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 all values associated with `key` in the `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 fetch(map, key) do
|
||||
{:ok, value} -> {value, delete(map, key)}
|
||||
:error -> {default, map}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily returns and removes all values associated with `key` in the `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 the map.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Map.drop(%{a: 1, b: 2, c: 3}, [:b, :d])
|
||||
%{a: 1, c: 3}
|
||||
|
||||
"""
|
||||
@spec drop(map, [key]) :: map
|
||||
def drop(map, keys) do
|
||||
Enum.reduce(keys, map, &delete(&2, &1))
|
||||
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 entires in the 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, [key]) :: {map, map}
|
||||
def split(map, keys) do
|
||||
Enum.reduce(keys, {new, map}, fn key, {inc, exc} = acc ->
|
||||
case fetch(exc, key) do
|
||||
{:ok, value} ->
|
||||
{put(inc, key, value), delete(exc, key)}
|
||||
:error ->
|
||||
acc
|
||||
end
|
||||
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} ->
|
||||
put(map, key, fun.(value))
|
||||
:error ->
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
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-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 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}}
|
||||
|
||||
"""
|
||||
@spec get_and_update(map, key, (value -> {get, value})) :: {get, map} when get: term
|
||||
def get_and_update(%{} = map, key, fun) do
|
||||
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-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 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
|
||||
|
||||
"""
|
||||
@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} ->
|
||||
{get, update} = fun.(value)
|
||||
{get, :maps.put(key, update, map)}
|
||||
:error ->
|
||||
:erlang.error({:badkey, key})
|
||||
end
|
||||
end
|
||||
|
||||
def get_and_update!(map, _key, _fun), do: :erlang.error({:badmap, map})
|
||||
|
||||
@doc """
|
||||
Converts a struct to map.
|
||||
|
||||
@@ -572,7 +57,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
|
||||
@@ -581,27 +65,5 @@ 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), do: map1 === map2
|
||||
|
||||
# TODO: Deprecate by 1.3
|
||||
# TODO: Remove by 1.4
|
||||
@doc false
|
||||
def size(map) do
|
||||
map_size(map)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,289 +0,0 @@
|
||||
defmodule MapSet do
|
||||
@moduledoc """
|
||||
A set of functions for working with sets.
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
@opaque t :: %__MODULE__{map: map}
|
||||
@type value :: term
|
||||
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(enumerable) do
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, &1))
|
||||
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
|
||||
Enum.reduce(enumerable, %MapSet{}, &put(&2, transform.(&1)))
|
||||
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, term) do
|
||||
%{set | map: Map.delete(map, term)}
|
||||
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
|
||||
def difference(%MapSet{map: map1}, %MapSet{map: map2}) do
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
Map.delete(acc, value)
|
||||
end, map1, map2)
|
||||
%MapSet{map: map}
|
||||
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
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
throw({:halt, false})
|
||||
else
|
||||
true
|
||||
end
|
||||
end, true, map1)
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
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
|
||||
if map_size(map1) > map_size(map2), do: {map1, map2} = {map2, map1}
|
||||
map = :maps.fold(fn value, _, acc ->
|
||||
if Map.has_key?(map2, value) do
|
||||
Map.put(acc, value, true)
|
||||
else
|
||||
acc
|
||||
end
|
||||
end, %{}, map1)
|
||||
%MapSet{map: map}
|
||||
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)}
|
||||
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
|
||||
:maps.fold(fn value, _, _ ->
|
||||
if Map.has_key?(map2, value) do
|
||||
true
|
||||
else
|
||||
throw({:halt, false})
|
||||
end
|
||||
end, true, map1)
|
||||
else
|
||||
false
|
||||
end
|
||||
catch
|
||||
{:halt, false} -> false
|
||||
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
|
||||
|
||||
defimpl Enumerable do
|
||||
def reduce(set, acc, fun), do: Enumerable.List.reduce(MapSet.to_list(set), acc, fun)
|
||||
def member?(set, val), do: {:ok, MapSet.member?(set, val)}
|
||||
def count(set), do: {:ok, MapSet.size(set)}
|
||||
end
|
||||
|
||||
defimpl Collectable do
|
||||
def into(original) do
|
||||
{original, fn
|
||||
set, {:cont, x} -> MapSet.put(set, x)
|
||||
set, :done -> set
|
||||
_, :halt -> :ok
|
||||
end}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect do
|
||||
import Inspect.Algebra
|
||||
|
||||
def inspect(set, opts) do
|
||||
concat ["#MapSet<", Inspect.List.inspect(MapSet.to_list(set), opts), ">"]
|
||||
end
|
||||
end
|
||||
end
|
||||
+177
-322
@@ -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
|
||||
@@ -66,31 +65,7 @@ defmodule Module do
|
||||
|
||||
* `@behaviour` (notice the British spelling)
|
||||
|
||||
Behaviours can be referenced by modules to ensure they implement
|
||||
required specific function signatures defined by `@callback`.
|
||||
|
||||
For example, you can specify the URI.Parser behaviour as follows:
|
||||
|
||||
defmodule URI.Parser do
|
||||
@doc "Parses the given URL"
|
||||
@callback parse(uri_info :: URI.t) :: URI.t
|
||||
|
||||
@doc "Defines a default port"
|
||||
@callback default_port() :: integer
|
||||
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
|
||||
end
|
||||
|
||||
If the behaviour changes or URI.HTTP does not implement one of the
|
||||
callbacks, a warning will be raised.
|
||||
|
||||
Specifies an OTP or user-defined behaviour.
|
||||
Specify an OTP or user-defined behaviour.
|
||||
|
||||
### Example
|
||||
|
||||
@@ -102,30 +77,30 @@ defmodule Module do
|
||||
|
||||
* `@compile`
|
||||
|
||||
Defines options for module compilation that are passed to the Erlang
|
||||
Define options for module compilation that are passed to the Erlang
|
||||
compiler.
|
||||
|
||||
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).
|
||||
See http://www.erlang.org/doc/man/compile.html for the list of supported
|
||||
options.
|
||||
|
||||
Multiple uses of `@compile` will accumulate instead of overriding
|
||||
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`
|
||||
|
||||
Provides documentation for the function or macro that follows the
|
||||
Provide documentation for the function or macro that follows the
|
||||
attribute.
|
||||
|
||||
Accepts a string (often a heredoc) or `false` where `@doc false` will
|
||||
@@ -136,40 +111,40 @@ defmodule Module do
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sums `a` to `b`.
|
||||
"""
|
||||
def sum(a, b) do
|
||||
a + b
|
||||
@doc """
|
||||
Sum.
|
||||
"""
|
||||
def sum(a, b) do
|
||||
a + b
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
* `@file`
|
||||
|
||||
Changes the filename used in stacktraces for the function or macro that
|
||||
Change the filename used in stacktraces for the function or macro that
|
||||
follows the attribute.
|
||||
|
||||
Accepts a string. Can be used more than once.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
@file "hello.ex"
|
||||
def hello do
|
||||
"world"
|
||||
defmodule M do
|
||||
@doc "Hello world"
|
||||
@file "hello.ex"
|
||||
def hello do
|
||||
"world"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
* `@moduledoc`
|
||||
|
||||
Provides documentation for the current module.
|
||||
Provide 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
|
||||
@@ -177,11 +152,11 @@ defmodule Module do
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@moduledoc """
|
||||
A very useful module
|
||||
"""
|
||||
end
|
||||
defmodule M do
|
||||
@moduledoc """
|
||||
A very useful module
|
||||
"""
|
||||
end
|
||||
|
||||
|
||||
* `@on_definition`
|
||||
@@ -195,13 +170,13 @@ defmodule Module do
|
||||
- the module environment
|
||||
- kind: `:def`, `:defp`, `:defmacro`, or `:defmacrop`
|
||||
- function/macro name
|
||||
- list of quoted arguments
|
||||
- list of quoted guards
|
||||
- quoted function body
|
||||
- list of expanded arguments
|
||||
- list of expanded guards
|
||||
- expanded function body
|
||||
|
||||
Note the hook receives the quoted arguments and it is invoked before
|
||||
Note the hook receives the expanded 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.
|
||||
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.
|
||||
@@ -216,28 +191,28 @@ defmodule Module do
|
||||
|
||||
### 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)
|
||||
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
|
||||
|
||||
def hello(_) do
|
||||
:ok
|
||||
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
|
||||
end
|
||||
|
||||
* `@on_load`
|
||||
|
||||
@@ -249,35 +224,35 @@ defmodule Module do
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@on_load :load_check
|
||||
defmodule M do
|
||||
@on_load :load_check
|
||||
|
||||
def load_check do
|
||||
if some_condition() do
|
||||
:ok
|
||||
else
|
||||
nil
|
||||
def load_check do
|
||||
if some_condition() do
|
||||
:ok
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def some_condition do
|
||||
false
|
||||
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
|
||||
defmodule M do
|
||||
@vsn "1.0"
|
||||
end
|
||||
|
||||
* `@external_resource`
|
||||
|
||||
Specifies an external resource to the current module.
|
||||
Specify an external resource to the current module.
|
||||
|
||||
Many times a module embeds information from an external file. This
|
||||
attribute allows the module to annotate which external resources
|
||||
@@ -286,46 +261,22 @@ defmodule Module do
|
||||
Tools like Mix may use this information to ensure the module is
|
||||
recompiled in case any of the external resources change.
|
||||
|
||||
* `@dialyzer`
|
||||
|
||||
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.
|
||||
|
||||
### Example
|
||||
|
||||
defmodule M do
|
||||
@dialyzer {:nowarn_function, myfun: 1}
|
||||
|
||||
def myfun(arg) do
|
||||
M.not_a_function(arg)
|
||||
end
|
||||
end
|
||||
|
||||
The following attributes are part of typespecs and are also reserved by
|
||||
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
|
||||
* `@macrocallback` - provides a specification for a macro 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 the behaviour callback
|
||||
|
||||
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`.
|
||||
@@ -350,14 +301,14 @@ defmodule Module do
|
||||
* `:module` - module name (`Module == Module.__info__(:module)`)
|
||||
|
||||
In addition to the above, you may also pass to `__info__/1` any atom supported
|
||||
by `:erlang.module_info/0` which also gets defined for each compiled module.
|
||||
|
||||
For a list of supported attributes and more information, see [Modules – Erlang Reference Manual](http://www.erlang.org/doc/reference_manual/modules.html#id77056).
|
||||
by Erlang's `module_info` function which also gets defined for each compiled
|
||||
module. See http://erlang.org/doc/reference_manual/modules.html#id69430 for
|
||||
more information.
|
||||
"""
|
||||
def __info__(kind)
|
||||
|
||||
@doc """
|
||||
Checks if a module is open, i.e. it is currently being defined
|
||||
Check if a module is open, i.e. it is currently being defined
|
||||
and its attributes and functions can be modified.
|
||||
"""
|
||||
def open?(module) do
|
||||
@@ -381,7 +332,7 @@ defmodule Module do
|
||||
|
||||
Foo.sum(1, 2) #=> 3
|
||||
|
||||
For convenience, you can pass `__ENV__` as an argument and
|
||||
For convenience, you can my pass `__ENV__` as argument and
|
||||
all options will be automatically extracted from the environment:
|
||||
|
||||
defmodule Foo do
|
||||
@@ -430,13 +381,13 @@ defmodule Module do
|
||||
|
||||
## Differences from `defmodule`
|
||||
|
||||
`Module.create/3` works similarly to `defmodule` and
|
||||
`Module.create` works similarly to `defmodule` and
|
||||
return the same results. While one could also use
|
||||
`defmodule` to define modules dynamically, this
|
||||
function is preferred when the module body is given
|
||||
by a quoted expression.
|
||||
|
||||
Another important distinction is that `Module.create/3`
|
||||
Another important distinction is that `Module.create`
|
||||
allows you to control the environment variables used
|
||||
when defining the module, while `defmodule` automatically
|
||||
shares the same environment.
|
||||
@@ -489,15 +440,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`.
|
||||
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])
|
||||
@@ -510,15 +460,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`.
|
||||
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)
|
||||
@@ -531,10 +480,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.
|
||||
@@ -558,7 +506,9 @@ defmodule Module do
|
||||
assert_not_compiled!(:add_doc, module)
|
||||
table = data_table_for(module)
|
||||
|
||||
signature = simplify_signature(signature)
|
||||
{signature, _} = :lists.mapfoldl fn(x, acc) ->
|
||||
{simplify_signature(x, acc), acc + 1}
|
||||
end, 1, signature
|
||||
|
||||
case :ets.lookup(table, {:doc, tuple}) do
|
||||
[] ->
|
||||
@@ -578,84 +528,47 @@ defmodule Module do
|
||||
|
||||
# Simplify signatures to be stored in docs
|
||||
|
||||
defp simplify_signature(signature) do
|
||||
{signature, acc} = :lists.mapfoldl(&simplify_signature/2, [], signature)
|
||||
{signature, _} = :lists.mapfoldl(&expand_signature/2, {acc, acc}, signature)
|
||||
signature
|
||||
defp simplify_signature({:\\, _, [left, right ]}, i) do
|
||||
{:\\, [], [simplify_signature(left, i), right]}
|
||||
end
|
||||
|
||||
defp simplify_signature({:\\, _, [left, right ]}, acc) do
|
||||
{left, acc} = simplify_signature(left, acc)
|
||||
{{:\\, [], [left, right]}, acc}
|
||||
defp simplify_signature({:%, _, [left, _]}, _i) when is_atom(left) do
|
||||
last = List.last(String.split(Atom.to_string(left), "."))
|
||||
atom = String.to_atom(downcase(last))
|
||||
{atom, [], nil}
|
||||
end
|
||||
|
||||
defp simplify_signature({:=, _, [_, right]}, acc) do
|
||||
simplify_signature(right, acc)
|
||||
defp simplify_signature({:=, _, [_, right]}, i) do
|
||||
simplify_signature(right, i)
|
||||
end
|
||||
|
||||
defp simplify_signature({var, _, atom}, acc) when is_atom(atom) do
|
||||
defp simplify_signature({var, _, atom}, _i) when is_atom(atom) do
|
||||
case Atom.to_string(var) do
|
||||
"_" <> rest -> {{String.to_atom(rest), [], Elixir}, acc}
|
||||
_ -> {{var, [], nil}, acc}
|
||||
"_" <> rest -> {String.to_atom(rest), [], Elixir}
|
||||
_ -> {var, [], nil}
|
||||
end
|
||||
end
|
||||
|
||||
defp simplify_signature({:%, _, [left, _]}, acc) when is_atom(left) do
|
||||
struct_name = String.to_atom(camelcase_to_underscore(List.last(split(left))))
|
||||
autogenerated(acc, struct_name)
|
||||
defp simplify_signature(other, i) when is_integer(other), do: {:"int#{i}", [], Elixir}
|
||||
defp simplify_signature(other, i) when is_boolean(other), do: {:"bool#{i}", [], Elixir}
|
||||
defp simplify_signature(other, i) when is_atom(other), do: {:"atom#{i}", [], Elixir}
|
||||
defp simplify_signature(other, i) when is_list(other), do: {:"list#{i}", [], Elixir}
|
||||
defp simplify_signature(other, i) when is_float(other), do: {:"float#{i}", [], Elixir}
|
||||
defp simplify_signature(other, i) when is_binary(other), do: {:"binary#{i}", [], Elixir}
|
||||
defp simplify_signature(_, i), do: {:"arg#{i}", [], Elixir}
|
||||
|
||||
defp downcase(<<c :: utf8, rest :: binary>>) when c >= ?A and c <= ?Z do
|
||||
<<c + 32 :: utf8, downcase(rest) :: binary>>
|
||||
end
|
||||
|
||||
defp simplify_signature({:%{}, _, _}, acc) do
|
||||
autogenerated(acc, :map)
|
||||
defp downcase(<<c, rest :: binary>>) do
|
||||
<<c, downcase(rest) :: binary>>
|
||||
end
|
||||
|
||||
defp simplify_signature(other, acc) when is_integer(other), do: autogenerated(acc, :int)
|
||||
defp simplify_signature(other, acc) when is_boolean(other), do: autogenerated(acc, :bool)
|
||||
defp simplify_signature(other, acc) when is_atom(other), do: autogenerated(acc, :atom)
|
||||
defp simplify_signature(other, acc) when is_list(other), do: autogenerated(acc, :list)
|
||||
defp simplify_signature(other, acc) when is_float(other), do: autogenerated(acc, :float)
|
||||
defp simplify_signature(other, acc) when is_binary(other), do: autogenerated(acc, :binary)
|
||||
defp simplify_signature(_, acc), do: autogenerated(acc, :arg)
|
||||
|
||||
defp autogenerated(acc, key) do
|
||||
{key, [key|acc]}
|
||||
defp downcase(<<>>) do
|
||||
<<>>
|
||||
end
|
||||
|
||||
defp expand_signature(key, {all_keys, acc}) when is_atom(key) do
|
||||
case previous_values(key, all_keys, acc) do
|
||||
{i, acc} -> {{:"#{key}#{i}", [], Elixir}, {all_keys, acc}}
|
||||
:none -> {{key, [], Elixir}, {all_keys, acc}}
|
||||
end
|
||||
end
|
||||
|
||||
defp expand_signature(term, {_, _} = acc) do
|
||||
{term, acc}
|
||||
end
|
||||
|
||||
defp previous_values(key, all_keys, acc) do
|
||||
total_occurrences = occurrences(key, all_keys)
|
||||
|
||||
if total_occurrences == 1 do
|
||||
:none
|
||||
else
|
||||
index = total_occurrences - occurrences(key, acc) + 1
|
||||
{index, :lists.delete(key, acc)}
|
||||
end
|
||||
end
|
||||
|
||||
defp occurrences(key, list) 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 do_camelcase_to_underscore(<<>>, acc),
|
||||
do: acc
|
||||
|
||||
# Merge
|
||||
|
||||
defp merge_signatures([h1|t1], [h2|t2], i) do
|
||||
@@ -686,7 +599,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
|
||||
@@ -706,9 +618,8 @@ defmodule Module do
|
||||
|
||||
@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
|
||||
|
||||
@@ -729,13 +640,13 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns all functions defined in `module`.
|
||||
Return all functions defined in `module`.
|
||||
|
||||
## Examples
|
||||
|
||||
defmodule Example do
|
||||
def version, do: 1
|
||||
Module.definitions_in __MODULE__ #=> [{:version, 0}]
|
||||
Module.definitions_in __MODULE__ #=> [{:version,0}]
|
||||
end
|
||||
|
||||
"""
|
||||
@@ -753,7 +664,7 @@ defmodule Module do
|
||||
|
||||
defmodule Example do
|
||||
def version, do: 1
|
||||
Module.definitions_in __MODULE__, :def #=> [{:version, 0}]
|
||||
Module.definitions_in __MODULE__, :def #=> [{:version,0}]
|
||||
Module.definitions_in __MODULE__, :defp #=> []
|
||||
end
|
||||
|
||||
@@ -766,7 +677,6 @@ defmodule Module do
|
||||
|
||||
@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.
|
||||
@@ -774,43 +684,39 @@ defmodule Module do
|
||||
def make_overridable(module, tuples) do
|
||||
assert_not_compiled!(:make_overridable, module)
|
||||
|
||||
:lists.foreach(fn tuple ->
|
||||
for tuple <- tuples 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"
|
||||
raise "Cannot make function #{name}/#{arity} overridable because it was not defined"
|
||||
clause ->
|
||||
:elixir_def.delete_definition(module, tuple)
|
||||
|
||||
neighbours = if :elixir_compiler.get_opt(:internal) do
|
||||
[]
|
||||
else
|
||||
neighbours = if loaded?(Module.LocalsTracker) do
|
||||
Module.LocalsTracker.yank(module, tuple)
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
||||
old = :elixir_def_overridable.overridable(module)
|
||||
count = case :maps.find(tuple, old) do
|
||||
{:ok, {count, _, _, _}} -> count + 1
|
||||
:error -> 1
|
||||
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
|
||||
end
|
||||
|
||||
@doc """
|
||||
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
|
||||
@@ -820,17 +726,11 @@ 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)
|
||||
value = normalize_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
|
||||
@@ -846,12 +746,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:
|
||||
@@ -945,7 +842,7 @@ defmodule Module do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Splits the given module name into binary parts.
|
||||
Split the given module name into binary parts.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -953,12 +850,8 @@ defmodule Module do
|
||||
#=> ["Very", "Long", "Module", "Name", "And", "Even", "Longer"]
|
||||
|
||||
"""
|
||||
def split(module) when is_atom(module) do
|
||||
split(String.Chars.to_string(module))
|
||||
end
|
||||
|
||||
def split("Elixir." <> name) do
|
||||
String.split(name, ".")
|
||||
def split(module) do
|
||||
tl(String.split(String.Chars.to_string(module), "."))
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -966,22 +859,10 @@ defmodule Module do
|
||||
# is private and must be used only internally.
|
||||
def compile_doc(env, kind, name, args, _guards, _body) do
|
||||
module = env.module
|
||||
table = data_table_for(module)
|
||||
line = env.line
|
||||
arity = length(args)
|
||||
pair = {name, arity}
|
||||
|
||||
{line, doc} = get_doc_info(table, env)
|
||||
|
||||
# Arguments are not expanded for the docs, but we make an exception for
|
||||
# module attributes and for structs (aliases to be precise).
|
||||
args = Macro.prewalk args, fn
|
||||
{:@, _, _} = attr ->
|
||||
Macro.expand_once(attr, env)
|
||||
{:%, meta, [aliases, fields]} ->
|
||||
{:%, meta, [Macro.expand_once(aliases, env), fields]}
|
||||
x ->
|
||||
x
|
||||
end
|
||||
doc = get_attribute(module, :doc)
|
||||
|
||||
case add_doc(module, line, kind, pair, args, doc) do
|
||||
:ok ->
|
||||
@@ -992,7 +873,7 @@ defmodule Module do
|
||||
"@doc's are always discarded for private functions"
|
||||
end
|
||||
|
||||
:ok
|
||||
delete_attribute(module, :doc)
|
||||
end
|
||||
|
||||
@doc false
|
||||
@@ -1012,22 +893,21 @@ 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}] ->
|
||||
postprocess_attribute(key, val)
|
||||
[{^key, val}] -> val
|
||||
[] ->
|
||||
acc = :ets.lookup_element(table, {:elixir, :acc_attributes}, 2)
|
||||
|
||||
cond do
|
||||
:lists.member(key, acc) ->
|
||||
[]
|
||||
is_list(stack) ->
|
||||
:elixir_errors.warn warn_info(stack), "undefined module attribute @#{key}, " <>
|
||||
"please remove access to @#{key} or explicitly set it before access"
|
||||
is_list(warn) ->
|
||||
:elixir_errors.warn warn_info(warn), "undefined module attribute @#{key}, " <>
|
||||
"please remove access to @#{key} or explicitly set it to nil before access"
|
||||
nil
|
||||
true ->
|
||||
nil
|
||||
@@ -1046,48 +926,34 @@ defmodule Module do
|
||||
|
||||
## Helpers
|
||||
|
||||
defp preprocess_attribute(:on_load, atom) when is_atom(atom) do
|
||||
defp normalize_attribute(:on_load, atom) when is_atom(atom) do
|
||||
{atom, 0}
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:behaviour, atom) when is_atom(atom) do
|
||||
defp normalize_attribute(:behaviour, atom) when is_atom(atom) do
|
||||
# Attempt to compile behaviour but ignore failure (will warn later)
|
||||
_ = Code.ensure_compiled(atom)
|
||||
atom
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:file, file) when is_binary(file) do
|
||||
defp normalize_attribute(:file, file) when is_binary(file) do
|
||||
file
|
||||
end
|
||||
|
||||
defp preprocess_attribute(:before_compile, atom) when is_atom(atom),
|
||||
do: {atom, :__before_compile__}
|
||||
defp preprocess_attribute(:after_compile, atom) when is_atom(atom),
|
||||
do: {atom, :__after_compile__}
|
||||
defp preprocess_attribute(:on_definition, atom) when is_atom(atom),
|
||||
do: {atom, :__on_definition__}
|
||||
defp normalize_attribute(key, atom) when is_atom(atom) and
|
||||
key in [:before_compile, :after_compile, :on_definition] do
|
||||
{atom, :"__#{key}__"}
|
||||
end
|
||||
|
||||
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 " <>
|
||||
defp normalize_attribute(key, _value) when key in [:type, :typep, :export_type, :opaque, :callback] do
|
||||
raise ArgumentError, "attributes type, typep, export_type, opaque and callback " <>
|
||||
"must be set via Kernel.Typespec"
|
||||
end
|
||||
|
||||
defp preprocess_attribute(_key, value) do
|
||||
defp normalize_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}
|
||||
end
|
||||
end
|
||||
|
||||
defp data_table_for(module) do
|
||||
:elixir_module.data_table(module)
|
||||
end
|
||||
@@ -1102,16 +968,5 @@ defmodule Module do
|
||||
"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 ->
|
||||
:elixir_errors.warn warn_info(stack),
|
||||
"redefining @#{key} attribute previously set at line #{line}"
|
||||
_ ->
|
||||
false
|
||||
end
|
||||
end
|
||||
|
||||
defp warn_if_redefining_doc_attribute(nil, _table, _key), do: false
|
||||
defp loaded?(module), do: is_tuple :code.is_loaded(module)
|
||||
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
|
||||
@@ -30,7 +30,7 @@
|
||||
# * out neighbours: `Module`
|
||||
#
|
||||
# Note that since this is required for bootstrap, we can't use
|
||||
# any of the `GenServer` conveniences.
|
||||
# any of the `GenServer.Behaviour` conveniences.
|
||||
defmodule Module.LocalsTracker do
|
||||
@moduledoc false
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -267,6 +267,10 @@ defmodule Module.LocalsTracker do
|
||||
{:reply, d, state}
|
||||
end
|
||||
|
||||
def handle_call(request, _from, state) do
|
||||
{:stop, {:bad_call, request}, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def handle_info(_msg, state) do
|
||||
{:noreply, state}
|
||||
@@ -313,6 +317,10 @@ defmodule Module.LocalsTracker do
|
||||
{:stop, :normal, state}
|
||||
end
|
||||
|
||||
def handle_cast(msg, state) do
|
||||
{:stop, {:bad_cast, msg}, state}
|
||||
end
|
||||
|
||||
@doc false
|
||||
def terminate(_reason, _state) do
|
||||
:ok
|
||||
|
||||
+17
-25
@@ -72,8 +72,7 @@ defmodule Node do
|
||||
The result returned when the argument is a list, is the list of nodes
|
||||
satisfying the disjunction(s) of the list elements.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.nodes/1`](http://www.erlang.org/doc/man/erlang.html#nodes-1).
|
||||
See http://www.erlang.org/doc/man/erlang.html#nodes-1 for more info.
|
||||
"""
|
||||
@typep state :: :visible | :hidden | :connected | :this | :known
|
||||
@spec list(state | [state]) :: [t]
|
||||
@@ -87,8 +86,7 @@ defmodule Node do
|
||||
If `flag` is `true`, monitoring is turned on.
|
||||
If `flag` is `false`, monitoring is turned off.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.monitor_node/2`](http://www.erlang.org/doc/man/erlang.html#monitor_node-2).
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor_node-2 for more info.
|
||||
"""
|
||||
@spec monitor(t, boolean) :: true
|
||||
def monitor(node, flag) do
|
||||
@@ -99,8 +97,7 @@ defmodule Node do
|
||||
Behaves as `monitor/2` except that it allows an extra
|
||||
option to be given, namely `:allow_passive_connect`.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.monitor_node/3`](http://www.erlang.org/doc/man/erlang.html#monitor_node-3).
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor_node-3 for more info.
|
||||
"""
|
||||
@spec monitor(t, boolean, [:allow_passive_connect]) :: true
|
||||
def monitor(node, flag, options) do
|
||||
@@ -131,8 +128,7 @@ defmodule Node do
|
||||
protocols. Returns `true` if disconnection succeeds, otherwise `false`.
|
||||
If the local node is not alive, the function returns `:ignored`.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.disconnect_node/1`](http://www.erlang.org/doc/man/erlang.html#disconnect_node-1).
|
||||
See http://www.erlang.org/doc/man/erlang.html#disconnect_node-1 for more info.
|
||||
"""
|
||||
@spec disconnect(t) :: boolean | :ignored
|
||||
def disconnect(node) do
|
||||
@@ -145,8 +141,7 @@ defmodule Node do
|
||||
Returns `true` if successful, `false` if not, and the atom
|
||||
`:ignored` if the local node is not alive.
|
||||
|
||||
For more information, see
|
||||
[`:erlang.connect_node/1`](http://www.erlang.org/doc/man/net_kernel.html#connect_node-1).
|
||||
See http://erlang.org/doc/man/net_kernel.html#connect_node-1 for more info.
|
||||
"""
|
||||
@spec connect(t) :: boolean | :ignored
|
||||
def connect(node) do
|
||||
@@ -157,8 +152,8 @@ defmodule Node do
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
on `node`. If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/2`](http://www.erlang.org/doc/man/erlang.html#spawn-2).
|
||||
Check http://www.erlang.org/doc/man/erlang.html#spawn-2 for
|
||||
the list of available options.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -171,10 +166,9 @@ defmodule Node do
|
||||
Returns the pid of a new process started by the application of `fun`
|
||||
on `node`.
|
||||
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn_opt/3`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-3).
|
||||
If `node` does not exist, a useless pid is returned. Check
|
||||
http://www.erlang.org/doc/man/erlang.html#spawn_opt-3 for the list of
|
||||
available options.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -187,10 +181,9 @@ defmodule Node do
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/4`](http://www.erlang.org/doc/man/erlang.html#spawn-4).
|
||||
If `node` does not exist, a useless pid is returned. Check
|
||||
http://www.erlang.org/doc/man/erlang.html#spawn-4 for the list of
|
||||
available options.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -203,10 +196,9 @@ defmodule Node do
|
||||
Returns the pid of a new process started by the application of
|
||||
`module.function(args)` on `node`.
|
||||
|
||||
If `node` does not exist, a useless pid is returned.
|
||||
|
||||
For the list of available options, see
|
||||
[`:erlang.spawn/5`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-5).
|
||||
If `node` does not exist, a useless pid is returned. Check
|
||||
http://www.erlang.org/doc/man/erlang.html#spawn_opt-5 for the list of
|
||||
available options.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -247,7 +239,7 @@ defmodule Node do
|
||||
@doc """
|
||||
Sets the magic cookie of `node` to the atom `cookie`.
|
||||
|
||||
The default node is `Node.self/0`, the local node. If `node` is the local node,
|
||||
The default node is `Node.self`, the local node. If `node` is the local node,
|
||||
the function also sets the cookie of all other unknown nodes to `cookie`.
|
||||
|
||||
This function will raise `FunctionClauseError` if the given `node` is not alive.
|
||||
|
||||
@@ -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]
|
||||
@@ -11,11 +11,8 @@ defmodule OptionParser do
|
||||
@doc """
|
||||
Parses `argv` into a keywords list.
|
||||
|
||||
It returns a three-element tuple as follows:
|
||||
|
||||
1. parsed switches,
|
||||
2. remaining arguments,
|
||||
3. invalid options.
|
||||
It returns the parsed values, remaining arguments and the
|
||||
invalid options.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -28,48 +25,47 @@ defmodule OptionParser do
|
||||
iex> OptionParser.parse(["--source-path", "lib", "test/enum_test.exs", "--verbose"])
|
||||
{[source_path: "lib", verbose: true], ["test/enum_test.exs"], []}
|
||||
|
||||
By default, Elixir will try to automatically parse all switches.
|
||||
Switches followed by a value will be assigned the value, as a string.
|
||||
By default, Elixir will try to automatically parse switches.
|
||||
Switches without an argument, like `--debug` will automatically
|
||||
be set to `true`.
|
||||
be set to true. Switches followed by a value will be assigned
|
||||
to the value, always as strings.
|
||||
|
||||
Note: Elixir also converts the switches to underscore atoms, so
|
||||
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 put in the invalid options list.
|
||||
underscores; such options will be reported as `:undefined` (in strict mode)
|
||||
or `:invalid` (in basic mode).
|
||||
|
||||
## Switch Definitions
|
||||
## Switches
|
||||
|
||||
Often it is better to explicitly list the known
|
||||
Many times though, it is better to explicitly list the available
|
||||
switches and their formats. The switches can be specified via two
|
||||
alternative options:
|
||||
different options:
|
||||
|
||||
* `:switches` - defines some switches. An attempt is still made to parse
|
||||
switches that do not appear in the list.
|
||||
* `:strict` - the switches are strict. Any switch that does not
|
||||
exist in the switch list is treated as an error.
|
||||
|
||||
* `:strict` - the switches are strict. Any switch that is not specified
|
||||
in the list is returned in the invalid options list.
|
||||
* `:switches` - defines some switches. Switches that does not
|
||||
exist in the switch list are still attempted to be parsed.
|
||||
|
||||
Note that you should only supply the `:switches` or `:strict` option. If you
|
||||
supply both, an error will be raised.
|
||||
Note only `:strict` or `:switches` may be given at once.
|
||||
|
||||
For each switch, the following types are supported:
|
||||
|
||||
* `:boolean` - marks the given switch as a boolean. Boolean switches
|
||||
never consume the following value unless it is `true` or
|
||||
`false`.
|
||||
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.
|
||||
|
||||
If a switch can't be parsed, it is returned in the invalid options list.
|
||||
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).
|
||||
|
||||
The following extra "types" are supported:
|
||||
|
||||
* `:keep` - keeps duplicated items in the list instead of overriding them.
|
||||
|
||||
Note: if you want to use `:keep` with a non-string type, use a list, e.g.
|
||||
`[foo: [:integer, :keep]]`.
|
||||
* `:keep` - keeps duplicated items in the list instead of overriding
|
||||
|
||||
Examples:
|
||||
|
||||
@@ -93,13 +89,10 @@ defmodule OptionParser do
|
||||
...> switches: [limit: :integer])
|
||||
{[limit: 3, unknown: "xyz"], [], []}
|
||||
|
||||
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`:
|
||||
which will set the option to false:
|
||||
|
||||
iex> OptionParser.parse(["--no-op", "path/to/file"], switches: [op: :boolean])
|
||||
{[op: false], ["path/to/file"], []}
|
||||
@@ -185,7 +178,7 @@ defmodule OptionParser do
|
||||
command line)
|
||||
|
||||
* `{:undefined, key, value, rest}` - the option `key` is undefined
|
||||
(returned in strict mode when the switch is unknown)
|
||||
(returned on strict cases and the switch is unknown)
|
||||
|
||||
* `{:error, rest}` - there are no switches at the top of the given argv
|
||||
"""
|
||||
@@ -239,7 +232,7 @@ 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,
|
||||
boolean values are converted to `--key` or `--no-key`
|
||||
@@ -284,24 +277,24 @@ defmodule OptionParser do
|
||||
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),
|
||||
# If we have a escaped quote, simply remove the escape
|
||||
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, ?', ?"],
|
||||
# If we have a 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, ?', ?"],
|
||||
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),
|
||||
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
|
||||
@@ -330,8 +323,6 @@ defmodule OptionParser 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] ->
|
||||
@@ -383,7 +374,7 @@ defmodule OptionParser do
|
||||
end
|
||||
end
|
||||
|
||||
defp tag_option(<<?-, option::binary>>, switches, _aliases) do
|
||||
defp tag_option(<<?-, option :: binary>>, switches, _aliases) do
|
||||
get_negated(option, switches)
|
||||
end
|
||||
|
||||
|
||||
+47
-44
@@ -73,7 +73,7 @@ defmodule Path do
|
||||
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.
|
||||
@@ -96,13 +96,13 @@ 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:
|
||||
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:
|
||||
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))
|
||||
@@ -112,7 +112,7 @@ defmodule Path 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:
|
||||
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:
|
||||
@@ -225,7 +225,7 @@ defmodule Path do
|
||||
end
|
||||
end
|
||||
|
||||
defp unix_pathtype(<<?/, relative::binary>>), do:
|
||||
defp unix_pathtype(<<?/, relative :: binary>>), do:
|
||||
{:absolute, relative}
|
||||
defp unix_pathtype([?/|relative]), do:
|
||||
{:absolute, relative}
|
||||
@@ -240,13 +240,13 @@ defmodule Path 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(<<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:
|
||||
@@ -267,7 +267,7 @@ defmodule Path do
|
||||
In other words, it tries to strip the `from` prefix from `path`.
|
||||
|
||||
This function does not query the file system, so it assumes
|
||||
no symlinks between the paths.
|
||||
no symlinks in between the paths.
|
||||
|
||||
In case a direct relative path cannot be found, it returns
|
||||
the original path.
|
||||
@@ -494,14 +494,14 @@ defmodule Path do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Path.split("")
|
||||
[]
|
||||
iex> Path.split("")
|
||||
[]
|
||||
|
||||
iex> Path.split("foo")
|
||||
["foo"]
|
||||
iex> Path.split("foo")
|
||||
["foo"]
|
||||
|
||||
iex> Path.split("/foo/bar")
|
||||
["/", "foo", "bar"]
|
||||
iex> Path.split("/foo/bar")
|
||||
["/", "foo", "bar"]
|
||||
|
||||
"""
|
||||
@spec split(t) :: [binary]
|
||||
@@ -545,8 +545,7 @@ defmodule Path do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Traverses paths according to the given `glob` expression, and returns a
|
||||
list of matches.
|
||||
Traverses paths according to the given `glob` expression.
|
||||
|
||||
The wildcard looks like an ordinary path, except that certain
|
||||
"wildcard characters" are interpreted in a special way. The
|
||||
@@ -560,17 +559,17 @@ 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
|
||||
* `[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
|
||||
* `{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
|
||||
that matching is case-sensitive; i.e. "a" will not match "A".
|
||||
|
||||
By default, the patterns `*` and `?` do not match files starting
|
||||
with a dot `.` unless `match_dot: true` is given in `opts`.
|
||||
with a dot `.` unless `match_dot: true` is given.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -585,7 +584,7 @@ defmodule Path do
|
||||
Path.wildcard("projects/*/ebin/**/*.{beam,app}")
|
||||
|
||||
"""
|
||||
@spec wildcard(t, Keyword.t) :: [binary]
|
||||
@spec wildcard(t) :: [binary]
|
||||
def wildcard(glob, opts \\ []) do
|
||||
mod = if Keyword.get(opts, :match_dot), do: :file, else: Path.Wildcard
|
||||
glob
|
||||
@@ -628,28 +627,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)
|
||||
|
||||
+14
-75
@@ -3,145 +3,84 @@ defmodule Port do
|
||||
Functions related to Erlang ports.
|
||||
"""
|
||||
|
||||
@type name :: {:spawn, char_list | binary} |
|
||||
{:spawn_driver, char_list | binary} |
|
||||
{:spawn_executable, char_list | 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 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 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 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 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 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 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
|
||||
|
||||
@doc """
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_info-1.
|
||||
|
||||
For more information, see [`:erlang.port_info/1`](http://www.erlang.org/doc/man/erlang.html#port_info-1).
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
def info(port) do
|
||||
nillify :erlang.port_info(port)
|
||||
:erlang.port_info(port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns information about the `port`
|
||||
or `nil` if the port is closed.
|
||||
See http://www.erlang.org/doc/man/erlang.html#port_info-2.
|
||||
|
||||
For more information, see [`:erlang.port_info/2`](http://www.erlang.org/doc/man/erlang.html#port_info-2).
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec info(port, atom) :: {atom, term} | nil
|
||||
def info(port, spec)
|
||||
|
||||
def info(port, :registered_name) do
|
||||
case :erlang.port_info(port, :registered_name) do
|
||||
[] -> {:registered_name, []}
|
||||
other -> nillify(other)
|
||||
end
|
||||
end
|
||||
|
||||
def info(port, item) do
|
||||
nillify :erlang.port_info(port, item)
|
||||
:erlang.port_info(port, item)
|
||||
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 http://www.erlang.org/doc/man/erlang.html#ports-0.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec list :: [port]
|
||||
def list do
|
||||
:erlang.ports
|
||||
end
|
||||
|
||||
@compile {:inline, nillify: 1}
|
||||
defp nillify(:undefined), do: nil
|
||||
defp nillify(other), do: other
|
||||
end
|
||||
end
|
||||
+25
-100
@@ -15,12 +15,10 @@ defmodule Process do
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns `true` if the process exists and is alive (i.e. it is not exiting
|
||||
and has not exited yet). Otherwise, returns `false`.
|
||||
Returns true if the process exists and is alive, that is,
|
||||
is not exiting and has not exited. 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
|
||||
@@ -29,8 +27,6 @@ defmodule Process do
|
||||
|
||||
@doc """
|
||||
Returns all key-values in the dictionary.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec get :: [{term, term}]
|
||||
def get do
|
||||
@@ -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
|
||||
@@ -73,9 +57,6 @@ defmodule Process do
|
||||
|
||||
@doc """
|
||||
Stores the given key-value 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`).
|
||||
"""
|
||||
@spec put(term, term) :: term | nil
|
||||
def put(key, value) do
|
||||
@@ -100,10 +81,9 @@ defmodule Process do
|
||||
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. 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
|
||||
@@ -139,10 +119,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
|
||||
@@ -156,7 +137,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
|
||||
@@ -168,46 +150,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 milli-seconds
|
||||
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 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 milli-seconds
|
||||
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} |
|
||||
@@ -224,7 +166,7 @@ defmodule Process do
|
||||
just the spawned process pid.
|
||||
|
||||
It also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -243,7 +185,7 @@ defmodule Process do
|
||||
just the spawned process pid.
|
||||
|
||||
It also accepts extra options, for the list of available options
|
||||
check [`:erlang.spawn_opt/4`](http://www.erlang.org/doc/man/erlang.html#spawn_opt-4).
|
||||
check http://www.erlang.org/doc/man/erlang.html#spawn_opt-4
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -256,9 +198,7 @@ defmodule Process do
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#monitor-2 for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -272,7 +212,7 @@ defmodule Process do
|
||||
obtained by calling `monitor/1`, this monitoring is turned off.
|
||||
If the monitoring is already turned off, nothing happens.
|
||||
|
||||
See [`:erlang.demonitor/2`](http://www.erlang.org/doc/man/erlang.html#demonitor-2) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#demonitor-2 for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -290,7 +230,7 @@ defmodule Process do
|
||||
`alive?/1` will return `false` for a process that is exiting,
|
||||
but its process identifier will be part of the result returned.
|
||||
|
||||
See [`:erlang.processes/0`](http://www.erlang.org/doc/man/erlang.html#processes-0) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#processes-0 for more info.
|
||||
"""
|
||||
@spec list :: [pid]
|
||||
def list do
|
||||
@@ -301,7 +241,7 @@ defmodule Process do
|
||||
Creates a link between the calling process and another process
|
||||
(or port) `pid`, if there is not such a link already.
|
||||
|
||||
See [`:erlang.link/1`](http://www.erlang.org/doc/man/erlang.html#link-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#link-1 for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -315,7 +255,7 @@ defmodule Process do
|
||||
the process or port referred to by `pid`. Returns `true` and does not
|
||||
fail, even if there is no link or `id` does not exist
|
||||
|
||||
See [`:erlang.unlink/1`](http://www.erlang.org/doc/man/erlang.html#unlink-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#unlink-1 for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@@ -341,7 +281,7 @@ defmodule Process do
|
||||
@doc """
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#unregister-1 for more info.
|
||||
"""
|
||||
@spec unregister(atom) :: true
|
||||
def unregister(name) do
|
||||
@@ -352,7 +292,7 @@ defmodule Process do
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#whereis-1 for more info.
|
||||
"""
|
||||
@spec whereis(atom) :: pid | port | nil
|
||||
def whereis(name) do
|
||||
@@ -391,7 +331,7 @@ defmodule Process do
|
||||
Sets certain flags for the process which calls this function.
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_flag-2 for more info.
|
||||
"""
|
||||
@spec flag(process_flag, term) :: term
|
||||
def flag(flag, value) do
|
||||
@@ -403,7 +343,7 @@ defmodule Process do
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_flag-3 for more info.
|
||||
"""
|
||||
@spec flag(pid, :save_calls, non_neg_integer) :: non_neg_integer
|
||||
def flag(pid, flag, value) do
|
||||
@@ -415,7 +355,7 @@ defmodule Process do
|
||||
is not alive.
|
||||
Use this only for debugging information.
|
||||
|
||||
See [`:erlang.process_info/1`](http://www.erlang.org/doc/man/erlang.html#process_info-1) for more info.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_info-1 for more info.
|
||||
"""
|
||||
@spec info(pid) :: Keyword.t
|
||||
def info(pid) do
|
||||
@@ -426,9 +366,9 @@ defmodule Process do
|
||||
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.
|
||||
See http://www.erlang.org/doc/man/erlang.html#process_info-2 for more info.
|
||||
"""
|
||||
@spec info(pid, atom | [atom]) :: {atom, term} | [{atom, term}] | nil
|
||||
@spec info(pid, atom) :: {atom, term} | nil
|
||||
def info(pid, spec)
|
||||
|
||||
def info(pid, :registered_name) do
|
||||
@@ -439,25 +379,10 @@ defmodule Process do
|
||||
end
|
||||
end
|
||||
|
||||
def info(pid, spec) when is_atom(spec) or is_list(spec) do
|
||||
def info(pid, spec) when is_atom(spec) do
|
||||
nillify :erlang.process_info(pid, spec)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Puts the calling process into a wait state
|
||||
where its memory allocation has been reduced as much as possible,
|
||||
which is useful if the process does not expect to receive any messages
|
||||
in the near future.
|
||||
|
||||
See [`:erlang.hibernate/3`](http://www.erlang.org/doc/man/erlang.html#hibernate-3) for more info.
|
||||
|
||||
Inlined by the compiler.
|
||||
"""
|
||||
@spec hibernate(module, atom, list) :: no_return
|
||||
def hibernate(mod, fun, args) do
|
||||
:erlang.hibernate(mod, fun, args)
|
||||
end
|
||||
|
||||
@compile {:inline, nillify: 1}
|
||||
defp nillify(:undefined), do: nil
|
||||
defp nillify(other), do: other
|
||||
|
||||
+55
-71
@@ -19,12 +19,11 @@ defmodule Protocol do
|
||||
defmacro def({name, _, args}) when is_atom(name) and is_list(args) do
|
||||
arity = length(args)
|
||||
|
||||
type_args = :lists.map(fn _ -> quote(do: term) end,
|
||||
:lists.seq(2, arity))
|
||||
type_args = for _ <- :lists.seq(2, arity), do: quote(do: term)
|
||||
type_args = [quote(do: t) | type_args]
|
||||
|
||||
call_args = :lists.map(fn i -> {String.to_atom(<<?x, i + 64>>), [], __MODULE__} end,
|
||||
:lists.seq(2, arity))
|
||||
call_args = for i <- :lists.seq(2, arity),
|
||||
do: {String.to_atom(<<?x, i + 64>>), [], __MODULE__}
|
||||
call_args = [quote(do: t) | call_args]
|
||||
|
||||
quote do
|
||||
@@ -70,7 +69,7 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
try do
|
||||
module.__protocol__(:module)
|
||||
module.__protocol__(:name)
|
||||
rescue
|
||||
UndefinedFunctionError ->
|
||||
raise ArgumentError, "#{inspect module} is not a protocol" <> extra
|
||||
@@ -115,7 +114,7 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Derives the `protocol` for `module` with the given options.
|
||||
Derive the `protocol` for `module` with the given options.
|
||||
"""
|
||||
defmacro derive(protocol, module, options \\ []) do
|
||||
quote do
|
||||
@@ -127,7 +126,7 @@ defmodule Protocol do
|
||||
## Consolidation
|
||||
|
||||
@doc """
|
||||
Extracts all protocols from the given paths.
|
||||
Extract all protocols from the given paths.
|
||||
|
||||
The paths can be either a char list or a string. Internally
|
||||
they are worked on as char lists, so passing them as lists
|
||||
@@ -149,14 +148,14 @@ defmodule Protocol do
|
||||
extract_matching_by_attribute paths, 'Elixir.',
|
||||
fn module, attributes ->
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: _] -> module
|
||||
[fallback_to_any: _, consolidated: _] -> module
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Extracts all types implemented for the given protocol from
|
||||
Extract all types implemented for the given protocol from
|
||||
the given paths.
|
||||
|
||||
The paths can be either a char list or a string. Internally
|
||||
@@ -217,12 +216,21 @@ 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.
|
||||
Returns true if the protocol was consolidated.
|
||||
"""
|
||||
@spec consolidated?(module) :: boolean
|
||||
def consolidated?(protocol) do
|
||||
protocol.__protocol__(:consolidated?)
|
||||
protocol.__info__(:attributes)[:protocol][:consolidated]
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -241,7 +249,7 @@ defmodule Protocol do
|
||||
|
||||
Protocol.consolidated?(Enumerable)
|
||||
|
||||
If the first element of the tuple is `true`, it means
|
||||
If the first element of the tuple is true, it means
|
||||
the protocol was consolidated.
|
||||
|
||||
This function does not load the protocol at any point
|
||||
@@ -254,9 +262,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'
|
||||
@@ -269,7 +277,7 @@ defmodule Protocol do
|
||||
{:attributes, attributes},
|
||||
{@docs_chunk, docs}]}} ->
|
||||
case attributes[:protocol] do
|
||||
[fallback_to_any: any] ->
|
||||
[fallback_to_any: any, consolidated: _] ->
|
||||
{:ok, {protocol, any, abstract_code, docs}}
|
||||
_ ->
|
||||
{:error, :not_a_protocol}
|
||||
@@ -293,25 +301,19 @@ defmodule Protocol do
|
||||
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
|
||||
clauses = :lists.map(fn
|
||||
{:clause, l, [{:atom, _, :consolidated?}], [], [{:atom, _, _}]} ->
|
||||
{:clause, l, [{:atom, 0, :consolidated?}], [], [{:atom, 0, true}]}
|
||||
{:clause, _, _, _, _} = c ->
|
||||
c
|
||||
end, clauses)
|
||||
|
||||
defp change_impl_for([{:attribute, line, :protocol, opts}|t], protocol, types, structs, _, acc) do
|
||||
opts = [fallback_to_any: opts[:fallback_to_any], consolidated: true]
|
||||
change_impl_for(t, protocol, types, structs, true,
|
||||
[{:function, line, :__protocol__, 1, clauses}|acc])
|
||||
[{:attribute, line, :protocol, opts}|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)
|
||||
fallback = if Any in types, do: load_impl(protocol, Any), else: nil
|
||||
|
||||
clauses = for {guard, mod} <- builtin,
|
||||
mod in types,
|
||||
@@ -325,7 +327,7 @@ defmodule Protocol do
|
||||
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)
|
||||
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)]
|
||||
|
||||
@@ -384,7 +386,7 @@ 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])
|
||||
unless docs == :missing_chunk do
|
||||
@@ -416,7 +418,7 @@ defmodule Protocol do
|
||||
@fallback_to_any false
|
||||
|
||||
# Invoke the user given block
|
||||
_ = unquote(block)
|
||||
unquote(block)
|
||||
|
||||
# Finalize expansion
|
||||
unquote(after_defprotocol)
|
||||
@@ -438,7 +440,7 @@ defmodule Protocol do
|
||||
end
|
||||
|
||||
# Define the implementation for builtins.
|
||||
:lists.foreach(fn {guard, mod} ->
|
||||
for {guard, mod} <- builtin do
|
||||
target = Module.concat(__MODULE__, mod)
|
||||
|
||||
Kernel.def impl_for(data) when :erlang.unquote(guard)(data) do
|
||||
@@ -447,7 +449,7 @@ defmodule Protocol do
|
||||
false -> any_impl_for
|
||||
end
|
||||
end
|
||||
end, builtin)
|
||||
end
|
||||
|
||||
@spec impl_for!(term) :: atom() | no_return()
|
||||
Kernel.def impl_for!(data) do
|
||||
@@ -456,7 +458,12 @@ defmodule Protocol do
|
||||
|
||||
# Internal handler for Any
|
||||
if @fallback_to_any do
|
||||
Kernel.defp any_impl_for, do: __MODULE__.Any.__impl__(:target)
|
||||
Kernel.defp any_impl_for do
|
||||
case impl_for?(__MODULE__.Any) do
|
||||
true -> __MODULE__.Any.__impl__(:target)
|
||||
false -> nil
|
||||
end
|
||||
end
|
||||
else
|
||||
Kernel.defp any_impl_for, do: nil
|
||||
end
|
||||
@@ -486,15 +493,13 @@ defmodule Protocol do
|
||||
# Store information as an attribute so it
|
||||
# can be read without loading the module.
|
||||
Module.register_attribute(__MODULE__, :protocol, persist: true)
|
||||
@protocol [fallback_to_any: !!@fallback_to_any]
|
||||
@protocol [fallback_to_any: !!@fallback_to_any, consolidated: false]
|
||||
|
||||
@doc false
|
||||
@spec __protocol__(:module) :: __MODULE__
|
||||
@spec __protocol__(:name) :: __MODULE__
|
||||
@spec __protocol__(:functions) :: unquote(Protocol.__functions_spec__(@functions))
|
||||
@spec __protocol__(:consolidated?) :: boolean
|
||||
Kernel.def __protocol__(:module), do: __MODULE__
|
||||
Kernel.def __protocol__(:name), do: __MODULE__
|
||||
Kernel.def __protocol__(:functions), do: unquote(:lists.sort(@functions))
|
||||
Kernel.def __protocol__(:consolidated?), do: false
|
||||
end
|
||||
end
|
||||
|
||||
@@ -517,7 +522,7 @@ defmodule Protocol do
|
||||
# Unquote the implementation just later
|
||||
# when all variables will already be injected
|
||||
# into the module body.
|
||||
impl =
|
||||
__impl__ =
|
||||
quote unquote: false do
|
||||
@doc false
|
||||
@spec __impl__(:for) :: unquote(for)
|
||||
@@ -533,15 +538,7 @@ defmodule Protocol do
|
||||
for = unquote(for)
|
||||
name = Module.concat(protocol, for)
|
||||
|
||||
# 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)
|
||||
Protocol.__ensure_defimpl__(protocol, for, __ENV__)
|
||||
end
|
||||
Protocol.assert_protocol!(protocol)
|
||||
|
||||
defmodule name do
|
||||
@behaviour protocol
|
||||
@@ -553,7 +550,7 @@ defmodule Protocol do
|
||||
Module.register_attribute(__MODULE__, :impl, persist: true)
|
||||
@impl [protocol: @protocol, for: @for]
|
||||
|
||||
unquote(impl)
|
||||
unquote(__impl__)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -581,13 +578,12 @@ 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)
|
||||
assert_impl!(protocol, Map, extra)
|
||||
|
||||
# Clean up variables from eval context
|
||||
env = %{env | vars: [], export_vars: nil}
|
||||
args = [for, struct, opts]
|
||||
impl = Module.concat(protocol, Any)
|
||||
impl = Module.concat(protocol, Map)
|
||||
|
||||
:elixir_module.expand_callback(env.line, impl, :__deriving__, args, env, fn
|
||||
mod, fun, args ->
|
||||
@@ -610,31 +606,19 @@ 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}
|
||||
specs = Module.get_attribute(module, :spec)
|
||||
|
||||
specs = Module.get_attribute(module, :spec)
|
||||
found =
|
||||
:lists.map(fn {:spec, expr, caller} ->
|
||||
if Kernel.Typespec.spec_to_signature(expr) == signature do
|
||||
Kernel.Typespec.define_spec(:callback, expr, caller)
|
||||
true
|
||||
end
|
||||
end, specs)
|
||||
for {:spec, expr, caller} <- specs,
|
||||
Kernel.Typespec.spec_to_signature(expr) == signature do
|
||||
Kernel.Typespec.define_spec(:callback, expr, caller)
|
||||
true
|
||||
end
|
||||
|
||||
:lists.any(& &1 == true, found)
|
||||
found != []
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
+47
-40
@@ -1,13 +1,6 @@
|
||||
defmodule Range do
|
||||
@moduledoc """
|
||||
Defines a range.
|
||||
|
||||
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
|
||||
inclusive.
|
||||
Defines a Range.
|
||||
|
||||
A Range is represented internally as a struct. However,
|
||||
the most common form of creating and matching on ranges
|
||||
@@ -15,7 +8,7 @@ defmodule Range do
|
||||
|
||||
iex> range = 1..3
|
||||
1..3
|
||||
iex> first..last = range
|
||||
iex> first .. last = range
|
||||
iex> first
|
||||
1
|
||||
iex> last
|
||||
@@ -31,21 +24,12 @@ defmodule Range do
|
||||
@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 range.
|
||||
|
||||
It does not check if the range is valid.
|
||||
Returns true if the given argument is a range.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -56,39 +40,52 @@ defmodule Range do
|
||||
false
|
||||
|
||||
"""
|
||||
@spec range?(%Range{}) :: true
|
||||
@spec range?(term) :: false
|
||||
def range?(term)
|
||||
def range?(%Range{}), do: true
|
||||
def range?(_), do: false
|
||||
end
|
||||
|
||||
defprotocol Range.Iterator do
|
||||
@moduledoc """
|
||||
A protocol used for iterating range elements.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Returns the function that calculates the next item.
|
||||
"""
|
||||
def next(first, range)
|
||||
|
||||
@doc """
|
||||
Count how many items are in the range.
|
||||
"""
|
||||
def count(first, range)
|
||||
end
|
||||
|
||||
defimpl Enumerable, for: Range do
|
||||
def reduce(first .. last, acc, fun) do
|
||||
reduce(first, last, acc, fun, last >= first)
|
||||
def reduce(first .. last = range, acc, fun) do
|
||||
reduce(first, last, acc, fun, Range.Iterator.next(first, range), last >= first)
|
||||
end
|
||||
|
||||
defp reduce(_x, _y, {:halt, acc}, _fun, _up) do
|
||||
defp reduce(_x, _y, {:halt, acc}, _fun, _next, _up) do
|
||||
{:halted, acc}
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:suspend, acc}, fun, up) do
|
||||
{:suspended, acc, &reduce(x, y, &1, fun, up)}
|
||||
defp reduce(x, y, {:suspend, acc}, fun, next, up) do
|
||||
{:suspended, acc, &reduce(x, y, &1, fun, next, up)}
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:cont, acc}, fun, true) when x <= y do
|
||||
reduce(x + 1, y, fun.(x, acc), fun, true)
|
||||
defp reduce(x, y, {:cont, acc}, fun, next, true) when x <= y do
|
||||
reduce(next.(x), y, fun.(x, acc), fun, next, true)
|
||||
end
|
||||
|
||||
defp reduce(x, y, {:cont, acc}, fun, false) when x >= y do
|
||||
reduce(x - 1, y, fun.(x, acc), fun, false)
|
||||
defp reduce(x, y, {:cont, acc}, fun, next, false) when x >= y do
|
||||
reduce(next.(x), y, fun.(x, acc), fun, next, false)
|
||||
end
|
||||
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _up) do
|
||||
defp reduce(_, _, {:cont, acc}, _fun, _next, _up) do
|
||||
{:done, acc}
|
||||
end
|
||||
|
||||
def member?(first .. last, value) when is_integer(value) do
|
||||
def member?(first .. last, value) do
|
||||
if first <= last do
|
||||
{:ok, first <= value and value <= last}
|
||||
else
|
||||
@@ -96,15 +93,25 @@ defimpl Enumerable, for: Range do
|
||||
end
|
||||
end
|
||||
|
||||
def member?(_ .. _, _value) do
|
||||
{:ok, false}
|
||||
def count(first .. _ = range) do
|
||||
{:ok, Range.Iterator.count(first, range)}
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Range.Iterator, for: Integer do
|
||||
def next(first, _ .. last) when is_integer(last) do
|
||||
if last >= first do
|
||||
&(&1 + 1)
|
||||
else
|
||||
&(&1 - 1)
|
||||
end
|
||||
end
|
||||
|
||||
def count(first .. last) do
|
||||
if first <= last do
|
||||
{:ok, last - first + 1}
|
||||
def count(first, _ .. last) when is_integer(last) do
|
||||
if last >= first do
|
||||
last - first + 1
|
||||
else
|
||||
{:ok, first - last + 1}
|
||||
first - last + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -29,10 +29,10 @@ defmodule Record do
|
||||
|
||||
defmodule MyModule do
|
||||
require Record
|
||||
Record.defrecord :user, name: "john", age: 25
|
||||
Record.defrecord :user name: "john", age: 25
|
||||
|
||||
@type user :: record(:user, name: String.t, age: integer)
|
||||
# expands to: "@type user :: {:user, String.t, integer}"
|
||||
# expands to: `@type user :: {:user, String.t, integer}`
|
||||
end
|
||||
"""
|
||||
|
||||
@@ -52,21 +52,8 @@ defmodule Record do
|
||||
uid: :undefined, gid: :undefined]
|
||||
|
||||
"""
|
||||
def extract(name, opts) when is_atom(name) and is_list(opts) do
|
||||
Record.Extractor.extract(name, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Extracts all records information from an Erlang file.
|
||||
|
||||
Returns a keyword list containing extracted record names as keys, and
|
||||
lists of tuples describing the fields as values. It expects a named
|
||||
argument :from or :from_lib, which correspond to *include* or
|
||||
*include_lib* attribute from Erlang modules, respectively.
|
||||
|
||||
"""
|
||||
def extract_all(opts) when is_list(opts) do
|
||||
Record.Extractor.extract_all(opts)
|
||||
defmacro extract(name, opts) when is_atom(name) and is_list(opts) do
|
||||
Macro.escape Record.Extractor.extract(name, opts)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -158,14 +145,6 @@ defmodule Record do
|
||||
# Convert a record to a keyword list
|
||||
user(record) #=> [name: "meg", age: 26]
|
||||
|
||||
The generated macros can also be used in order to pattern match on records and
|
||||
to bind variables during the match:
|
||||
|
||||
record = user() #=> {:user, "meg", 25}
|
||||
|
||||
user(name: name) = record
|
||||
name #=> "meg"
|
||||
|
||||
By default, Elixir uses the record name as the first element of
|
||||
the tuple (the tag). But it can be changed to something else:
|
||||
|
||||
@@ -177,25 +156,6 @@ defmodule Record do
|
||||
require User
|
||||
User.user() #=> {User, nil}
|
||||
|
||||
## Defining extracted records with anonymous functions
|
||||
|
||||
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.
|
||||
|
||||
Record.defrecord :my_rec, Record.extract(...)
|
||||
#=> ** (ArgumentError) invalid value for record field fun_field,
|
||||
cannot escape #Function<12.90072148/2 in :erl_eval.expr/5>.
|
||||
|
||||
To work around this error, redefine the field with your own &M.f/a function,
|
||||
like so:
|
||||
|
||||
defmodule MyRec do
|
||||
require Record
|
||||
Record.defrecord :my_rec, Record.extract(...) |> Keyword.merge(fun_field: &__MODULE__.foo/2)
|
||||
def foo(bar, baz), do: IO.inspect({bar, baz})
|
||||
end
|
||||
"""
|
||||
defmacro defrecord(name, tag \\ nil, kv) do
|
||||
quote bind_quoted: [name: name, tag: tag, kv: kv] do
|
||||
|
||||
@@ -4,44 +4,27 @@ defmodule Record.Extractor do
|
||||
# Retrieve a record definition from an Erlang file using
|
||||
# the same lookup as the *include* attribute from Erlang modules.
|
||||
def extract(name, from: file) when is_binary(file) do
|
||||
extract_record(name, from_file(file))
|
||||
file = String.to_char_list(file)
|
||||
|
||||
realfile =
|
||||
case :code.where_is_file(file) do
|
||||
:non_existing -> file
|
||||
realfile -> realfile
|
||||
end
|
||||
|
||||
extract_record(name, realfile)
|
||||
end
|
||||
|
||||
# Retrieve a record definition from an Erlang file using
|
||||
# the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
def extract(name, from_lib: file) when is_binary(file) do
|
||||
extract_record(name, from_lib_file(file))
|
||||
end
|
||||
|
||||
# Retrieve all records definitions from an Erlang file using
|
||||
# the same lookup as the *include* attribute from Erlang modules.
|
||||
def extract_all(from: file) when is_binary(file) do
|
||||
extract_all_records(from_file(file))
|
||||
end
|
||||
|
||||
# Retrieve all records definitions from an Erlang file using
|
||||
# the same lookup as the *include_lib* attribute from Erlang modules.
|
||||
def extract_all(from_lib: file) when is_binary(file) do
|
||||
extract_all_records(from_lib_file(file))
|
||||
end
|
||||
|
||||
# Find file using the same lookup as the *include* attribute from Erlang modules.
|
||||
defp from_file(file) do
|
||||
file = String.to_char_list(file)
|
||||
case :code.where_is_file(file) do
|
||||
:non_existing -> file
|
||||
realfile -> realfile
|
||||
end
|
||||
end
|
||||
|
||||
# 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_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])
|
||||
extract_record name, :filename.join([libpath|path])
|
||||
end
|
||||
end
|
||||
|
||||
@@ -56,24 +39,16 @@ defmodule Record.Extractor do
|
||||
end
|
||||
end
|
||||
|
||||
# Retrieve all records from the given file
|
||||
defp extract_all_records(file) do
|
||||
form = read_file(file)
|
||||
records = extract_records(form)
|
||||
for rec = {name, _fields} <- records, do: {name, parse_record(rec, form)}
|
||||
end
|
||||
|
||||
# Parse the given file and extract all existent records.
|
||||
defp extract_records(form) do
|
||||
for {:attribute, _, :record, record} <- form, do: record
|
||||
end
|
||||
|
||||
# Read a file and return its abstract syntax form that also
|
||||
# includes record but with macros and other attributes expanded,
|
||||
# such as "-include(...)" and "-include_lib(...)". This is done
|
||||
# by using Erlang's epp.
|
||||
# includes record and other preprocessor modules. This is done
|
||||
# by using Erlang's epp_dodger.
|
||||
defp read_file(file) do
|
||||
case :epp.parse_file(file, []) do
|
||||
case :epp_dodger.quick_parse_file(file) do
|
||||
{:ok, form} ->
|
||||
form
|
||||
other ->
|
||||
@@ -105,11 +80,11 @@ defmodule Record.Extractor do
|
||||
|
||||
defp eval_record(cons, form) do
|
||||
form = form ++
|
||||
[{:function, 0, :hello, 0, [
|
||||
{:clause, 0, [], [], [cons]}]}]
|
||||
[ {:function, 0, :hello, 0, [
|
||||
{:clause, 0, [], [], [ cons ]} ]} ]
|
||||
|
||||
{:function, 0, :hello, 0, [
|
||||
{:clause, 0, [], [], [record_ast]}]} = :erl_expand_records.module(form, []) |> List.last
|
||||
{:clause, 0, [], [], [ record_ast ]} ]} = :erl_expand_records.module(form, []) |> List.last
|
||||
|
||||
{:value, record, _} = :erl_eval.expr(record_ast, [])
|
||||
record
|
||||
|
||||
+60
-84
@@ -1,14 +1,13 @@
|
||||
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
|
||||
As the `re` module, Regex is based on PCRE
|
||||
(Perl Compatible Regular Expressions). More information can be
|
||||
found in the [`:re` module documentation](http://www.erlang.org/doc/man/re.html).
|
||||
found in the [`re` documentation](http://www.erlang.org/doc/man/re.html).
|
||||
|
||||
Regular expressions in Elixir can be created using `Regex.compile!/2`
|
||||
or using the special form with [`~r`](Kernel.html#sigil_r/2) or [`~R`](Kernel.html#sigil_R/2):
|
||||
or using the special form with [`~r`](Kernel.html#sigil_r/2):
|
||||
|
||||
# A simple regular expressions that matches foo anywhere in the string
|
||||
~r/foo/
|
||||
@@ -23,7 +22,7 @@ defmodule Regex do
|
||||
|
||||
The modifiers available when creating a Regex are:
|
||||
|
||||
* `unicode` (u) - enables unicode specific patterns like `\p` and change
|
||||
* `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
|
||||
|
||||
@@ -42,8 +41,7 @@ defmodule Regex do
|
||||
* `firstline` (f) - forces the unanchored pattern to match before or at the
|
||||
first newline, though the matched text may continue over the newline
|
||||
|
||||
* `ungreedy` (U) - inverts the "greediness" of the regexp
|
||||
(the previous `r` option is deprecated in favor of `U`)
|
||||
* `ungreedy` (r) - inverts the "greediness" of the regexp
|
||||
|
||||
The options not available are:
|
||||
|
||||
@@ -56,7 +54,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
|
||||
@@ -91,7 +89,7 @@ defmodule Regex do
|
||||
|
||||
The given options can either be a binary with the characters
|
||||
representing the same regex options given to the `~r` sigil,
|
||||
or a list of options, as expected by the Erlang's [`:re` module](http://www.erlang.org/doc/man/re.html).
|
||||
or a list of options, as expected by the [Erlang `re` docs](http://www.erlang.org/doc/man/re.html).
|
||||
|
||||
It returns `{:ok, regex}` in case of success,
|
||||
`{:error, reason}` otherwise.
|
||||
@@ -161,8 +159,7 @@ defmodule Regex do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns `true` if the given `term` is a regex.
|
||||
Otherwise returns `false`.
|
||||
Returns true if the given argument is a regex.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -175,7 +172,6 @@ defmodule Regex do
|
||||
"""
|
||||
@spec regex?(t) :: true
|
||||
@spec regex?(any) :: false
|
||||
def regex?(term)
|
||||
def regex?(%Regex{}), do: true
|
||||
def regex?(_), do: false
|
||||
|
||||
@@ -187,7 +183,7 @@ defmodule Regex do
|
||||
|
||||
* `:return` - set to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
to see the possible capture values.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -198,7 +194,7 @@ defmodule Regex do
|
||||
nil
|
||||
|
||||
iex> Regex.run(~r/c(d)/, "abcd", return: :index)
|
||||
[{2, 2}, {3, 1}]
|
||||
[{2,2},{3,1}]
|
||||
|
||||
"""
|
||||
@spec run(t, binary, [term]) :: nil | [binary] | [{integer, integer}]
|
||||
@@ -293,16 +289,15 @@ defmodule Regex do
|
||||
|
||||
@doc """
|
||||
Same as `run/3`, but scans the target several times collecting all
|
||||
matches of the regular expression.
|
||||
|
||||
A list of lists is returned, where each entry in the primary list represents a
|
||||
match and each entry in the secondary list represents the captured contents.
|
||||
matches of the regular expression. A list of lists is returned,
|
||||
where each entry in the primary list represents a match and each
|
||||
entry in the secondary list represents the captured contents.
|
||||
|
||||
## Options
|
||||
|
||||
* `:return` - set to `:index` to return indexes. Defaults to `:binary`.
|
||||
* `:capture` - what to capture in the result. Check the moduledoc for `Regex`
|
||||
to see the possible capture values.
|
||||
to see the possible capture values.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -332,8 +327,7 @@ defmodule Regex do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Splits the given target based on the given pattern and in the given number of
|
||||
parts.
|
||||
Splits the given target into the number of parts specified.
|
||||
|
||||
## Options
|
||||
|
||||
@@ -342,19 +336,20 @@ defmodule Regex do
|
||||
split the string into the maximum number of parts possible based on the
|
||||
given pattern.
|
||||
|
||||
* `:trim` - when `true`, removes empty strings (`""`) from the result.
|
||||
* `:trim` - when true, remove blank strings from the result.
|
||||
|
||||
* `:on` - specifies which captures to split the string on, and in what
|
||||
order. Defaults to `:first` which means captures inside the regex do not
|
||||
affect the splitting process.
|
||||
* `:on` - specifies which captures and order to split the string
|
||||
on. Check the moduledoc for `Regex` to see the possible capture
|
||||
values. Defaults to `:first` which means captures inside the
|
||||
Regex does not affect the split result.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Regex.split(~r/-/, "a-b-c")
|
||||
["a", "b", "c"]
|
||||
["a","b","c"]
|
||||
|
||||
iex> Regex.split(~r/-/, "a-b-c", [parts: 2])
|
||||
["a", "b-c"]
|
||||
["a","b-c"]
|
||||
|
||||
iex> Regex.split(~r/-/, "abc")
|
||||
["abc"]
|
||||
@@ -372,15 +367,9 @@ defmodule Regex do
|
||||
@spec split(t, String.t, [term]) :: [String.t]
|
||||
def split(regex, string, options \\ [])
|
||||
|
||||
def split(%Regex{}, "", opts) do
|
||||
if Keyword.get(opts, :trim, false) do
|
||||
[]
|
||||
else
|
||||
[""]
|
||||
end
|
||||
end
|
||||
def split(%Regex{}, "", _opts), do: [""]
|
||||
|
||||
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} ->
|
||||
@@ -426,12 +415,12 @@ defmodule Regex do
|
||||
|
||||
@doc ~S"""
|
||||
Receives a regex, a binary and a replacement, returns a new
|
||||
binary where all matches are replaced by the replacement.
|
||||
binary where the all matches are replaced by replacement.
|
||||
|
||||
The replacement can be either a string or a function. The string
|
||||
is used as a replacement for every match and it allows specific
|
||||
captures to be accessed via `\\N` or `\g{N}`, where `N` is the
|
||||
capture. In case `\\0` is used, the whole match is inserted.
|
||||
captures to be accessed via `\N`, where `N` is the capture. In
|
||||
case `\0` is used, the whole match is inserted.
|
||||
|
||||
When the replacement is a function, the function may have arity
|
||||
N where each argument maps to a capture, with the first argument
|
||||
@@ -441,7 +430,7 @@ defmodule Regex do
|
||||
## Options
|
||||
|
||||
* `:global` - when `false`, replaces only the first occurrence
|
||||
(defaults to `true`)
|
||||
(defaults to true)
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -457,26 +446,18 @@ defmodule Regex do
|
||||
iex> Regex.replace(~r/a(b|d)c/, "abcadc", "[\\1]")
|
||||
"[b][d]"
|
||||
|
||||
iex> Regex.replace(~r/\.(\d)$/, "500.5", ".\\g{1}0")
|
||||
"500.50"
|
||||
|
||||
iex> Regex.replace(~r/a(b|d)c/, "abcadc", fn _, x -> "[#{x}]" end)
|
||||
"[b][d]"
|
||||
|
||||
iex> Regex.replace(~r/a/, "abcadc", "A", global: false)
|
||||
"Abcadc"
|
||||
|
||||
"""
|
||||
@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
|
||||
@@ -498,30 +479,30 @@ 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)
|
||||
[List.to_integer(ns) | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<?\\, ?\\, rest::binary>>) do
|
||||
[<<?\\>> | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
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)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<x, rest::binary>>) do
|
||||
defp precompile_replacement(<<?\\, x, rest :: binary>>) when x < ?0 or x > ?9 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 precompile_replacement(<<?\\, rest :: binary>>) when byte_size(rest) > 0 do
|
||||
{ns, rest} = pick_int(rest)
|
||||
[List.to_integer(ns) | precompile_replacement(rest)]
|
||||
end
|
||||
|
||||
defp precompile_replacement(<<x, rest :: binary>>) do
|
||||
case precompile_replacement(rest) do
|
||||
[head | t] when is_binary(head) ->
|
||||
[<<x, head :: binary>> | t]
|
||||
other ->
|
||||
[<<x>> | other]
|
||||
end
|
||||
end
|
||||
|
||||
defp pick_int(<<x, rest :: binary>>) when x in ?0..?9 do
|
||||
{found, rest} = pick_int(rest)
|
||||
{[x|found], rest}
|
||||
end
|
||||
@@ -544,12 +525,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
|
||||
defp apply_list(whole, string, pos, replacement, [[{mpos, length} | _] = head | tail]) when mpos == pos do
|
||||
<<_ :: size(length)-binary, rest :: binary>> = string
|
||||
new_data = apply_replace(whole, replacement, head)
|
||||
[new_data | apply_list(whole, rest, pos + length, replacement, tail)]
|
||||
end
|
||||
@@ -569,7 +550,7 @@ defmodule Regex do
|
||||
cond do
|
||||
is_binary(part) ->
|
||||
part
|
||||
part >= tuple_size(indexes) ->
|
||||
part > tuple_size(indexes) ->
|
||||
""
|
||||
true ->
|
||||
get_index(string, elem(indexes, part))
|
||||
@@ -582,7 +563,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
|
||||
|
||||
@@ -632,18 +613,13 @@ 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])
|
||||
|
||||
# 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(<<?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(<<?r, 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(<<>>, acc), do: acc
|
||||
defp translate_options(rest, _acc), do: {:error, rest}
|
||||
end
|
||||
|
||||
+198
-22
@@ -1,28 +1,49 @@
|
||||
defmodule Set do
|
||||
@moduledoc ~S"""
|
||||
WARNING: this module is deprecated.
|
||||
This module specifies the Set API expected to be
|
||||
implemented by different representations.
|
||||
|
||||
Use the `MapSet` module instead.
|
||||
It also provides functions that redirect to the
|
||||
underlying Set, allowing a developer to work with
|
||||
different Set implementations using one API.
|
||||
|
||||
To create a new set, use the `new` functions defined
|
||||
by each set type:
|
||||
|
||||
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 `Enumerable` and `Collectable`
|
||||
protocols.
|
||||
|
||||
## Match
|
||||
|
||||
Sets are required to implement all operations using the match (`===`)
|
||||
operator.
|
||||
"""
|
||||
|
||||
use Behaviour
|
||||
|
||||
@type value :: any
|
||||
@type values :: [ value ]
|
||||
@type t :: map
|
||||
|
||||
# TODO: Remove callbacks on 1.3
|
||||
# TODO: Deprecate every function on 1.3
|
||||
@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
|
||||
defcallback new :: t
|
||||
defcallback delete(t, value) :: t
|
||||
defcallback difference(t, t) :: t
|
||||
defcallback disjoint?(t, t) :: boolean
|
||||
defcallback equal?(t, t) :: boolean
|
||||
defcallback intersection(t, t) :: t
|
||||
defcallback member?(t, value) :: boolean
|
||||
defcallback put(t, value) :: t
|
||||
defcallback size(t) :: non_neg_integer
|
||||
defcallback subset?(t, t) :: boolean
|
||||
defcallback to_list(t) :: list()
|
||||
defcallback union(t, t) :: t
|
||||
|
||||
defmacrop target(set) do
|
||||
quote do
|
||||
@@ -35,10 +56,39 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes `value` from `set`.
|
||||
|
||||
## 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`.
|
||||
|
||||
Notice this function is polymorphic as it calculates the difference
|
||||
for of any type. Each set implementation also provides a `difference`
|
||||
function, but they can only work with sets of the same 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)
|
||||
@@ -46,12 +96,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.
|
||||
|
||||
Notice this function is polymorphic as it checks for disjoint sets of
|
||||
any type. Each set implementation also provides a `disjoint?` function,
|
||||
but they 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)
|
||||
@@ -59,7 +126,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}
|
||||
@@ -69,10 +136,28 @@ defmodule Set do
|
||||
end
|
||||
|
||||
@doc false
|
||||
@spec empty(t) :: t
|
||||
def empty(set) do
|
||||
target(set).empty(set)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Check if two sets are equal using `===`.
|
||||
|
||||
Notice this function is polymorphic as it compares sets of
|
||||
any type. Each set implementation also provides an `equal?`
|
||||
function, but they 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)
|
||||
@@ -89,7 +174,23 @@ defmodule Set do
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns a set containing only members in common between `set1` and `set2`.
|
||||
|
||||
Notice this function is polymorphic as it calculates the intersection of
|
||||
any type. Each set implementation also provides a `intersection` function,
|
||||
but they 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)
|
||||
@@ -97,26 +198,77 @@ 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 it does not 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`.
|
||||
|
||||
Notice this function is polymorphic as it checks the subset for
|
||||
any type. Each set implementation also provides a `subset?` function,
|
||||
but they 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)
|
||||
@@ -128,10 +280,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`.
|
||||
|
||||
Notice this function is polymorphic as it calculates the union of
|
||||
any type. Each set implementation also provides a `union` function,
|
||||
but they 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)
|
||||
@@ -139,14 +315,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}
|
||||
|
||||
+120
-192
@@ -9,7 +9,7 @@ defmodule Stream do
|
||||
iex> range = 1..5
|
||||
1..5
|
||||
iex> Enum.map range, &(&1 * 2)
|
||||
[2, 4, 6, 8, 10]
|
||||
[2,4,6,8,10]
|
||||
|
||||
In the example above, as we mapped over the range, the elements being
|
||||
enumerated were created one by one, during enumeration. The `Stream`
|
||||
@@ -18,11 +18,11 @@ defmodule Stream do
|
||||
iex> range = 1..3
|
||||
iex> stream = Stream.map(range, &(&1 * 2))
|
||||
iex> Enum.map(stream, &(&1 + 1))
|
||||
[3, 5, 7]
|
||||
[3,5,7]
|
||||
|
||||
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,26 +33,26 @@ 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
|
||||
2
|
||||
4
|
||||
6
|
||||
#=> [2, 4, 6]
|
||||
#=> [2,4,6]
|
||||
|
||||
Notice that we first printed each item in the list, then multiplied each
|
||||
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
|
||||
@@ -60,16 +60,16 @@ defmodule Stream do
|
||||
4
|
||||
3
|
||||
6
|
||||
#=> [2, 4, 6]
|
||||
#=> [2,4,6]
|
||||
|
||||
Although the end result is the same, the order in which the items were
|
||||
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,27 +96,23 @@ defmodule Stream do
|
||||
@type element :: any
|
||||
@type index :: non_neg_integer
|
||||
@type default :: any
|
||||
@opaque t :: %__MODULE__{}
|
||||
@type t :: %__MODULE__{}
|
||||
|
||||
# Require Stream.Reducers and its callbacks
|
||||
require Stream.Reducers, as: R
|
||||
|
||||
defmacrop skip(acc) do
|
||||
{:cont, acc}
|
||||
end
|
||||
|
||||
defmacrop next(f, entry, acc) do
|
||||
defmacrop cont(f, entry, acc) do
|
||||
quote do: unquote(f).(unquote(entry), unquote(acc))
|
||||
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
|
||||
defmacrop cont_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,unquote(n)|t]}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -156,8 +152,8 @@ defmodule Stream do
|
||||
[[1, 2, 3], [4, 5, 6]]
|
||||
|
||||
"""
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, pos_integer, pos_integer, Enumerable.t | nil) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer) :: Enumerable.t
|
||||
@spec chunk(Enumerable.t, non_neg_integer, non_neg_integer, Enumerable.t | nil) :: Enumerable.t
|
||||
def chunk(enum, n, step, pad \\ nil) when n > 0 and step > 0 do
|
||||
limit = :erlang.max(n, step)
|
||||
if is_nil(pad) do
|
||||
@@ -174,8 +170,8 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
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)
|
||||
buffer = :lists.reverse(buffer) ++ Enum.take(pad, n - count)
|
||||
cont_with_acc(f1, buffer, h, old, t)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -202,41 +198,7 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
defp do_chunk_by(acc(h, {buffer, _}, t), f1) do
|
||||
next_with_acc(f1, :lists.reverse(buffer), h, nil, t)
|
||||
end
|
||||
|
||||
|
||||
@doc """
|
||||
Creates a stream that only emits elements if they are different from the last emitted element.
|
||||
|
||||
This function only ever needs to store the last emitted element.
|
||||
|
||||
Elements are compared using `===`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.dedup([1, 2, 3, 3, 2, 1]) |> Enum.to_list
|
||||
[1, 2, 3, 2, 1]
|
||||
|
||||
"""
|
||||
@spec dedup(Enumerable.t) :: Enumerable.t
|
||||
def dedup(enum) do
|
||||
dedup_by(enum, fn x -> x end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a stream that only emits elements if the result of calling `fun` on the element is
|
||||
different from the (stored) result of calling `fun` on the last emitted element.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.dedup_by([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) |> Enum.to_list
|
||||
[{1, :x}, {2, :y}, {1, :x}]
|
||||
|
||||
"""
|
||||
@spec dedup_by(Enumerable.t, (element -> term)) :: Enumerable.t
|
||||
def dedup_by(enum, fun) when is_function(fun, 1) do
|
||||
lazy enum, nil, fn f1 -> R.dedup(fun, f1) end
|
||||
cont_with_acc(f1, :lists.reverse(buffer), h, nil, t)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -251,11 +213,11 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.drop(1..10, 5)
|
||||
iex> Enum.to_list(stream)
|
||||
[6, 7, 8, 9, 10]
|
||||
[6,7,8,9,10]
|
||||
|
||||
iex> stream = Stream.drop(1..10, -5)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
[1,2,3,4,5]
|
||||
|
||||
"""
|
||||
@spec drop(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
@@ -295,7 +257,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.drop_while(1..10, &(&1 <= 5))
|
||||
iex> Enum.to_list(stream)
|
||||
[6, 7, 8, 9, 10]
|
||||
[6,7,8,9,10]
|
||||
|
||||
"""
|
||||
@spec drop_while(Enumerable.t, (element -> as_boolean(term))) :: Enumerable.t
|
||||
@@ -304,7 +266,7 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Executes the given function for each item.
|
||||
Execute the given function for each item.
|
||||
|
||||
Useful for adding side effects (like printing) to a stream.
|
||||
|
||||
@@ -372,7 +334,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.filter_map(1..6, fn(x) -> rem(x, 2) == 0 end, &(&1 * 2))
|
||||
iex> Enum.to_list(stream)
|
||||
[4, 8, 12]
|
||||
[4,8,12]
|
||||
|
||||
"""
|
||||
@spec filter_map(Enumerable.t, (element -> as_boolean(term)), (element -> any)) :: Enumerable.t
|
||||
@@ -381,17 +343,16 @@ 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 every `n` milliseconds.
|
||||
|
||||
The values emitted are an increasing counter starting at `0`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.interval(10) |> Enum.take(10)
|
||||
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
[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) ->
|
||||
@@ -446,7 +407,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.map([1, 2, 3], fn(x) -> x * 2 end)
|
||||
iex> Enum.to_list(stream)
|
||||
[2, 4, 6]
|
||||
[2,4,6]
|
||||
|
||||
"""
|
||||
@spec map(Enumerable.t, (element -> any)) :: Enumerable.t
|
||||
@@ -462,7 +423,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.reject([1, 2, 3], fn(x) -> rem(x, 2) == 0 end)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 3]
|
||||
[1,3]
|
||||
|
||||
"""
|
||||
@spec reject(Enumerable.t, (element -> as_boolean(term))) :: Enumerable.t
|
||||
@@ -504,7 +465,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.scan(1..5, &(&1 + &2))
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 3, 6, 10, 15]
|
||||
[1,3,6,10,15]
|
||||
|
||||
"""
|
||||
@spec scan(Enumerable.t, (element, acc -> any)) :: Enumerable.t
|
||||
@@ -521,7 +482,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.scan(1..5, 0, &(&1 + &2))
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 3, 6, 10, 15]
|
||||
[1,3,6,10,15]
|
||||
|
||||
"""
|
||||
@spec scan(Enumerable.t, acc, (element, acc -> any)) :: Enumerable.t
|
||||
@@ -530,72 +491,85 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lazily takes the next `count` items from the enumerable and stops
|
||||
Lazily takes the next `n` items from the enumerable and stops
|
||||
enumeration.
|
||||
|
||||
If a negative `count` is given, the last `count` values will be taken.
|
||||
For such, the collection is fully enumerated keeping up to `2 * count`
|
||||
If a negative `n` is given, the last `n` values will be taken.
|
||||
For such, the collection is fully enumerated keeping up to `2 * n`
|
||||
elements in memory. Once the end of the collection is reached,
|
||||
the last `count` elements will be executed. Therefore, using
|
||||
a negative `count` on an infinite collection will never return.
|
||||
a negative `n` on an infinite collection will never return.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.take(1..100, 5)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
[1,2,3,4,5]
|
||||
|
||||
iex> stream = Stream.take(1..100, -5)
|
||||
iex> Enum.to_list(stream)
|
||||
[96, 97, 98, 99, 100]
|
||||
[96,97,98,99,100]
|
||||
|
||||
iex> stream = Stream.cycle([1, 2, 3]) |> Stream.take(5)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 1, 2]
|
||||
[1,2,3,1,2]
|
||||
|
||||
"""
|
||||
@spec take(Enumerable.t, integer) :: Enumerable.t
|
||||
@spec take(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def take(_enum, 0), do: %Stream{enum: []}
|
||||
def take([], _count), do: %Stream{enum: []}
|
||||
|
||||
def take(enum, count) when is_integer(count) and count > 0 do
|
||||
lazy enum, count, fn(f1) -> R.take(f1) end
|
||||
def take(enum, n) when n > 0 do
|
||||
lazy enum, n, fn(f1) -> R.take(f1) end
|
||||
end
|
||||
|
||||
def take(enum, count) when is_integer(count) and count < 0 do
|
||||
&Enumerable.reduce(Enum.take(enum, count), &1, &2)
|
||||
def take(enum, n) when n < 0 do
|
||||
&do_take(enum, abs(n), &1, &2)
|
||||
end
|
||||
|
||||
defp do_take(enum, n, acc, f) do
|
||||
{_, {_count, buf1, buf2}} =
|
||||
Enumerable.reduce(enum, {:cont, {0, [], []}}, fn
|
||||
entry, {count, buf1, buf2} ->
|
||||
buf1 = [entry|buf1]
|
||||
count = count + 1
|
||||
if count == n do
|
||||
{:cont, {0, [], buf1}}
|
||||
else
|
||||
{:cont, {count, buf1, buf2}}
|
||||
end
|
||||
end)
|
||||
|
||||
Enumerable.reduce(do_take_last(buf1, buf2, n, []), acc, f)
|
||||
end
|
||||
|
||||
defp do_take_last(_buf1, _buf2, 0, acc),
|
||||
do: acc
|
||||
defp do_take_last([], [], _, acc),
|
||||
do: acc
|
||||
defp do_take_last([], [h|t], n, acc),
|
||||
do: do_take_last([], t, n-1, [h|acc])
|
||||
defp do_take_last([h|t], buf2, n, acc),
|
||||
do: do_take_last(t, buf2, n-1, [h|acc])
|
||||
|
||||
@doc """
|
||||
Creates a stream that takes every `nth` item from the enumerable.
|
||||
Creates a stream that takes every `n` item from the enumerable.
|
||||
|
||||
The first item is always included, unless `nth` is 0.
|
||||
|
||||
`nth` must be a non-negative integer, or `FunctionClauseError` will be thrown.
|
||||
The first item is always included, unless `n` is 0.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.take_every(1..10, 2)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 3, 5, 7, 9]
|
||||
|
||||
iex> stream = Stream.take_every([1, 2, 3, 4, 5], 1)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
|
||||
iex> stream = Stream.take_every(1..1000, 0)
|
||||
iex> Enum.to_list(stream)
|
||||
[]
|
||||
[1,3,5,7,9]
|
||||
|
||||
"""
|
||||
@spec take_every(Enumerable.t, non_neg_integer) :: Enumerable.t
|
||||
def take_every(_enum, 0), do: %Stream{enum: []}
|
||||
def take_every([], _nth), do: %Stream{enum: []}
|
||||
|
||||
def take_every(enum, nth) when is_integer(nth) and nth > 0 do
|
||||
lazy enum, nth, fn(f1) -> R.take_every(nth, f1) end
|
||||
def take_every(enum, n) when n > 0 do
|
||||
lazy enum, n, fn(f1) -> R.take_every(n, f1) end
|
||||
end
|
||||
|
||||
def take_every(_enum, 0), do: %Stream{enum: []}
|
||||
|
||||
@doc """
|
||||
Lazily takes elements of the enumerable while the given
|
||||
function returns `true`.
|
||||
@@ -604,7 +578,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.take_while(1..100, &(&1 <= 5))
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5]
|
||||
[1,2,3,4,5]
|
||||
|
||||
"""
|
||||
@spec take_while(Enumerable.t, (element -> as_boolean(term))) :: Enumerable.t
|
||||
@@ -652,51 +626,24 @@ defmodule Stream do
|
||||
...> if acc < n, do: {[i], acc + 1}, else: {:halt, acc}
|
||||
...> end)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3]
|
||||
[1,2,3]
|
||||
|
||||
"""
|
||||
@spec transform(Enumerable.t, acc, fun) :: Enumerable.t when
|
||||
fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, acc, reducer) when is_function(reducer, 2) do
|
||||
&do_transform(enum, fn -> acc end, reducer, &1, &2, nil)
|
||||
def transform(enum, acc, reducer) do
|
||||
&do_transform(enum, acc, reducer, &1, &2)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Transforms an existing stream with function-based start and finish.
|
||||
|
||||
The accumulator is only calculated when transformation starts. It also
|
||||
allows an after function to be given which is invoked when the stream
|
||||
halts or completes.
|
||||
|
||||
This function can be seen as a combination of `Stream.resource/3` with
|
||||
`Stream.transform/3`.
|
||||
"""
|
||||
@spec transform(Enumerable.t, (() -> acc), fun, (acc -> term)) :: Enumerable.t when
|
||||
fun: (element, acc -> {Enumerable.t, acc} | {:halt, acc}),
|
||||
acc: any
|
||||
def transform(enum, start_fun, reducer, after_fun)
|
||||
when is_function(start_fun, 0) and is_function(reducer, 2) and is_function(after_fun, 1) do
|
||||
&do_transform(enum, start_fun, reducer, &1, &2, after_fun)
|
||||
end
|
||||
|
||||
defp do_transform(enumerables, user_acc, user, inner_acc, fun, after_fun) do
|
||||
defp do_transform(enumerables, user_acc, user, inner_acc, fun) do
|
||||
inner = &do_transform_each(&1, &2, fun)
|
||||
step = &do_transform_step(&1, &2)
|
||||
next = &Enumerable.reduce(enumerables, &1, step)
|
||||
do_transform(user_acc.(), user, fun, [], next, inner_acc, inner, after_fun)
|
||||
do_transform(user_acc, user, fun, [], next, inner_acc, inner)
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, _user, _fun, _next_acc, _next, {:halt, inner_acc}, _inner, after_fun) do
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, inner_acc}
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, {:suspend, inner_acc}, inner, after_fun) do
|
||||
{:suspended, inner_acc, &do_transform(user_acc, user, fun, next_acc, next, &1, inner, after_fun)}
|
||||
end
|
||||
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, after_fun) do
|
||||
defp do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner) do
|
||||
case next.({:cont, next_acc}) do
|
||||
{:suspended, [val|next_acc], next} ->
|
||||
try do
|
||||
@@ -705,77 +652,65 @@ defmodule Stream do
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{[], user_acc} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, after_fun)
|
||||
do_transform(user_acc, user, fun, next_acc, next, inner_acc, inner)
|
||||
{list, user_acc} when is_list(list) ->
|
||||
do_list_transform(user_acc, user, fun, next_acc, next, inner_acc, inner,
|
||||
&Enumerable.List.reduce(list, &1, fun), after_fun)
|
||||
{:halt, user_acc} ->
|
||||
&Enumerable.List.reduce(list, &1, fun))
|
||||
{:halt, _user_acc} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, elem(inner_acc, 1)}
|
||||
{other, user_acc} ->
|
||||
do_enum_transform(user_acc, user, fun, next_acc, next, inner_acc, inner,
|
||||
&Enumerable.reduce(other, &1, inner), after_fun)
|
||||
&Enumerable.reduce(other, &1, inner))
|
||||
end
|
||||
{reason, _} ->
|
||||
do_after(after_fun, user_acc)
|
||||
{reason, elem(inner_acc, 1)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_list_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, reduce, after_fun) do
|
||||
defp do_list_transform(user_acc, user, fun, next_acc, next, inner_acc, inner, reduce) do
|
||||
try do
|
||||
reduce.(inner_acc)
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
else
|
||||
{:done, acc} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner)
|
||||
{:halted, acc} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, acc}
|
||||
{:suspended, acc, c} ->
|
||||
{:suspended, acc, &do_list_transform(user_acc, user, fun, next_acc, next, &1, inner, c, after_fun)}
|
||||
{:suspended, acc, &do_list_transform(user_acc, user, fun, next_acc, next, &1, inner, c)}
|
||||
end
|
||||
end
|
||||
|
||||
defp do_enum_transform(user_acc, user, fun, next_acc, next, {op, inner_acc}, inner, reduce, after_fun) do
|
||||
defp do_enum_transform(user_acc, user, fun, next_acc, next, {op, inner_acc}, inner, reduce) do
|
||||
try do
|
||||
reduce.({op, [:outer|inner_acc]})
|
||||
catch
|
||||
kind, reason ->
|
||||
stacktrace = System.stacktrace
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
:erlang.raise(kind, reason, stacktrace)
|
||||
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 ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
{:halted, [_|acc]} ->
|
||||
{:halted, [:outer|acc]} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner)
|
||||
{:halted, [:inner|acc]} ->
|
||||
next.({:halt, next_acc})
|
||||
do_after(after_fun, user_acc)
|
||||
{:halted, acc}
|
||||
{:done, [_|acc]} ->
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner, after_fun)
|
||||
do_transform(user_acc, user, fun, next_acc, next, {:cont, acc}, inner)
|
||||
{:suspended, [_|acc], c} ->
|
||||
{:suspended, acc, &do_enum_transform(user_acc, user, fun, next_acc, next, &1, inner, c, after_fun)}
|
||||
{:suspended, acc, &do_enum_transform(user_acc, user, fun, next_acc, next, &1, inner, c)}
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
case f.(x, acc) do
|
||||
{:halt, res} -> {:halt, [:inner|res]}
|
||||
@@ -797,17 +732,17 @@ defmodule Stream do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.uniq([1, 2, 3, 3, 2, 1]) |> Enum.to_list
|
||||
iex> Stream.uniq([1, 2, 3, 2, 1]) |> Enum.to_list
|
||||
[1, 2, 3]
|
||||
|
||||
iex> Stream.uniq([{1, :x}, {2, :y}, {2, :z}, {1, :x}], fn {x, _} -> x end) |> Enum.to_list
|
||||
[{1, :x}, {2, :y}]
|
||||
iex> Stream.uniq([{1, :x}, {2, :y}, {1, :z}], fn {x, _} -> x end) |> Enum.to_list
|
||||
[{1,:x}, {2,:y}]
|
||||
|
||||
"""
|
||||
@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, [], fn f1 -> R.uniq(fun, f1) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -818,17 +753,12 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.with_index([1, 2, 3])
|
||||
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}]
|
||||
[{1,0},{2,1},{3,2}]
|
||||
|
||||
"""
|
||||
@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
|
||||
@@ -840,7 +770,7 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.concat([1..3, 4..6, 7..9])
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
[1,2,3,4,5,6,7,8,9]
|
||||
|
||||
"""
|
||||
@spec concat(Enumerable.t) :: Enumerable.t
|
||||
@@ -855,13 +785,13 @@ defmodule Stream do
|
||||
|
||||
iex> stream = Stream.concat(1..3, 4..6)
|
||||
iex> Enum.to_list(stream)
|
||||
[1, 2, 3, 4, 5, 6]
|
||||
[1,2,3,4,5,6]
|
||||
|
||||
iex> stream1 = Stream.cycle([1, 2, 3])
|
||||
iex> stream2 = Stream.cycle([4, 5, 6])
|
||||
iex> stream = Stream.concat(stream1, stream2)
|
||||
iex> Enum.take(stream, 6)
|
||||
[1, 2, 3, 1, 2, 3]
|
||||
[1,2,3,1,2,3]
|
||||
|
||||
"""
|
||||
@spec concat(Enumerable.t, Enumerable.t) :: Enumerable.t
|
||||
@@ -879,7 +809,7 @@ defmodule Stream do
|
||||
iex> concat = Stream.concat(1..3, 4..6)
|
||||
iex> cycle = Stream.cycle([:a, :b, :c])
|
||||
iex> Stream.zip(concat, cycle) |> Enum.to_list
|
||||
[{1, :a}, {2, :b}, {3, :c}, {4, :a}, {5, :b}, {6, :c}]
|
||||
[{1,:a},{2,:b},{3,:c},{4,:a},{5,:b},{6,:c}]
|
||||
|
||||
"""
|
||||
@spec zip(Enumerable.t, Enumerable.t) :: Enumerable.t
|
||||
@@ -922,7 +852,7 @@ defmodule Stream do
|
||||
{:suspended, [i|fun_acc], fun} ->
|
||||
do_zip(t, acc, callback, [i|list], [{fun, fun_acc}|buffer])
|
||||
{_, _} ->
|
||||
do_zip_close(:lists.reverse(buffer, t))
|
||||
do_zip_close(:lists.reverse(buffer) ++ t)
|
||||
{:done, acc}
|
||||
end
|
||||
end
|
||||
@@ -950,9 +880,9 @@ defmodule Stream do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> stream = Stream.cycle([1, 2, 3])
|
||||
iex> stream = Stream.cycle([1,2,3])
|
||||
iex> Enum.take(stream, 5)
|
||||
[1, 2, 3, 1, 2]
|
||||
[1,2,3,1,2]
|
||||
|
||||
"""
|
||||
@spec cycle(Enumerable.t) :: Enumerable.t
|
||||
@@ -1003,13 +933,13 @@ defmodule Stream do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Emits a sequence of values, starting with `start_value`. Successive
|
||||
Emit a sequence of values, starting with `start_value`. Successive
|
||||
values are generated by calling `next_fun` on the previous value.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Stream.iterate(0, &(&1+1)) |> Enum.take(5)
|
||||
[0, 1, 2, 3, 4]
|
||||
[0,1,2,3,4]
|
||||
|
||||
"""
|
||||
@spec iterate(element, (element -> element)) :: Enumerable.t
|
||||
@@ -1028,10 +958,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
|
||||
|
||||
@@ -17,9 +17,9 @@ defmodule Stream.Reducers do
|
||||
end
|
||||
|
||||
if count == unquote(n) do
|
||||
next_with_acc(unquote(f), :lists.reverse(buffer), h, new, t)
|
||||
cont_with_acc(unquote(f), :lists.reverse(buffer), h, new, t)
|
||||
else
|
||||
skip(acc(h, new, t))
|
||||
{:cont, acc(h, new, t)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -31,24 +31,12 @@ 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))
|
||||
{:cont, acc(h, {[entry|buffer], value}, t)}
|
||||
else
|
||||
next_with_acc(unquote(f), :lists.reverse(buffer), h, {[entry], new_value}, t)
|
||||
cont_with_acc(unquote(f), :lists.reverse(buffer), h, {[entry], new_value}, t)
|
||||
end
|
||||
entry, acc(h, nil, t) ->
|
||||
skip(acc(h, {[entry], unquote(callback).(entry)}, t))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defmacro dedup(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(h, prev, t) = acc) ->
|
||||
value = unquote(callback).(entry)
|
||||
case prev do
|
||||
{:value, ^value} -> skip(acc)
|
||||
_ -> next_with_acc(unquote(f), entry, h, {:value, value}, t)
|
||||
end
|
||||
{:cont, acc(h, {[entry], unquote(callback).(entry)}, t)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -57,9 +45,9 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn
|
||||
_entry, acc(h, n, t) when n > 0 ->
|
||||
skip(acc(h, n-1, t))
|
||||
{:cont, acc(h, n-1, t)}
|
||||
entry, acc(h, n, t) ->
|
||||
next_with_acc(unquote(f), entry, h, n, t)
|
||||
cont_with_acc(unquote(f), entry, h, n, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -68,9 +56,9 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn entry, acc(h, bool, t) = orig ->
|
||||
if bool and unquote(callback).(entry) do
|
||||
skip(orig)
|
||||
{:cont, orig}
|
||||
else
|
||||
next_with_acc(unquote(f), entry, h, false, t)
|
||||
cont_with_acc(unquote(f), entry, h, false, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -80,9 +68,9 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(callback).(entry) do
|
||||
next(unquote(f), entry, acc)
|
||||
cont(unquote(f), entry, acc)
|
||||
else
|
||||
skip(acc)
|
||||
{:cont, acc}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -92,9 +80,9 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(filter).(entry) do
|
||||
next(unquote(f), unquote(mapper).(entry), acc)
|
||||
cont(unquote(f), unquote(mapper).(entry), acc)
|
||||
else
|
||||
skip(acc)
|
||||
{:cont, acc}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -103,7 +91,7 @@ defmodule Stream.Reducers do
|
||||
defmacro map(callback, f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
next(unquote(f), unquote(callback).(entry), acc)
|
||||
cont(unquote(f), unquote(callback).(entry), acc)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -112,9 +100,9 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
unless unquote(callback).(entry) do
|
||||
next(unquote(f), entry, acc)
|
||||
cont(unquote(f), entry, acc)
|
||||
else
|
||||
skip(acc)
|
||||
{:cont, acc}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -124,10 +112,10 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn
|
||||
entry, acc(h, :first, t) ->
|
||||
next_with_acc(unquote(f), entry, h, {:ok, entry}, t)
|
||||
cont_with_acc(unquote(f), entry, h, {:ok, entry}, t)
|
||||
entry, acc(h, {:ok, acc}, t) ->
|
||||
value = unquote(callback).(entry, acc)
|
||||
next_with_acc(unquote(f), value, h, {:ok, value}, t)
|
||||
cont_with_acc(unquote(f), value, h, {:ok, value}, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -136,7 +124,7 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc(h, acc, t)) ->
|
||||
value = unquote(callback).(entry, acc)
|
||||
next_with_acc(unquote(f), value, h, value, t)
|
||||
cont_with_acc(unquote(f), value, h, value, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -144,16 +132,10 @@ defmodule Stream.Reducers do
|
||||
defmacro take(f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(h, n, t) = orig) ->
|
||||
case n do
|
||||
0 ->
|
||||
{:halt, orig}
|
||||
1 ->
|
||||
case next_with_acc(unquote(f), entry, h, n-1, t) do
|
||||
{:cont, acc} -> {:halt, acc}
|
||||
reason -> reason
|
||||
end
|
||||
_ ->
|
||||
next_with_acc(unquote(f), entry, h, n-1, t)
|
||||
if n >= 1 do
|
||||
cont_with_acc(unquote(f), entry, h, n-1, t)
|
||||
else
|
||||
{:halt, orig}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -164,9 +146,9 @@ defmodule Stream.Reducers do
|
||||
fn
|
||||
entry, acc(h, n, t) when n === :first
|
||||
when n === unquote(nth) ->
|
||||
next_with_acc(unquote(f), entry, h, 1, t)
|
||||
cont_with_acc(unquote(f), entry, h, 1, t)
|
||||
entry, acc(h, n, t) ->
|
||||
skip(acc(h, n+1, t))
|
||||
{:cont, acc(h, n+1, t)}
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -175,7 +157,7 @@ defmodule Stream.Reducers do
|
||||
quote do
|
||||
fn(entry, acc) ->
|
||||
if unquote(callback).(entry) do
|
||||
next(unquote(f), entry, acc)
|
||||
cont(unquote(f), entry, acc)
|
||||
else
|
||||
{:halt, acc}
|
||||
end
|
||||
@@ -187,10 +169,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
|
||||
skip(acc)
|
||||
if :lists.member(value, prev) do
|
||||
{:cont, acc}
|
||||
else
|
||||
next_with_acc(unquote(f), entry, h, Map.put(prev, value, true), t)
|
||||
cont_with_acc(unquote(f), entry, h, [value|prev], t)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -199,7 +181,7 @@ defmodule Stream.Reducers do
|
||||
defmacro with_index(f \\ nil) do
|
||||
quote do
|
||||
fn(entry, acc(h, counter, t)) ->
|
||||
next_with_acc(unquote(f), {entry, counter}, h, counter + 1, t)
|
||||
cont_with_acc(unquote(f), {entry, counter}, h, counter + 1, t)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
+314
-705
File diff suppressed because it is too large
Load Diff
@@ -2,14 +2,14 @@ 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)`.
|
||||
"""
|
||||
|
||||
@@ -55,24 +55,6 @@ defmodule StringIO do
|
||||
GenServer.call(pid, :contents)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Flushes output buffer.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> {:ok, pid} = StringIO.open("in")
|
||||
iex> IO.write(pid, "out")
|
||||
iex> StringIO.flush(pid)
|
||||
"out"
|
||||
iex> StringIO.contents(pid)
|
||||
{"in", ""}
|
||||
|
||||
"""
|
||||
@spec flush(pid) :: binary
|
||||
def flush(pid) when is_pid(pid) do
|
||||
GenServer.call(pid, :flush)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the IO device and returns remaining buffers.
|
||||
|
||||
@@ -109,10 +91,6 @@ defmodule StringIO do
|
||||
{:reply, {input, output}, s}
|
||||
end
|
||||
|
||||
def handle_call(:flush, _from, %{output: output} = s) do
|
||||
{:reply, output, %{s | output: ""}}
|
||||
end
|
||||
|
||||
def handle_call(:close, _from, %{input: input, output: output} = s) do
|
||||
{:stop, :normal, {:ok, {input, output}}, s}
|
||||
end
|
||||
@@ -128,12 +106,12 @@ defmodule StringIO do
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, chars}, %{output: output} = s) do
|
||||
{:ok, %{s | output: <<output::binary, IO.chardata_to_string(chars)::binary>>}}
|
||||
{:ok, %{s | output: << output :: binary, IO.chardata_to_string(chars) :: binary >>}}
|
||||
end
|
||||
|
||||
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>>}}
|
||||
{:ok, %{s | output: << output :: binary, IO.chardata_to_string(chars) :: binary >>}}
|
||||
end
|
||||
|
||||
defp io_request({:put_chars, _encoding, chars}, s) do
|
||||
@@ -205,7 +183,7 @@ defmodule StringIO do
|
||||
{error, s}
|
||||
{result, input} ->
|
||||
if capture_prompt do
|
||||
output = <<output::binary, IO.chardata_to_string(prompt)::binary>>
|
||||
output = << output :: binary, IO.chardata_to_string(prompt) :: binary >>
|
||||
end
|
||||
|
||||
{result, %{s | input: input, output: output}}
|
||||
@@ -221,7 +199,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
|
||||
|
||||
@@ -231,7 +209,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
|
||||
@@ -253,7 +231,7 @@ defmodule StringIO do
|
||||
{result, input} = do_get_line(chars, encoding)
|
||||
|
||||
if capture_prompt do
|
||||
output = <<output::binary, IO.chardata_to_string(prompt)::binary>>
|
||||
output = << output :: binary, IO.chardata_to_string(prompt) :: binary >>
|
||||
end
|
||||
|
||||
{result, %{s | input: input, output: output}}
|
||||
@@ -283,7 +261,7 @@ defmodule StringIO do
|
||||
{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>>
|
||||
output = << output :: binary, :binary.copy(IO.chardata_to_string(prompt), count) :: binary >>
|
||||
end
|
||||
|
||||
input =
|
||||
|
||||
+23
-105
@@ -1,15 +1,15 @@
|
||||
defmodule Supervisor do
|
||||
@moduledoc ~S"""
|
||||
@moduledoc """
|
||||
A behaviour module for implementing supervision functionality.
|
||||
|
||||
A supervisor is a process which supervises other processes, called
|
||||
child processes. Supervisors are used to build a hierarchical process
|
||||
A supervisor is a process which supervises other processes called
|
||||
child processes. Supervisors are used to build an hierarchical process
|
||||
structure called a supervision tree, a nice way to structure fault-tolerant
|
||||
applications.
|
||||
|
||||
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.
|
||||
reporting. It will also fit into an supervision tree.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -20,8 +20,8 @@ defmodule Supervisor do
|
||||
defmodule Stack do
|
||||
use GenServer
|
||||
|
||||
def start_link(state, opts \\ []) do
|
||||
GenServer.start_link(__MODULE__, state, opts)
|
||||
def start_link(state) do
|
||||
GenServer.start_link(__MODULE__, state, [name: :sup_stack])
|
||||
end
|
||||
|
||||
def handle_call(:pop, _from, [h|t]) do
|
||||
@@ -38,17 +38,16 @@ defmodule Supervisor do
|
||||
# Import helpers for defining supervisors
|
||||
import Supervisor.Spec
|
||||
|
||||
# We are going to supervise the Stack server which
|
||||
# will be started with a single argument [:hello]
|
||||
# and the default name of :sup_stack.
|
||||
# We are going to supervise the Stack server which will
|
||||
# be started with a single argument [:hello]
|
||||
children = [
|
||||
worker(Stack, [[:hello], [name: :sup_stack]])
|
||||
worker(Stack, [[:hello]])
|
||||
]
|
||||
|
||||
# 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
|
||||
Notice that when starting the GenServer, we have registered it
|
||||
with name `:sup_stack`, which allows us to call it directly and
|
||||
get what is on the stack:
|
||||
|
||||
@@ -66,7 +65,7 @@ defmodule Supervisor do
|
||||
Let's try it:
|
||||
|
||||
GenServer.call(:sup_stack, :pop)
|
||||
** (exit) exited in: GenServer.call(:sup_stack, :pop, 5000)
|
||||
=ERROR REPORT====
|
||||
|
||||
Luckily, since the server is being supervised by a supervisor, the
|
||||
supervisor will automatically start a new one, with the default stack
|
||||
@@ -80,7 +79,7 @@ defmodule Supervisor do
|
||||
configuration, shutdown values, and restart strategies.
|
||||
|
||||
Continue reading this moduledoc to learn more about supervision strategies
|
||||
and then proceed to the `Supervisor.Spec` module documentation to learn
|
||||
and then follow to the `Supervisor.Spec` module documentation to learn
|
||||
about the specification for workers and supervisors.
|
||||
|
||||
## Module-based supervisors
|
||||
@@ -107,13 +106,13 @@ defmodule Supervisor do
|
||||
|
||||
You may want to use a module-based supervisor if:
|
||||
|
||||
* You need to perform some particular action on supervisor
|
||||
initialization, like setting up an ETS table.
|
||||
* You need to do some particular action on supervisor
|
||||
initialization, like setting up a ETS table.
|
||||
|
||||
* You want to perform partial hot-code swapping of the
|
||||
tree. For example, if you add or remove children,
|
||||
tree. For example, if you add or remove a 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.
|
||||
|
||||
@@ -137,70 +136,6 @@ defmodule Supervisor do
|
||||
in this module behave slightly differently when this strategy is
|
||||
used.
|
||||
|
||||
## Simple one for one
|
||||
|
||||
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
|
||||
|
||||
# This time, we don't pass any argument because
|
||||
# the argument will be given when we start the child
|
||||
children = [
|
||||
worker(Stack, [], restart: :transient)
|
||||
]
|
||||
|
||||
# Start the supervisor with our one child
|
||||
{:ok, sup_pid} = Supervisor.start_link(children, strategy: :simple_one_for_one)
|
||||
|
||||
There are a couple differences here:
|
||||
|
||||
* 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`
|
||||
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 on `Supervisor.Spec`
|
||||
|
||||
With the supervisor defined, let's dynamically start stacks:
|
||||
|
||||
{:ok, pid} = Supervisor.start_child(sup_pid, [[:hello, :world], []])
|
||||
GenServer.call(pid, :pop) #=> :hello
|
||||
GenServer.call(pid, :pop) #=> :world
|
||||
|
||||
{:ok, pid} = Supervisor.start_child(sup_pid, [[:something, :else], []])
|
||||
GenServer.call(pid, :pop) #=> :something
|
||||
GenServer.call(pid, :pop) #=> :else
|
||||
|
||||
Supervisor.count_children(sup_pid)
|
||||
#=> %{active: 2, specs: 1, supervisors: 0, workers: 2}
|
||||
|
||||
## 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 crashes with
|
||||
reason `:normal`, `:shutdown` or `{:shutdown, term}`.
|
||||
|
||||
So one may ask: which exit reason should I choose when exiting 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
|
||||
|
||||
* `: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 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 trapping exits
|
||||
|
||||
## Name Registration
|
||||
|
||||
A supervisor is bound to the same name registration rules as a `GenServer`.
|
||||
@@ -245,7 +180,7 @@ defmodule Supervisor do
|
||||
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
|
||||
@@ -265,7 +200,7 @@ defmodule Supervisor do
|
||||
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
|
||||
@spec start_link([tuple], options) :: on_start
|
||||
def start_link(children, options) when is_list(children) do
|
||||
spec = Supervisor.Spec.supervise(children, options)
|
||||
start_link(Supervisor.Default, spec, options)
|
||||
@@ -291,7 +226,6 @@ defmodule Supervisor do
|
||||
|
||||
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
|
||||
def start_link(module, arg, options \\ []) when is_list(options) do
|
||||
case Keyword.get(options, :name) do
|
||||
@@ -316,19 +250,19 @@ defmodule Supervisor do
|
||||
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,
|
||||
If there already exists a child specification with the specified id,
|
||||
`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 starts, function returns `{:ok, child}` or `{:ok, child, info}`,
|
||||
If the child process start 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 starts, function returns `:ignore`, the child specification is
|
||||
If the child process start 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 starts, function returns an error tuple or an erroneous value,
|
||||
If the child process start 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.
|
||||
@@ -428,7 +362,7 @@ defmodule Supervisor do
|
||||
|
||||
* `type` - `:worker` or `:supervisor` as defined in the child specification
|
||||
|
||||
* `modules` - as defined in the child specification
|
||||
* `modules` – as defined in the child specification
|
||||
"""
|
||||
@spec which_children(supervisor) ::
|
||||
[{Supervisor.Spec.child_id | :undefined,
|
||||
@@ -463,22 +397,6 @@ defmodule Supervisor do
|
||||
call(supervisor, :count_children) |> :maps.from_list
|
||||
end
|
||||
|
||||
@doc """
|
||||
Stops the supervisor with the given `reason`.
|
||||
|
||||
It returns `:ok` if the supervisor 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.
|
||||
"""
|
||||
@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
|
||||
|
||||
@@ -3,7 +3,7 @@ defmodule Supervisor.Default do
|
||||
@behaviour :supervisor
|
||||
|
||||
@doc """
|
||||
Supervisor callback that simply returns the given args.
|
||||
Supevisor callback that simply returns the given args.
|
||||
|
||||
This is the supervisor used by `Supervisor.start_link/2`.
|
||||
"""
|
||||
|
||||
@@ -40,7 +40,7 @@ defmodule Supervisor.Spec do
|
||||
|
||||
Explicit supervisors as above are required when there is a need to:
|
||||
|
||||
1. Partially change the supervision tree during hot-code swaps.
|
||||
1. Partialy change the supervision tree during hot-code swaps.
|
||||
|
||||
2. Define supervisors inside other supervisors.
|
||||
|
||||
@@ -52,7 +52,7 @@ defmodule Supervisor.Spec do
|
||||
|
||||
## Supervisor and worker options
|
||||
|
||||
In the example above, we defined workers and supervisors
|
||||
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
|
||||
@@ -61,7 +61,7 @@ defmodule Supervisor.Spec do
|
||||
|
||||
* `:function` - the function to invoke on the child to start it
|
||||
|
||||
* `:restart` - defines when a terminated child process should be restarted
|
||||
* `:restart` - defines when the child process should restart
|
||||
|
||||
* `:shutdown` - defines how a child process should be terminated
|
||||
|
||||
@@ -70,7 +70,7 @@ defmodule Supervisor.Spec do
|
||||
child process is a `Supervisor` or `GenServer`; if the child
|
||||
process is a `GenEvent`, modules should be `:dynamic`
|
||||
|
||||
### Restart values (:restart)
|
||||
### Restart values
|
||||
|
||||
The following restart values are supported:
|
||||
|
||||
@@ -83,21 +83,21 @@ defmodule Supervisor.Spec do
|
||||
terminates abnormally, i.e. with another exit reason than
|
||||
`:normal`, `:shutdown` or `{:shutdown, term}`
|
||||
|
||||
### Shutdown values (:shutdown)
|
||||
### Shutdown values
|
||||
|
||||
The following shutdown values are supported:
|
||||
|
||||
* `:brutal_kill` - the child process is unconditionally terminated
|
||||
using `exit(child, :kill)`.
|
||||
|
||||
* `:infinity` - if the child process is a supervisor, this is a mechanism
|
||||
* `: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, the value can also be any integer meaning that the supervisor tells
|
||||
* 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
|
||||
within the specified time (in miliseconds), the child process is
|
||||
unconditionally terminated using `Process.exit(child, :kill)`.
|
||||
"""
|
||||
|
||||
|
||||
+48
-201
@@ -17,7 +17,7 @@ defmodule System do
|
||||
end
|
||||
end
|
||||
|
||||
# Read and strip the version from the VERSION file.
|
||||
# Read and strip the version from the `VERSION` file.
|
||||
defmacrop get_version do
|
||||
case read_stripped(:filename.join(__DIR__, "../../../VERSION")) do
|
||||
"" -> raise RuntimeError, message: "could not read the version number from VERSION"
|
||||
@@ -25,15 +25,15 @@ defmodule System do
|
||||
end
|
||||
end
|
||||
|
||||
# Tries to run "git rev-parse --short HEAD". In the case of success returns
|
||||
# the short revision hash. If that is not available, tries to read the commit hash
|
||||
# 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_revision do
|
||||
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 rev-parse --short HEAD')
|
||||
data = :os.cmd('git describe --always --tags')
|
||||
strip_re(data, "\n")
|
||||
else
|
||||
read_stripped(:filename.join(".git", "HEAD"))
|
||||
@@ -47,21 +47,6 @@ defmodule System 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.
|
||||
|
||||
@@ -73,32 +58,32 @@ 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
|
||||
%{version: version, date: get_date, revision: get_revision}
|
||||
%{version: version, tag: get_describe, date: get_date}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Lists command line arguments.
|
||||
List command line arguments.
|
||||
|
||||
Returns the list of command line arguments passed to the program.
|
||||
"""
|
||||
@spec argv() :: [String.t]
|
||||
def argv do
|
||||
:elixir_config.get(:argv)
|
||||
:elixir_code_server.call :argv
|
||||
end
|
||||
|
||||
@doc """
|
||||
Modifies command line arguments.
|
||||
Modify command line arguments.
|
||||
|
||||
Changes the list of command line arguments. Use it with caution,
|
||||
as it destroys any previous argv information.
|
||||
"""
|
||||
@spec argv([String.t]) :: :ok
|
||||
def argv(args) do
|
||||
:elixir_config.put(:argv, args)
|
||||
:elixir_code_server.cast({:argv, args})
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -137,9 +122,13 @@ defmodule System do
|
||||
User home directory.
|
||||
|
||||
Returns the user home directory (platform independent).
|
||||
Returns `nil` if no user home is set.
|
||||
"""
|
||||
def user_home do
|
||||
:elixir_config.get(:home)
|
||||
case :os.type() do
|
||||
{:win32, _} -> get_windows_home
|
||||
_ -> get_unix_home
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -153,6 +142,20 @@ defmodule System do
|
||||
raise RuntimeError, message: "could not find the user home, please set the HOME environment variable"
|
||||
end
|
||||
|
||||
defp get_unix_home do
|
||||
get_env("HOME")
|
||||
end
|
||||
|
||||
defp get_windows_home do
|
||||
:filename.absname(
|
||||
get_env("USERPROFILE") || (
|
||||
hd = get_env("HOMEDRIVE")
|
||||
hp = get_env("HOMEPATH")
|
||||
hd && hp && hd <> hp
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
Writable temporary directory.
|
||||
|
||||
@@ -221,7 +224,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_code_server.cast {:at_exit, fun}
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -277,13 +280,13 @@ defmodule System do
|
||||
Returns the process identifier of the current Erlang emulator
|
||||
in the format most commonly used by the operating system environment.
|
||||
|
||||
For more information, see [`:os.getpid/0`](http://www.erlang.org/doc/man/os.html#getpid-0).
|
||||
See http://www.erlang.org/doc/man/os.html#getpid-0 for more info.
|
||||
"""
|
||||
@spec get_pid() :: binary
|
||||
def get_pid, do: IO.iodata_to_binary(:os.getpid)
|
||||
|
||||
@doc """
|
||||
Sets an environment variable value.
|
||||
Set an environment variable value.
|
||||
|
||||
Sets a new `value` for the environment variable `varname`.
|
||||
"""
|
||||
@@ -294,14 +297,14 @@ defmodule System do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Sets multiple environment variables.
|
||||
Set multiple environment variables.
|
||||
|
||||
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 """
|
||||
@@ -329,7 +332,7 @@ defmodule System do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Halts the Erlang runtime system.
|
||||
Halt the Erlang runtime system.
|
||||
|
||||
Halts the Erlang runtime system where the argument `status` must be a
|
||||
non-negative integer, the atom `:abort` or a binary.
|
||||
@@ -340,13 +343,13 @@ defmodule System do
|
||||
* If `:abort`, the runtime system aborts producing a core dump, if that is
|
||||
enabled in the operating system.
|
||||
|
||||
* If a string, an Erlang crash dump is produced with status as slogan,
|
||||
* If a string, an erlang crash dump is produced with status as slogan,
|
||||
and then the runtime system exits with status code 1.
|
||||
|
||||
Note that on many platforms, only the status codes 0-255 are supported
|
||||
by the operating system.
|
||||
|
||||
For more information, see [`:erlang.halt/1`](http://www.erlang.org/doc/man/erlang.html#halt-1).
|
||||
For more information, check: http://www.erlang.org/doc/man/erlang.html#halt-1
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -367,7 +370,7 @@ defmodule System do
|
||||
:erlang.halt(String.to_char_list(status))
|
||||
end
|
||||
|
||||
@doc ~S"""
|
||||
@doc """
|
||||
Executes the given `command` with `args`.
|
||||
|
||||
`command` is expected to be an executable available in PATH
|
||||
@@ -378,20 +381,7 @@ defmodule System do
|
||||
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}
|
||||
A set of options are also supported and described below.
|
||||
|
||||
## Options
|
||||
|
||||
@@ -399,9 +389,9 @@ defmodule System do
|
||||
* `:cd` - the directory to run the command in
|
||||
* `:env` - an enumerable of tuples containing environment key-value as binary
|
||||
* `:arg0` - set the command arg0
|
||||
* `:stderr_to_stdout` - redirects stderr to stdout when `true`
|
||||
* `:parallelism` - when `true`, the VM will schedule port tasks to improve
|
||||
parallelism in the system. If set to `false`, the VM will try to perform
|
||||
* `:stderr_to_stdout` - redirects stderr to stdout when true
|
||||
* `:parallelism` - when true, the VM will schedule port tasks to improve
|
||||
parallelism in the system. If set to false, the VM will try to perform
|
||||
commands immediately, improving latency at the expense of parallelism.
|
||||
The default can be set on system startup by passing the "+spp" argument
|
||||
to `--erl`.
|
||||
@@ -436,7 +426,7 @@ defmodule System do
|
||||
|
||||
If you desire to execute a trusted command inside a shell, with pipes,
|
||||
redirecting and so on, please check
|
||||
[`:os.cmd/1`](http://www.erlang.org/doc/man/os.html#cmd-1).
|
||||
[Erlang's :os.cmd/1 function](http://www.erlang.org/doc/man/os.html#cmd-1).
|
||||
"""
|
||||
@spec cmd(binary, [binary], Keyword.t) ::
|
||||
{Collectable.t, exit_status :: non_neg_integer}
|
||||
@@ -452,16 +442,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
|
||||
@@ -469,7 +450,7 @@ 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
|
||||
|
||||
@@ -502,144 +483,10 @@ defmodule System do
|
||||
|
||||
defp validate_env(enum) do
|
||||
Enum.map enum, fn
|
||||
{k, nil} ->
|
||||
{String.to_char_list(k), false}
|
||||
{k, 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.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
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.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.monotonic_time/1`.
|
||||
"""
|
||||
@spec monotonic_time(:erlang.time_unit) :: integer
|
||||
def monotonic_time(unit) do
|
||||
:erlang.monotonic_time(unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current system time in the `:native` time unit.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
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.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.system_time/1`.
|
||||
"""
|
||||
@spec system_time(:erlang.time_unit) :: integer
|
||||
def system_time(unit) do
|
||||
:erlang.system_time(unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts `time` from time unit `from_unit` to time unit `to_unit`. The result
|
||||
is rounded via the floor function.
|
||||
|
||||
Inlined by the compiler into `:erlang.convert_time_unit/3`.
|
||||
"""
|
||||
@spec convert_time_unit(integer, :erlang.time_unit, :erlang.time_unit) :: integer
|
||||
def convert_time_unit(time, from_unit, to_unit) do
|
||||
:erlang.convert_time_unit(time, from_unit, to_unit)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns the current time offset between the Erlang monotonic time and the
|
||||
Erlang 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 monotonic time and the
|
||||
Erlang 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.
|
||||
|
||||
For more information, see the [chapter on time and time
|
||||
correction](http://www.erlang.org/doc/apps/erts/time_correction.html) in the
|
||||
Erlang docs.
|
||||
|
||||
Inlined by the compiler into `:erlang.time_offset/1`.
|
||||
"""
|
||||
@spec time_offset(:erlang.time_unit) :: integer
|
||||
def time_offset(unit) do
|
||||
:erlang.time_offset(unit)
|
||||
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 an 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
|
||||
end
|
||||
|
||||
+68
-454
@@ -1,58 +1,36 @@
|
||||
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 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 waited 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 supervision tree and dynamically spawned
|
||||
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 use 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:
|
||||
|
||||
1. If you are using async tasks, you must await a reply
|
||||
as they are *always* sent. If you are not expecting a reply,
|
||||
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
|
||||
meant to receive the result no longer exists, there is
|
||||
no purpose in completing computation of the result. 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 or to handle a timeout. If a reply does not arrive within the
|
||||
desired time, `Task.shutdown/2` can be used to stop the task.
|
||||
`Task.await/2` is used to read the message sent by the task. On
|
||||
`await`, Elixir will also setup a monitor to verify if the process
|
||||
exited for any abnormal reason (or in case exits are being
|
||||
trapped by the caller).
|
||||
|
||||
## Supervised tasks
|
||||
|
||||
@@ -70,70 +48,44 @@ defmodule Task do
|
||||
]
|
||||
|
||||
Since these tasks are supervised and not directly linked to
|
||||
the caller, they cannot be waited on. Note `start_link/1`,
|
||||
the caller, they cannot be awaited on. Note `start_link/1`,
|
||||
unlike `async/1`, returns `{:ok, pid}` (which is
|
||||
the result expected by supervision trees).
|
||||
|
||||
By default, most supervision strategies will try to restart
|
||||
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`.
|
||||
## Supervision trees
|
||||
|
||||
## Dynamically supervised tasks
|
||||
|
||||
The `Task.Supervisor` module allows developers to dynamically
|
||||
create multiple supervised tasks.
|
||||
|
||||
A short example is:
|
||||
The `Task.Supervisor` module allows developers to start supervisors
|
||||
that dynamically supervise tasks:
|
||||
|
||||
{:ok, pid} = Task.Supervisor.start_link()
|
||||
task = Task.Supervisor.async(pid, fn ->
|
||||
# Do something
|
||||
end)
|
||||
Task.await(task)
|
||||
Task.Supervisor.async(pid, MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
However, in the majority of cases, you want to add the task supervisor
|
||||
to your supervision tree:
|
||||
`Task.Supervisor` also makes it possible to spawn tasks in remote nodes as
|
||||
long as the supervisor is registered locally or globally:
|
||||
|
||||
# In the remote node
|
||||
Task.Supervisor.start_link(name: :tasks_sup)
|
||||
|
||||
# In the client
|
||||
Task.Supervisor.async({:tasks_sup, :remote@local}, MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
`Task.Supervisor` is more often started in your supervision tree as:
|
||||
|
||||
import Supervisor.Spec
|
||||
|
||||
children = [
|
||||
supervisor(Task.Supervisor, [[name: MyApp.TaskSupervisor]])
|
||||
supervisor(Task.Supervisor, [[name: :tasks_sup]])
|
||||
]
|
||||
|
||||
Now you can dynamically start supervised tasks:
|
||||
|
||||
Task.Supervisor.start_child(MyApp.TaskSupervisor, fn ->
|
||||
# Do something
|
||||
end)
|
||||
|
||||
Or even use the async/await pattern:
|
||||
|
||||
Task.Supervisor.async(MyApp.TaskSupervisor, fn ->
|
||||
# Do something
|
||||
end) |> Task.await()
|
||||
|
||||
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 a task
|
||||
supervisor to dynamically spawn tasks across nodes:
|
||||
|
||||
# In the remote node
|
||||
Task.Supervisor.start_link(name: MyApp.DistSupervisor)
|
||||
|
||||
# In the client
|
||||
Task.Supervisor.async({MyApp.DistSupervisor, :remote@local},
|
||||
MyMod, :my_fun, [arg1, arg2, arg3])
|
||||
|
||||
Note that, when working with distributed tasks, one should use the `async/4` function
|
||||
that expects explicit module, function and arguments, instead of `async/2` that
|
||||
works with anonymous functions. That's because anonymous functions expect
|
||||
Note that, when working with distributed tasks, one should use the `async/3` API,
|
||||
that expects explicit module, function and arguments, instead of `async/1` that
|
||||
works with anonymous functions. That's because the anonymous function API expects
|
||||
the same module version to exist on all involved nodes. Check the `Agent` module
|
||||
documentation for more information on distributed processes as the limitations
|
||||
documentation for more information on distributed processes, as the limitations
|
||||
described in the agents documentation apply to the whole ecosystem.
|
||||
|
||||
Finally, check `Task.Supervisor` for other operations supported by the Task
|
||||
supervisor.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
@@ -141,15 +93,13 @@ defmodule Task do
|
||||
|
||||
It contains two fields:
|
||||
|
||||
* `:pid` - the process reference of the task process; `nil` if the task does
|
||||
not use a task process.
|
||||
* `:pid` - the process reference of the task process; it may be a pid
|
||||
or a tuple containing the process and node names
|
||||
|
||||
* `: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__{}
|
||||
|
||||
@@ -173,7 +123,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}
|
||||
@@ -185,7 +135,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}
|
||||
@@ -200,9 +150,6 @@ defmodule Task do
|
||||
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`.
|
||||
|
||||
## Task's message format
|
||||
|
||||
The reply sent by the task will be in the format `{ref, msg}`,
|
||||
@@ -214,71 +161,18 @@ 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.
|
||||
|
||||
Read the `Task` module documentation for more info on general
|
||||
usage of `async/1` and `async/3`.
|
||||
|
||||
## Linking
|
||||
|
||||
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 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 considered 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 exists 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.
|
||||
|
||||
* 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, msg}`,
|
||||
where `ref` is the monitoring reference held by the task.
|
||||
Similar to `async/1`, but the task is specified by the given
|
||||
module, function and arguments.
|
||||
"""
|
||||
@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
|
||||
@@ -295,41 +189,18 @@ defmodule Task do
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
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,
|
||||
the task will continue to run. When the calling process exits, its
|
||||
exit signal will terminate 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 may wait for the duration of the
|
||||
timeout awaiting the message.
|
||||
|
||||
This function will always exit and demonitor if the task crashes or if
|
||||
it times out, so the task can not be used again. To explicitly handle
|
||||
the timeout or the crash, use `yield/2` instead.
|
||||
"""
|
||||
@spec await(t, timeout) :: term | no_return
|
||||
def await(task, timeout \\ 5000)
|
||||
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def await(%Task{owner: owner}=task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def await(%Task{ref: ref, owner: owner}=task, timeout) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
def await(%Task{ref: ref}=task, timeout \\ 5000) do
|
||||
receive do
|
||||
{^ref, reply} ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
reply
|
||||
{:DOWN, ^ref, _, proc, reason} ->
|
||||
exit({reason(reason, proc), {__MODULE__, :await, [task, timeout]}})
|
||||
{:DOWN, ^ref, _, _, :noconnection} ->
|
||||
mfa = {__MODULE__, :await, [task, timeout]}
|
||||
exit({{:nodedown, node(task.pid)}, mfa})
|
||||
{:DOWN, ^ref, _, _, reason} ->
|
||||
exit({reason, {__MODULE__, :await, [task, timeout]}})
|
||||
after
|
||||
timeout ->
|
||||
Process.demonitor(ref, [:flush])
|
||||
@@ -341,10 +212,10 @@ defmodule Task do
|
||||
Receives a group of tasks and a message and finds
|
||||
a task that matches the given message.
|
||||
|
||||
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 returns `nil`
|
||||
if no task was found. It exits if the task has failed.
|
||||
This function returns a tuple with the task and the
|
||||
returned value in case the message matches a task that
|
||||
exited with success, 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
|
||||
@@ -357,284 +228,27 @@ defmodule Task do
|
||||
|
||||
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
|
||||
|
||||
def 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, _, _, reason} = msg) when is_reference(ref) do
|
||||
find = fn(%Task{ref: task_ref}) -> task_ref == ref end
|
||||
case Enum.find(tasks, find) do
|
||||
%Task{pid: pid} when reason == :noconnection ->
|
||||
exit({{:nodedown, node(pid)}, {__MODULE__, :find, [tasks, msg]}})
|
||||
%Task{} ->
|
||||
exit({reason, {__MODULE__, :find, [tasks, msg]}})
|
||||
nil ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
def find(_tasks, _msg) do
|
||||
nil
|
||||
end
|
||||
|
||||
@doc """
|
||||
Yields for a task reply in the given time interval.
|
||||
|
||||
Returns `{:ok, reply}` if the reply is received, `{:exit, reason}`
|
||||
if the task exited or `nil` if no reply arrived.
|
||||
|
||||
A timeout, in milliseconds, can be given with default value
|
||||
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.
|
||||
|
||||
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 waits
|
||||
for the duration of the timeout awaiting the message.
|
||||
"""
|
||||
@spec yield(t, timeout) :: {:ok, term} | {:exit, term} | nil
|
||||
def yield(task, timeout \\ 5_000)
|
||||
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def yield(%Task{owner: owner} = task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def yield(%Task{ref: ref, owner: owner} = task, timeout) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
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}
|
||||
after
|
||||
timeout ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Yields to multiple tasks in the given time interval.
|
||||
|
||||
This function receives a list of tasks and await for their
|
||||
replies at once in the given time interval. It returns a list
|
||||
of tuples of two elements, with tasks as the first element and
|
||||
the `yield` result as the second.
|
||||
|
||||
Similar to `yield/2`, if the task replied in the given interval,
|
||||
it will return `{:ok, term}`, `{:exit, reason}`if it crashed or
|
||||
`nil` if it timed out. 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 shutdown, according
|
||||
to 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 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 exited abornormally, otherwise `nil`.
|
||||
|
||||
The shutdown method is either a timeout or `:brutal_kill`. In 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 exits abnormally, 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 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 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
|
||||
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
|
||||
|
||||
# TODO: Remove nil check in Elixir 1.3
|
||||
def shutdown(%Task{owner: owner} = task, _) when owner != nil and owner != self() do
|
||||
raise ArgumentError, invalid_owner_error(task)
|
||||
end
|
||||
|
||||
def shutdown(%Task{pid: pid, owner: owner} = task, :brutal_kill) do
|
||||
if is_nil(owner) do
|
||||
IO.write :stderr, "warning: a Task was created with the :owner field no set, " <>
|
||||
"please ensure the owner field is correctly set to self()\n" <>
|
||||
Exception.format_stacktrace
|
||||
end
|
||||
|
||||
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}
|
||||
result ->
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
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}
|
||||
result ->
|
||||
result
|
||||
end
|
||||
end
|
||||
|
||||
## Helpers
|
||||
|
||||
defp reason(:noconnection, proc), do: {:nodedown, monitor_node(proc)}
|
||||
defp reason(reason, _), do: reason
|
||||
|
||||
defp monitor_node(pid) when is_pid(pid), do: node(pid)
|
||||
defp monitor_node({_, node}), do: node
|
||||
|
||||
# spawn a process to ensure task gets exit signal if process dies from exit signal
|
||||
# between unlink and exit.
|
||||
defp exit(task, reason) do
|
||||
caller = self()
|
||||
ref = make_ref()
|
||||
enforcer = spawn(fn() -> enforce_exit(task, reason, caller, ref) end)
|
||||
Process.unlink(task)
|
||||
Process.exit(task, reason)
|
||||
send(enforcer, {:done, ref})
|
||||
:ok
|
||||
end
|
||||
|
||||
defp enforce_exit(pid, reason, caller, ref) do
|
||||
mon = Process.monitor(caller)
|
||||
receive do
|
||||
{:done, ^ref} -> :ok
|
||||
{:DOWN, ^mon, _, _, _} -> Process.exit(pid, reason)
|
||||
end
|
||||
end
|
||||
|
||||
defp shutdown_receive(%{ref: ref} = task, type, timeout) do
|
||||
receive do
|
||||
{:DOWN, ^ref, _, _, :shutdown} when type in [:shutdown, :timeout_kill] ->
|
||||
flush_reply(ref)
|
||||
{:DOWN, ^ref, _, _, :killed} when type == :brutal_kill ->
|
||||
flush_reply(ref)
|
||||
{:DOWN, ^ref, _, proc, reason} ->
|
||||
flush_reply(ref) || {:down, proc, reason}
|
||||
after
|
||||
timeout ->
|
||||
Process.exit(task.pid, :kill)
|
||||
shutdown_receive(task, :timeout_kill, :infinity)
|
||||
end
|
||||
end
|
||||
|
||||
defp flush_reply(ref) do
|
||||
receive do
|
||||
{^ref, reply} -> {:ok, reply}
|
||||
after
|
||||
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,38 +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 ->
|
||||
Process.link(caller)
|
||||
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(reason)
|
||||
after
|
||||
# There is a race condition on this operation when working across
|
||||
# node that manifests if a "Task.Supervisor.async/2" call is made
|
||||
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 accross
|
||||
# node that manifests if a `Task.Supervisor.async/1` call is made
|
||||
# while the supervisor is busy spawning previous tasks.
|
||||
#
|
||||
# Imagine the following workflow:
|
||||
@@ -54,15 +38,16 @@ defmodule Task.Supervised do
|
||||
# 5. The spawned task waits forever for the monitor reference so it can begin
|
||||
#
|
||||
# We have solved this by specifying a timeout of 5000 seconds.
|
||||
# Given no work is done in the client between the task start and
|
||||
# Given no work is done in the client in 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
|
||||
@@ -89,24 +74,22 @@ defmodule Task.Supervised do
|
||||
apply(module, fun, args)
|
||||
catch
|
||||
:error, value ->
|
||||
reason = {value, System.stacktrace()}
|
||||
exit(info, mfa, reason, reason)
|
||||
exit(info, mfa, {value, System.stacktrace()})
|
||||
:throw, value ->
|
||||
reason = {{:nocatch, value}, System.stacktrace()}
|
||||
exit(info, mfa, reason, reason)
|
||||
exit(info, mfa, {{:nocatch, value}, System.stacktrace()})
|
||||
:exit, value ->
|
||||
exit(info, mfa, {value, System.stacktrace()}, value)
|
||||
exit(info, mfa, value)
|
||||
end
|
||||
end
|
||||
|
||||
defp exit(_info, _mfa, _log_reason, reason)
|
||||
defp exit(_info, _mfa, reason)
|
||||
when reason == :normal
|
||||
when reason == :shutdown
|
||||
when tuple_size(reason) == 2 and elem(reason, 0) == :shutdown do
|
||||
exit(reason)
|
||||
end
|
||||
|
||||
defp exit(info, mfa, log_reason, reason) do
|
||||
defp exit(info, mfa, reason) do
|
||||
{fun, args} = get_running(mfa)
|
||||
|
||||
:error_logger.format(
|
||||
@@ -115,7 +98,7 @@ defmodule Task.Supervised do
|
||||
'** When function == ~p~n' ++
|
||||
'** arguments == ~p~n' ++
|
||||
'** Reason for termination == ~n' ++
|
||||
'** ~p~n', [self, get_from(info), fun, args, get_reason(log_reason)])
|
||||
'** ~p~n', [self, get_from(info), fun, args, reason])
|
||||
|
||||
exit(reason)
|
||||
end
|
||||
@@ -125,22 +108,4 @@ defmodule Task.Supervised do
|
||||
|
||||
defp get_running({:erlang, :apply, [fun, []]}) when is_function(fun, 0), do: {fun, []}
|
||||
defp get_running({mod, fun, args}), do: {:erlang.make_fun(mod, fun, length(args)), args}
|
||||
|
||||
defp get_reason({:undef, [{mod, fun, args, _info} | _] = stacktrace} = reason)
|
||||
when is_atom(mod) and is_atom(fun) do
|
||||
cond do
|
||||
:code.is_loaded(mod) === false ->
|
||||
{:"module could not be loaded", stacktrace}
|
||||
is_list(args) and not function_exported?(mod, fun, length(args)) ->
|
||||
{:"function not exported", stacktrace}
|
||||
is_integer(args) and not function_exported?(mod, fun, args) ->
|
||||
{:"function not exported", stacktrace}
|
||||
true ->
|
||||
reason
|
||||
end
|
||||
end
|
||||
|
||||
defp get_reason(reason) do
|
||||
reason
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,18 +1,19 @@
|
||||
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
|
||||
`:simple_one_for_one` supervisor where the workers are temporary
|
||||
(i.e. they are not restarted after they die).
|
||||
|
||||
See the `Task` module for more information.
|
||||
The functions in this module allow tasks to be spawned and awaited
|
||||
from a supervisor, similar to the functions defined in the `Task` module.
|
||||
|
||||
## 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,7 +27,7 @@ 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` as most tasks can't be effectively restarted after
|
||||
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
|
||||
@@ -48,8 +49,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,47 +60,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
|
||||
owner = self()
|
||||
args = [owner, :link, get_info(owner), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
Process.link(pid)
|
||||
ref = Process.monitor(pid)
|
||||
send pid, {owner, ref}
|
||||
%Task{pid: pid, ref: ref, owner: owner}
|
||||
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, 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
|
||||
owner = self()
|
||||
args = [owner, :monitor, get_info(owner), {module, fun, args}]
|
||||
args = [self, get_info(self), {module, fun, args}]
|
||||
{:ok, pid} = Supervisor.start_child(supervisor, args)
|
||||
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
|
||||
|
||||
@doc """
|
||||
@@ -108,7 +76,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 """
|
||||
@@ -116,7 +84,7 @@ defmodule Task.Supervisor do
|
||||
"""
|
||||
@spec children(Supervisor.supervisor) :: [pid]
|
||||
def children(supervisor) do
|
||||
Supervisor.which_children(supervisor) |> Enum.map(&elem(&1, 1))
|
||||
:supervisor.which_children(supervisor) |> Enum.map(&elem(&1, 1))
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
||||
+9
-42
@@ -1,15 +1,12 @@
|
||||
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`.
|
||||
"""
|
||||
|
||||
@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 +25,9 @@ defmodule Tuple do
|
||||
@doc """
|
||||
Inserts an element into a tuple.
|
||||
|
||||
Inserts `value` into `tuple` at the given `index`.
|
||||
Raises an `ArgumentError` if `index` is negative or greater than the
|
||||
length of `tuple`. Index is zero-based.
|
||||
Inserts `value` into `tuple` at the given zero-based `index`.
|
||||
Raises an `ArgumentError` if `index` is greater than the
|
||||
length of `tuple`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -39,40 +36,19 @@ defmodule Tuple do
|
||||
iex> tuple = {:bar, :baz}
|
||||
iex> Tuple.insert_at(tuple, 0, :foo)
|
||||
{:foo, :bar, :baz}
|
||||
iex> Tuple.insert_at(tuple, 2, :bong)
|
||||
{:bar, :baz, :bong}
|
||||
|
||||
"""
|
||||
@spec insert_at(tuple, non_neg_integer, term) :: tuple
|
||||
def insert_at(tuple, index, value) do
|
||||
:erlang.insert_element(index + 1, tuple, value)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Inserts an element at the end of a tuple.
|
||||
|
||||
Returns a new tuple with the element appended at the end, and contains
|
||||
the elements in `tuple` followed by `value` as the last element.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
iex> tuple = {:foo, :bar}
|
||||
iex> Tuple.append(tuple, :baz)
|
||||
{:foo, :bar, :baz}
|
||||
|
||||
"""
|
||||
@spec append(tuple, term) :: tuple
|
||||
def append(tuple, value) do
|
||||
:erlang.append_element(tuple, value)
|
||||
def insert_at(tuple, index, term) do
|
||||
:erlang.insert_element(index + 1, tuple, term)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Removes an element from a tuple.
|
||||
|
||||
Deletes the element at the given `index` from `tuple`.
|
||||
Raises an `ArgumentError` if `index` is negative or greater than
|
||||
or equal to the length of `tuple`. Index is zero-based.
|
||||
Deletes the element at the zero-based `index` from `tuple`.
|
||||
Raises an `ArgumentError` if `index` is greater than
|
||||
or equal to the length of `tuple`.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
@@ -91,16 +67,7 @@ defmodule Tuple do
|
||||
@doc """
|
||||
Converts a tuple to a list.
|
||||
|
||||
Returns a new list with all the tuple elements.
|
||||
|
||||
Inlined by the compiler.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> tuple = {:foo, :bar, :baz}
|
||||
iex> Tuple.to_list(tuple)
|
||||
[:foo, :bar, :baz]
|
||||
|
||||
"""
|
||||
@spec to_list(tuple) :: list
|
||||
def to_list(tuple) do
|
||||
|
||||
+65
-78
@@ -11,6 +11,26 @@ defmodule URI do
|
||||
|
||||
import Bitwise
|
||||
|
||||
@ports %{
|
||||
"ftp" => 21,
|
||||
"http" => 80,
|
||||
"https" => 443,
|
||||
"ldap" => 389,
|
||||
"sftp" => 22,
|
||||
"tftp" => 69,
|
||||
}
|
||||
|
||||
Enum.each @ports, fn {scheme, port} ->
|
||||
def normalize_scheme(unquote(scheme)), do: unquote(scheme)
|
||||
def default_port(unquote(scheme)), do: unquote(port)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Normalizes the scheme according to the spec by downcasing it.
|
||||
"""
|
||||
def normalize_scheme(nil), do: nil
|
||||
def normalize_scheme(scheme), do: String.downcase(scheme)
|
||||
|
||||
@doc """
|
||||
Returns the default port for a given scheme.
|
||||
|
||||
@@ -27,7 +47,8 @@ defmodule URI do
|
||||
|
||||
"""
|
||||
def default_port(scheme) when is_binary(scheme) do
|
||||
:elixir_config.get({:uri, scheme})
|
||||
{:ok, dict} = Application.fetch_env(:elixir, :uri)
|
||||
Map.get(dict, scheme)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -38,7 +59,8 @@ defmodule URI do
|
||||
new URIs.
|
||||
"""
|
||||
def default_port(scheme, port) when is_binary(scheme) and port > 0 do
|
||||
:elixir_config.put({:uri, scheme}, port)
|
||||
{:ok, dict} = Application.fetch_env(:elixir, :uri)
|
||||
Application.put_env(:elixir, :uri, Map.put(dict, scheme, port), persistent: true)
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -75,12 +97,8 @@ defmodule URI do
|
||||
%{"bar" => "2", "foo" => "1"}
|
||||
|
||||
"""
|
||||
# TODO: Deprecate giving not a map on 1.3
|
||||
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
|
||||
Enum.reduce query_decoder(q), dict, fn({k, v}, acc) -> Dict.put(acc, k, v) end
|
||||
end
|
||||
|
||||
@doc """
|
||||
@@ -89,19 +107,19 @@ defmodule URI do
|
||||
|
||||
## Examples
|
||||
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map(&(&1))
|
||||
iex> URI.query_decoder("foo=1&bar=2") |> Enum.map &(&1)
|
||||
[{"foo", "1"}, {"bar", "2"}]
|
||||
|
||||
"""
|
||||
def query_decoder(q) when is_binary(q) do
|
||||
Stream.unfold(q, &do_decode_query/1)
|
||||
Stream.unfold(q, &do_decoder/1)
|
||||
end
|
||||
|
||||
defp do_decode_query("") do
|
||||
defp do_decoder("") do
|
||||
nil
|
||||
end
|
||||
|
||||
defp do_decode_query(q) do
|
||||
defp do_decoder(q) do
|
||||
{first, next} =
|
||||
case :binary.split(q, "&") do
|
||||
[first, rest] -> {first, rest}
|
||||
@@ -128,14 +146,14 @@ defmodule URI do
|
||||
end
|
||||
|
||||
defp pair({k, v}) do
|
||||
encode_www_form(Kernel.to_string(k)) <>
|
||||
"=" <> encode_www_form(Kernel.to_string(v))
|
||||
encode_www_form(to_string(k)) <>
|
||||
"=" <> encode_www_form(to_string(v))
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the character is a "reserved" character in a URI.
|
||||
|
||||
Reserved characters are specified in [RFC3986, section 2.2](http://tools.ietf.org/html/rfc3986#section-2.2).
|
||||
Reserved characters are specified in RFC3986, section 2.2.
|
||||
"""
|
||||
def char_reserved?(c) do
|
||||
c in ':/?#[]@!$&\'()*+,;='
|
||||
@@ -144,7 +162,7 @@ defmodule URI do
|
||||
@doc """
|
||||
Checks if the character is a "unreserved" character in a URI.
|
||||
|
||||
Unreserved characters are specified in [RFC3986, section 2.3](http://tools.ietf.org/html/rfc3986#section-2.3).
|
||||
Unreserved characters are specified in RFC3986, section 2.3.
|
||||
"""
|
||||
def char_unreserved?(c) do
|
||||
c in ?0..?9 or
|
||||
@@ -164,7 +182,7 @@ defmodule URI do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Percent-escapes a URI.
|
||||
Percent-escape a URI.
|
||||
Accepts `predicate` function as an argument to specify if char can be left as is.
|
||||
|
||||
## Example
|
||||
@@ -178,7 +196,7 @@ defmodule URI do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Encodes a string as "x-www-form-urlencoded".
|
||||
Encode a string as "x-www-urlencoded".
|
||||
|
||||
## Example
|
||||
|
||||
@@ -207,7 +225,7 @@ defmodule URI do
|
||||
defp hex(n), do: <<n + ?A - 10>>
|
||||
|
||||
@doc """
|
||||
Percent-unescapes a URI.
|
||||
Percent-unescape a URI.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -216,14 +234,14 @@ defmodule URI do
|
||||
|
||||
"""
|
||||
def decode(uri) do
|
||||
unpercent(uri, "", false)
|
||||
unpercent(uri)
|
||||
catch
|
||||
:malformed_uri ->
|
||||
raise ArgumentError, "malformed URI #{inspect uri}"
|
||||
end
|
||||
|
||||
@doc """
|
||||
Decodes a string as "x-www-form-urlencoded".
|
||||
Decode a string as "x-www-urlencoded".
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -232,25 +250,23 @@ defmodule URI do
|
||||
|
||||
"""
|
||||
def decode_www_form(str) do
|
||||
unpercent(str, "", true)
|
||||
String.split(str, "+") |> Enum.map_join(" ", &unpercent/1)
|
||||
catch
|
||||
:malformed_uri ->
|
||||
raise ArgumentError, "malformed URI #{inspect str}"
|
||||
end
|
||||
|
||||
defp unpercent(<<?+, tail::binary>>, acc, spaces = true) do
|
||||
unpercent(tail, <<acc::binary, ?\s>>, spaces)
|
||||
defp unpercent(<<?%, hex_1, hex_2, tail :: binary>>) do
|
||||
<<bsl(hex_to_dec(hex_1), 4) + hex_to_dec(hex_2)>> <> unpercent(tail)
|
||||
end
|
||||
defp unpercent(<<?%, _>>), do: throw(:malformed_uri)
|
||||
defp unpercent(<<?%>>), do: throw(:malformed_uri)
|
||||
|
||||
defp unpercent(<<head, tail :: binary>>) do
|
||||
<<head>> <> unpercent(tail)
|
||||
end
|
||||
|
||||
defp unpercent(<<?%, hex_1, hex_2, tail::binary>>, acc, spaces) do
|
||||
unpercent(tail, <<acc::binary, bsl(hex_to_dec(hex_1), 4) + hex_to_dec(hex_2)>>, spaces)
|
||||
end
|
||||
defp unpercent(<<?%, _::binary>>, _acc, _spaces), do: throw(:malformed_uri)
|
||||
|
||||
defp unpercent(<<head, tail::binary>>, acc, spaces) do
|
||||
unpercent(tail, <<acc::binary, head>>, spaces)
|
||||
end
|
||||
defp unpercent(<<>>, acc, _spaces), do: acc
|
||||
defp unpercent(<<>>), do: <<>>
|
||||
|
||||
defp hex_to_dec(n) when n in ?A..?F, do: n - ?A + 10
|
||||
defp hex_to_dec(n) when n in ?a..?f, do: n - ?a + 10
|
||||
@@ -258,17 +274,12 @@ defmodule URI do
|
||||
defp hex_to_dec(_n), do: throw(:malformed_uri)
|
||||
|
||||
@doc """
|
||||
Parses a well-formed URI reference into its components.
|
||||
Parses a URI into components.
|
||||
|
||||
Note this function expects a well-formed URI and does not perform
|
||||
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 the [Appendix B of RFC3986](http://tools.ietf.org/html/rfc3986#appendix-B).
|
||||
|
||||
When a URI is given without a port, the values registered via
|
||||
`URI.default_port/1` and `URI.default_port/2` are used.
|
||||
URIs have portions that are handled specially for the particular
|
||||
scheme of the URI. For example, http and https have different
|
||||
default ports. Such values can be accessed and registered via
|
||||
`URI.default_port/1` and `URI.default_port/2`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -277,24 +288,12 @@ defmodule URI do
|
||||
authority: "elixir-lang.org", userinfo: nil,
|
||||
host: "elixir-lang.org", port: 80}
|
||||
|
||||
iex> URI.parse("//elixir-lang.org/")
|
||||
%URI{authority: "elixir-lang.org", fragment: nil, host: "elixir-lang.org",
|
||||
path: "/", port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||
|
||||
iex> URI.parse("/foo/bar")
|
||||
%URI{authority: nil, fragment: nil, host: nil, path: "/foo/bar",
|
||||
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||
|
||||
iex> URI.parse("foo/bar")
|
||||
%URI{authority: nil, fragment: nil, host: nil, path: "foo/bar",
|
||||
port: nil, query: nil, scheme: nil, userinfo: nil}
|
||||
|
||||
"""
|
||||
def parse(%URI{} = uri), do: uri
|
||||
|
||||
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
|
||||
regex = ~r/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/
|
||||
parts = nillify(Regex.run(regex, s))
|
||||
|
||||
destructure [_, _, scheme, _, authority, path, _, query, _, fragment], parts
|
||||
@@ -333,24 +332,13 @@ defmodule URI do
|
||||
{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(l) do
|
||||
for s <- l do
|
||||
if byte_size(s) > 0, do: s
|
||||
if byte_size(s) > 0, do: s, else: nil
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Converts the URI to string.
|
||||
|
||||
iex> URI.to_string(URI.parse("http://google.com"))
|
||||
"http://google.com"
|
||||
"""
|
||||
defdelegate to_string(uri), to: String.Chars.URI
|
||||
end
|
||||
|
||||
defimpl String.Chars, for: URI do
|
||||
@@ -362,24 +350,23 @@ defimpl String.Chars, for: URI do
|
||||
end
|
||||
|
||||
# Based on http://tools.ietf.org/html/rfc3986#section-5.3
|
||||
authority = extract_authority(uri)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
|
||||
defp extract_authority(%{host: nil, authority: authority}) do
|
||||
authority
|
||||
end
|
||||
defp extract_authority(%{host: host, userinfo: userinfo, port: port}) do
|
||||
authority = host
|
||||
if userinfo, do: authority = userinfo <> "@" <> authority
|
||||
if port, do: authority = authority <> ":" <> Integer.to_string(port)
|
||||
authority
|
||||
end
|
||||
end
|
||||
|
||||
+24
-48
@@ -10,7 +10,7 @@ defmodule Version do
|
||||
|
||||
## Versions
|
||||
|
||||
In a nutshell, a version is represented by three numbers:
|
||||
In a nutshell, a version is given by three numbers:
|
||||
|
||||
MAJOR.MINOR.PATCH
|
||||
|
||||
@@ -24,8 +24,8 @@ defmodule Version do
|
||||
|
||||
## Struct
|
||||
|
||||
The version is represented by the Version struct and fields
|
||||
are named according to Semver: `:major`, `:minor`, `:patch`,
|
||||
The version is represented by the Version struct and it has its
|
||||
fields named according to Semver: `:major`, `:minor`, `:patch`,
|
||||
`:pre` and `:build`.
|
||||
|
||||
## Requirements
|
||||
@@ -51,19 +51,6 @@ defmodule Version do
|
||||
|
||||
"~> 2.0.0"
|
||||
|
||||
`~>` will never include pre-release versions of its upper bound.
|
||||
It can also be used to set an upper bound on only the major
|
||||
version part. See the table below for `~>` requirements and
|
||||
their corresponding translation.
|
||||
|
||||
`~>` | Translation
|
||||
:------------- | :---------------------
|
||||
`~> 2.0.0` | `>= 2.0.0 and < 2.1.0`
|
||||
`~> 2.1.2` | `>= 2.1.2 and < 2.2.0`
|
||||
`~> 2.1.3-dev` | `>= 2.1.3-dev and < 2.2.0`
|
||||
`~> 2.0` | `>= 2.0.0 and < 3.0.0`
|
||||
`~> 2.1` | `>= 2.1.0 and < 3.0.0`
|
||||
|
||||
"""
|
||||
|
||||
import Kernel, except: [match?: 2]
|
||||
@@ -74,7 +61,7 @@ defmodule Version do
|
||||
@type major :: String.t | non_neg_integer
|
||||
@type minor :: non_neg_integer | nil
|
||||
@type patch :: non_neg_integer | nil
|
||||
@type pre :: [String.t | non_neg_integer]
|
||||
@type pre :: [String.t]
|
||||
@type build :: String.t | nil
|
||||
@type matchable :: {major :: major,
|
||||
minor :: minor,
|
||||
@@ -88,6 +75,7 @@ defmodule Version do
|
||||
build: build}
|
||||
|
||||
defmodule Requirement do
|
||||
@moduledoc false
|
||||
defstruct [:source, :matchspec]
|
||||
@type t :: %__MODULE__{}
|
||||
end
|
||||
@@ -101,11 +89,11 @@ defmodule Version do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Checks if the given version matches the specification.
|
||||
Check if the given version matches the specification.
|
||||
|
||||
Returns `true` if `version` satisfies `requirement`, `false` otherwise.
|
||||
Raises a `Version.InvalidRequirementError` exception if `requirement` is not
|
||||
parsable, or `Version.InvalidVersionError` if `version` is not parsable.
|
||||
parseable, or `Version.InvalidVersionError` if `version` is not parseable.
|
||||
If given an already parsed version and requirement this function won't
|
||||
raise.
|
||||
|
||||
@@ -125,12 +113,12 @@ defmodule Version do
|
||||
|
||||
"""
|
||||
@spec match?(version, requirement) :: boolean
|
||||
def match?(version, requirement) when is_binary(requirement) do
|
||||
case parse_requirement(requirement) do
|
||||
{:ok, requirement} ->
|
||||
match?(version, requirement)
|
||||
def match?(vsn, req) when is_binary(req) do
|
||||
case parse_requirement(req) do
|
||||
{:ok, req} ->
|
||||
match?(vsn, req)
|
||||
:error ->
|
||||
raise InvalidRequirementError, message: requirement
|
||||
raise InvalidRequirementError, message: req
|
||||
end
|
||||
end
|
||||
|
||||
@@ -144,7 +132,7 @@ defmodule Version do
|
||||
the second and `:lt` for vice versa. If the two versions are equal `:eq`
|
||||
is returned
|
||||
|
||||
Raises a `Version.InvalidVersionError` exception if `version` is not parsable.
|
||||
Raises a `Version.InvalidVersionError` exception if `version` is not parseable.
|
||||
If given an already parsed version this function won't raise.
|
||||
|
||||
## Examples
|
||||
@@ -160,8 +148,8 @@ defmodule Version do
|
||||
|
||||
"""
|
||||
@spec compare(version, version) :: :gt | :eq | :lt
|
||||
def compare(version1, version2) do
|
||||
do_compare(to_matchable(version1), to_matchable(version2))
|
||||
def compare(vsn1, vsn2) do
|
||||
do_compare(to_matchable(vsn1), to_matchable(vsn2))
|
||||
end
|
||||
|
||||
defp do_compare({major1, minor1, patch1, pre1}, {major2, minor2, patch2, pre2}) do
|
||||
@@ -177,7 +165,7 @@ defmodule Version do
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a version string into a `Version`.
|
||||
Parse a version string into a `Version`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -193,16 +181,16 @@ defmodule Version do
|
||||
def parse(string) when is_binary(string) do
|
||||
case Version.Parser.parse_version(string) do
|
||||
{:ok, {major, minor, patch, pre}} ->
|
||||
version = %Version{major: major, minor: minor, patch: patch,
|
||||
vsn = %Version{major: major, minor: minor, patch: patch,
|
||||
pre: pre, build: get_build(string)}
|
||||
{:ok, version}
|
||||
{:ok, vsn}
|
||||
:error ->
|
||||
:error
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Parses a version requirement string into a `Version.Requirement`.
|
||||
Parse a version requirement string into a `Version.Requirement`.
|
||||
|
||||
## Examples
|
||||
|
||||
@@ -230,7 +218,7 @@ defmodule Version do
|
||||
|
||||
defp to_matchable(string) do
|
||||
case Version.Parser.parse_version(string) do
|
||||
{:ok, version} -> version
|
||||
{:ok, vsn} -> vsn
|
||||
:error -> raise InvalidVersionError, message: string
|
||||
end
|
||||
end
|
||||
@@ -266,8 +254,8 @@ defmodule Version do
|
||||
|
||||
defmacro deflexer(char, acc, do: body) do
|
||||
quote do
|
||||
def lexer(<<unquote(char)::utf8, rest::binary>>, unquote(acc)) do
|
||||
unquote(char) = <<unquote(char)::utf8>>
|
||||
def lexer(<< unquote(char) :: utf8, rest :: binary >>, unquote(acc)) do
|
||||
unquote(char) = << unquote(char) :: utf8 >>
|
||||
|
||||
lexer(rest, unquote(body))
|
||||
end
|
||||
@@ -317,7 +305,7 @@ defmodule Version do
|
||||
(?:\.(\d+))? # minor
|
||||
(?:\.(\d+))? # patch
|
||||
(?:\-([\d\w\.\-]+))? # pre
|
||||
(?:\+([\d\w\.\-]+))? # build
|
||||
(?:\+([\d\w\-]+))? # build
|
||||
$/x
|
||||
|
||||
@spec parse_requirement(String.t) :: {:ok, Version.Requirement.t} | :error
|
||||
@@ -518,22 +506,10 @@ end
|
||||
|
||||
defimpl String.Chars, for: Version do
|
||||
def to_string(version) do
|
||||
pre = pre(version.pre)
|
||||
pre = unless Enum.empty?(pre = version.pre), do: "-#{pre}"
|
||||
build = if build = version.build, do: "+#{build}"
|
||||
"#{version.major}.#{version.minor}.#{version.patch}#{pre}#{build}"
|
||||
end
|
||||
|
||||
defp pre([]) do
|
||||
""
|
||||
end
|
||||
|
||||
defp pre(pre) do
|
||||
"-" <>
|
||||
Enum.map_join(pre, ".", fn
|
||||
int when is_integer(int) -> Integer.to_string(int)
|
||||
string when is_binary(string) -> string
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
||||
defimpl Inspect, for: Version do
|
||||
|
||||
@@ -1,168 +0,0 @@
|
||||
# Typespecs
|
||||
|
||||
Elixir comes with a notation for declaring types and specifications. Elixir is
|
||||
dynamically typed, and 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 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
|
||||
|
||||
These 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())`
|
||||
`iolist()` | `maybe_improper_list(byte() \| binary() \| iolist(), binary() \| [])`
|
||||
`iodata()` | `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()}`
|
||||
`as_boolean(t)` | `t`
|
||||
|
||||
### Remote types
|
||||
|
||||
Any module is also able to define its 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.
|
||||
@@ -1,102 +0,0 @@
|
||||
# Writing Documentation
|
||||
|
||||
Elixir treats documentation as a first-class citizen. This means documentation should be easy to write and easy to read. In this document you will learn how to write documentation in Elixir, covering constructs like module attributes, style practices and doctests.
|
||||
|
||||
## Markdown
|
||||
|
||||
Elixir documentation is written using Markdown. There are plenty of guides on Markdown online, we recommend the ones available at GitHub as a getting started point:
|
||||
|
||||
* https://help.github.com/articles/markdown-basics/
|
||||
* https://help.github.com/articles/github-flavored-markdown/
|
||||
|
||||
## Module Attributes
|
||||
|
||||
Documentation in Elixir is usually attached to module attributes. Let's see an example:
|
||||
|
||||
defmodule MyApp.Hello do
|
||||
@moduledoc """
|
||||
This is the Hello module.
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Says hello to the given `name`.
|
||||
|
||||
Returns `:ok`.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> MyApp.Hello.world(:john)
|
||||
:ok
|
||||
|
||||
"""
|
||||
def world(name) do
|
||||
IO.puts "hello #{name}"
|
||||
end
|
||||
end
|
||||
|
||||
The `@moduledoc` attribute is used to attach documentation to the module. `@doc` is used before a function to attach documentation to it. Besides the attributes above, `@typedoc` can also be used to attach documentation to types defined as part of typespecs.
|
||||
|
||||
## Function Arguments
|
||||
|
||||
When documenting a function, argument names are inferred by the compiler. For example:
|
||||
|
||||
def size(%HashDict{size: size}) do
|
||||
size
|
||||
end
|
||||
|
||||
The compiler will infer this argument as `hash_dict`. Sometimes the inference will be suboptimal, specially if the function contains multiple clauses with the argument matching on different values each time. You can specify the proper names for documentation by using a bodyless clause:
|
||||
|
||||
def size(dict)
|
||||
|
||||
## Recommendations
|
||||
|
||||
There are a couple tips we recommend developers to follow when writing documentation:
|
||||
|
||||
* Keep the first paragraph of the documentation concise and simple. Tools like ExDoc uses the first line to generate a summary.
|
||||
|
||||
* Markdown uses backticks (`` ` ``) to quote code. Elixir builds on top of that to automatically generate links when modules or function names are referenced. For this reason, always use full module names. If you have a module called `MyApp.Hello`, always reference it as `` `MyApp.Hello` `` and never as `` `Hello` ``. Function names must be referenced by name and arity if they are local, as in `` `world/1` ``, or by module, name and arity if pointing to an external module: `` `MyApp.Hello.world/1` ``. Referencing a `@callback` can be done by prepending `c:`, as in `` `c:world/1` ``.
|
||||
|
||||
* If using headings, always start from the second heading by using `##`. The first heading is reserved to the module or function name itself.
|
||||
|
||||
## Doctests
|
||||
|
||||
We recommend developers to include examples in their documentation, often under its own `## Examples` heading. To ensure examples do not get out of date, Elixir's test framework (ExUnit) provides a feature called doctests that allows developers to test the examples in their documentation. Doctests work by parsing out code samples starting with `iex>` from the documentation. You can read more about it at `ExUnit.DocTest`.
|
||||
|
||||
Notice doctests have limitations. When you cannot doctest a function, because it relies on state or side-effects, we recommend developers to include examples directly without the `iex>` prompt.
|
||||
|
||||
## Privacy
|
||||
|
||||
Elixir allows developers to hide both modules and functions from the documentation by setting the doc attribute to false:
|
||||
|
||||
defmodule MyApp.Hidden do
|
||||
@moduledoc false
|
||||
|
||||
def this_will_be_ignored_by_tools do
|
||||
# ...
|
||||
end
|
||||
end
|
||||
|
||||
Notice that, although developers can add `@doc false` to functions, it does not make the function private:
|
||||
|
||||
defmodule MyApp.Sample do
|
||||
@doc false
|
||||
def add(a, b), do: a + b
|
||||
end
|
||||
|
||||
The function above can still be invoked as `MyApp.Sample.add(1, 2)`. Not only that, if the `MyApp.Sample` is imported, the `add/2` function will also be imported into the caller. For those reasons, be wary when adding `@doc false` to functions, instead prefer one of:
|
||||
|
||||
* Move the private function to a module with `@moduledoc false`, like `MyApp.Hidden`, ensuring the function won't be accidentally exposed or imported. In fact, you can use `@moduledoc false` to hide a whole module and still document each function with `@doc`. Tools will still ignore the module.
|
||||
|
||||
* Start the function name with underscores, for example, `__add__/2`, and add `@doc false`. The compiler does not import functions with underscore and the underscore will tell users to be wary of using it.
|
||||
|
||||
## Documentation != Comments
|
||||
|
||||
Elixir makes the difference between documentation and code comments. Documentation are for users of your API, be it your co-worker or your future self. Modules and functions must always be documented if they are part of your application public interface (API).
|
||||
|
||||
Code comments are for developers reading the code. They are useful to mark improvements, leave notes for developers reading the code (for example, you decided to not call a function due to a bug in a library) and so forth.
|
||||
|
||||
In other words, documentation is required, code comments are optional.
|
||||
|
||||
## Code.get_docs/2
|
||||
|
||||
Elixir stores documentation inside pre-defined chunks in the bytecode. It can be accessed from Elixir by using the `Code.get_docs/2` function. This also means documentation is only accessed when required and not when modules are loaded by the Virtual Machine. The only downside is that modules defined in-memory, like the ones defined via IEx, cannot have their documentation accessed.
|
||||
+20
-68
@@ -7,12 +7,10 @@
|
||||
env_for_eval/1, env_for_eval/2, quoted_to_erl/2, quoted_to_erl/3,
|
||||
eval/2, eval/3, eval_forms/3, eval_forms/4, eval_quoted/3]).
|
||||
-include("elixir.hrl").
|
||||
-define(system, 'Elixir.System').
|
||||
|
||||
%% Top level types
|
||||
-export_type([char_list/0, struct/0, as_boolean/1]).
|
||||
-export_type([char_list/0, as_boolean/1]).
|
||||
-type char_list() :: string().
|
||||
-type struct() :: #{'__struct__' => atom()}.
|
||||
-type as_boolean(T) :: T.
|
||||
|
||||
%% OTP Application API
|
||||
@@ -26,38 +24,15 @@ start(_Type, _Args) ->
|
||||
%% has encoding set to latin1.
|
||||
Opts =
|
||||
case init:get_argument(noshell) of
|
||||
{ok, _} -> [binary, {encoding, utf8}];
|
||||
{ok, _} -> [binary,{encoding,utf8}];
|
||||
error -> [binary]
|
||||
end,
|
||||
|
||||
case string:to_integer(erlang:system_info(otp_release)) of
|
||||
{Num, _} when Num >= 18 ->
|
||||
ok;
|
||||
_ ->
|
||||
io:format(standard_error, "unsupported Erlang version, expected Erlang 18+~n", []),
|
||||
erlang:halt(1)
|
||||
end,
|
||||
|
||||
case code:ensure_loaded(?system) of
|
||||
{module, ?system} ->
|
||||
Endianness = ?system:endianness(),
|
||||
case ?system:compiled_endianness() of
|
||||
Endianness -> ok;
|
||||
_ ->
|
||||
io:format(standard_error,
|
||||
"warning: Elixir is running in a system with a different endianness than the one its "
|
||||
"source code was compiled in. Please make sure Elixir and all source files were compiled "
|
||||
"in a machine with the same endianness as the current one: ~ts~n", [Endianness])
|
||||
end;
|
||||
{error, _} ->
|
||||
ok
|
||||
end,
|
||||
|
||||
ok = io:setopts(standard_io, Opts),
|
||||
ok = io:setopts(standard_error, [{encoding, utf8}]),
|
||||
|
||||
Encoding = file:native_name_encoding(),
|
||||
case Encoding of
|
||||
%% Must use undocument {unicode, true} to set unicode on standard_error, more
|
||||
%% info: http://erlang.org/pipermail/erlang-bugs/2014-April/004310.html
|
||||
ok = io:setopts(standard_error, [{unicode,true}]),
|
||||
case file:native_name_encoding() of
|
||||
latin1 ->
|
||||
io:format(standard_error,
|
||||
"warning: the VM is running with native name encoding of latin1 which may cause "
|
||||
@@ -67,31 +42,10 @@ start(_Type, _Args) ->
|
||||
ok
|
||||
end,
|
||||
|
||||
URIs = [{<<"ftp">>, 21},
|
||||
{<<"sftp">>, 22},
|
||||
{<<"tftp">>, 69},
|
||||
{<<"http">>, 80},
|
||||
{<<"https">>, 443},
|
||||
{<<"ldap">>, 389}],
|
||||
URIConfig = [{{uri, Scheme}, Port} || {Scheme, Port} <- URIs],
|
||||
CompilerOpts = #{docs => true, ignore_module_conflict => false,
|
||||
debug_info => true, warnings_as_errors => false},
|
||||
{ok, [[Home] | _]} = init:get_argument(home),
|
||||
Config = [{at_exit, []},
|
||||
{home, unicode:characters_to_binary(Home, Encoding, Encoding)},
|
||||
{compiler_options, CompilerOpts}
|
||||
| URIConfig],
|
||||
Tab = elixir_config:new(Config),
|
||||
case elixir_sup:start_link() of
|
||||
{ok, Sup} ->
|
||||
{ok, Sup, Tab};
|
||||
{error, _Reason} = Error ->
|
||||
elixir_config:delete(Tab),
|
||||
Error
|
||||
end.
|
||||
elixir_sup:start_link().
|
||||
|
||||
stop(Tab) ->
|
||||
elixir_config:delete(Tab).
|
||||
stop(_S) ->
|
||||
ok.
|
||||
|
||||
config_change(_Changed, _New, _Remove) ->
|
||||
ok.
|
||||
@@ -117,6 +71,7 @@ start_cli() ->
|
||||
|
||||
env_for_eval(Opts) ->
|
||||
env_for_eval((elixir_env:new())#{
|
||||
local := nil,
|
||||
requires := elixir_dispatch:default_requires(),
|
||||
functions := elixir_dispatch:default_functions(),
|
||||
macros := elixir_dispatch:default_macros()
|
||||
@@ -133,6 +88,11 @@ env_for_eval(Env, Opts) ->
|
||||
false -> ?m(Env, file)
|
||||
end,
|
||||
|
||||
Local = case lists:keyfind(delegate_locals_to, 1, Opts) of
|
||||
{delegate_locals_to, LocalOpt} when is_atom(LocalOpt) -> LocalOpt;
|
||||
false -> ?m(Env, local)
|
||||
end,
|
||||
|
||||
Aliases = case lists:keyfind(aliases, 1, Opts) of
|
||||
{aliases, AliasesOpt} when is_list(AliasesOpt) -> AliasesOpt;
|
||||
false -> ?m(Env, aliases)
|
||||
@@ -159,7 +119,7 @@ env_for_eval(Env, Opts) ->
|
||||
end,
|
||||
|
||||
Env#{
|
||||
file := File, module := Module,
|
||||
file := File, local := Local, module := Module,
|
||||
macros := Macros, functions := Functions,
|
||||
requires := Requires, aliases := Aliases, line := Line
|
||||
}.
|
||||
@@ -192,8 +152,8 @@ eval_forms(Tree, Binding, Opts) when is_list(Opts) ->
|
||||
eval_forms(Tree, Binding, E) ->
|
||||
eval_forms(Tree, Binding, E, elixir_env:env_to_scope(E)).
|
||||
eval_forms(Tree, Binding, Env, Scope) ->
|
||||
{ParsedBinding, ParsedVars, ParsedScope} = elixir_scope:load_binding(Binding, Scope),
|
||||
ParsedEnv = Env#{vars := ParsedVars},
|
||||
{ParsedBinding, ParsedScope} = elixir_scope:load_binding(Binding, Scope),
|
||||
ParsedEnv = Env#{vars := [K || {K,_} <- ParsedScope#elixir_scope.vars]},
|
||||
{Erl, NewEnv, NewScope} = quoted_to_erl(Tree, ParsedEnv, ParsedScope),
|
||||
|
||||
case Erl of
|
||||
@@ -251,22 +211,14 @@ quoted_to_erl(Quoted, Env, Scope) ->
|
||||
|
||||
string_to_quoted(String, StartLine, File, Opts) when is_integer(StartLine), is_binary(File) ->
|
||||
case elixir_tokenizer:tokenize(String, StartLine, [{file, File}|Opts]) of
|
||||
{ok, _Line, _Column, Tokens} ->
|
||||
put(elixir_parser_file, File),
|
||||
{ok, _Line, Tokens} ->
|
||||
try elixir_parser:parse(Tokens) of
|
||||
{ok, Forms} -> {ok, Forms};
|
||||
{error, {{Line, _, _}, _, [Error, Token]}} -> {error, {Line, to_binary(Error), to_binary(Token)}};
|
||||
{error, {Line, _, [Error, Token]}} -> {error, {Line, to_binary(Error), to_binary(Token)}}
|
||||
catch
|
||||
{error, {{Line, _, _}, _, [Error, Token]}} -> {error, {Line, to_binary(Error), to_binary(Token)}};
|
||||
{error, {Line, _, [Error, Token]}} -> {error, {Line, to_binary(Error), to_binary(Token)}}
|
||||
after
|
||||
erase(elixir_parser_file)
|
||||
end;
|
||||
{error, {Line, {ErrorPrefix, ErrorSuffix}, Token}, _Rest, _SoFar} ->
|
||||
{error, {Line, {to_binary(ErrorPrefix), to_binary(ErrorSuffix)}, to_binary(Token)}};
|
||||
{error, {Line, Error, Token}, _Rest, _SoFar} ->
|
||||
{error, {Line, to_binary(Error), to_binary(Token)}}
|
||||
{error, {Line, Error, Token}, _Rest, _SoFar} -> {error, {Line, to_binary(Error), to_binary(Token)}}
|
||||
end.
|
||||
|
||||
'string_to_quoted!'(String, StartLine, File, Opts) ->
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user