Stratus3D

Software Engineering, Web Development and 3D Design

Foray Into Functional Programming With Elixir

  • Abstract: Functional programming languages provide many advantages over traditional OO languages. Functional languages make data manipulation (e.g. parsing binary data) easier. Their lack of mutable state (all data is immutable) also makes functional applications simpler than their OO counterparts. Learning how to solve problems and write software in functional languages like Elixir can be difficult at first. Writing real-world applications in functional languages requires a different approach to problem solving. In this talk I cover core concepts specific to functional programming required to start writing simple Elixir applications quickly. I will also share some things I have discovered over the past year that have helped me make the transition from OO to functional programming. Focusing on data and data manipulations is key to being productive in a functional language.
  • Slides: https://speakerdeck.com/stratus3d/foray-into-functional-programming-with-elixir

Everything You Need to Know About Erlang's Magic _ Variable

I had someone ask me what happened to values assigned to the underscore variable in Erlang. I realized I didn’t have a clear understand of the way variables beginning with an underscore handled. I decided to re-read the Erlang documentation on variables.

To summarizing what the documentation says, all variables could be put into three groups:

  • Normal variables
  • Normal variables prefixed with with an underscore
  • The anonymous variable (a single underscore)

###Erlang Variables All types of variables must start with either an uppercase letter or an underscore. They may contain alphanumeric characters, the underscore and the @ symbol. All variables are bound via Pattern Matching. At runtime no variables are special except for the anonymous variable.

##Normal variables Normal variables start with an uppercase letter. Pattern matching and all the compile time warnings work normally. Example:

1> {First, Second} = {foo, bar}.
{foo, bar}
2> First.
foo
3> Second.
bar

##Normal variables prefixed with an underscore When a variable is prefixed with an underscore pattern matching and everything at runtime works like a normally. Values are still bound as they normally would and patterns such as this fail like they normally would. {_Val,_Val} = {foo,bar} fails because the first and second items in the tuple are different. The only difference is at compile time. Normally the compiler generates a warning when a value is bound to a variable and the variable is not later used. Often an important value is returned from a function call but the value is unused. By prefixing the variable with an underscore, the variable retains it’s useful name and does not generate any compile time warnings. Example:

1> {_First, _Second} = {foo, bar}.
{foo, bar}
2> _First.
foo
3> _Second.
bar

##The anonymous variable The anonymous variable is denoted by a single underscore (_). The anonymous variable is used when a variable is required but the value needs to be ignored. When a value is assigned to the anonymous variable the value is never bound to the variable. Since the value is never bound it can be used multiple times in a pattern and each time it is allowed to match a different value. Example:

1> {_, _} = {foo, bar}.
{foo, bar}
2> _.
* 1: variable '_' is unbound 
4> {_, Second} = {foo, bar}.
{foo, bar}
5> _.
* 1: variable '_' is unbound 
6> Second.
bar

For more information check out the Erlang documentation on variables

Install Erlang 16 on Mac OSX 10.9 With Kerl

I recently was working on a project that required me to connect to an ODBC database. I hadn’t ever worked with ODBC before, but I saw Erlang had an application for interacting with ODBC databases. I fired up the Erlang console and typed odbc:module_info(). but the function was undefined. After double checking everything and trying to start the odbc application I realized that the odbc module wasn’t part of my Erlang installation. I tried reinstalling Erlang but that didn’t fix the problem. Turns out Erlang’s ODBC application relies on unixodbc, which isn’t included on OSX anymore. Here is how you get Erlang R16B03-1 on Mac OSX 10.9.5:

I use Kerl for building and installing Erlang versions. It’s a relatively simple tool that I have found to be very helpful. I am assuming you are using Kerl in the steps below. If you aren’t using Kerl it should still be possible to build Erlang with ODBC support on OSX, but you will need to alter these commands appropriately. I also assume you are using brew.

  1. Brew does all the hard work for us. Run $ brew install freetds. Make sure freetds was installed successfully and then try running $ tsql. If tsql exists it should print out some help information.
  2. We can use brew install unixodbc. Run $ brew install unixodbc. Make sure it installs successfully and then verify by running $ isql. isql should print out some help information.
  3. Install Erlang with Kerl. Before you build Erlang open up ~/.kerlrc (or create it if it doesn’t exist) and make sure it looks like this:

         KERL_CONFIGURE_OPTIONS="--enable-darwin-64bit --disable-hipe --enable-vm-probes --with-dynamic-trace=dtrace --disable-native-libs --enable-kernel-poll --enable-threads --enable-smp-support --with-wx"
    

    Also, one more thing before running the build command - make sure $GREP_OPTIONS is set to ''. On my system I had customized it and it was causing the build to crash:

         GREP_OPTIONS=''
    

    Finally, you should be able to run the build command. Here I chose erlang_16 as the build name:

         kerl build R16B031 erlang_16
    
  4. If all goes well you should be able to install the build by running kerl install erlang_16 <dir> where <dir> is the directory you want to install the build in. You should now have an installation that includes the odbc application!

If this quick post wasn’t able to help you here are some other resources I found that helped me fix this issue: