On Github mhhussain / FunctionalProgramming
by yours truly
Best answer: In computer science, functional programming is a programming paradigm — a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. Just kidding... I thought it was something similar to procedural programming.
Church and Kleene were studying the foundation of mathematics and wanted to get to its very roots. They wanted to abstract it out as far as they possibly could.
They abstracted mathematics down to what they believed was its most basic and fundamental construct, the function. They created a new notion for their math using the greek letter lambda (λ)
Back in the 30s, it didn't exactly catch on because it didn't fulfill its original purpose, but once computers took over, it served as the theoretical basis and inspiration for functional programming. Wow.
Example: We're making a cup of coffee. The cup of coffee is our object. We add cream and sugar to it, and thereby change the object.
class CupOfCoffee { int cream; int sugar }; var yay = new CupOfCoffee(); yay.cream++; yay.sugar++;
Example: Same coffee example, let's say you want to add cream and sugar to it
var yay = new CupOfCoffee(); var nowWithSugar = yay.AddSugar(1); var nowWithCreamAndSugar = nowWithSugar.AddCream(1);
Execution of commands class CupOfCoffee { int cream; int sugar }; var yay = new CupOfCoffee(); yay.cream++; yay.sugar++;
Evaluation of expressions var yay = new CupOfCoffee(); var nowWithSugar = yay.AddSugar(1); var nowWithCreamAndSugar = nowWithSugar.AddCream(1);
Imperative programming is the execution of commands. The program consists of statements that specify how to change the state of the program. It is a step-by-step description of telling the computer how to accomplist it's overall task.
In declarative programming style, we express the logic of the program without specifying the execution details.
This is where the difficulty lies. You can turn it over in your head as much as you want, but it won't make sense. This is where you would need to start unravel the theory of computer science that you have learned. We are mostly taught that the logic of a program is tightly wound to the programs execution.You have a list of customer and you want to pluck from that list all of the customers who's age is greater than 65.
Initialize a loop, iterate through every customer, and if the age is greater than 65, then add that customer to some temp list. Once the loop terminates, return the temp list.
for (var i = 0; i < customers.Count(); i++) { if (customers[i].age > 65) { tempList.Add(customers[i]); } } return tempList;
Filter from customers where customer age is greater than 65.
SELECT * FROM Customer WHERE Age > 65
return (from c in customers where c.age > 65 select c)
return customers.Where(c => c.age > 65);
return _.filter(customers, function (c) { return c.age > 65 });
filter(customers.begin(), customers.end(), [] (Customer c) { return c.age > 65; } );
return customers.stream() .filter(c -> c.age > 65) .collect(Collectors.toList());
filter(lambda c: c.age > 65, customers)
[c for customer in customers if customer.age > 65]
customers |> List.filter (fun (c) -> c.age > 65)
let over65(c) = c.age > 65 customers |> List.filter over65
Imperative - execution of commands for (var i = 0; i < customers.Count(); i++) { if (customers[i].age > 65) { tempList.Add(customers[i]); } } return tempList;
Declarative - evaluation of expressions customers |> List.filter (fun (c) -> c.age > 65)
Example: Sum up all number between two numbers (i.e. SumNumbers(5, 10) -> 45)
int SumNumbers(int from, int to) { int retVal = 0; for (var i = from; i <= to; i++) { retVal += i; } return retVal; }
Example: Sum up all number between two numbers (i.e. SumNumbers(5, 10) -> 45)
int SumNumbers(int from, int to) { return (from > to) ? 0 : { var sumRest = SumNumbers(from + 1, to); from + sumRest; }; }
$scope.createEmployee = function() { $scope.loading++; var newEmployee = { Person_Sk: $scope.employee.Person_Sk, UserName: $scope.employee.UserName, FirstName: $scope.employee.FirstName, MiddleName: $scope.employee.MiddleName, LastName: $scope.employee.LastName, IsAdmin: $scope.employee.IsAdmin, IsActive: $scope.employee.IsActive, HireDate: moment($scope.employee.HireDate).format("YYYY-MM-DD"), EmployeeTypeLookup_Sk: $scope.employeeType.LookupElement_Sk, YearsProfessionalPrior: $scope.employee.YearsProfessionalPrior }; employeeRepository.Post(newEmployee) .done(function() { $scope.loading--; $scope.goToEmployees(); }); };
int Factorial(int n, int total = 1) { if (n == 0) return total; return Factorial(n - 1, n * total); }
Final note: Functional programming is a different way of thinking. Where most programming langauges and paradigms sprouted from mathematice, functional is and always will be very, very deeply rooted in it.