..


Sponsored Links

Functional Programming in Ruby

Article written by Alessio Saltarini
Page 1 of 2

Introduction

Functional programming (FP) is a programming paradigm alternative to the traditional (structural or imperative programming and object-oriented programming) invented, as the "lambda calculus" by Alonso Church in 30 years, well before it was known that exactly what it was a computer.

Studies of Church served as the basis for the development of the programming language Lisp, and then they were almost completely abandoned, as they claimed the imperative programming the computers that generated the Basic and as Pascal, C, and today the modern C + + and Java.

The basic idea of the lambda calculus is that a computer program can be expressed, instead of a mandatory set of instructions (do this, then do this, then do that), with a series of functions whose parameters are the same number functions.

A functional program is usually composed of a function that takes as input another function that takes another function as input, and so on.

This means that while a traditional program, written in an imperative paradigm or objects, consists of a series of commands that act on the variable whose value represents the "state" of the program, functional programming in the very concept of variable does not exist (There is no concept of "state") and the execution is entrusted to a number of functions that operate on constant function.

The interest in functional languages ​​has been lost over time for several reasons, most notably the difficulty of learning (usually a man thinks of objects rather than functions, unless he is a mathematician!) And the difficulty in finding efficient interpreters or compilers.

Ruby and FP

Today, this interest has been awakened, however, because functional programming brings with it a consequence of very valuable: it can not by definition lead to bugs at run-time. In other words, at compile time or the first time either it works or does not work. Can not behave in ways not anticipated a priori (in a functional program, in fact, there is no concept of exception).

Thus were born and some are thriving functional languages ​​(more or less pure, and with more or less support for traditional programming) such as: ml / OCaml , Haskell , F # .

Ruby is not a functional language, but uses functional programming techniques that can help us to formulate algorithms more synthetic, more potent and more effective. Besides that, usually, easier to read.

Each and map

The first characteristic of languages ​​that support in some way the FP, is to have in your standard library iterators functional. For example:






 $ Elements = [1,2,3,4,5]







 Elementi.map $ {| elem | puts elem} + 1



The functions "map" and "each" of Ruby are nothing more than applied FP! In fact there are functions that have other functions as arguments. In Ruby, then, that those parts of the blocks of code enclosed in {} or between do / end anonymous functions are built specifically to be topics of many functions.

These functions are called in FP high-order functions, ie functions that take as many functions as input.

In particular "map" acts as a classical and mathematical function can be read as follows: for each element of the series from one to five, run the function: print the next natural number.

That "map" or "associates", for each element of a collection (an Array in Ruby we would say) a certain function defined in the block.

The same thing would happen naturally with:

 



 $ Elementi.each {...}

 
Closures: Proc and lambda

The closures are a concept similar to that of high-order function: the ability to define substantially a function of variables that act on living in a context different from that of the function (eg global variables or variables of other functions).

In Ruby, I can write a closure using anonymous functions that can be defined with the keywords Proc.new or lambda.

Here's an example:






 def multiply (multiplier)



    



 return lambda {| n | n * multiplier}







 end









 per3 = multiplication (3)









 per3.call puts (3) # => 9







 puts per3.call (per8.call (2)) # => 48



Not exactly self-evident in this example I define a function "multiply". What is so strange this function? Here I simply do not use any variable!

The argument is not a variable multiplier is simply a placeholder for a constant or another function.

But, in traditional programming, I'd write:






 def multiply (a, b)



    



 return a * b







 end









 puts the multiplication (3.3)



As can be seen, however, I define a closure using an "operator" I call per3, which defines the behavior of all the multiplications "x3". Then I call this operator the number 3. I can also call it recursively! Or call it, instead of a constant, on another function.

In traditional programming, however, I am forced to define in advance the number of variables involved in the transaction - thus limiting the multiplication of two numbers - and, most important, I have to insert a concept of the state, allocating memory for two variables that contain the values ​​to be multiplied.

In the same category ...
E-Learning
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
Sponsored Links