Prev | Current Page 57 | Next

Brad Ediger

"Advanced Rails"

rb
C.send(:include, M)
C.class_eval { alias_method_chain :test_method, :module }
C.new.test_method # => "Test from M"
Functional Programming
The paradigm of functional programming focuses on values rather than the side
effects of evaluation. In contrast to imperative programming, the functional style
deals with the values of expressions in a mathematical sense. Function application
and composition are first-class concepts, and mutable state (although it obviously
exists at a low level) is abstracted away from the programmer.
This is a somewhat confusing concept, and it is often unfamiliar even to experienced
programmers. The best parallels are drawn from mathematics, from which functional
programming is derived.
Consider the mathematical equation x = 3. The equals sign in that expression indicates
equivalence: ???x is equal to 3.??? On the contrary, the Ruby statement x = 3 is of a
completely different nature. That equals sign denotes assignment: ???assign 3 to x.??? In
a functional programming language, equals usually denotes equality rather than
assignment. The key difference here is that functional programming languages specify
what is to be calculated; imperative programming languages tend to specify how
to calculate it.
Higher-Order Functions
The cornerstone of functional programming, of course, is functions. The primary
way that the functional paradigm influences mainstream Ruby programming is in the
use of higher-order functions (also called first-class functions, though these two terms
are not strictly equivalent).


Pages:
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69