View Source Warder.Element protocol (warder v0.1.0)

Protocol for Element Implementations.

Any data type that can be used as an element in a range must implement this protocol.

Discrete Ranges

Discrete ranges are ranges where the elements are not continuous. For example, a range of integers from 1 to 10 is a discrete range. There is no elements between 1 and 2, 2 and 3, and so on. The elements are separated by a step of 1.

In discrete ranges, the consecutive?/2 function must return true if the two elements are consecutive. For example, in a range of integers, 1 and 2 are consecutive, but 1 and 3 are not.

The canonicalize/1 function must return the next element in the range. For example, if the range is from 1 to 10, and the element is 1, the canonicalize/1 function must return 2.

Indiscrete ranges are ranges where the elements are continuous. For example, a range of floats from 1.0 to 10.0 is an indiscrete range. There are infinite elements between 1.0 and 2.0, 2.0 and 3.0, and so on.

In indiscrete ranges, the consecutive?/2 function must return false for any two elements. The canonicalize/1 function must return :error.

Example

defimpl Warder.Element, for: Date do
  def compare(left, right), do: Date.compare(left, right)

  def consecutive?(left, right), do: Date.diff(left, right) == 1

  def canonicalize(value), do: {:ok, Date.add(value, 1)}
end

Implementations

The protocol is defined for the following types:

Summary

Functions

Canonicalize value a step up.

Compare two elements.

Are those two elements consecutive?

Types

@type t() :: term()

Functions

Link to this function

canonicalize(value)

View Source (since 0.1.0)
@spec canonicalize(value :: element) :: {:ok, element} | :error when element: t()

Canonicalize value a step up.

Always returns :error for indiscrete ranges.

Link to this function

compare(left, right)

View Source (since 0.1.0)
@spec compare(left :: element, right :: element) :: :lt | :eq | :gt when element: t()

Compare two elements.

Returns :lt if the left element is less than the right element, :eq if they are equal, and :gt if the left element is greater than the right element.

Link to this function

consecutive?(left, right)

View Source (since 0.1.0)
@spec consecutive?(left :: element, right :: element) :: boolean() when element: t()

Are those two elements consecutive?

That means that no other elements can be between left and right.

Always returns false for indiscrete ranges.