Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improve docs for dicts and bitwise modules

  • Loading branch information...
commit a1f76187d06824b31de38ce45c25e65429a80c22 1 parent b7200c4
@josevalim josevalim authored
View
26 lib/bitwise.ex
@@ -1,27 +1,47 @@
-# This namespace provide macros for bitwise operators
-# provided by Erlang. Those can be used in guards.
-
defmodule Bitwise do
+ @moduledoc """
+ This module provide macros for bitwise operators
+ provided by Erlang. These macros can be used in guards.
+ """
+
+ @doc """
+ Bitwise not.
+ """
defmacro bnot(expr) do
quote do: __op__ :bnot, unquote(expr)
end
+ @doc """
+ Bitwise and.
+ """
defmacro band(left, right) do
quote do: __op__ :band, unquote(left), unquote(right)
end
+ @doc """
+ Bitwise or.
+ """
defmacro bor(left, right) do
quote do: __op__ :bor, unquote(left), unquote(right)
end
+ @doc """
+ Bitwise xor.
+ """
defmacro bxor(left, right) do
quote do: __op__ :bxor, unquote(left), unquote(right)
end
+ @doc """
+ Arithmetic bitshift left.
+ """
defmacro bsl(left, right) do
quote do: __op__ :bsl, unquote(left), unquote(right)
end
+ @doc """
+ Arithmetic bitshift right.
+ """
defmacro bsr(left, right) do
quote do: __op__ :bsr, unquote(left), unquote(right)
end
View
2  lib/code.ex
@@ -1,7 +1,7 @@
defmodule Code do
@moduledoc """
The Code module is responsible to manage code compilation,
- evaluation and loading.
+ code evaluation and code loading.
"""
@doc """
View
14 lib/dict.ex
@@ -1,11 +1,16 @@
defprotocol Dict do
@only [Record]
+ @moduledoc """
+ This module provides the Dict protocol
+ with the goal of being a common API
+ to work with dictionaries.
+ """
+
@doc """
Returns a list containing all dict's keys.
-
- For Keyword and Orddict, the keys are guaranteed to be sorted. For other
- types of dicts, the order is not enforced.
+ The keys are not guaranteed to be sorted, unless
+ the underlying dict implementation defines so.
## Examples
@@ -67,11 +72,8 @@ defprotocol Dict do
Dict.put [a: 1, b: 2], :a, 3
#=> [a: 3, b: 2]
- Dict.put [a: 1, b: 2], {:c, 3}
- #=> [a: 1, b: 2, c: 3]
"""
- def put(dict, pair)
def put(dict, key, val)
@doc """
View
14 lib/dict/common.ex
@@ -1,4 +1,6 @@
defmodule Dict.Common do
+ @moduledoc false
+
defmacro __using__(_module, ref) do
quote do
@doc """
@@ -13,13 +15,13 @@ defmodule Dict.Common do
## Examples
- Dict.new [{:b,1},{:a,2}]
+ #{unquote(__MODULE__)}.new [{:b,1},{:a,2}]
#=> [a: 1, b: 2]
"""
def new(pairs) do
- Enum.reduce pairs, new, fn(pair, dict) ->
- unquote(ref).put(dict, pair)
+ Enum.reduce pairs, new, fn({ k, v }, dict) ->
+ unquote(ref).put(dict, k, v)
end
end
@@ -29,13 +31,13 @@ defmodule Dict.Common do
## Examples
- Dict.new ["a", "b"], fn(x) -> {x, x} end
+ #{unquote(__MODULE__)}.new ["a", "b"], fn(x) -> {x, x} end
#=> ["a": "a", "b": "b"]
"""
def new(list, transform) when is_function(transform) do
Enum.reduce list, new(), fn(i, dict) ->
- pair = transform.(i)
- unquote(ref).put(dict, pair)
+ { k, v } = transform.(i)
+ unquote(ref).put(dict, k, v)
end
end
end
View
12 lib/hash_dict.ex
@@ -1,4 +1,4 @@
-defrecord HashDict.Record, data: nil
+defrecord HashDict.Record, [data: nil], moduledoc: false
defimpl Dict, for: HashDict.Record do
refer HashDict.Record, as: HD
@@ -34,10 +34,6 @@ defimpl Dict, for: HashDict.Record do
HD[data: :dict.store key, value, data]
end
- def put(HD[data: data], {key, value}) do
- HD[data: :dict.store key, value, data]
- end
-
def delete(HD[data: data], key) do
HD[data: :dict.erase key, data]
end
@@ -68,5 +64,11 @@ defimpl Dict, for: HashDict.Record do
end
defmodule HashDict do
+ @moduledoc """
+ This module implements a dictionary based on hashing of the keys.
+ It is a simple wrapper around [Erlang's dict module](http://www.erlang.org/doc/man/dict.html)
+ and exposed via the `Dict` protocol.
+ """
+
use Dict.Common, Dict.HashDict.Record
end
View
12 lib/orddict.ex
@@ -1,4 +1,4 @@
-defrecord Orddict.Record, data: nil
+defrecord Orddict.Record, [data: nil], moduledoc: false
defimpl Dict, for: Orddict.Record do
refer Orddict.Record, as: O
@@ -32,10 +32,6 @@ defimpl Dict, for: Orddict.Record do
O[data: :orddict.store key, value, data]
end
- def put(O[data: data], {key, value}) do
- O[data: :orddict.store key, value, data]
- end
-
def delete(O[data: data], key) do
O[data: :orddict.erase key, data]
end
@@ -66,5 +62,11 @@ defimpl Dict, for: Orddict.Record do
end
defmodule Orddict do
+ @moduledoc """
+ This module implements a dictionary based that stores items
+ as a list of tuples. It is a simple wrapper around
+ [Erlang's orddict module](http://www.erlang.org/doc/man/orddict.html)
+ and exposed via the `Dict` protocol.
+ """
use Dict.Common, Dict.Orddict.Record
end
View
5 lib/uri.ex
@@ -37,13 +37,12 @@ defmodule URI do
try do
List.foldl parts, dict, fn(kvstr, acc) ->
- pair = case Regex.split(%r/=/, kvstr) do
+ case Regex.split(%r/=/, kvstr) do
match: [ key, value ] when key != ""
- { decode(key), decode(value) }
+ impl.put acc, decode(key), decode(value)
else:
throw :malformed_query_string
end
- impl.put acc, pair
end
catch: :malformed_query_string
nil
View
3  test/elixir/dict_test.exs
@@ -29,9 +29,6 @@ defmodule DictTest.Common do
end
test :put do
- dict = Dict.put(empty_dict, {"first_key", 1})
- assert 1 == Dict.get dict, "first_key"
-
dict = Dict.put(new_dict, "first_key", {1})
assert {1} == Dict.get dict, "first_key"
assert 2 == Dict.get dict, "second_key"
Please sign in to comment.
Something went wrong with that request. Please try again.