xxxxxxxxxx
class HigherOrderFunctions {
public static void main(String[] args) {
IFactory<Integer> createFactory = createFactory(() -> Math.random()*100, r -> r.intValue());
Integer product = createFactory.create();
System.out.println(product);
}
public static <T,R> IFactory<R> createFactory(IProducer<T> producer, IConfigurator<T,R> configurator){
return () -> {
T product = producer.produce();
return configurator.configure(product);
};
}
}
xxxxxxxxxx
// A function that returns a function is called a higher-order function.
/*
In this example, we are returning a function from another function -
We can return a function because functions in JavaScript are
treated as values.
*/
function sayHello() {
return () => {
console.log("Hello!");
}
}
xxxxxxxxxx
function evenNumberFilter(number) {
return number%2==0
}
function oddNumberFilter(number) {
return !evenNumberFilter(number)
}
evenNumberFilter(2) // true
oddNumberFilter(3) // true
xxxxxxxxxx
function (n) => {
unless(n%2 == 1, () => {
console.log(`${n} is even`);
});
xxxxxxxxxx
function evenNumberFilter(number) {
return number %2 == 0
}
function oddNumberFilter(number) {
return !evenNumberFilter(number)
}
function filter(predicate) {
const result = []
for(number=0; number<10; number++) {
if (predicate(number)) {
result.push(number)
}
}
return result
}
filter(evenNumberFilter); //[0,2,4,6,8]
filter(oddNumberFilter); //[1,3,5,7,9]
xxxxxxxxxx
function evenNumberFilter(number) {
return number %2 == 0
}
function oddNumberFilter(number) {
return !evenNumberFilter(number)
}
function evenNumberBeforeTen() {
const result = []
for(number=0; number<10; number++) {
if (evenNumberFilter(number)) {
result.push(number)
}
}
return result
}
function oddNumberBeforeTen() {
const result = []
for(number=0; number<10; number++) {
if (oddNumberFilter(number)) {
result.push(number)
}
}
return result
}
evenNumberBeforeTen(); //[0,2,4,6,8]
oddNumberBeforeTen(); //[1,3,5,7,9]
xxxxxxxxxx
fun main(args: Array<String>) {
val result = calculate(4, 5, ::sum)
println( result )
}
fun sum(a: Int, b: Int) = a + b
fun calculate(a: Int, b: Int, operation:(Int, Int) -> Int): Int {
return operation(a, b)
}
OCI runtime exec failed: exec failed: unable to start container process: exec /usr/bin/sh: resource temporarily unavailable: unknown
In this chapter, we will see the actual power of functional programming and how functions in a functional programming language differ from functions in a non-functional programming language.
Higher-Order Functions
Functional programming languages treat functions as first-class values. What this means is that, like any other value, such as Integers or Strings, a function can be passed as a parameter to another function and can also be returned as a result.
Functions that take other functions as parameters or that return functions as results are called higher-order functions.
Syntax
As we already know, the syntax for defining a function which takes a simple variable as a parameter is:
When we want to define a function which takes another function as a parameter, the syntax of the parameter would be the following:
We have taken the function name to be f. f is the function to be passed as a parameter. It is followed by a : which is further followed by the data type of f's argument. We then insert an arrow => which is followed by the data type of f's result.
In the next lesson, we will be looking at an example which will be carried forward for the next several lessons.