Operator v0.0.1 Operator

Helpers for defining operator aliases for functions

Operators can be hard to follow, especially with the limited number available in Elixir. Always having a named function backing an operator makes it easy to fall back to the named version. Named fall backs are also very useful for piping (|>).

Examples

defmodule Example do
  use Operator

  @doc "Divide two numbers"
  @operator a ~> b
  def divide(a, b), do: a / b

  @doc "Multiply two numbers"
  @operator a <~> b
  def multiply(a, b), do: a * b
end

import Example

divide(10, 5)
#=> 5

10 ~> 2
#=> 5

multiply(10, 2)
#=> 20

10 <~> 2
#=> 20

Summary

Macros

Modified def that applies the most recent @operator

Define an operator alias for a named function

Macros

def(call, expr \\ nil)
def(term, any, nil | any) :: any

Modified def that applies the most recent @operator

Examples

@operator x <~> y
# ...
def add(a, b), do: a + b

add(1, 2)
#=> 3

1 <~> 2
#=> 3
defalias(fun_name, list)
defalias(term, atom, [{:as, any}]) :: any

Define an operator alias for a named function

Examples

defalias(max, a <|> b)

10 <|> 8
#=> 10