Skip to content

Latest commit

 

History

History
139 lines (85 loc) · 3.59 KB

optic_tuples.md

File metadata and controls

139 lines (85 loc) · 3.59 KB

Module optic_tuples

A set of optics specific to tuples.

Function Index

all/0
all/1 Focus on all elements of a tuple.
element/1
element/2 Focus on the nth element of a tuple.
field/3
field/4 Focus on a record field.

Function Details

all/0


all() -> optic:optic()

See also: all/1.

all/1


all(Options) -> optic:optic()

Options: Common optic options.

returns: An opaque optic record.

Focus on all elements of a tuple.

Example:

  > optic:get([optic_tuples:all()], {1,2,3}).
  {ok,[1,2,3]}

element/1


element(N) -> optic:optic()
  • N = pos_integer()

See also: element/2.

element/2


element(N, Options) -> optic:optic()

N: The index of the tuple element to focus on.
Options: Common optic options.

returns: An opaque optic record.

Focus on the nth element of a tuple. As with erlang:element/2, indexing begins at 1.

Example:

  > optic:get([optic_tuples:element(1)], {1,2,3}).
  {ok,[1]}

field/3


field(Tag, Size, N) -> optic:optic()
  • Tag = atom()
  • Size = pos_integer()
  • N = pos_integer()

See also: field/4.

field/4


field(Tag, Size, N, Options) -> optic:optic()

Tag: The expected record tag.
Size: The expected record size.
N: The index of the field in the record tuple.
Options: Common optic options.

returns: An opaque optic record.

Focus on a record field. As records are a compiler construct, this depends on the ?OPTIC_FIELD macro in include/optic_tuples.hrl to construct the required arguments from the record definition.

Given the record definition:

  -include_lib("optic/include/optic_tuples.hrl").
  -record(example, {first}).

Example:

  > optic:get([optic_tuples:field(?OPTIC_FIELD(example, first))],
              #example{first=1}).
  {ok,[1]}