View Source ValueAt protocol (Dsv v0.1.1)
A protocol to retrive an element at a specific position within a data structure.
Default Implementations
The protocol provides default implementations for common data types:
BitString
: Retrieves the grapheme at the specified position.List
: Retrieves the element at the specified index.
Position start from zero:
iex> ValueAt.at("Hello! Dzień dobry!", 5)
"!"
Implementation return value at the given position:
iex> ValueAt.at(["a", :b, ["c"], %{d: "d"}], 3)
%{d: "d"}
In both implementations, List
and BitString
, when an index is out of bounds, nil
is returned.
iex> ValueAt.at("Hello", 10)
nil
Custom implementation example
Implementation of this protocl require a ValueAt.at(data, position)
function.
defmodule MyStruct do
defstruct data: [1, 2, 3]
end
defimpl At, for: MyStruct do
def at(%MyStruct{data: data}, index) when is_integer(index) and index >= 0 do
Enum.at(data, index)
end
end
Summary
Functions
Retrieves an element at the specified position within the data structure.
Types
@type t() :: term()
Functions
Retrieves an element at the specified position within the data structure.
Parameters
data
- The data structure from which to retrieve the element.index
- The position (index) at which to retrieve the element.
Returns
- The element at the specified position within the data structure.
- If position is out of bound,
nil
should be returned.