• RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

sudo apt-get install erlang-xmerl can solve following exception while starting phoenix.server.

=INFO REPORT==== 18-Jan-2016::15:45:50 ===
    application: logger
    exited: stopped
    type: temporary
** (Mix) Could not start application xmerl: could not find application file: xmerl.app

I started getting this exception right after I added :floki in applications: under mix.exs.

Continue

Creating command line programs in Elixir

Note: This is a very basic example of mix tool and escript utility. One can skip this post if already have an idea about these tools.

Lets write a simple command line program which would take a String as a command line argument and display the number of vowels in it. We’ll use Elixir’s mix build tool and OptionParser to parse command line arguments.

Lets start by setting up the application:

$ mix new vowel_counter

Open mix.exs, you will see something like:


defmodule VowelCounter.Mixfile do
  use Mix.Project

  def project do
    [app: :vowel_counter,
     version: "0.0.1",
     elixir: "~> 1.1",
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    []
  end
end

We’ll be using Erlang’s escript utility to precompile our app and package our application’s code along with its dependencies(no dependencies in this example app) in one single executable. So lets add escript: escript_config to project method in our mix.exs to define our escript configuration, also define a private method escript_config to specify the the main_module. In this case, after these changes our mix.exs would look like:

defmodule VowelCounter.Mixfile do
  use Mix.Project

  def project do
    [app: :vowel_counter,
     version: "0.0.1",
     elixir: "~> 1.1",
     escript: escript_config, 
     build_embedded: Mix.env == :prod,
     start_permanent: Mix.env == :prod,
     deps: deps]
  end

  # Configuration for the OTP application
  #
  # Type "mix help compile.app" for more information
  def application do
    [applications: [:logger]]
  end

  # Dependencies can be Hex packages:
  #
  #   {:mydep, "~> 0.3.0"}
  #
  # Or git/path repositories:
  #
  #   {:mydep, git: "https://github.com/elixir-lang/mydep.git", tag: "0.1.0"}
  #
  # Type "mix help deps" for more examples and options
  defp deps do
    []
  end

  defp escript_config do
    [main_module: VowelCounter]
  end
end

Lets compile and run our application by running:
$ mix escript.build
$ ./vowel_counter "my test string"

Since we have specified VovelCounter module as our main_module in elixir_config, our app expects and call the main method from this module and pass command line arguments to it. We haven’t defined this main method yet, so we should see something similar when we execute our app:

** (UndefinedFunctionError) undefined function: VowelCounter.main/1
    (vowel_counter) VowelCounter.main(["my test string"])
    (elixir) lib/kernel/cli.ex:76: anonymous fn/3 in Kernel.CLI.exec_fun/2

Lets open lib/vowel_counter.ex and define main method as:

defmodule VowelCounter do
  # Take command line arguments
  # Then parse them
  # Then count number of vowel
  # Print the outpu
  def main(args) do
    args 
      |> parse_args
      |> count_vowel
      |> IO.puts
  end

  defp parse_args(args) do
    # Covering only expected arguments. We can add code to handle cases when unexpected arguments are passed or help text etc.
    # Checkout OptionParser lib for details. 
    {_, [str], _} = OptionParser.parse(args)
    str
  end

  defp count_vowel(str) do
    # Scan for vowels and take the list's length
    len = length(Regex.scan(~r/[aeiou]/i, str))
    "There are #{len} vowels"
  end
end

Lets re-compile and re-run our application by:
$ mix escript.build
$ ./vowel_counter "This string has 5 vowels."
We should see: “There are 5 vowels”

Your comments/suggestions are most welcome.

Continue

My first version of Ping-Pong in Elixir

Recently I have started spending some of my time with Elixir and following Dave Thomas’ Programming Elixir. Though I am half way through with the book, and still don’t have a very good hold on the language. After reading about processes in Elixir, I thought to write something my own so I wrote a small ping-pong program


defmodule PingPong do
  import :timer
  @timer  500
  def ping(x) do
    receive do
      {pong_pid, n} when n <= x ->  IO.puts ("Ping #{n}")
                                        send pong_pid, { self, (n) }
                                        sleep @timer
                                        ping(x)
    end
  end

  def pong(x) do
    receive do
      {ping_pid, n} when n <= x -> IO.puts ("Pong #{n}")
                                        send ping_pid, {self, (n + 1) }
                                        pong(x)
    end
  end

  def run(n) do
    {ping_pid, _} = spawn_monitor(PingPong, :ping, [n])
    {pong_pid, _} = spawn_monitor(PingPong, :pong, [n])
    send ping_pid, {pong_pid, 1}

    receive do
      msg -> IO.puts "Message received: #{msg}"
    end
  end
end

PingPong.run(30)

I am pretty sure that there would be much scope of improvement, but this is what I could write as of now. Your comments/suggestions are welcome, kindly add them in comments

Continue

Could not start application xmerl: ...

sudo apt-get install erlang-xmerl can solve following exception while starting ...

Creating command line programs in E...

Note: This is a very basic example of mix tool ...

My first version of Ping-Pong in El...

Recently I have started spending some of my time with ...

Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Asset Pipeline has been extracted a...

The asset pipeline which was introduced in Rails 3.1, has ...

ClientSideValidations: Add/Map mode...

https://twitter.com/bansalakhil/status/256699237230845952

Action and Page caching has been ex...

With the commit c82cf81f00f Action and Page caching has been extracted ...

turbo-sprockets-rails3: Speeds up ...

http://twitter.com/bansalakhil/status/253420357841723393

EdgeRails: ActiveRecord::SessionSto...

With the commit 3324e28804 ActiveRecord::SessionStore is extracted out of Rails into ...