Ring/Lessons/Functional Programming

Functional Programming
In previous chapters we learned about Functions and Recursion.

In this chapter we are going to learn about more Functional Programming (FP) concepts like


 * Pure Functions
 * First-class functions
 * Higher-order functions
 * Anonymous and nested functions.
 * Equality of functions

Pure Functions
We can create pure functions (functions that doesn't change the state) by the help of the assignment operator to copy variables (Lists & Objects) by value to create new variables instead of working on the original data that are passed to the function by reference.

Example:

Output:

First-class Functions
Functions inside the Ring programming language are first-class citizens, you can pass functions as parameters, return them as value or store them in variables.

We can pass/return the function by typing the function name as literal like "FunctionName" or :FunctionName for example.

We can pass/return functions using the variable that contains the function name.

We can call function from variables contains the function name using the Call command

Syntax:

Example:

Output:

Higher-order Functions
Higher-order functions are the functions that takes other functions as parameters.

Example:

Output:

Anonymous and Nested Functions
Anonymous Functions are functions without names that can be passed as parameters to other functions or stored in variables.

Syntax:

Example:

Output:

Example:

Output:

Equality of functions
We can test if function = function or not using the '=' or '!=' operators

Example:

Output: