Comparable v1.0.0 Comp View Source

Provides utilities to implement and work with Comparable types

Link to this section Summary

Functions

Infix shortcut for Comp.less_than?/2

Infix shortcut for Comp.not_equal?/2

Infix shortcut for Comp.equal?/2

Enables <~>, <|>, >>>, <<<, ~>>, <<~ infix shortcuts and Comp.gt, Comp.lt, Comp.eq, defcomparable, gen_ne_test, gen_eq_test macro

Compare left and right term

Helper to define ordering relation for pair of types, accepts two term :: type pairs and block of code where relation is described

Is left term equal to right term?

Is left term greater or equal to right term?

Is left term greater than right term?

Is left term less or equal to right term?

Is left term less than right term?

Returns the biggest of the two given terms, if terms are equal - then the first one is returned

Returns the smallest of the two given terms, if terms are equal - then the first one is returned

Is left term not equal to right term?

Link to this section Types

Link to this section Functions

Link to this macro

left <<< right View Source (macro)

Infix shortcut for Comp.less_than?/2

Examples

iex> use Comp
Comp
iex> 1 <<< 1
false
iex> 1 <<< 2
true
iex> 2 <<< 1
false
Link to this macro

left <<~ right View Source (macro)

Infix shortcut for Comp.less_or_equal?/2

Examples

iex> use Comp
Comp
iex> 1 <<~ 1
true
iex> 1 <<~ 2
true
iex> 2 <<~ 1
false
Link to this macro

left <|> right View Source (macro)

Infix shortcut for Comp.not_equal?/2

Examples

iex> use Comp
Comp
iex> 1 <|> 1
false
iex> 1 <|> :hello
true
Link to this macro

left <~> right View Source (macro)

Infix shortcut for Comp.equal?/2

Examples

iex> use Comp
Comp
iex> 1 <~> 1
true
iex> 1 <~> :hello
false
Link to this macro

left >>> right View Source (macro)

Infix shortcut for Comp.greater_than?/2

Examples

iex> use Comp
Comp
iex> 1 >>> 1
false
iex> 1 >>> 2
false
iex> 2 >>> 1
true

Enables <~>, <|>, >>>, <<<, ~>>, <<~ infix shortcuts and Comp.gt, Comp.lt, Comp.eq, defcomparable, gen_ne_test, gen_eq_test macro

Link to this function

compare(left, right) View Source
compare(left(), right()) :: Comparable.ord()

Compare left and right term

Examples

iex> Comp.compare(1, 2)
:lt
iex> Comp.compare(2, 1)
:gt
iex> Comp.compare(1, 1)
:eq
Link to this macro

defcomparable(arg1, arg2, list) View Source (macro)

Helper to define ordering relation for pair of types, accepts two term :: type pairs and block of code where relation is described.

Examples

iex> quote do
...>   use Comp
...>   defmodule Foo do
...>     defstruct [:value, :meta]
...>   end
...>   defmodule Bar do
...>     defstruct [:value, :meta]
...>   end
...>   defcomparable %Foo{value: left} :: Foo, %Foo{value: right} :: Foo do
...>     Comp.compare(left, right)
...>   end
...>   defcomparable %Foo{value: left} :: Foo, %Bar{value: right} :: Bar do
...>     Comp.compare(left, right)
...>   end
...>   defcomparable %Foo{value: left} :: Foo, right :: Integer do
...>     Comp.compare(left, right)
...>   end
...> end
...> |> Code.compile_quoted
iex> quote do
...>   x = %Foo{value: 1, meta: 1}
...>   y = %Foo{value: 1, meta: 2}
...>   Comp.equal?(x, y) && Comp.equal?(y, x)
...> end
...> |> Code.eval_quoted
...> |> elem(0)
true
iex> quote do
...>   x = %Foo{value: 1, meta: 1}
...>   y = %Bar{value: 1, meta: 2}
...>   Comp.equal?(x, y) && Comp.equal?(y, x)
...> end
...> |> Code.eval_quoted
...> |> elem(0)
true
iex> quote do
...>   x = %Foo{value: 1, meta: 1}
...>   y = 1
...>   Comp.equal?(x, y) && Comp.equal?(y, x)
...> end
...> |> Code.eval_quoted
...> |> elem(0)
true
Link to this function

equal?(left, right) View Source
equal?(left(), right()) :: boolean()

Is left term equal to right term?

Examples

iex> Comp.equal?(1, 1)
true
iex> Comp.equal?(1, :hello)
false
Link to this macro

gen_eq_test(name, left, right) View Source (macro)

Link to this macro

gen_ne_test(name, left, right) View Source (macro)

Link to this function

greater_or_equal?(left, right) View Source
greater_or_equal?(left(), right()) :: boolean()

Is left term greater or equal to right term?

Examples

iex> Comp.greater_or_equal?(1, 1)
true
iex> Comp.greater_or_equal?(1, 2)
false
iex> Comp.greater_or_equal?(2, 1)
true
Link to this function

greater_than?(left, right) View Source
greater_than?(left(), right()) :: boolean()

Is left term greater than right term?

Examples

iex> Comp.greater_than?(1, 1)
false
iex> Comp.greater_than?(1, 2)
false
iex> Comp.greater_than?(2, 1)
true
Link to this function

less_or_equal?(left, right) View Source
less_or_equal?(left(), right()) :: boolean()

Is left term less or equal to right term?

Examples

iex> Comp.less_or_equal?(1, 1)
true
iex> Comp.less_or_equal?(1, 2)
true
iex> Comp.less_or_equal?(2, 1)
false
Link to this function

less_than?(left, right) View Source
less_than?(left(), right()) :: boolean()

Is left term less than right term?

Examples

iex> Comp.less_than?(1, 1)
false
iex> Comp.less_than?(1, 2)
true
iex> Comp.less_than?(2, 1)
false
Link to this function

max(left, right) View Source
max(left(), right()) :: left() | right()

Returns the biggest of the two given terms, if terms are equal - then the first one is returned

Examples

iex> Comp.max(1, 1)
1
iex> Comp.max(1, 2)
2
iex> Comp.max(2, 1)
2
Link to this function

min(left, right) View Source
min(left(), right()) :: left() | right()

Returns the smallest of the two given terms, if terms are equal - then the first one is returned

Examples

iex> Comp.min(1, 1)
1
iex> Comp.min(1, 2)
1
iex> Comp.min(2, 1)
1
Link to this function

not_equal?(left, right) View Source
not_equal?(left(), right()) :: boolean()

Is left term not equal to right term?

Examples

iex> Comp.not_equal?(1, 1)
false
iex> Comp.not_equal?(1, :hello)
true
Link to this macro

left ~>> right View Source (macro)

Infix shortcut for Comp.greater_or_equal?/2

Examples

iex> use Comp
Comp
iex> 1 ~>> 1
true
iex> 1 ~>> 2
false
iex> 2 ~>> 1
true