ElixirDose

Journal of the overlooked tiny bits in young programming language. Will update weekly or two, as a way to build the habit of writing something on a regular basis. Authored by Riza and Augie as Editor In Chief.


Cowboy Tutorial Part 2: Creating Flat File Blog

If you follow ElixirDose blog, we did tried to create a very simple web framework over here. In that article we did serve some markdown file as the example. In this article, we take the markdown example a little bit further by creating a drop dead simple flat file blogging engine. Because the first article is little bit outdated, we will learn together how to get started with cowboy, a small, fast, and modular HTTP server written in Erlang.

Let's talk more detail about blogging engine that we will create. Our blogging engine will read through one folder that have several markdown files. No database whatsoever and it should be blazingly fast as a blog. The idea is to edit your content with your favorite text editor using markdown formatting, then you put on destination folder and viola! You've published your content. Quick and easy. We also

Read

Functional Programming Basic Features

A couple week ago I read this article about why you should take a look at functional programming. Well, when you read and follow this blog you already did. One part that interesting about that article is the basic features of FP, which is:

  1. First-class functions
  2. Higher-order functions
  3. Closure
  4. Immutable state

This is good practice for us to learn the very basic of functional programming through Elixir. So let's start!

First-Class Functions

The basic definition of first-class functions is simply that you can store functions into a variable. You can also passes the functions around and invoked from other functions. This also known as anonymous functions in Elixir to be specific.

Let’s see the example.

add = fn num1, num2 ->
  num1 + num2
end

substract = fn num1, num2 ->
  num1 - num2
end
Read

Phoenix Flying High: Deploying Phoenix The Final Part

Introduction

If you follow along with Phoenix series in this blog, I assume you have awesome web app ready spread to the world and start to get some traffic and profit :)

We're using Phoenix version 0.4.1, postgrex version 0.6.0, ecto version 0.2.5 and Elixir version 1.0.0.

If you want to follow along, you can clone a repo from github or you always can simply use phoenix new new_project for this purposes.

Are you ready? This is how we will do:

  1. Add exrm into our project,
  2. Generate a release,
  3. Preparing the production server,
  4. Deploying our app into production server,
  5. Expose the app to the world.

Let's do it!

What Is exrm?

exrm is Elixir Release Manager. Well, that's explain everything. exrm sole mission is to help us release our Elixir applications.

How to use it? Well, that's what we will try to answer like, right now. Pretty easy, actually, just

Read

Phoenix Part 4

Introduction

This week we continue our Phoenix series. In case you missed previous three part of this series, here you go:

  1. Let's Build Web App With Phoenix And Ecto
  2. Phoenix, Ecto And Jobs Portal Project Part 2
  3. Phoenix, Ecto And Jobs Portal Project Part 3

Now we go further. We will add user registration and login. So if you want to post a new job, you need to login first. And if you don't have an account, you have to register first. You know the drill, I'm sure.

And this is the plan to tackle the issues:

  1. Add Register User Form
  2. Add Insert New User To Database
  3. Add Login Form And Query
  4. Using Session For Login Mekanism
  5. Using Phoenix Flash To Messaging User

This will be one hell of the naive approach for login mechanism. But it's ok for now, we will improve and iterate

Read

Introduction

At the end of this article, we'll be able to create a simple guessing game using Elixir. To accommodate that, we need to learn the following:

Getting User Input

It's simple enough to use IO.gets like this:

iex> IO.gets "Please enter your age "

It will return the user's input as a string. You could save it into a variable, if you wished.

Data Type Conversion

In Elixir, we can convert certain data types into other data types. For example, we can convert string/binary into integer or float, atom to list, float to list, and much more. Check the documentation here to get the full list of data type conversions. The usage is simple:

iex> binary_to_integer "22"
22
iex> binary_to_float "22.2"
22.2

String Concatenation and Interpolation

Let's say we have two strings that we wan

Read

Phoenix Part 3

It's feels great when we use one framework and its owner gives us suggestions directly. That's what I got from the last article about Phoenix. Chris McCord gave us three valuable suggestions to improve our Phoenix Job Portal. In this article, we will refactor the code a bit based on his valuable suggestion:

  1. Change the "index" route helper to as: :pages, so you can do pages_path(:index) instead of index_path(:index).
  2. The Router should be updated to use the resources macro for conventional REST endpoints and conventionally named route helpers.
  3. Alias PhoenixJobs.Queriers and PhoenixJobs.Jobs to keep the controller actions tidy.

Change Index Route Helper

This change is a minor one and easy to implement. It's just a naming convention. We want to do pages_path(:index) instead of index_path(:index). To do that, we need to change as: :index to as: pages inside our route file:

get "/", PhoenixJobsThree.
Read

Phoenix Ecto Part 2

Let's continue building our Elixir Job Portal this week. In the last article, we did following:

This week we will add a job creation feature to our app. Let's get the ball rolling!

Create Job

First, let's add a new route for creating a job by opening up web/router.ex and adding a new route for a new job.

defmodule PhoenixJobs.Router do
  use Phoenix.Router

  get "/", PhoenixJobs.PageController, :index, as: :page
  get "/new", PhoenixJobs.PageController, :new
end

Then open web/controller/page_controller.ex and add a new function to handle our :new route.

defmodule PhoenixJobs.PageController do
  use Phoenix.Controller

  def index(conn, _params) do
    jobs = PhoenixJobs.Queries.jobs_query
    render conn, "index", jobs: jobs
  end

  def new(conn, _params) do
    render conn, "new"
  end
end

After

Read

Let's Build Web App With Phoenix and Ecto

Phoenix is the Elixir web framework targeting full-featured, fault tolerant applications with realtime functionality. Phoenix focuses on making developers highly productive without sacrificing performance. Phoenix is created by Chris McCord, available on github, and has reached version 0.3.1.

If you want to know more about Phoenix, I suggest you watch McCord's talk at ElixirConf 2014 about the past, present, and future of Phoenix here.

For this article, we will focus on how to create a web application using Phoenix, with Ecto as the persistence layer. We will build a simple job board that a user can browse for Elixir jobs and submit Elixir job opportunities.

Ok, let's get started by installing Phoenix. We will use the most recent version of Phoenix wh

Read

Let’s Mix Your Elixir

Even though Elixir is still young as a programming language, not 1.0 version yet (v0.12.5 at the moment this blog written), but it ships with great applications to create and deploying your projects. IEx, we already cover before. Today we will talk about mix.

According to Elixir Website mix is a build tool that provides tasks for creating, compiling, testing and soon releasing Elixir projects. Mix is inspired by the Leiningen build tool for Clojure and was written by one of it contributors.

Today we will learn together how to create projects using mix and install dependencies for your projects.

Create Project

To create a project, simply use mix new command followed by your project name:

$> mix new dream_project

Mix automatically create a directory named dream_project with some files in it ready to start your engine and build your dream project.

Read

Migrating to Maps

It's not that big a stretch to go from a record to a map. I rewrote one of my Elixir projects to accommodate the new data type and discovered it was easy.

Not surprisingly -- to those of you who've been reading this site for any time now -- I used my Deck module as practice. It simulates a deck of cards where each card is a record. For this exercise, I'll change those records to maps and see if there's any interactions that will need to be adjusted, as well.

Redefining a Card

This is the original code to define and describe a single card:

    defrecord Card, suit: nil, rank: nil, points: nil do

      def describe({ :no_card }) do
        "No card exists"
      end

      def describe(record) do
          "#{record.rank} of #{record.suit} (#{record.points})"
      end

    end

This is not a module. This is a record with a couple of functions along the way for good measure. The first thing a map version of this code will need is a

Read