Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #33 from djotto/chapter3-edits
Browse files Browse the repository at this point in the history
Chapter3 edits
  • Loading branch information
miekg committed Sep 12, 2012
2 parents 72d78f9 + ecc5c34 commit 61f1bb9
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 40 deletions.
2 changes: 1 addition & 1 deletion ex-functions/ex-average.tex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\end{Exercise}

\begin{Answer}
\Question The following function calculates the average.
\Question The following function calculates the average:
\lstinputlisting[caption=Average function in Go,linerange={3,14}]{ex-functions/src/ave.go}
\showremarks
\end{Answer}
6 changes: 3 additions & 3 deletions ex-functions/ex-bubblesort.tex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
\begin{Exercise}[title={Bubble sort},difficulty=1]
\label{ex:bubble}
\Question\label{ex:bubble q1} Write a function that performs
Bubble sort on slice of ints. From \cite{bubblesort}:
a bubble sort on a slice of ints. From \cite{bubblesort}:
\begin{quote}
It works by repeatedly stepping through the list to be sorted, comparing each
pair of adjacent items and swapping them if they are in the wrong order. The
Expand All @@ -28,10 +28,10 @@

\begin{Answer}
\Question
The Bubble sort isn't terribly efficient, for $n$ elements it scales
Bubble sort isn't terribly efficient, for $n$ elements it scales
$O(n^2)$. See QuickSort \cite{quicksort} for a better sorting algorithm.

But Bubble sort is easy to implement, the following is an example.
But bubble sort is easy to implement:
\lstinputlisting[caption=Bubble sort,linerange=4-19]{ex-functions/src/bubblesort.go}

Because a slice is a reference type the \func{bubblesort} function works and
Expand Down
2 changes: 1 addition & 1 deletion ex-functions/ex-fib.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

\begin{Answer}
\Question
The following program calculates the Fibonacci numbers.
The following program calculates Fibonacci numbers:
\lstinputlisting[label=src:fib,caption=Fibonacci function in Go]{ex-functions/src/fib.go}

\showremarks
Expand Down
2 changes: 1 addition & 1 deletion ex-functions/ex-funcfunc.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
about this topic.

\Question\label{ex:function q2} Generalize the function from \ref{ex:function q1},
and create a \func{plusX(x)} which returns a functions that add \var{x} to an
and create a \func{plusX(x)} which returns functions that add \var{x} to an
integer.
\end{Exercise}

Expand Down
10 changes: 5 additions & 5 deletions ex-functions/ex-minmax.tex
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
\begin{Exercise}[title={Minimum and maximum},difficulty=0]
\label{ex:minmax}
\Question\label{ex:minmax q1} Write a function that calculates the
\Question\label{ex:minmax q1} Write a function that finds the
maximum value in an \type{int} slice (\type{[]int}).

\Question\label{ex:minmax q2} Write a function that calculates the
minimum value in a \type{int} slice (\type{[]int}).
\Question\label{ex:minmax q2} Write a function that finds the
minimum value in an \type{int} slice (\type{[]int}).

\end{Exercise}

\begin{Answer}
\Question This a function for calculating a maximum:
\Question This function returns the largest int in the slice \var{l}:
\begin{lstlisting}
func max(l []int) (max int) { |\longremark{We use a named return parameter;}|
max = l[0]
Expand All @@ -24,7 +24,7 @@
\end{lstlisting}
\showremarks

\Question This a function for calculating a minimum, that is almost identical to \func{max}:
\Question This function returns the smallest int in the slice \var{l}. It is almost identical to \func{max}:
\begin{lstlisting}
func min(l []int) (min int) {
min = l[0]
Expand Down
6 changes: 3 additions & 3 deletions ex-functions/ex-stack.tex
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
\begin{Exercise}[title={Stack},difficulty=1]
\label{ex:stack}
\Question \label{ex:stack q1} Create a simple stack which can hold a
fixed amount of ints. It does not have to grow beyond this limit.
Define both a \func{push} -- put something on the stack -- and a \func{pop}
-- retrieve something from the stack -- function. The stack should be
fixed number of ints. It does not have to grow beyond this limit.
Define \func{push} -- put something on the stack -- and \func{pop}
-- retrieve something from the stack -- functions. The stack should be
a LIFO (last in, first out) stack.

\begin{figure}[H]
Expand Down
2 changes: 1 addition & 1 deletion ex-functions/ex-vararg.tex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
\begin{Exercise}[title={Var args},difficulty=1]
\label{ex:varargs}
\Question\label{ex:varargs q1}
Write a function that takes a variable numbers of ints and prints
Write a function that takes a variable number of ints and prints
each integer on a separate line
\end{Exercise}

Expand Down
4 changes: 2 additions & 2 deletions ex-functions/src/fib.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import "fmt"

func fibonacci(value int) []int {
x := make([]int, value) |\longremark{We create an \key{array} to hold the integers up to the value given in the function call;}|
x[0], x[1] = 1, 1 |\longremark{Starting point of the Fibonicai calculation;}|
x[0], x[1] = 1, 1 |\longremark{Starting point of the Fibonacci calculation;}|
for n := 2; n < value; n++ {
x[n] = x[n-1] + x[n-2] |\longremark{$x_n = x_{n-1} + x_{n-2}$;}|
}
return x |\longremark{Return the \emph{entire} array;}|
}

func main() {
for _, term := range fibonacci(10) { |\longremark{Using the \key{range} keyword we "walk" the numbers returned by the fibonacci funcion. Here up to 10. And we print them.}|
for _, term := range fibonacci(10) { |\longremark{Using the \key{range} keyword we ``walk'' the numbers returned by the Fibonacci function. Here up to 10. And we print them.}|
fmt.Printf("%v ", term)
}
}
9 changes: 4 additions & 5 deletions fig/function.tex
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
|\begin{tikzpicture}[overlay]
\ubrace{0.6,-1.5}{0.0,-1.5}{The keyword \key{func} is used to declare a function;}
%
\ubrace{2.2,-1.5}{0.8,-1.5}{A function can be defined to work on a specific type, a %
more common name for such a function is \index{method}{method}. This part is %
called a \first{\emph{receiver}}{receiver} and it is optional. This %
will be used in chapter \ref{chap:interfaces};}
\ubrace{2.2,-1.5}{0.8,-1.5}{A function can optionally be bound to a specific type. %
This is called the \first{\emph{receiver}}{receiver}. A function with a receiver is %
a \index{method}{method}. This will be explored in chapter \ref{chap:interfaces};}
%
\ubrace{3.4,-1.5}{2.4,-1.5}{\emph{funcname} is the name of your function;}
%
Expand All @@ -24,7 +23,7 @@
the parentheses. If your function is a subroutine and does not have %
anything to return you may omit this entirely;}
%
\ubrace{8.2,-1.5}{6.3,-1.5}{This is the function's body, note that %
\ubrace{8.2,-1.5}{6.3,-1.5}{This is the function's body. Note that %
\func{return} is a statement so the braces around the parameter(s) are %
optional.}
\end{tikzpicture}|
Expand Down
33 changes: 15 additions & 18 deletions go-functions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@

Functions can be declared in any order you wish. The compiler scans the
entire file before execution, so function prototyping is a thing of the
past in Go.
Go disallows nested functions. You can however
work around this by using anonymous functions, see section
``\titleref{sec:functions as values}'' on page \pageref{sec:functions as values}
in this chapter.
past in Go. Go disallows nested functions, but you can work around this with
anonymous functions. See section ``\titleref{sec:functions as values}'' on page
\pageref{sec:functions as values} in this chapter.

Recursive functions work just as in other languages:
\begin{lstlisting}[caption=Recursive function]
Expand Down Expand Up @@ -114,7 +112,7 @@ \section{Multiple return values}
x := 0
// Naively assume everything is a number
for ; i < len(b); i++ {
x = x*10 + int(b[i])-'0'
x = x*10 + int(b[i]) - '0'
}
return x, i
}
Expand All @@ -125,7 +123,7 @@ \section{Multiple return values}
var x int
for i := 0; i < len(a); { |\coderemark{No \texttt{i++}}|
x, i = nextInt(a, i)
println(x)
fmt.Println(x)
}
\end{lstlisting}
In the absence of tuples as a native type, multiple return values are the next
Expand All @@ -136,9 +134,9 @@ \section{Named result parameters}
\label{sec:named result parameters}
The return or result parameters of a Go function can be given names and used
as regular variables, just like the incoming parameters. When named, they are
initialized to the zero values for their types when the function begins; if the
initialized to the zero values for their types when the function begins. If the
function executes a \key{return} statement with no arguments, the current values of
the result parameters are used as the returned values. Using these
the result parameters are returned. Using these
features enables you (again) to do more with less code \footnote{This is
a motto of Go; ``Do \emph{more} with \emph{less} code''}.

Expand Down Expand Up @@ -250,9 +248,8 @@ \section{Deferred code}
\end{lstlisting}

\section{Variadic parameters}
Functions that take variadic parameters are functions that have a
variable number of parameters. To do this, you first
need to declare your function to take variadic arguments:
Functions that take a variable number of parameters are known as variadic functions.
To declare a function as variadic:
\begin{lstlisting}
func myfunc(arg ...int) {}
\end{lstlisting}
Expand Down Expand Up @@ -287,7 +284,7 @@ \section{Functions as values}
If we use \lstinline{fmt.Printf("%T\n", a)} to print the type of
\var{a}, it prints \func{func()}.

Functions--as--values may also be used in other places, like in maps.
Functions--as--values may be used in other places, for example maps.
Here we convert from integers to functions:
\begin{lstlisting}[caption=Functions as values in maps]
var xs = map[int]func() int{
Expand All @@ -304,7 +301,7 @@ \section{Functions as values}

\section{Callbacks}
\label{sec:callbacks}
With functions as values they are easy to pass to functions, from where
Because functions are values they are easy to pass to functions, from where
they can be used as callbacks. First define a function that
does ``something'' with an integer value:
\begin{lstlisting}
Expand All @@ -323,7 +320,7 @@ \section{Callbacks}

\section{Panic and recovering}
\label{sec:panic}
Go does not have an exception mechanism, like that in Java for instance: you can not throw exceptions.
Go does not have an exception mechanism, like that in Java for instance: you cannot throw exceptions.
Instead it uses a panic-and-recover mechanism. It is worth remembering that you should use this as
a last resort, your code will not look, or be, better if it is littered with panics. It's a powerful tool:
use it wisely. So, how do you use it?
Expand Down Expand Up @@ -352,9 +349,9 @@ \section{Panic and recovering}
executed\footnote{Copied from a presentation of Eleanor McHugh.}:
\begin{lstlisting}
func throwsPanic(f func()) (b bool) { |\longremark{We define a new function \func{throwsPanic} that takes %
a fuction as an argument, see ``\titleref{sec:functions as values}''. It returns true when this function %
will panic, otherwise false;}|
defer func() { |\longremark{We define a \func{defer} function that utilizes \func{recover}, \emph{if} the %
a function as an argument (see ``\titleref{sec:functions as values}''). It returns true if \func{f} panics %
when run, else false;}|
defer func() { |\longremark{We define a \func{defer} function that utilizes \func{recover}. If the %
current goroutine panics, this defer function will notice that. If \func{recover()} returns non-\var{nil} we set \var{b} %
to true;}|
if x := recover(); x != nil {
Expand Down

0 comments on commit 61f1bb9

Please sign in to comment.