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.
Hey fellow alchemist, how’s going? Long time no see. I’m back! After loooong hiatus. I kinda missed it.
Ok, back to the topic. What will we build today? Well, I have more than one device in my house that I would like to play some movies or musics. And I hate copying stuff from one device to another. And I believe you have one of you have similar problem, or not, whatever. So today we will build some simple streaming media application using Phoenix Web Framework from Elixir language. We will build a web app that list all our media directory so then we can play literally anywhere. As long as you have modern browser and connected to the network.
Now let’s write our plRead
As software developers, we tend to depend on command line, especially me. Command line interface (CLI) are on fire this current time. Python, Ruby, Erlang and Elixir provide us with awesome tools on command line.
So in this article we will attempt to creating a command line tools. And I have feeling that Elixir will do great in this area.
Let’s start with a new project using mix.
$> mix new awesome_cli $> cd awesome_cli
lib/awesome_cli.ex and you’ll see something like this:
defmodule AwesomeCli do use Application.Behaviour # See http://elixir-lang.org/docs/stable/elixir/Application.html # for more information on OTP Applications def start(_type, _args) do AwesomeCli.Supervisor.start_link end end
Let's do me a favor to create hello world message in the project, will you?!
defmodule AwesomeCli do use Application.Behaviour # See http://elixir
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 alsoRead
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:
This is good practice for us to learn the very basic of functional programming through Elixir. So let's start!
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
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:
Let's do it!
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, justRead
This week we continue our Phoenix series. In case you missed previous three part of this series, here you go:
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:
This will be one hell of the naive approach for login mechanism. But it's ok for now, we will improve and iterateRead
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:
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.
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
Let's say we have two strings that we wanRead
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:
as: :pages, so you can do
resourcesmacro for conventional REST endpoints and conventionally named route helpers.
PhoenixJobs.Jobsto keep the controller actions tidy.
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.
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!
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
web/controller/page_controller.ex and add a new function to handle our
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
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 whRead