Skip to content

Commit 45b088b

Browse files
feat: add Complex.Kernel (#12)
* feat: add Complex.Kernel * chore: make formatter happy * chore: bump elixir requirement to Nx requirement * Apply suggestions from code review Co-authored-by: José Valim <jose.valim@gmail.com> * Update lib/complex/kernel.ex * chore: format * docs: update readme * Update lib/complex/kernel.ex Co-authored-by: José Valim <jose.valim@gmail.com> * Update README.md Co-authored-by: José Valim <jose.valim@gmail.com> Co-authored-by: José Valim <jose.valim@gmail.com>
1 parent 7b25e30 commit 45b088b

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ numbers.
55

66
Each complex number is represented as a structure holding the real and
77
imaginary part. There are functions for creation and manipulation of
8-
them. Unfortunately since there is no operator overloading in Elixir the
9-
math functions (add, subtract, etc.) are implemented as add/2, sub/2, etc.
8+
them. Users can `use Complex.Kernel` to get operators to work on a mix
9+
of real and complex numbers.
1010

1111
## Installation
1212

1313
Add *complex* as a dependency in your `mix.exs` file.
1414

1515
```elixir
1616
def deps do
17-
[ { :complex, "~> 0.2.0" } ]
17+
[{:complex, "~> 0.3.0"}]
1818
end
1919
```
20+
2021
After you are done, run `mix deps.get` in your shell to fetch and compile
2122
Complex. Start an interactive Elixir shell with `iex -S mix` and try the examples
2223
in the [examples section](#examples).
@@ -26,12 +27,15 @@ in the [examples section](#examples).
2627
Documentation for the package is available online via Hex at
2728
[http://hexdocs.pm/complex](http://hexdocs.pm/complex). You can also generate
2829
local docs via the mix task
30+
2931
```elixir
3032
mix docs
3133
```
34+
3235
This will generate the HTML documentation and place it into the `doc` subdirectory.
3336

3437
## Examples
38+
3539
```elixir
3640
iex> Complex.new(3, 4)
3741
%Complex{im: 4.0, re: 3.0}

lib/complex/kernel.ex

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
defmodule Complex.Kernel do
2+
@moduledoc """
3+
Provides operator overloading for Elixir operators.
4+
5+
When you `use Complex.Kernel`, be aware that the arithmetic operators
6+
won't work in clause guards. For that you need to use the fully qualified
7+
functions (i.e.: `when Kernel.+(a, b) == 1`) instead.
8+
"""
9+
10+
# This is defined as such so that Elixir 1.12 formatters don't complain
11+
# and Elixir 1.13+ formatters don't remove the quotes from :"**" that
12+
# would make this work in Elixir <= 1.12
13+
@pow_atom String.to_atom("**")
14+
15+
defmacro __using__(_) do
16+
operators = [{@pow_atom, 2}, +: 2, -: 2, /: 2, *: 2]
17+
18+
quote do
19+
import Kernel, except: unquote(operators)
20+
import Complex.Kernel, only: unquote(operators)
21+
end
22+
end
23+
24+
def a + b, do: Complex.add(a, b)
25+
def a - b, do: Complex.subtract(a, b)
26+
def a * b, do: Complex.multiply(a, b)
27+
def a / b, do: Complex.divide(a, b)
28+
29+
def unquote(@pow_atom)(a, b), do: Complex.power(a, b)
30+
end

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule Complex.Mixfile do
77
version: "0.3.0",
88
description: description(),
99
package: package(),
10-
elixir: "~> 1.1",
10+
elixir: "~> 1.12",
1111
deps: deps(),
1212
docs: [extras: [], before_closing_body_tag: &before_closing_body_tag/1]
1313
]

0 commit comments

Comments
 (0)