

Noether — noether v0.2.4

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

Noether

(noether v0.2.4)

=====================================

Noether aims to ease common data manipulation tasks by introducing simple algebraic functions and other utilities.

Functions and names are inspired (sometimes taken as-is) from Haskell.

The [`Noether.Maybe`](Noether.Maybe.html) module introduces operations on nullable values.  

The [`Noether.Either`](Noether.Either.html) module introduces operations on `{:ok, _} | {:error, _}` values.  

The [`Noether.List`](Noether.List.html) module introduces operations on lists.

The root module has a few simple functions one might find of use.

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[fun1()](#t:fun1/0)

[fun2()](#t:fun2/0)

[Functions](#functions)

------------------------

[curry(arg, f)](#curry/2)

Takes a tuple and a function of arity 2. It applies the two values in the tuple to the function.

[flip(f)](#flip/1)

Takes a function of arity 2 and returns the same function with its arguments in reverse order, i.e., "flipped".

Please note that if a function of different arity is given, a function of arity 2 is returned where the two arguments will be applied to the given function.

[uncurry(a, b, f)](#uncurry/3)

Takes two values and applies them to a function or arity 1 in form of a tuple.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:fun1/0 "Link to this type")

fun1()

======

```

@type fun1() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this type](#t:fun2/0 "Link to this type")

fun2()

======

```

@type fun2() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#curry/2 "Link to this function")

curry(arg, f)

=============

```

@spec curry([tuple](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun2](#t:fun2/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Takes a tuple and a function of arity 2. It applies the two values in the tuple to the function.

[examples](#curry/2-examples)

 Examples

----------------------------------------

```

iex> curry({1, 2}, &Kernel.+/2)

3

```

[Link to this function](#flip/1 "Link to this function")

flip(f)

=======

```

@spec flip([fun2](#t:fun2/0)()) :: [fun2](#t:fun2/0)()

```

Takes a function of arity 2 and returns the same function with its arguments in reverse order, i.e., "flipped".

Please note that if a function of different arity is given, a function of arity 2 is returned where the two arguments will be applied to the given function.

[examples](#flip/1-examples)

 Examples

---------------------------------------

```

iex> flip(&Kernel.-/2).(3, 4)

1

```

[Link to this function](#uncurry/3 "Link to this function")

uncurry(a, b, f)

================

```

@spec uncurry([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Takes two values and applies them to a function or arity 1 in form of a tuple.

[examples](#uncurry/3-examples)

 Examples

------------------------------------------

```

iex> uncurry(1, 2, &(&1))

{1, 2}

```





Noether.Either — noether v0.2.4

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

Noether.Either

(noether v0.2.4)

============================================

This module hosts several utility functions to work with `{:ok, _} | {:error, _}` values.

These type of values will be then called `Either`.

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[either()](#t:either/0)

[fun0()](#t:fun0/0)

[fun1()](#t:fun1/0)

[Functions](#functions)

------------------------

[bind(a, f)](#bind/2)

Given an `{:ok, value}` and a function that returns an Either value, it applies the function on the `value`. It effectively "squashes" an `{:ok, {:ok, v}}` or `{:ok, {:error, _}}` to its most appropriate representation.

If an `{:error, _}` is given, it is returned as-is.

[cat\_either(a, f)](#cat_either/2)

Given a list of Either, the function is mapped only on the elements of type `{:ok, _}`. Other values will be discarded. A list of the results is returned outside of the tuple.

[choose(a, f, g)](#choose/3)

Given a value and two functions that return an Either, it applies the first one and returns the result if it matches `{:ok, _}`. Otherwise the second function is applied.

[either(a, f, g)](#either/3)

Given an Either and two functions, it applies the first or second one on the second value of the tuple, depending if the value is `{:ok, _}` or `{:error, _}` respectively.

[error?(a)](#error?/1)

It returns `true` only if the value given matches a `{:error, value}` type.

[flat\_map(either, f)](#flat_map/2)

Alias for [`bind/2`](#bind/2)

[flatten(either)](#flatten/1)

Alias for [`join/1`](#join/1).

[join(a)](#join/1)

Given an `{:ok, {:ok, value}}` it flattens the ok unwrapping the `value` and returning `{:ok, value}`.

If an `{:error, _}` is given, it is returned as-is.

[map(a, f)](#map/2)

Given an `{:ok, value}` and a function, it applies the function on the `value` returning `{:ok, f.(value)}`.

If an `{:error, _}` is given, it is returned as-is.

[map\_all(values, f)](#map_all/2)

Given a list of values and a function returning `{:ok, any}` or `{:error, any}`, it applies the function on every

`value` returning `{:ok, values}` if every `f.(v)` results in `{:ok, v}`; returning `{:error, _}` if `f.(v) results in an`.

[map\_error(a, f)](#map_error/2)

Given an Either and one function, it applies the function to the `{:error, _}` tuple.

[ok?(a)](#ok?/1)

It returns `true` only if the value given matches a `{:ok, value}` type.

[or\_else(a, f)](#or_else/2)

Given an Either and a function, it returns the value as-is when it's ok, or executes the function and returns its result.

[try(value, f)](#try/2)

Given a `value` and a function, it applies the function on the `value` returning `{:ok, f.(value)}`.

If the function throws an exception `e` then it is wrapped into an `{:error, e}`.

[unwrap(a, b \\ nil)](#unwrap/2)

It returns the value of an `{:ok, value}` only if such a tuple is given. If not, the default value (`nil` if not provided) is returned.

[wrap(a)](#wrap/1)

Given any value, it makes sure the result is an Either type.

[wrap\_err(a)](#wrap_err/1)

Given any value, it makes sure the result is an Either type.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:either/0 "Link to this type")

either()

========

```

@type either() :: {:ok, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()} | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

[Link to this type](#t:fun0/0 "Link to this type")

fun0()

======

```

@type fun0() :: (() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this type](#t:fun1/0 "Link to this type")

fun1()

======

```

@type fun1() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#bind/2 "Link to this function")

bind(a, f)

==========

```

@spec bind([either](#t:either/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given an `{:ok, value}` and a function that returns an Either value, it applies the function on the `value`. It effectively "squashes" an `{:ok, {:ok, v}}` or `{:ok, {:error, _}}` to its most appropriate representation.

If an `{:error, _}` is given, it is returned as-is.

[examples](#bind/2-examples)

 Examples

---------------------------------------

```

iex> bind({:ok, 1}, fn a -> {:ok, a + 1} end)

{:ok, 2}

iex> bind({:ok, 1}, fn \_ -> {:error, 5} end)

{:error, 5}

iex> bind({:error, 1}, fn \_ -> {:ok, 45} end)

{:error, 1}

```

[Link to this function](#cat_either/2 "Link to this function")

cat\_either(a, f)

=================

```

@spec cat_either([[either](#t:either/0)()], [fun1](#t:fun1/0)()) :: [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]

```

Given a list of Either, the function is mapped only on the elements of type `{:ok, _}`. Other values will be discarded. A list of the results is returned outside of the tuple.

[examples](#cat_either/2-examples)

 Examples

---------------------------------------------

```

iex> cat\_either([{:ok, 1}], &(&1 + 1))

[2]

iex> cat\_either([{:ok, 1}, {:error, 2}, {:ok, 3}], &(&1 + 1))

[2, 4]

```

[Link to this function](#choose/3 "Link to this function")

choose(a, f, g)

===============

```

@spec choose([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given a value and two functions that return an Either, it applies the first one and returns the result if it matches `{:ok, _}`. Otherwise the second function is applied.

[examples](#choose/3-examples)

 Examples

-----------------------------------------

```

iex> choose(0, fn a -> {:ok, a + 1} end, fn b -> {:ok, b + 2} end)

{:ok, 1}

iex> choose(0, fn \_ -> {:error, 1} end, fn b -> {:ok, b + 2} end)

{:ok, 2}

iex> choose(0, fn \_ -> {:error, 1} end, fn \_ -> {:error, 2} end)

{:error, 2}

```

[Link to this function](#either/3 "Link to this function")

either(a, f, g)

===============

```

@spec either([either](#t:either/0)(), [fun1](#t:fun1/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given an Either and two functions, it applies the first or second one on the second value of the tuple, depending if the value is `{:ok, _}` or `{:error, _}` respectively.

[examples](#either/3-examples)

 Examples

-----------------------------------------

```

iex> either({:ok, 1}, &(&1 + 1), &(&1 + 2))

{:ok, 2}

iex> either({:error, 1}, &(&1 + 1), &(&1 + 2))

{:error, 3}

```

[Link to this function](#error?/1 "Link to this function")

error?(a)

=========

```

@spec error?([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

It returns `true` only if the value given matches a `{:error, value}` type.

[examples](#error?/1-examples)

 Examples

-----------------------------------------

```

iex> error?({:ok, 1})

false

iex> error?({:error, 2})

true

iex> error?(3)

false

```

[Link to this function](#flat_map/2 "Link to this function")

flat\_map(either, f)

====================

```

@spec flat_map([either](#t:either/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Alias for [`bind/2`](#bind/2)

[examples](#flat_map/2-examples)

 Examples

-------------------------------------------

```

iex> flat\_map({:ok, 1}, fn a -> {:ok, a + 1} end)

{:ok, 2}

iex> flat\_map({:ok, 1}, fn \_ -> {:error, 5} end)

{:error, 5}

iex> flat\_map({:error, 1}, fn \_ -> {:ok, 45} end)

{:error, 1}

```

[Link to this function](#flatten/1 "Link to this function")

flatten(either)

===============

```

@spec flatten([either](#t:either/0)()) :: [either](#t:either/0)()

```

Alias for [`join/1`](#join/1).

[examples](#flatten/1-examples)

 Examples

------------------------------------------

```

iex> flatten({:ok, {:ok, 1}})

{:ok, 1}

iex> flatten({:ok, 1})

\*\* (FunctionClauseError) no function clause matching in Noether.Either.join/1

iex> flatten({:error, "Value not found"})

{:error, "Value not found"}

```

[Link to this function](#join/1 "Link to this function")

join(a)

=======

```

@spec join([either](#t:either/0)()) :: [either](#t:either/0)()

```

Given an `{:ok, {:ok, value}}` it flattens the ok unwrapping the `value` and returning `{:ok, value}`.

If an `{:error, _}` is given, it is returned as-is.

[examples](#join/1-examples)

 Examples

---------------------------------------

```

iex> join({:ok, {:ok, 1}})

{:ok, 1}

iex> join({:ok, 1})

\*\* (FunctionClauseError) no function clause matching in Noether.Either.join/1

iex> join({:error, "Value not found"})

{:error, "Value not found"}

```

[Link to this function](#map/2 "Link to this function")

map(a, f)

=========

```

@spec map([either](#t:either/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given an `{:ok, value}` and a function, it applies the function on the `value` returning `{:ok, f.(value)}`.

If an `{:error, _}` is given, it is returned as-is.

[examples](#map/2-examples)

 Examples

--------------------------------------

```

iex> map({:ok, -1}, &Kernel.abs/1)

{:ok, 1}

iex> map({:error, "Value not found"}, &Kernel.abs/1)

{:error, "Value not found"}

```

[Link to this function](#map_all/2 "Link to this function")

map\_all(values, f)

===================

```

@spec map_all([[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()], ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [either](#t:either/0)())) :: {:ok, [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]} | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

Given a list of values and a function returning `{:ok, any}` or `{:error, any}`, it applies the function on every

`value` returning `{:ok, values}` if every `f.(v)` results in `{:ok, v}`; returning `{:error, _}` if `f.(v) results in an`.

[examples](#map_all/2-examples)

 Examples

------------------------------------------

```

iex> map\_all(["23:50:07.0123456", "23:50:07.123Z"], &Time.from\_iso8601/1)

{:ok, [~T[23:50:07.012345], ~T[23:50:07.123]]}

iex> map\_all(["23:50:61", "23:50:07.123Z"], &Time.from\_iso8601/1)

{:error, :invalid\_time}

```

[Link to this function](#map_error/2 "Link to this function")

map\_error(a, f)

================

```

@spec map_error([either](#t:either/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given an Either and one function, it applies the function to the `{:error, _}` tuple.

[examples](#map_error/2-examples)

 Examples

--------------------------------------------

```

iex> map\_error({:ok, 1}, &(&1 + 1))

{:ok, 1}

iex> map\_error({:error, 1}, &(&1 + 1))

{:error, 2}

```

[Link to this function](#ok?/1 "Link to this function")

ok?(a)

======

```

@spec ok?([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [boolean](https://hexdocs.pm/elixir/typespecs.html#built-in-types)()

```

It returns `true` only if the value given matches a `{:ok, value}` type.

[examples](#ok?/1-examples)

 Examples

--------------------------------------

```

iex> ok?({:ok, 1})

true

iex> ok?({:error, 2})

false

iex> ok?(3)

false

```

[Link to this function](#or_else/2 "Link to this function")

or\_else(a, f)

==============

```

@spec or_else([either](#t:either/0)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given an Either and a function, it returns the value as-is when it's ok, or executes the function and returns its result.

[examples](#or_else/2-examples)

 Examples

------------------------------------------

```

iex> or\_else({:ok, 1}, fn \_ -> {:ok, 2} end)

{:ok, 1}

iex> or\_else({:error, 1}, fn \_ -> {:ok, 2} end)

{:ok, 2}

iex> or\_else({:error, 1}, fn x -> {:ok, x + 2} end)

{:ok, 3}

```

[Link to this function](#try/2 "Link to this function")

try(value, f)

=============

```

@spec try([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)()) :: [either](#t:either/0)()

```

Given a `value` and a function, it applies the function on the `value` returning `{:ok, f.(value)}`.

If the function throws an exception `e` then it is wrapped into an `{:error, e}`.

[examples](#try/2-examples)

 Examples

--------------------------------------

```

iex> try("42", &String.to\_integer/1)

{:ok, 42}

iex> try("nan", &String.to\_integer/1)

{:error, %ArgumentError{message: "errors were found at the given arguments:\n\n \* 1st argument: not a textual representation of an integer\n"}}

```

[Link to this function](#unwrap/2 "Link to this function")

unwrap(a, b \\ nil)

===================

It returns the value of an `{:ok, value}` only if such a tuple is given. If not, the default value (`nil` if not provided) is returned.

[examples](#unwrap/2-examples)

 Examples

-----------------------------------------

```

iex> unwrap({:ok, 1})

1

iex> unwrap(2)

nil

iex> unwrap({:ok, 1}, :default\_value)

1

iex> unwrap(2, :default\_value)

:default\_value

```

[Link to this function](#wrap/1 "Link to this function")

wrap(a)

=======

```

@spec wrap([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [either](#t:either/0)()

```

Given any value, it makes sure the result is an Either type.

[examples](#wrap/1-examples)

 Examples

---------------------------------------

```

iex> wrap({:ok, 1})

{:ok, 1}

iex> wrap({:error, 2})

{:error, 2}

iex> wrap(3)

{:ok, 3}

```

[Link to this function](#wrap_err/1 "Link to this function")

wrap\_err(a)

============

```

@spec wrap_err([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [either](#t:either/0)()

```

Given any value, it makes sure the result is an Either type.

[examples](#wrap_err/1-examples)

 Examples

-------------------------------------------

```

iex> wrap\_err({:ok, 1})

{:ok, 1}

iex> wrap\_err({:error, 2})

{:error, 2}

iex> wrap\_err(3)

{:error, 3}

```





Noether.List — noether v0.2.4

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

Noether.List

(noether v0.2.4)

==========================================

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[fun1()](#t:fun1/0)

[fun2()](#t:fun2/0)

[Functions](#functions)

------------------------

[sequence(a)](#sequence/1)

Given a list, it returns `{:ok, list}` if every element of the list is different from nil or `{:error, _}`. Otherwise `{:error, :nil_found}` is returned.

[until(p, f, a)](#until/3)

Given a predicate, a function of arity 1, and a value, the function is applied repeatedly until the predicate applied to the value returns either `nil`, `false`, or `{:error, _}`. The list of results is returned.

[zip\_with(a, b, f)](#zip_with/3)

Given two lists and a function of arity 2, the lists are first zipped and then each tuple is applied (curried) to the function.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:fun1/0 "Link to this type")

fun1()

======

```

@type fun1() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this type](#t:fun2/0 "Link to this type")

fun2()

======

```

@type fun2() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#sequence/1 "Link to this function")

sequence(a)

===========

```

@spec sequence([[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]) :: {:ok, [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]} | {:error, [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()}

```

Given a list, it returns `{:ok, list}` if every element of the list is different from nil or `{:error, _}`. Otherwise `{:error, :nil_found}` is returned.

[examples](#sequence/1-examples)

 Examples

-------------------------------------------

```

iex> sequence([1, 2])

{:ok, [1, 2]}

iex> sequence([1, nil, 3])

{:error, :nil\_found}

iex> sequence([{:ok, 1}, {:ok, 2}])

{:ok, [1, 2]}

iex> sequence([{:ok, 1}, {:error, 2}, {:ok, 3}])

{:error, 2}

iex> sequence([{:error, 1}, {:error, 2}])

{:error, 1}

```

[Link to this function](#until/3 "Link to this function")

until(p, f, a)

==============

```

@spec until([fun1](#t:fun1/0)(), [fun1](#t:fun1/0)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]

```

Given a predicate, a function of arity 1, and a value, the function is applied repeatedly until the predicate applied to the value returns either `nil`, `false`, or `{:error, _}`. The list of results is returned.

[examples](#until/3-examples)

 Examples

----------------------------------------

```

iex> until(fn a -> a < 10 end, &(&1 + 1), 0)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

```

[Link to this function](#zip_with/3 "Link to this function")

zip\_with(a, b, f)

==================

```

@spec zip_with([[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()], [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()], [fun2](#t:fun2/0)()) :: [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]

```

Given two lists and a function of arity 2, the lists are first zipped and then each tuple is applied (curried) to the function.

[examples](#zip_with/3-examples)

 Examples

-------------------------------------------

```

iex> zip\_with([1, 2, 3], [4, 5, 6], &Kernel.+/2)

[5, 7, 9]

```





Noether.Maybe — noether v0.2.4

 try {

 var settings = JSON.parse(localStorage.getItem('ex\_doc:settings') || '{}');

 if (settings.theme === 'dark' ||

 ((settings.theme === 'system' || settings.theme == null) &&

 window.matchMedia('(prefers-color-scheme: dark)').matches)

 ) {

 document.body.classList.add('dark')

 }

 } catch (error) { }

 

Settings

Noether.Maybe

(noether v0.2.4)

===========================================

[Link to this section](#summary)

 Summary

==========================================

[Types](#types)

----------------

[fun1()](#t:fun1/0)

[Functions](#functions)

------------------------

[cat\_maybe(a, f)](#cat_maybe/2)

Given a list of values, the function is mapped only on the elements different from `nil`. `nil` values will be discarded. A list of the results is returned.

[choose(a, f, g)](#choose/3)

Given a value and two functions, it applies the first one and returns the result if it's different from nil. Otherwise the second function is applied.

[map(a, f)](#map/2)

Given a value and a function, the function is applied only if the value is different from `nil`. `nil` is returned otherwise.

[maybe(a, f, default)](#maybe/3)

Given a value, a function, and a default, it applies the function on the value if the latter is different from `nil`. It returns the default otherwise.

[required(a, default)](#required/2)

Given a value and a default, `{:ok, value}` is returned only if the value is different from `nil`. `{:error, default}` is returned otherwise.

[Link to this section](#types)

Types

=====================================

[Link to this type](#t:fun1/0 "Link to this type")

fun1()

======

```

@type fun1() :: ([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)() -> [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)())

```

[Link to this section](#functions)

Functions

=============================================

[Link to this function](#cat_maybe/2 "Link to this function")

cat\_maybe(a, f)

================

```

@spec cat_maybe([[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()], [fun1](#t:fun1/0)()) :: [[any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()]

```

Given a list of values, the function is mapped only on the elements different from `nil`. `nil` values will be discarded. A list of the results is returned.

[examples](#cat_maybe/2-examples)

 Examples

--------------------------------------------

```

iex> cat\_maybe([1], &(&1 + 1))

[2]

iex> cat\_maybe([1, nil, 3], &(&1 + 1))

[2, 4]

```

[Link to this function](#choose/3 "Link to this function")

choose(a, f, g)

===============

```

@spec choose([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)(), [fun1](#t:fun1/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Given a value and two functions, it applies the first one and returns the result if it's different from nil. Otherwise the second function is applied.

[examples](#choose/3-examples)

 Examples

-----------------------------------------

```

iex> choose(0, fn a -> a + 1 end, fn b -> b + 2 end)

1

iex> choose(0, fn \_ -> nil end, fn b -> b + 2 end)

2

iex> choose(0, fn \_ -> nil end, fn \_ -> nil end)

nil

```

[Link to this function](#map/2 "Link to this function")

map(a, f)

=========

```

@spec map([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Given a value and a function, the function is applied only if the value is different from `nil`. `nil` is returned otherwise.

[examples](#map/2-examples)

 Examples

--------------------------------------

```

iex> map(nil, &Kernel.abs/1)

nil

iex> map(-1, &Kernel.abs/1)

1

```

[Link to this function](#maybe/3 "Link to this function")

maybe(a, f, default)

====================

```

@spec maybe([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [fun1](#t:fun1/0)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()

```

Given a value, a function, and a default, it applies the function on the value if the latter is different from `nil`. It returns the default otherwise.

[examples](#maybe/3-examples)

 Examples

----------------------------------------

```

iex> maybe(-1, &Kernel.abs/1, :hello)

1

iex> maybe(nil, &Kernel.abs/1, :hello)

:hello

```

[Link to this function](#required/2 "Link to this function")

required(a, default)

====================

```

@spec required([any](https://hexdocs.pm/elixir/typespecs.html#basic-types)(), [any](https://hexdocs.pm/elixir/typespecs.html#basic-types)()) :: [Noether.Either.either](Noether.Either.html#t:either/0)()

```

Given a value and a default, `{:ok, value}` is returned only if the value is different from `nil`. `{:error, default}` is returned otherwise.

[examples](#required/2-examples)

 Examples

-------------------------------------------

```

iex> required(nil, :hello)

{:error, :hello}

iex> required(1, :hello)

{:ok, 1}

```

