\documentclass[english]{beamer} \usepackage{babel} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage{amsmath} \usepackage{unicode-math} \usepackage{fontspec} \usepackage{xunicode} \usepackage{xltxtra} \usepackage{xeCJK} \usepackage{booktabs} \usepackage{lmodern} \usepackage{listings} \usepackage{color} \usepackage{tikz} \usetikzlibrary{trees, shapes.misc, arrows} \usepackage{pgfkeys} \usepackage{graphicx} \setmainfont{DejaVuSans} \setCJKmainfont{HanaMinA} \setCJKsansfont{HanaMinA} \setCJKmonofont{HanaMinA} \usetheme{Berlin} \usecolortheme[light,accent=blue]{solarized} \graphicspath{{./images/}} \setbeamertemplate{headline}{} \lstset{% basicstyle=\footnotesize\ttfamily, breakatwhitespace=false, breaklines=true, captionpos=b, frame=signle, keepspaces=true, columns=flexible, language=Ruby, numbers=none, numbersep=5pt, numberstyle=\tiny\color{solarizedBase00}, showspaces=false, showstringspaces=false, stepnumber=1, showtabs=false, stringstyle=\color{solarizedMagenta}, keywordstyle=\color{solarizedCyan}, commentstyle=\color{solarizedGreen}, tabsize=2, extendedchars=true } \title{Learning Functional Programming with Elixir\texttt{|>}} \subtitle{A Short Guide Through Functional Programming} \author[Ballou]{Kenny Ballou} \institute[zData]{% \inst{}% zData, Inc. } \AtBeginSection[]{% \begin{frame} \tableofcontents[ currentsection, sectionstyle=show/shaded, subsectionstyle=show/show/hide, subsubsectionstyle=show/show/hide] \end{frame} } \begin{document} % TikZ Styles \tikzstyle{every node}=[% fill=solarizedBase02, draw=solarizedBase01, thick, rounded corners, anchor=north, sibling distance=6cm] \tikzstyle{edge from parent}=[% solarizedBase00, -o, thick, draw] %\tikzstyle{edge from parent path}=[% % \tikzparentnode.east |- \tikzchildnode.south] \begin{frame}[label=titleslide] \titlepage{} \end{frame} \begin{frame} \tableofcontents[subsectionstyle=hide,subsubsectionstyle=hide] \end{frame} \begin{frame} \frametitle{Who am I?} \begin{itemize} \item{Hacker} \item{Developer (read gardener)} \item{Mathematician} \item{Student} \end{itemize} \end{frame} \section{Introduction} \begin{frame} \begin{figure} \includegraphics[scale=0.45]{xkcd_functional.png} \caption{``Functional programming combines the flexibility and power of abstract mathematics with the intuitive clarity of abstract mathematics.''} \end{figure} XKCD on Functional Programming\cite{website:xkcd_functional} \end{frame} \begin{frame} \frametitle{Motivation} \framesubtitle{Why Functional Programming?} \begin{itemize} \item<1->{Easy to Reason About} \item<2->{Trivial to Test} \item<3->{Functional Composition} \item<4->{State or Side-Effects are explicit} \end{itemize} \end{frame} \begin{frame} \frametitle{Anti-Motivation} \framesubtitle{Why not Functional Programming?} \begin{itemize} \item<1->{Explicit state can be hard} \item<2->{Side-effect free programming seems cumbersome} \item<3->{Performance} \item<4->{Learning Curve} \end{itemize} \end{frame} \begin{frame} \frametitle{What is Functional Programming?} \begin{itemize} \item<1->{Functional programming is a paradigm} %you might say pattern \item<2->{Prefers ``mathematical'' functions} \item<3->{Uses (mostly) immutable data} %depending on "purity" \item<4->{Everything is an expression} \item<5->{Functions are 1\textsuperscript{st} class} \begin{itemize} \item<6->{This gives higher-order functions} \end{itemize} \end{itemize} \end{frame} \begin{frame} \frametitle{What is Elixir? Erlang/OTP?} \begin{itemize} \item<1->{Elixir is a new functional, dynamically typed language} \item<2->{Elixir's design is heavily influenced by Erlang's design} \begin{itemize} \item<3->{Erlang is also a functional, dynamically typed language, first appeared in 1986} \item<4->{Built around concurrency, fault-tolerance, and high-availability} \end{itemize} \item<5->{Elixir compiles to BEAM (Erlang) bytecode} \item<6->{Elixir ``looks'' like Ruby} %but don't even try to write ruby... \end{itemize} \end{frame} \section{Elixir\texttt{|>} Basics} \subsection{Syntax Crash Course} \begin{frame}[fragile] \frametitle{Interactive Elixir} \framesubtitle{\texttt{iex}} \lstinputlisting{code/1/iex} \end{frame} \begin{frame}[fragile] \frametitle{Hello, World} \lstinputlisting[lastline=2]{code/1/helloworld} \end{frame} \begin{frame}[fragile] \frametitle{Hello, World} \lstinputlisting[firstline=4]{code/1/helloworld} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Essentials} \lstinputlisting[lastline=18]{code/1/essentials} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Essentials} \lstinputlisting[firstline=20,lastline=28]{code/1/essentials} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Essentials} \lstinputlisting[firstline=30]{code/1/essentials} \end{frame} \subsection{General Concepts} \begin{frame} \frametitle{Dispelling Assignment} \framesubtitle{There is no spoon} \begin{itemize} \item{\texttt{=} does \textbf{not} mean \textit{assign}} \begin{itemize} \item{\texttt{x = 1} is not \textit{assign 1 to \texttt{x}}} \end{itemize} \item{\texttt{=} is a \textit{match} operator} \begin{itemize} \item{\texttt{=} is a constraint-solving operator} \end{itemize} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Pattern Matching} \lstinputlisting[lastline=8]{code/1/patterns} \end{frame} \begin{frame}[fragile] \frametitle{Pattern Matching} \lstinputlisting[firstline=10,lastline=28]{code/1/patterns} \end{frame} \begin{frame}[fragile] \frametitle{Pattern Matching} \lstinputlisting[firstline=30,lastline=33]{code/1/patterns} \end{frame} \begin{frame}[fragile] \frametitle{Pattern Matching} \lstinputlisting[firstline=35,lastline=41]{code/1/patterns} \end{frame} \begin{frame} \frametitle{Pattern Matching} \framesubtitle{Brief Introduction to IEEE-754} \begin{itemize} \item{64-bit floating point (doubles) numbers are represented using IEEE-754} \item{32-bit (single/\texttt{float}) and 128-bit (quadurpals) are similarly represented with varying number of bits for each component} \item{There are four main components} \begin{itemize} \item{sign, ±, 1 bit} \item{exponent, 11 bits} \item{fraction (mantissa), 52 bits} \item{bias, built-in, typically $1023$ for doubles} \end{itemize} \end{itemize} \end{frame} \begin{frame} \frametitle{Pattern Matching} \framesubtitle{Brief Introduction to IEEE-754} To convert from binary bits to a ``float'', we can use the following formula: $$ \left({}{-1}^{\text{sign}}\right){} \cdot{} \left({} 1 + \frac{\text{mantissa}}{2^{52}} \right){} \cdot{} \left({}2^{\text{exponent} - \text{bias}}\right){} $$ \end{frame} \begin{frame}[fragile] \frametitle{Pattern Matching} \lstinputlisting[firstline=43]{code/1/patterns} \end{frame} \section{Functional Approach} \begin{frame} \frametitle{The Functional Approach} \framesubtitle{To all the things!} \begin{itemize} \item<1->{Less iteration} %sorta \item<2->{More (Tail) Recursion} %yay! \item<3->{Must Relearn Patterns} \item<4->{Performance} \end{itemize} \end{frame} \subsection{Example Problems} \subsubsection{Fibonacci} \begin{frame} \frametitle{Fibonacci} \begin{align*} F_n &= F_{n-1} + F_{n-2} \\ F_0 &= 0 \\ F_1 &= 1 \end{align*} \end{frame} \begin{frame}[fragile] \frametitle{Fibonacci} \lstinputlisting[numbers=left]{code/2/fib_rec.exs} \end{frame} \begin{frame}[fragile] \frametitle{Fibonacci} \framesubtitle{Iteratively} \lstinputlisting[numbers=left]{code/2/fib_itr.exs} \end{frame} \begin{frame}[fragile] \frametitle{Fibonacci Performance} \lstinputlisting[language=bash]{code/2/fib_perf.out} \end{frame} \subsubsection{Quicksort} \begin{frame} \frametitle{Quicksort} \begin{itemize} \item{Similar to merge sort} \item{Sort by partitioning} \item{Has a nice recursive definition} %more yay! \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Quicksort} \lstinputlisting[numbers=left]{code/2/qs.exs} \end{frame} \begin{frame}[fragile] \frametitle{Quicksort} \lstinputlisting{code/2/qs.out} \end{frame} \subsubsection{Map-Reduce} \begin{frame} \frametitle{Map-Reduce} \begin{itemize} \item<1->{Functional way to process collections} \item<2->{Can be (partially) pipelined} \item<3->{Mapping can be lazy} \item<4->{Map is Reduce} %with a widening accumulator \item<5->{They are ``folds''} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Map-Reduce} \framesubtitle{Implementing our own Map} \lstinputlisting[numbers=left]{code/2/my_map.exs} \lstinputlisting{code/2/my_map.out} \end{frame} \begin{frame}[fragile] \frametitle{Map-Reduce} \framesubtitle{Implementing our own (simple) Reduce} \lstinputlisting[numbers=left]{code/2/my_red.exs} \lstinputlisting{code/2/my_red.out} \end{frame} \begin{frame}[fragile] \frametitle{Map-Reduce} \framesubtitle{Map-Redux} \lstinputlisting[numbers=left]{code/2/my_map_red.exs} \end{frame} \begin{frame}[fragile] \frametitle{Map-Reduce} \framesubtitle{Map-Redux} \lstinputlisting{code/2/my_map_red.out} \end{frame} \begin{frame} \frametitle{Word Counting} \begin{itemize} \item{Stream lines out of a file} \item{Tokenize into words} \item{Reduce words and print results} \end{itemize} \end{frame} \begin{frame} \frametitle{Word Counting} \begin{figure} \begin{tikzpicture} \node (stream) at (0, 0) {Stream}; \node (tokenize) at (2, 0) {Tokenize}; \node (reduce) at (4, 0) {Reduce}; \node (print) at (4, -2) {Print}; \draw[-o,thick] (stream) -- (tokenize); \draw[-o,thick] (tokenize) -- (reduce); \draw[-o,thick] (reduce) -- (print); \end{tikzpicture} \end{figure} \end{frame} \begin{frame}[fragile] \frametitle{Word Counting} \framesubtitle{Streaming Data} \lstinputlisting[numbers=left,firstline=3,lastline=5]{code/2/wc.exs} \end{frame} \begin{frame}[fragile] \frametitle{Word Counting} \framesubtitle{Tokenizing Words} \lstinputlisting[numbers=left,firstline=7,lastline=9]{code/2/wc.exs} \end{frame} \begin{frame}[fragile] \frametitle{Word Counting} \framesubtitle{Reducing Words} \lstinputlisting[numbers=left,firstline=11,lastline=17]{code/2/wc.exs} \end{frame} \begin{frame}[fragile] \frametitle{Word Counting} \framesubtitle{Counting Words} \lstinputlisting[numbers=left,firstline=19,lastline=24]{code/2/wc.exs} \end{frame} \begin{frame}[fragile] \frametitle{Word Counting} \framesubtitle{Results} \lstinputlisting{code/2/wc.out} \end{frame} \begin{frame}[fragile] \frametitle{Improve Word Counting} \lstinputlisting[numbers=left,firstline=19,lastline=25]{code/2/wc_2.exs} \end{frame} \subsubsection{Parallel Map} \begin{frame} \frametitle{Parallel Map} \begin{itemize} \item{Spawn a process for each element in the list} \item{Evaluate the provided function for the element} \item{Gather and Return results} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{Parallel Map} \lstinputlisting{code/2/pmap.exs} \end{frame} \begin{frame}[fragile] \frametitle{Parallel Map} \lstinputlisting{code/2/pmap.out} \end{frame} \subsubsection{Echo Server} \begin{frame} \frametitle{Echo Server} \begin{itemize} \item<2->{Handle connections from clients (\texttt{netcat})} \item<3->{Echo back contents of clients messages} \end{itemize} \end{frame} \begin{frame} \frametitle{\texttt{mix}: Ladle of Elixir\texttt{|>}} \begin{itemize} \item<1->{Creates Skeleton Projects} \item<2->{Compiles Code} \item<3->{Runs Test Suites} \item<4->{Creates Releases} \item<5->{Anything you want} \end{itemize} \end{frame} \begin{frame}[fragile] \frametitle{\texttt{mix} Output} \lstinputlisting{code/2/mix_echo.out} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Applications} \lstinputlisting[% numbers=left, title=mix.exs, firstline=13, lastline=16]{code/2/echo_server/mix.exs} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Applications} \lstinputlisting[% numbers=left, title=lib/echo\_server.ex]{code/2/echo_server/lib/echo_server.ex} \end{frame} \begin{frame}[fragile] \frametitle{Elixir\texttt{|>} Supervisors} \lstinputlisting[% numbers=left, title=lib/echo\_server/supervisor.ex] {code/2/echo_server/lib/echo_server/supervisor.ex} \end{frame} \begin{frame}[fragile] \frametitle{Echo Server: Echo} \lstinputlisting[% numbers=left, title=lib/echo\_server/echo.ex, firstline=3, lastline=16] {code/2/echo_server/lib/echo_server/echo.ex} \end{frame} \begin{frame}[fragile] \frametitle{Echo Server: Echo} \lstinputlisting[% numbers=left, title=lib/echo\_server/echo.ex, firstline=18, lastline=34] {code/2/echo_server/lib/echo_server/echo.ex} \end{frame} \begin{frame}[fragile] \frametitle{Echo Server} \begin{columns}[c] \column{0.5\textwidth{}} \lstinputlisting{code/2/echo_server_iex.out} \column{0.5\textwidth{}} \lstinputlisting[stringstyle=\footnotesize\ttfamily]{code/2/echo_server_nc.out} \end{columns} \end{frame} \section*{Going Forward} \subsection*{Resources} \begin{frame} \frametitle{Elixir\texttt{|>} Resources} \begin{itemize} \item<2->{\url{http://elixir-lang.org/}} \begin{itemize} \item<2->{\url{http://elixir-lang.org/getting-started/introduction.html}} \end{itemize} \item<3->{Learn X in Y minutes~\cite{website:learnxiny}} \item<4->{Programming Elixir~\cite{book:programming_elixir}} \item<4->{Metaprogramming Elixir~\cite{book:metaprogramming_elixir}} \end{itemize} \end{frame} \section*{References} \begin{frame}[allowframebreaks] \frametitle{References} \nocite{*} \renewcommand{\refname}{} \bibliographystyle{plain} \bibliography{references} \end{frame} \againframe{titleslide} \end{document}