View Source Funx.Monad.Identity (funx v0.2.3)

The Funx.Monad.Identity module represents the identity monad, where values are simply wrapped in a structure and operations are applied directly to those values.

Functions

  • pure/1: Wraps a value in the Identity monad.
  • extract/1: Extracts the wrapped value from an Identity.
  • tap/2: Executes a side-effect function on the wrapped value, returning the original Identity unchanged.

Protocols

This module implements the following protocols:

  • Funx.Monad: Implements the bind/2, map/2, and ap/2 functions for monadic operations.
  • Funx.Eq: Defines equality checks for Identity values.
  • Funx.Ord: Defines ordering logic for Identity values.
  • String.Chars: Converts an Identity value into a string representation.

Summary

Functions

Extracts the value from an Identity.

Creates a new Identity value by wrapping a given value.

Executes a side-effect function on the wrapped value and returns the original Identity unchanged.

Types

@type t(value) :: %Funx.Monad.Identity{value: value}

Functions

@spec extract(t(value)) :: value when value: term()

Extracts the value from an Identity.

Examples

iex> Funx.Monad.Identity.extract(Funx.Monad.Identity.pure(5))
5
@spec pure(value) :: t(value) when value: term()

Creates a new Identity value by wrapping a given value.

Examples

iex> Funx.Monad.Identity.pure(5)
%Funx.Monad.Identity{value: 5}
@spec tap(t(value), (value -> any())) :: t(value) when value: term()

Executes a side-effect function on the wrapped value and returns the original Identity unchanged.

Useful for debugging, logging, or performing side effects in the middle of a pipeline without changing the value.

Examples

iex> Funx.Monad.Identity.pure(5) |> Funx.Monad.Identity.tap(fn x -> x * 2 end)
%Funx.Monad.Identity{value: 5}