Skip to content

Implement some kind of "first matching" function in Either #28

Description

@sphaso

Big picture:
having a function that applies another function on each element until it results in an {:ok, a} and stops at that point.

Example:
find the first format that matches a particular date. The "first matching" function iterates over the list of formats, applies Timex.parse and returns the first result matching an :ok pattern

Naive-ish implementation using the existing choose:

def first_matching(list, f) do
  Enum.reduce(list, {:error, nil}, fn el, acc ->
     Either.map(acc, fn _ -> Either.choose(el, f, & &1) end)
  end)
end

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions