From 905f4fb7085adf5c577f68830c6fd44a93e31f27 Mon Sep 17 00:00:00 2001 From: kballou Date: Fri, 18 Mar 2016 23:20:51 -0600 Subject: Update slides * Add more justification for functional programming * Add some more examples toward the end --- src/code/2/pingpong.exs | 26 ++++++++++++++++ src/code/2/pmap.exs | 9 ++++++ src/func-w-elixir.tex | 81 +++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 src/code/2/pingpong.exs create mode 100644 src/code/2/pmap.exs diff --git a/src/code/2/pingpong.exs b/src/code/2/pingpong.exs new file mode 100644 index 0000000..9d05b38 --- /dev/null +++ b/src/code/2/pingpong.exs @@ -0,0 +1,26 @@ +defmodule PingPong do + def start_link do + {:ok, spawn_link(fn -> loop() end)} + end + + defp loop do + receive do + {:ping, sender} -> + IO.puts "[PingPong]: Received Ping" + IO.puts "[PingPong]: Sending Pong..." + send sender, {:pong, self} + end + loop + end +end + +{:ok, pid} = PingPong.start_link +IO.puts "Sending Ping..." +send pid, {:ping, self} + +receive do + {:pong, ^pid} -> + IO.puts "Received Pong" + after 5000 -> + IO.puts "Never Received Pong" +end diff --git a/src/code/2/pmap.exs b/src/code/2/pmap.exs new file mode 100644 index 0000000..faf6c82 --- /dev/null +++ b/src/code/2/pmap.exs @@ -0,0 +1,9 @@ +defmodule MyMap do + def pmap(collection, f) do + collection |> + Enum.map(&(Task.async(fn -> f.(&1) end))) |> + Enum.map(&Task.await/1) + end +end + +MyMap.pmap(1..10_000, &(&1 * &1)) |> IO.inspect diff --git a/src/func-w-elixir.tex b/src/func-w-elixir.tex index e10934c..7d011f1 100644 --- a/src/func-w-elixir.tex +++ b/src/func-w-elixir.tex @@ -1,3 +1,4 @@ + \documentclass[english]{beamer} \usepackage{babel} \usepackage[utf8]{inputenc} @@ -121,24 +122,32 @@ XKCD on Functional Programming\cite{website:xkcd_functional} \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} +\item<7->{Functions compose} \end{itemize} \end{frame} \begin{frame} \frametitle{What is Elixir? Erlang/OTP?} \begin{itemize} -\item{Elixir is a new functional, dynamically typed language} -\item{Elixir's design is heavily influenced by Erlang's design} -\item{Erlang is an old functional, dynamically typed language, first appeared -in 1986} -\item{Built around concurrency, fault-tolerance, and high-availability} -\item{Elixir compiles to BEAM (Erlang) bytecode} -\item{Elixir ``looks'' like Ruby} %but don't even try to write ruby... +\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 Basics} +\subsection{Syntax Crash Course} \begin{frame}[fragile] \frametitle{Interactive Elixir} \framesubtitle{\texttt{iex}} @@ -221,9 +230,11 @@ represented with varying number of bits for each component} \framesubtitle{Brief Introduction to IEEE-754} To convert from binary bits to a ``float'', we can use the following formula: $$ -{-1}^{\text{sign}} \cdot{} -\frac{1 + \text{mantissa}}{2^{52}} -\cdot{} 2^{\text{exponent} - \text{bias}} +\left({}{-1}^{\text{sign}}\right){} \cdot{} +\left({} +1 + \frac{\text{mantissa}}{2^{52}} +\right){} +\cdot{} \left({}2^{\text{exponent} - \text{bias}}\right){} $$ \end{frame} @@ -238,12 +249,15 @@ $$ \frametitle{The Functional Approach} \framesubtitle{To all the things!} \begin{itemize} -\item{Less iteration} %sorta -\item{More (Tail) Recursion} %yay! -\item{Performance} +\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} Because no functional introduction is complete without it. @@ -270,6 +284,7 @@ F_1 &= 1 \lstinputlisting[language=bash]{code/2/fib_perf.out} \end{frame} +\subsubsection{Quicksort} \begin{frame} \frametitle{Quicksort} \begin{itemize} @@ -282,9 +297,14 @@ F_1 &= 1 \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} @@ -376,6 +396,41 @@ F_1 &= 1 \lstinputlisting{code/2/wc.out} \end{frame} +\subsubsection{Parallel Map} +\begin{frame} +\frametitle{Parallel Map} +\begin{itemize} +\item{Spawn a process for each element in the list} +\item{Evalutate 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} + +\section*{Going Forward} +\subsection*{Resources} +\begin{frame} +\frametitle{Elixir 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} -- cgit v1.2.1