Swiss.List (swiss v3.12.0) View Source

Helper functions for Lists

Link to this section Summary

Functions

Returns whether a given value is a non-empty list.

Replaces a value in a list at the position indicated by the given predicate.

Link to this section Functions

Specs

non_empty?(value :: any()) :: boolean()

Returns whether a given value is a non-empty list.

Examples

iex> Swiss.List.non_empty?([])
false

iex> Swiss.List.non_empty?([1])
true

iex> Swiss.List.non_empty?(nil)
false
Link to this function

replace_with(list, value, finder)

View Source

Specs

replace_with(list :: [any()], value :: any(), finder :: (any() -> boolean())) ::
  [any()]

Replaces a value in a list at the position indicated by the given predicate.

The predicate receives list values as its sole argument and should return true when the value to be replaced is found.

When a value isn't found, list is returned as is.

Examples

iex> Swiss.List.replace_with([1, 2, 3], 5, &(&1 == 2))
[1, 5, 3]

iex> Swiss.List.replace_with([1, 2, 3], 5, &(&1 == 5))
[1, 2, 3]