Higher Order Functions


Advertisements

A higher order function (HOF) is a function that follows at least one of the following conditions −

  • Takes on or more functions as argument
  • Returns a function as its result

HOF in PHP

The following example shows how to write a higher order function in PHP, which is an object-oriented programming language −

<?php  
$twice = function($f, $v) { 
   return $f($f($v)); 
};  

$f = function($v) { 
   return $v + 3; 
}; 

echo($twice($f, 7));

It will produce the following output −

13

HOF in Python

The following example shows how to write a higher order function in Python, which is an object-oriented programming language −

def twice(function): 
   return lambda x: function(function(x))  
def f(x): 
   return x + 3 
g = twice(f)
print g(7)      

It will produce the following output −

13

Useful Video Courses


Video

IBM Rational Functional Tester

32 Lectures 3.5 hours

Pavan Lalwani

Video

Functional Programming and Lambda- Learn Java8 by Coding it

Best Seller

72 Lectures 10.5 hours

Arun Ammasai

Video

Functional Programming using JavaScript

51 Lectures 2 hours

Skillbakerystudios

Video

Elixir and Phoenix: Real World Functional Programming

43 Lectures 4 hours

Mohammad Nauman

Video

How to Write a Smart Functional Spec for SAP SD Z-Report

8 Lectures 1 hours

Santharam Sivalenka

Advertisements