Search
 
SCRIPT & CODE EXAMPLE
 

ELIXIR

elixir with syntax

def call(conn, _options) do
  with user_id when not is_nil(user_id) <- get_session(conn, :user_id),
       user when not is_nil(user) <- Repo.get(User, user_id)
  do
    assign(conn, :current_user, user)
  else
    _ ->
      conn
      |> Controller.put_flash(:error, "You have to sign in to access this page.")
      |> Controller.redirect(to: "/sign_in_links/new")
      |> halt
  end
end
Comment

elixir with

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 concatenate list 
Scala :: scala schemaPayload json 
Scala :: scala enum 
Scala :: scala string to boolean 
Scala :: How to make immutable variable in scala 
Scala :: dataframe column json parser spark scala 
Scala :: How to have scalable images using image view xamarin XML 
Scala :: scala get set of chars from string 
Actionscript :: DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead. 
Excel :: google query select first 10 
Excel :: google sheets count dates that fall within date range 
Excel :: and in excel 
Perl :: perl do while loop 
Perl :: fonction perl 
Pascal :: pascal halt program until any button is pressed 
Pascal :: print pascal triangle 
Powershell :: how to download git on windows using power shell 
Gdscript :: GAScript - Google Sheets - QBO API - Consent Dialogue Box 
Abap :: the interface between the abap dictionary and the underlying database management system 
Assembly :: x86 assembly hello world 
Assembly :: Z language latex 
Assembly :: flutter button border radius 
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
6+5 =