


\documentclass[xetex,12pt,dvipsnames]{beamer}
\setbeamertemplate{navigation symbols}{}%remove navigation symbols
\usepackage{fancyvrb}
% \DefineShortVerb{∆}
\usepackage{multimedia}
\usepackage{fontspec}
\usefonttheme{professionalfonts}
% \setsansfont[Mapping=tex-text]{Source Code Pro Medium}
\setsansfont[Mapping=tex-text]{EB Garamond 12 Regular}
\newfontfamily\boldFont[Scale=0.8,Mapping=tex-text]{Myriad Pro Bold}
\newfontfamily\italicFont[Mapping=tex-text,Ligatures=Rare,Ligatures=Historical,Contextuals=Swash,Contextuals=Alternate,Style=Alternate]{EB Garamond 12 Italic}
% \setlength{\baselineskip}{0pt}
\setlength{\parskip}{0.5\baselineskip}
% \setlength{\parindent}{0pt}
\renewcommand{\frametitle}[1]{{\boldFont\large #1}}

\centering
\setbeamertemplate{frametitle}[default][center]
\setbeamercolor{frametitle}{fg=black}

\newfontfamily\ttFont[Scale=0.8]{Flow DejaVu Sans Mono}
\newfontfamily\verbatimFont[Scale=0.8]{Flow DejaVu Sans Mono}
\makeatletter
\def\verbatim@font{\verbatimFont}
\makeatother
\begin{document}
% ##########################################################################################################
% ##########################################################################################################



% \rm % ??

% \begin{frame}
% \frametitle{Foo bar}
% \framesubtitle{This is \textit{outrageous}.}

% \begin{enumerate}
% \item<1-| alert@1> Helo world.
% \item<2-| alert@2> program
% \item<3-| alert@3> flow
% \end{enumerate}

% hullo

% \end{frame}





% \begin{frame}
% \transboxin
% % \transdissolve
% % \transblindsvertical[duration=0.2]
% % \transwipe[duration=0.4,direction=0]
% \transglitter[duration=0.4]

% \frametitle{There Is No Largest Prime Number}
% \framesubtitle{The proof uses \textit{reductio ad absurdum}.}
% \begin{theorem}
% There is no largest prime number.
% \end{theorem}
% \begin{proof}
% \begin{enumerate}
% \item<1-| alert@1> Suppose $p$ were the largest prime number.
% \item<2-| alert@2> Let $q$ be the product of the first $p$ numbers.
% \item<3-> Then $q+1$ is not divisible by any of them.
% \item<4-> But $q + 1$ is greater than $1$, thus divisible by some prime
% number not in the first $p$ numbers.\qedhere
% \end{enumerate}
% \end{proof}
% \end{frame}


% ##########################################################################################################
\begin{frame}
  Control Flow

  The way we move through the data that is the program, \\
  doing things on our way

\end{frame}


% ##########################################################################################################
\begin{frame}
  First, let's take a look at history.

  This is a program that has been written \\
  (probably between 1940 and 1970) \\
  on an IBM 444 \\
  (a programmable accounting calculator)

\end{frame}


% ##########################################################################################################
{\setbeamercolor{background canvas}{bg=black}
\begin{frame}
  \includegraphics[width=100mm]{Plugboard-for-Tabulator-IBM-444.jpg}
\end{frame}}


% ##########################################################################################################
\begin{frame}

  And this is a scientist in the late 1950s / early 1960s \\
  doing neurological / psychological research

\end{frame}


% ##########################################################################################################
{\setbeamercolor{background canvas}{bg=black}
\begin{frame}
  \includegraphics[width=75mm]{univac-490.jpg}
\end{frame}}


% ##########################################################################################################
\begin{frame}

  This is what a program library looks like\\
  when you're using plugboards\\
  as this Texan company still does in 2013

\end{frame}


% ##########################################################################################################
{\setbeamercolor{background canvas}{bg=black}
\begin{frame}
\includegraphics{ibm_402_4-11322346.jpg}
\end{frame}}

% ##########################################################################################################
\begin{frame}
John von Neumann asserted in 1945 that programs themselves can be represented as data.

The first computer to implement his ideas became operational in 1949.

It became possible to program by entering long series of numbers that represented machine code.
\end{frame}

% ##########################################################################################################
\begin{frame}


Then, Grace Hopper (1906–1992) had the idea to collect such programs in `libraries' and to `call functions'
like you `call a book' in a real-world library.

This led her to conceiving A-0, the world's first compiler, which turned into commercial products
ARITH-MATIC and MATH-MATIC.

Next, she came up with the idea to write programs in languages similar to English (and other natural
languages!)—which we're still doing today.

The first eloquent programming language in history was developed by Hopper between 1953 and 1959;
she called it FLOW-MATIC; this would later evolve into COBOL.

Here is a FLOW-MATIC code snippet.

\end{frame}




% ##########################################################################################################
\begin{frame}[fragile]
\makeatletter
{\def\verbatim@font{\ttFont\tiny}
\makeatother
\begin{verbatim}
# sample.flow-matic
# A sample (slightly edited):

      ....................................................................................
 (0)  input  inventory  file-a price        file-b;
      output priced-inv file-c unpriced-inv file-d;
      hsp d.

      ....................................................................................
 (1)  compare product-no (a) with product-no (b);
      if greater  go to operation 10;
      if equal    go to operation 5;
      otherwise   go to operation 2.

      ....................................................................................
 (2)  transfer a to d.
 (3)  write-item d.
 (4)  jump to operation 8.

      ....................................................................................
 (5)  transfer a to c.
 (6)  move unit-price (b) to unit-price (c).
 (7)  write-item c.
 (8)  read-item a;
      if end of data go to operation 14.
 (9)  jump to operation 1.

      ....................................................................................
(10)  read item b;
      if end of data go to operation 12.
(11)  jump to operation 1.

      ....................................................................................
(12)  set operation 9 to go to operation 2.  # Holy Cow! Metaprogramming!
(13)  jump to operation 2.

      ....................................................................................
(14)  test product-no (b) against zzzzzzzzzzzz;
      if equal  go to operation 16;
      otherwise go to operation 15.
(15)  rewind b.

      ....................................................................................
(16)  close-out files c; d.
(17)  stop. (end)

\end{verbatim}}
\end{frame}


% ##########################################################################################################
\begin{frame}

one can immediately see the huge step forward\\
that Hopper had made

code execution happens from top to bottom\\
with explicitly labelled GOTOs

...which is conceptually simple, but doesn't scale well\\
already in 1968, Dijkstra wrote his famous\\
{\italicFont GOTO considered harmful}

\end{frame}


% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{`simple' doesn't always equal `readable'}

when branching, most of the time the condition comes before the conclusion:

{\ttFont if a < 0 then print "negative"}

but sometimes we seem to be happier with the inversion of that\\
(postfix if, possibly introduced by perl):

{\ttFont print "negative" if a < 0}


\end{frame}


% ##########################################################################################################
\begin{frame}[fragile]
FORTH has maybe the most `logical' control flow

every line is strictly read from left to right

% \begin{center}
% \begin{verbatim}
{\ttFont : FLOOR5 DUP 6 < IF DROP 5 ELSE 1 - THEN ;}
% \end{verbatim}
% \end{center}

everything is either a data item or a postfix operator

observe however it was felt necessary to make word definition a bracketing operator

who uses a stack-based language these days?

LISP is not all too popular, either
\end{frame}





% ##########################################################################################################
\begin{frame}

in FLOW-MATIC,\\
subroutine calls could be either called\\
in their original location on tape (incurring a time penalty)\\
or be inlined (incurring a space penalty)

this is the origin of functions and macros

(TeX comes to mind, a programming language that\\
does everything the inline/expanding way)

today, the options for control flow have multiplied

\end{frame}



% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
\begin{frame}
\includegraphics[width=100mm]{triangles-01.png}
\end{frame}

% ##########################################################################################################
\begin{frame}
\includegraphics[width=100mm]{triangles-02.png}
\end{frame}


% ##########################################################################################################
\begin{frame}
{\boldFont contiguous}

\includegraphics[width=100mm]{triangles-03.png}
\end{frame}

% ##########################################################################################################
\begin{frame}
{\boldFont contiguous}

CTYR ({\ttFont call}, {\ttFont throw}, {\ttFont yield}, {\ttFont return})\\
are basic constructs that break out of contiguous mode

like subtraction and division break out of\\ natural-number and integer-number mode

{\ttFont call} may be non-blocking, which leads to asynchronous mode;\\
{\ttFont throw} gives you exceptional mode;\\
{\ttFont yield} gives you iteration;\\
{\ttFont return} goes back to ... anywhere! Kansas maybe!

\end{frame}

% ##########################################################################################################
\begin{frame}
\frametitle{exceptional}

{\boldFont contractual}

each language function(ality) has a usage contract\\
when that contract gets violated, an `exception is thrown'\\
execution recursively `falls back' to caller\\
exception may be `handled' with a {\ttFont try} / {\ttFont catch} clause

{\boldFont exceptions contract}

(1) exceptions are thrown ifff there is a breach of contract

(2) exceptions carry a `routing slip' (a stacktrace)\\
with all the file names, function names, and line numbers\\
they fell through

(3) you can catch them within a {\ttFont catch} clause

\end{frame}


% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{exceptional}

{\boldFont contractual, handled}

example:

\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{verbatim}
# try-catch-stacktrace.coffee

f = ->
  try
    Rg = g()
  catch Rx               # note: imagine `Rx = catch` or `catch as Rx` here
    ...

g = -> return h()
h = -> throw Rh
\end{verbatim}}
\end{frame}


% ##########################################################################################################
\begin{frame}
\frametitle{exceptional}

{\boldFont contractual}

{\ttFont f} and {\ttFont h} don't know about each other\\
but if {\ttFont f} catches an exception, it may have come from {\ttFont h}\\
so while {\ttFont f} just wanted to talk to {\ttFont g}\\
suddenly it's being talked to by {\ttFont h}\\
this can be hard to grasp

\end{frame}

% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{exceptional}

{\boldFont handled}

control flow in Python's exception handling constructs\\
{\italicFont can} be difficult:

\makeatletter{\def\verbatim@font{\ttFont\tiny}\makeatother
\begin{verbatim}
try:
  do_1a()
  do_1b()                                 # only executed if `do_1a` was OK

except Exception_class_2a as error_2a:    # only up to one `except` clause gets executed
  do_2a()

except Exception_class_2b as error_2b:
  do_2b()

else:                                     # only if `do_1x` was OK; used to avoid catching exceptions
  do_3()                                  # from other sources than `do_1x`

finally:                                  # executed after `do_3`; exceptions will be re-raised
  do_4()

\end{verbatim}}
\end{frame}


% ##########################################################################################################
\begin{frame}[fragile]
\fontsize{10pt}{7.2}\selectfont

(originally about why i never use Python's {\ttFont for} / {\ttFont in} / {\ttFont else})\\
Nr. 1 when googling ``python else clause in for in statement'':

{\boldFont conceptual stackoverflow.com}

—I've noticed the following code is legal in Python. My question is why? Is there a specific reason?

\makeatletter{\def\verbatim@font{\ttFont\tiny}\makeatother
\begin{verbatim}
n = 5
while n != 0:
    print n
    n -= 1
else:
    print "what the..."
\end{verbatim}}

—Wow, I never even knew about this.

—@detly: That's because most people avoid this construct. :)
I believe Guido mentioned during the Py3k process that, at the very least, the choice of the word
{\ttFont else} for this use had been a remarkably bad idea, and that they wouldn't be doing any more of these.

\end{frame}



% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{exceptional}

{\boldFont broken}

example:

\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{verbatim}
# try-catch-no-stacktrace.coffee
f = -> f()
f()
\end{verbatim}}

in JavaScript the exceptions contract is broken here\\
because of missing stacktrace\\
which can be very annoying to debug
\end{frame}


% ##########################################################################################################
\begin{frame}
\frametitle{exceptional}

{\boldFont broken}

Java and some frameworks are dreaded for their long stacktraces

some exceptions in JavaScript do not have stacktraces at all

exceedingly long stacktraces\\
missing stacktraces\\
incomplete stacktraces (resulting from asynchronous calls)\\
falsely positive and negative exceptions\\
and exceptions you can't catch in a {\ttFont catch} clause\\

may be regarded as {\boldFont broken exceptions}

\end{frame}




% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{exceptional}

{\boldFont contractual, handled (and slightly broken)}

example:

\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{verbatim}
# try-catch-0.coffee

f = -> log g()
g = -> return h()
h = -> return k()
k = -> return d[ 'x' ]
f()

\end{verbatim}}
\end{frame}

% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
\begin{frame}
\frametitle{exceptions as a tool}

inline expression control flow—can't be so difficult!!?

{\ttFont d[ key ]  = g( x * 2, y + 1 )}


{\italicFont Q: in what order are the sub-expressions evaluated?}

{\italicFont A: it depends!}

{\scriptsize{\ttFont \# order-of-evaluation}}

\end{frame}

% ##########################################################################################################
\begin{frame}
\includegraphics[width=100mm]{triangles-04.png}
\end{frame}

% ##########################################################################################################
\begin{frame}
clearly, it would be nice to have a good way to picture control flow

but all that exists is of marginal popularity

(UML `Activity Diagrams' are ... behold ... Flowcharts!)

{\italicFont is visualized control flow doomed like 3D desktops
\\\& graphical programming languages?}

\end{frame}


% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
\begin{frame}

{\boldFont ... back to the beginning}


\end{frame}

% ##########################################################################################################
\begin{frame}
\includegraphics[width=100mm]{triangles-01.png}
\end{frame}

% ##########################################################################################################
{\color{gray}
\begin{frame}
\frametitle{outsourced / packaged}

{\boldFont blocking}

code in function runs to completion\\
it is completed when {\ttFont return} is encountered

{\boldFont iterative}

code runs until {\ttFont yield} is encountered\\
{\ttFont yield} can return a value\\
function may resume; state is preserved in closure

{\boldFont deferred}

a function {\ttFont f} is called that returns `immediately'\\
it may cause an action {\ttFont g} to run in parallel\\
an event {\ttFont e} may become available at the earliest on the next turn

\end{frame}}

% ##########################################################################################################
{\color{gray}
\begin{frame}
\frametitle{parallel / preëmptive}

{\boldFont forked}

code runs in other process on same machine

{\boldFont remote}

code runs on another CPU

{\boldFont threaded / dreaded}

several instances of the same process run in the same shared memory

\end{frame}}

% ##########################################################################################################
\begin{frame}
\frametitle{asynchronous / coöperative}

{\color{gray}\scriptsize http://ejohn.org/blog/how-javascript-timers-work\\
{\ttFont \# intervals.coffee}}


{\boldFont callbacks}

pass in callback function when calling an async method\\
return value of async method most of the time throwaway value\\
most of NodeJS API works like this

{\boldFont evented}

get back Event Emitter on calling async method\\
can attach handlers to events on this emitter\\
JS Promises/A+ builds on this

{\boldFont folded}

code looks like it's synchronous—but it's not!


\end{frame}

% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{asynchronous / coöperative\\ ... and ... handling exceptions ...?}

exceptions thrown by asynchronous code\\
can not be handled in a {\ttFont try} / {\ttFont catch} clause

in NodeJS, you can use {\ttFont process.on 'uncaughtException'}\\
and that's what i do in coffeenode-stacktrace:

\begin{verbatim}
# This is *so* 1990s VBA!
process.on 'uncaughtException', ( error ) =>
  @log_stacktrace error
\end{verbatim}


\end{frame}

% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
% ##########################################################################################################
\begin{frame}[fragile]
\frametitle{asynchronous folded code}

discovered this is @ {\color{blue}http://pogoscript.org}\\
\& {\color{blue}https://github.com/jiangmiao/toffee-script}:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{black}read_file '/tmp/whatever.txt', ( error, text ) ->}
  {\color{black}throw error if error?
  log "here is the text:", text}
{\color{black}log "the file is being read; results to be announced shortly"}
\end{semiverbatim}}
becomes:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{black}log "going to read file"}
{\color{black}text =} {\color{black}read_file}{\color{black}!} {\color{black}'/tmp/whatever.txt'}
{\color{black}log "here is the text:", text}
\end{semiverbatim}}
or even:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{black}log "going to read file"}
{\color{black}try}
  {\color{black}text =} {\color{black}read_file}{\color{black}!} {\color{black}'/tmp/whatever.txt'}
{\color{black}catch error
  throw error}
{\color{black}log text}
\end{semiverbatim}}


\end{frame}

\begin{frame}[fragile]
\frametitle{asynchronous folded code}

discovered this is @ {\color{blue}http://pogoscript.org}\\
\& {\color{blue}https://github.com/jiangmiao/toffee-script}:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{ForestGreen}read_file '/tmp/whatever.txt', ( error, text ) ->}
  {\color{red}throw error if error?
  log "here is the text:", text}
{\color{YellowGreen}log "the file is being read; results to be announced shortly"}
\end{semiverbatim}}
becomes:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{ForestGreen}log "going to read file"}
{\color{red}text =} {\color{ForestGreen}read_file}{\color{red}!} {\color{ForestGreen}'/tmp/whatever.txt'}
{\color{red}log "here is the text:", text}
\end{semiverbatim}}
or even:
\makeatletter{\def\verbatim@font{\ttFont\scriptsize}\makeatother
\begin{semiverbatim}
{\color{ForestGreen}log "going to read file"}
{\color{red}try}
  {\color{red}text =} {\color{ForestGreen}read_file}{\color{red}!} {\color{ForestGreen}'/tmp/whatever.txt'}
{\color{red}catch error
  throw error}
{\color{red}log text}
\end{semiverbatim}}


\end{frame}


% ##########################################################################################################
\begin{frame}
\frametitle{asynchronous folded code}

observations:

{\italicFont it does look tempting!

but what if you wanted to have code executed right after the call??

how do they do it?}

well, maybe that's part of the problem...
\end{frame}


% ##########################################################################################################
\begin{frame}

{\italicFont Lo and Behold.\\
Just look at it.}

{\ttFont \# asynchronous-folded-pogo.pogo}\\
{\ttFont \# asynchronous-folded-pogo.js}

{\ttFont \# async-try-catch.pogo}\\
{\ttFont \# async-try-catch.js}

\end{frame}


% ##########################################################################################################
\begin{frame}

kthxbye

https://github.com/loveencounterflow/CONTROLFLOW

\end{frame}




% ##########################################################################################################
% ##########################################################################################################
\end{document}

