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
Elixir :: elixir string interpolation 
Elixir :: elixir pick out elements in list 
Elixir :: elixir comparison 2 different data types 
Elixir :: elixir enum map_every 
Scala :: ValueError: If using all scalar values, you must pass an index 
Scala :: scala length of string 
Scala :: scala order list 
Scala :: scala jrtpath 
Scala :: scala learn 
Actionscript :: how to resize a base64 image 
Actionscript :: mount_osxfuse: /Users/em/mount_dev: Input/output error 
Excel :: excel formula not updating after inserting rows 
Excel :: excel and function 
Perl :: perl do while loop 
Perl :: perl mechanize check mirror response for errors 
Pascal :: pascal wait for key 
Pascal :: pascal special characters 
Powershell :: change increase windows scroll bar slider width 
Clojure :: Clojure defn 
Cobol :: python pygments install 
Assembly :: cmd to know the version of a programm 
Assembly :: what do you mean by io address and memory address 
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+2 =