Search
 
SCRIPT & CODE EXAMPLE
 

ELIXIR

elixir with else

defmodule SomeModule do
  require Logger

  @doc """
      iex> SomeModule.do_something("Richard")
      {:ok, "Message sent!"}

      iex> SomeModule.do_something("Jian")
      {:error, "Invalid phone number"}

      iex> SomeModule.do_something("Erlich")
      {:error, "Not found"}
  """
  def do_something(name) do
    with {:ok, phone_number} <- look_up(name),
         :sent <- send_text(phone_number) do
      {:ok, "Message sent!"}
    end
  end

  def do_something_and_log_error(name) do
    with {:ok, phone_number} <- look_up(name),
         :sent <- send_text(phone_number) do
      {:ok, "Message sent!"}
    else
      {:error, error_msg} = error ->
        Logger.error(error_msg)
        error
    end
  end

  defp look_up("Richard"), do: {:ok, "+15629998888"}
  defp look_up("Jian"), do: {:ok, "+864009999999"}
  defp look_up(_), do: {:error, "Not found"}

  defp send_text("+1" <> _phone_number), do: :sent
  defp send_text(_), do: {:error, "Invalid phone number"}
end
Comment

PREVIOUS NEXT
Code Example
Scala :: scala hello world 
Scala :: scala random number 
Scala :: else if scala 
Scala :: scala empty list 
Scala :: How to declare constant variable in scala 
Scala :: how to make anonymous function in scala 
Scala :: Exception: sbt.TrapExitSecurityException thrown from the UncaughtExceptionHandler in thread "run-main-0" 
Scala :: scala list of options to option of list 
Actionscript :: Application insights powershell 
Excel :: google sheets select first n matches 
Excel :: excel conditionally highlight rows based on column 
Excel :: AND logic in excel formula 
Perl :: perl until loop 
Perl :: Perl (perl 5.28.1) sample 
Pascal :: pascal pause until key is pressed 
Pascal :: contoh dasar pascal 
Powershell :: How to test HDD health in PowerShell 
Gdscript :: GDScript typed variables 
Lisp :: lisp input 
Assembly :: MOD OPERATOR for register in arm assembly 
Assembly :: that long word from mary poppins 
Assembly :: links in markdown 
Javascript :: jquery vslidation remove spaces from input 
Javascript :: datatable disable searching 
Javascript :: js random hex color 
Javascript :: WebForms UnobtrusiveValidationMode requires a ScriptResourceMapping for jquery 
Javascript :: jquery slim min js url 
Javascript :: js element exists 
Javascript :: select2 in modal not work 
Javascript :: angular validators number only in reactive form 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =