ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - sviridov/anaphora-elixir: The anaphoric macro collection for Elixir
The anaphoric macro collection for Elixir. Contribute to sviridov/anaphora-elixir development by creating an account on GitHub.
Visit Site

GitHub - sviridov/anaphora-elixir: The anaphoric macro collection for Elixir

GitHub - sviridov/anaphora-elixir: The anaphoric macro collection for Elixir

Anaphora Build Status license hex.pm

Anaphora is the anaphoric macro collection for Elixir. An anaphoric macro is one that deliberately captures a variable (typically it) from forms supplied to the macro.

Getting Started

  • Add the Anaphora dependency to your mix.exs file:
def deps do
  [{:anaphora, "~> 0.1.2"}]
end
  • After you are done, run mix deps.get in your shell.

Provided API

alet

alet is basic anaphoric macro. It's not very useful in user code but other anaphoric macros are built on top of it. alet binds result of the expression to the it variable (via case) in the scope of the body:

defmodule User do
  use Anaphora

  ...
  def user_email(user_id) do
    alet fetch_user(user_id) do
      if it, do: it.email, else: raise "Failed to fetch user"
    end
  end
end

aif

Works like if, except that result of the condition is bound to it (via alet) for the scope of the then and else clauses:

defmodule User do
  use Anaphora

  ...
  def user_email(user_id) do
    aif fetch_user(user_id), do: it.email, else: raise "Failed to fetch user"
  end
end

acond

Works like cond, except that result of each condition is bound to it (via alet) for the scope of the corresponding body:

defmodule Notification do
  use Anaphora

  ...
  def send_notification(user, message) do
    acond do
      user.email -> send_notification_by_email(it, message)
      user.phone -> send_notification_by_sms(it, message)
      :else -> raise "Unable to send notification"
    end
  end
end

acase

Works like case, except that result of the key expression is bound to it (via alet) for the scope of the cases.

afn

Works like fn, except that anonymous function is bound to it (via blood magic) for the scope of the function body:

import Anaphora

in_order_tree_traversal = afn do
  {left, center, right}, callback ->
    it.(left, callback)
    callback.(center)
    it.(right, callback)
  nil, _ -> nil
end

in_order_tree_traversal.({{nil, 1, nil}, 2, {nil, 3, {nil, 4, nil}}}, &IO.puts/1)
# => 1, 2, 3, 4

warning: Invocation of it takes a lot of time. Don't use afn in performance critical code. It will be fixed after Elixir 1.0 release.

aand

Evaluates each clause one at a time and binds result to it. As soon as any clause evaluates to nil (or false), aand returns nil without evaluating the remaining clauses. If all clauses but the last evaluate to true values, aand returns the results produced by evaluating the last clause:

defmodule Notification do
  use Anaphora

  ...
  def send_email_to_user(user_id, message) do
    aand do
      fetch_user(user_id)
      it.email
      send_email(it, message)
    end || raise "Unable to send email"
  end
end

License

Anaphora source code is released under The MIT License. Check LICENSE file for more information.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory