containers v0.7.1 Containers.Text View Source

This container wraps a string value. This is useful for when you want to append strings together with not wanting to worry about nil throwing runtime errors.

Implemented Protocols

  1. Appendable
  2. Mappable
  3. Unwrappable
  4. Sequenceable

Link to this section Summary

Functions

Wraps the String.at function in Elixir to return an optional

Wraps the String.first function in Elixir to reutrn an optional. This will allow the Optional container protocols to be called

takes a normal string and puts in a Text Container

Wraps the String.myer_difference function in Elixir to return an Optional

Wraps the String.next_codepiont function in Elixir to return an Optional

Wraps the String.get_grapheme function in Elixir to return an Optional

Wraps the String.to_atom function but returns a Result container if it unable to parse string into an atom

Wraps the String.to_integer function but will return a Result Conainer

Link to this section Types

Link to this type t() View Source
t() :: %Containers.Text{value: String.t}

Link to this section Functions

Wraps the String.at function in Elixir to return an optional

Examples

iex> hello = Containers.Text.from_string("hello")
iex> Containers.Text.at(hello, 0)
%Containers.Optional{value: %Containers.Text{value: "h"}}

Advanced Example

In below example at returns an Optional, and since Optional implements a Mappable we can map over the in inner value and append another Text value to the inner Text value. This is safely done because if the Option value is nil it will skip the appending.

Plus, the code does not need to break the pipe to handle nil cases.

some_string
|> Text.from_string()
|> Text.at(0)
|> Containers.map(& Containers.append(&1, Text.from_string("!")))

Wraps the String.first function in Elixir to reutrn an optional. This will allow the Optional container protocols to be called

Examples

iex> hello = Containers.Text.from_string("hello")
iex> Containers.Text.first(hello)
%Containers.Optional{value: %Containers.Text{value: "h"}}
Link to this function from_string(str) View Source
from_string(String.t) :: t

takes a normal string and puts in a Text Container.

Examples

iex> Containers.Text.from_string("hello world")
%Containers.Text{value: "hello world"}
Link to this function myers_difference(text1, text2) View Source
myers_difference(t, t) :: Containers.Optional.t

Wraps the String.myer_difference function in Elixir to return an Optional.

Examples

iex> string1 = Containers.Text.from_string("fox hops over the dog")
iex> string2 = Containers.Text.from_string("fox jumps over the lazy cat")
iex> Containers.Text.myers_difference(string1, string2)
%Containers.Optional{value: [eq: "fox ", del: "ho", ins: "jum", eq: "ps over the ", del: "dog", ins: "lazy cat"]}
Link to this function next_codepoint(text) View Source
next_codepoint(t) :: Containers.Optional.t

Wraps the String.next_codepiont function in Elixir to return an Optional.

Link to this function next_grapheme(text) View Source
next_grapheme(t) :: Containers.Optional.t

Wraps the String.get_grapheme function in Elixir to return an Optional.

Wraps the String.to_atom function but returns a Result container if it unable to parse string into an atom.

Examples

iex> hello = "hello" |> Containers.Text.from_string()
iex> Containers.Text.to_atom(hello)
%Containers.Result{value: {:ok, :hello}}

Wraps the String.to_integer function but will return a Result Conainer

Examples

iex> one = "1" |> Containers.Text.from_string()
iex> Containers.Text.to_integer(one)
%Containers.Result{value: {:ok, 1}}