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
- Appendable
- Mappable
- Unwrappable
- 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 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"}}
takes a normal string and puts in a Text Container.
Examples
iex> Containers.Text.from_string("hello world")
%Containers.Text{value: "hello world"}
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"]}
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.
Examples
iex> hello = "hello" |> Containers.Text.from_string()
iex> Containers.Text.to_atom(hello)
%Containers.Result{value: {:ok, :hello}}