Skip to content
Newer
Older
100644 171 lines (123 sloc) 5.37 KB
a885fa5 @citizen428 Outline
authored Oct 6, 2009
1 %
2 % Functional Programming in Ruby
3 %
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
4 % Created by Michael Kohl on 2009-11-13.
a885fa5 @citizen428 Outline
authored Oct 6, 2009
5 %
6 % Copyright (c) 2009 Michael Kohl.
7 % Licensed under the Creative Commons
8 % Attribution-Noncommercial-Share Alike 3.0 Austria License
9 \documentclass[a4paper]{scrreprt}
10
11 \usepackage[utf8]{inputenc}
12 \usepackage{fullpage}
13 \usepackage{fancyhdr}
14 \pagestyle{fancy}
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
15 \usepackage[usenames,dvipsnames]{color}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
16 \usepackage{listings}
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
17 \lstset{
18 language=Ruby,
19 basicstyle=\footnotesize,
20 frame=single,
21 backgroundcolor=\color{White},
22 keywordstyle=\color{Violet},
23 commentstyle=\color{Bittersweet},
24 stringstyle=\color{Melon},
25 tabsize=2,
26 breaklines=true,
27 breakatwhitespace=false,
28 }
a885fa5 @citizen428 Outline
authored Oct 6, 2009
29 \usepackage{hyperref}
30 \hypersetup{
31 colorlinks,
32 linkcolor=blue,
33 urlcolor=blue
34 }
35 \usepackage{ifpdf}
36 \ifpdf
37 \usepackage[pdftex]{graphicx} \else
38 \usepackage{graphicx} \fi
39
40 \title{Functional Programming in Ruby}
41 \author{Michael Kohl}
42
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
43 \date{2009-11-13}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
44
45 \begin{document}
46
47 \ifpdf \DeclareGraphicsExtensions{.pdf, .jpg, .tif} \else \DeclareGraphicsExtensions{.eps, .jpg} \fi
48
49 \maketitle
50 \tableofcontents
51
52 \chapter{About this eBook}
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
53 In recent years functional programming languages \cite{wiki} managed to somewhat overcome the prejudice of only being used in - and by - academics and managed to attract a wider audience. A lot of people attribute this recent success to the ubiquitous presence of multicore systems, leading to an increased need in concurrent software designs, an area where functional programming languages excel.
a885fa5 @citizen428 Outline
authored Oct 6, 2009
54
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
55 Whatever the reasons really are, it seems functional languages are slowly arriving in the programming mainstream, and even though Ruby per se isn't one of them, it provides enough concepts to incorporate a functional-like style into your programs.
a885fa5 @citizen428 Outline
authored Oct 6, 2009
56
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
57 This eBook tries to give a brief overview of functional programming and the functional features of Ruby. It will by definition always be a work in progress, so if you have any corrections or additions, please head over to
58 \begin{center}\url{http://github.com/citizen428/fp-ruby-ebook}\end{center}
59
60 fork and send me a pull request!
61
62 \paragraph{Acknowledgements}$\;$\\
63 \\
64 The contents of this eBook are heavily based on Dean Wampler's RubyConf 2008 talk \cite{video-wampler}, as well as Tom Stuart's presentation at the London RUG meeting \cite{video-stuart} and the accompanying slides \cite{pdf-stuart}.
65
66 I also want to thank my colleagues at \href{http://rubylearning.org}{RubyLearning.org} for always keeping me on my toes and constantly proving how much fun programming in Ruby can be.
67
68 \paragraph{Support}$\;$\\
69 \\
70 \includegraphics{pledgie.png}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
71
72 \paragraph{License}$\;$\\
73 \\
74 This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 Austria License. To view a copy of this license, visit \url{http://creativecommons.org/licenses/by-nc-sa/3.0/at/} or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
75
76 \chapter{Functional Programming}
77
78 \section{What is Functional Programming?}
79
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
80 \subsection{Lambda Calculus}
81
82 \cite{wiki-lambda}
83
84 \section{Important Concepts}
85
86 \subsection{Mathematical Functions}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
87
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
88 also composition and partial application
a885fa5 @citizen428 Outline
authored Oct 6, 2009
89
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
90 \subsection{Higher-order Functions}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
91
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
92 \subsection{Immutability}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
93
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
94 \subsection{Lazyness}
95
96 \subsection{Recursion}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
97
98 \section{Advantages of Functional Programming}
99 concurrency easier (no locking, no synchronization, no-side effects)
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
100 terser code
101 easier to debug
a885fa5 @citizen428 Outline
authored Oct 6, 2009
102
103 \section{Functional Programming Languages}
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
104 pure vs impure
a885fa5 @citizen428 Outline
authored Oct 6, 2009
105
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
106 statically typed, pattern matching, code not data: ml family, ocaml, haskell
a885fa5 @citizen428 Outline
authored Oct 6, 2009
107
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
108 dynamically typed, homoiconic, lists: cl, scheme, clojure
a885fa5 @citizen428 Outline
authored Oct 6, 2009
109
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
110 languages: ML, Haskell, Erlang, F\#, Clojure...
a885fa5 @citizen428 Outline
authored Oct 6, 2009
111
112 \chapter{FP in Ruby}
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
113
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
114 \section{The Enumerable Module}
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
115
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
116 The first taste many people get of FP programming are the functions map, fold, find and zip (note: names vary between languages). Here is a list of their Ruby equivalents:
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
117
118 \begin{itemize}
119 \item \textbf{map/collect}: Returns a new array with the results of running the block for every element in an enum.
120
121 \begin{lstlisting}
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
122 DECK = %w[A 2 3 4 5 6 7 8 9 T J Q K].product(%w[c d h s]).map(&:join)
123 # => ["Ac", "Ad", "Ah", "As", "2c", "2d", "2h", "2s", "3c", "3d", "3h", ...]
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
124 \end{lstlisting}
125
126 \item \textbf{inject/reduce}: Combines all elements in an enum by applying a binary operation.
127
128 \begin{lstlisting}
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
129 def get_all_urls
130 get_all['posts']['post'].inject([]) { |urls, post| urls << post['href'] }
131 end
132 # instead of
133 def get_all_urls
134 urls = []
135 get_all['posts']['post'].each { |post| urls << post['href'] }
136 urls
137 end
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
138 \end{lstlisting}
139
140 \item \textbf{select/find\_all}: Returns a new array with all elements of an enum for which the block is true.
141
142 \begin{lstlisting}
143 [1,2,3,4,5].find_all { |i| i % 2 == 0 } # => [2, 4]
144
145 # a Ruby 1.9 version
146 [1,2,3,4,5].select(&:even?) # => [2, 4]
147 \end{lstlisting}
148
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
149 \item \textbf{zip}: Takes all elements of an enum and combines it with the corresponding element in the argument(s).
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
150
151 \begin{lstlisting}
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
152 sbox = Hash[letters.zip(letters.rotate(rand(25)+1))]
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
153 \end{lstlisting}
9510463 @citizen428 More code examples and updated bibliography
authored Nov 3, 2009
154 \end{itemize}
d032505 @citizen428 Started work on chapter 3
authored Nov 2, 2009
155
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
156 \section{Immutability}
157 read-only attributes, clone and freeze, no bang methods
a885fa5 @citizen428 Outline
authored Oct 6, 2009
158
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
159 \section{Higher-order Functions}
160 blocks, lambdas, procs
a885fa5 @citizen428 Outline
authored Oct 6, 2009
161
3db1529 @citizen428 Restructuring, wrote chapter 1
authored Nov 13, 2009
162 \section{Avoid side-effects}
a885fa5 @citizen428 Outline
authored Oct 6, 2009
163 side-effect free methods - attr\_readers, filter, select
164 return new objects from within methods
165
166 \bibliographystyle{plain}
167 \addcontentsline{toc}{chapter}{Bibliography}
168 \bibliography{functional-ruby}
169
170 \end{document}
Something went wrong with that request. Please try again.